-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_aws_secret.py
40 lines (38 loc) · 1.26 KB
/
get_aws_secret.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import boto3
from botocore.exceptions import ClientError
import json
import base64
import os
def get_secret(name):
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name='us-east-1'
)
print(f'Grabbing secret {name}..')
try:
get_secret_value_response = client.get_secret_value(
SecretId=name
)
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
raise e
else:
raise e
else:
if 'SecretString' in get_secret_value_response:
secret = get_secret_value_response['SecretString']
else:
secret = base64.b64decode(get_secret_value_response['SecretBinary'])
secret = json.loads(secret)
# Close session client so we don't get warnings from boto3
#client.close()
return secret