-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bitwarden_secrets_manager: Handle rate limits #8230
Comments
Files identified in the description: If these files are incorrect, please update the |
I got an implementation of that working... --- bitwarden_secrets_manager.py.original 2024-04-17 11:37:56.288578282 -0500
+++ bitwarden_secrets_manager.py 2024-04-17 14:36:56.322357518 -0500
@@ -70,6 +70,7 @@
"""
from subprocess import Popen, PIPE
+from time import sleep
from ansible.errors import AnsibleLookupError
from ansible.module_utils.common.text.converters import to_text
@@ -84,11 +85,28 @@
class BitwardenSecretsManager(object):
def __init__(self, path='bws'):
self._cli_path = path
+ self._max_retries = 3
+ self._retry_delay = 1
@property
def cli_path(self):
return self._cli_path
+ def _run_with_retry(self, args, stdin=None, retries=0):
+ if retries > self._max_retries:
+ raise BitwardenSecretsManagerException("Max retries exceeded. Unable to retrieve secret.")
+
+ out, err, rc = self._run(args, stdin)
+
+ if "Too many requests" in err:
+ delay = self._retry_delay * (2 ** retries)
+ sleep(delay)
+ return self._run_with_retry(args, stdin, retries + 1)
+ elif rc != 0:
+ raise BitwardenSecretsManagerException(f"Command failed with return code {rc}: {err}")
+
+ return out, err, rc
+
def _run(self, args, stdin=None):
p = Popen([self.cli_path] + args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
out, err = p.communicate(stdin)
@@ -107,7 +125,7 @@
'get', 'secret', secret_id
]
- out, err, rc = self._run(params)
+ out, err, rc = self._run_with_retry(params)
if rc != 0:
raise BitwardenSecretsManagerException(to_text(err)) I do however think there's a better approach now that I've done this. Instead of being reactive to the rate limit errors it's probably more appropriate to take a more proactive approach and avoid hitting them to begin with. |
!component =plugins/lookup/bitwarden_secrets_manager.py |
Files identified in the description: If these files are incorrect, please update the |
I think avoiding the rate limit will be difficult because I think ansible will do a lookup/templating for every host (no caching?) so unless you set Reacting and respecting 429 though should be easier, and has to happen anyways I feel. |
Using |
@mintyhippoxyz do you want to contribute your edits as a PR? If not, I would likely implement something pretty close to that. btw, longer term I would recommend trying the - now finally available - official lookup plugin from bitwarden: https://bitwarden.com/blog/bitwarden-secrets-manager-and-ansible/ |
…8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url
…8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url (cherry picked from commit a05a598)
…8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url (cherry picked from commit a05a598)
…lement rate limit retry with backoff (#8260) * bitwarden_secrets_manager: implement rate limit retry with backoff (#8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url (cherry picked from commit a05a598) * Make Python 2 compatible. --------- Co-authored-by: Matt Adams <matt@4dk.me> Co-authored-by: Felix Fontein <felix@fontein.de>
…lement rate limit retry with backoff (#8261) bitwarden_secrets_manager: implement rate limit retry with backoff (#8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url (cherry picked from commit a05a598) Co-authored-by: Matt Adams <matt@4dk.me>
…nsible-collections#8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (ansible-collections#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (ansible-collections#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (ansible-collections#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url
Hopefully this helps some other people - I use bws-cache, a project that another Ansible user and I created, along with the included lookup plugin to avoid the rate limiting. I found it pretty difficult to avoid rate limits without such a solution as I use Bitwarden Secrets Manager heavily and would hit the limit very fast in most playbooks. |
…nsible-collections#8238) * bitwarden_secrets_manager: implement rate limit retry with backoff (ansible-collections#8230) * bitwarden_secrets_manager: add changelog fragment for 90cd2d6 (ansible-collections#8238) * bitwarden_secrets_manager: clarify "Too many requests" is an error condition (ansible-collections#8238) * bitwarden_secrets_manager: avoid an extra _run_with_retry execution after the last (very long) delay * bitwarden_secrets_manager: changelog fragment key and reference issue url
Summary
I'm not finding any official documentation on it yet but Bitwarden's Secret Manager seems to have a rate limit of 5 requests per second. When the rate limit is hit, the lookup fails with an error: 429 Too Many Requests; Slow down! Too many requests. Try again in 1s.
Issue Type
Bug Report
Component Name
bitwarden_secret_manager
Ansible Version
Community.general Version
Configuration
OS / Environment
Alpine Linux 3.19
Steps to Reproduce
Expected Results
I would expect the module to handle the 429 error with a back-off and retry until it succeeds
Actual Results
Code of Conduct
The text was updated successfully, but these errors were encountered: