agent: |
rzspDcFpayEOCHvPWd6cGet All Secrets from AWS Secrets Manager
Get All Secrets from AWS Secrets Manager
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task retrieves a list of all secrets stored in the AWS Secrets Manager for your account. AWS Secrets Manager is a service designed to safeguard sensitive information such as database credentials and API keys. By executing this task, users will obtain a comprehensive list of secret names or ARNs, aiding in audit, management, or automation processes. Note that this task will list the secrets' identifiers, but not their actual values. To fetch a specific secret's value, additional steps involving the get_secret_value method are required. Ensure you have the appropriate AWS IAM permissions before executing this task.
inputs
outputs
import boto3
from botocore.exceptions import ClientError
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def get_all_secrets(secrets_client):
try:
secrets = secrets_client.list_secrets()
return [secret['Name'] for secret in secrets['SecretList']]
except ClientError as e:
error_code = e.response['Error']['Code']
if error_code == 'UnrecognizedClientException':
print(f"Invalid security token or service not available in {secrets_client.meta.region_name}. Skipping.")
else:
print(f"ClientError {error_code} in {secrets_client.meta.region_name}: {e}")
return []
except Exception as e:
print(f"An unexpected error occurred in {secrets_client.meta.region_name}: {e}")
return []
# Main block
# Specify the region here. If None, it will loop through all available regions.
#REGION = 'us-east-1'
#print(f"regions received from top task {regions}")
REGION=None # Hardcoded for One Time Execution Result
regions = [REGION] if REGION else regions
all_secrets_data = []
for region in regions:
try:
secrets_client = boto3.client('secretsmanager',aws_access_key_id=access_key,aws_secret_access_key=secret_key, region_name=region)
all_secrets = get_all_secrets(secrets_client)
if all_secrets:
print(f"All Secrets in {region}:")
for secret in all_secrets:
print(secret)
all_secrets_data.append({'region': region, 'secret': secret})
else:
#print(f"No secrets found in {region}.")
p=1 #Dummy line to remove bad input error
except Exception as e:
print(f"An unexpected error occurred while processing {region}: {e}")
'''
# Print the all_secrets_data list to check the content
print("\nAll Secrets Data:")
for secret_data in all_secrets_data:
print(f"Region: {secret_data['region']}, Secret: {secret_data['secret']}")
'''
copied