# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use the AWS SDK for Python (Boto3) to manage secrets in AWS Secrets Manager. """ import logging import json logger = logging.getLogger(__name__) # snippet-start:[python.example_code.python.BatchGetSecretValue.full] # snippet-start:[python.example_code.python.BatchGetSecretValue.decl] class BatchGetSecretsWrapper: def __init__(self, secretsmanager_client): self.client = secretsmanager_client # snippet-end:[python.example_code.python.BatchGetSecretValue.decl] def batch_get_secrets(self, filter_name): """ Retrieve multiple secrets from AWS Secrets Manager using the batch_get_secret_value API. This function assumes the stack mentioned in the source code README has been successfully deployed. This stack includes 7 secrets, all of which have names beginning with "mySecret". :param filter_name: The full or partial name of secrets to be fetched. :type filter_name: str """ try: secrets = [] response = self.client.batch_get_secret_value( Filters=[{"Key": "name", "Values": [f"{filter_name}"]}] ) for secret in response["SecretValues"]: secrets.append(json.loads(secret["SecretString"])) if secrets: logger.info("Secrets retrieved successfully.") else: logger.info("Zero secrets returned without error.") return secrets except self.client.exceptions.ResourceNotFoundException: msg = f"One or more requested secrets were not found with filter: {filter_name}" logger.info(msg) return msg except Exception as e: logger.error(f"An unknown error occurred:\n{str(e)}.") raise # snippet-end:[python.example_code.python.BatchGetSecretValue.full]