1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

bitwarden_secrets_manager: implement rate limit retry with backoff (#8230)

This commit is contained in:
Matt Adams 2024-04-18 22:15:18 -05:00
parent 12b76ead29
commit 90cd2d61b7

View file

@ -70,6 +70,7 @@ RETURN = """
""" """
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from time import sleep
from ansible.errors import AnsibleLookupError from ansible.errors import AnsibleLookupError
from ansible.module_utils.common.text.converters import to_text from ansible.module_utils.common.text.converters import to_text
@ -84,11 +85,28 @@ class BitwardenSecretsManagerException(AnsibleLookupError):
class BitwardenSecretsManager(object): class BitwardenSecretsManager(object):
def __init__(self, path='bws'): def __init__(self, path='bws'):
self._cli_path = path self._cli_path = path
self._max_retries = 3
self._retry_delay = 1
@property @property
def cli_path(self): def cli_path(self):
return self._cli_path 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): def _run(self, args, stdin=None):
p = Popen([self.cli_path] + args, stdout=PIPE, stderr=PIPE, stdin=PIPE) p = Popen([self.cli_path] + args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
out, err = p.communicate(stdin) out, err = p.communicate(stdin)
@ -107,7 +125,7 @@ class BitwardenSecretsManager(object):
'get', 'secret', secret_id 'get', 'secret', secret_id
] ]
out, err, rc = self._run(params) out, err, rc = self._run_with_retry(params)
if rc != 0: if rc != 0:
raise BitwardenSecretsManagerException(to_text(err)) raise BitwardenSecretsManagerException(to_text(err))