-
Notifications
You must be signed in to change notification settings - Fork 29
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Hi,
InjectSecretString
decorator does not work for class methods, because it overwrites the self argument.
Instead of (or additionally to) fixing this method a new one would make more sense appending a dictionary of secret strings to non-keyworded arguments, for example:
class InjectSecretsDictionary:
"""Decorator implementing high-level Secrets Manager caching client"""
def __init__(self, secret_id, cache):
"""
Constructs a decorator to inject a single non-keyworded argument, which is a dictionary,
loaded from a cached secret for a given function.
:type secret_id: str
:param secret_id: The secret identifier
:type cache: aws_secretsmanager_caching.SecretCache
:param cache: Secret cache
"""
self.cache = cache
self.secret_id = secret_id
def __call__(self, func):
"""
Return a function with a dictionary produced from the cached secret injected as the last non-keyworded argument
:type func: object
:param func: The function for injecting a single non-keyworded argument.
:return The function with the injected argument.
"""
try:
secret = json.loads(self.cache.get_secret_string(secret_id=self.secret_id))
except json.decoder.JSONDecodeError as decoder_error:
raise RuntimeError('Cached secret is not valid JSON') from decoder_error
def _wrapped_func(*args, **kwargs):
"""
Internal function to execute wrapped function
"""
arguments = list(args)
arguments.append(secret)
return func(*tuple(arguments), **kwargs)
return _wrapped_func
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request