-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to pass aws_creds argument in get_secret function
- Loading branch information
tim.reichard
committed
Jan 13, 2023
1 parent
48e31a5
commit 574e8dc
Showing
7 changed files
with
39 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,27 @@ | ||
"""Generic async AWS functions for Secrets Manager.""" | ||
|
||
import base64 | ||
from typing import List | ||
from base64 import b64decode | ||
from typing import Dict | ||
|
||
from aioradio.aws.utils import AwsServiceManager | ||
import boto3 | ||
|
||
AWS_SERVICE = AwsServiceManager(service='secretsmanager', regions=['us-east-1']) | ||
SECRETS = AWS_SERVICE.service_dict | ||
|
||
|
||
async def add_regions(regions: List[str]): | ||
"""Add regions to Secret Manager AWS service. | ||
Args: | ||
regions (List[str]): List of AWS regions | ||
""" | ||
|
||
AWS_SERVICE.add_regions(regions) | ||
|
||
|
||
@AWS_SERVICE.active | ||
async def get_secret(secret_name: str, region: str) -> str: | ||
async def get_secret(secret_name: str, region: str, aws_creds: Dict[str, str]=None) -> str: | ||
"""Get secret from AWS Secrets Manager. | ||
Args: | ||
secret_name (str): secret name | ||
region (str): AWS region | ||
aws_creds (Dict[str, str], optional): AWS credentials | ||
Returns: | ||
str: secret value | ||
""" | ||
|
||
secret = '' | ||
response = await SECRETS[region]['client']['obj'].get_secret_value(SecretId=secret_name) | ||
if 'SecretString' in response: | ||
secret = response['SecretString'] | ||
if aws_creds: | ||
client = boto3.client(service_name='secretsmanager', region_name=region, **aws_creds) | ||
else: | ||
secret = base64.b64decode(response['SecretBinary']) | ||
client = boto3.client(service_name='secretsmanager', region_name=region) | ||
|
||
return secret | ||
resp = client.get_secret_value(SecretId=secret_name) | ||
return resp['SecretString'] if 'SecretString' in resp else b64decode(resp['SecretBinary']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters