Skip to content

InjectSecretString decorator does not work for class methods #24

@vatima2016

Description

@vatima2016

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

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions