"""AWS Secret Manager connector.""" import json import boto3 from users import config TOKEN_TYPES = {'feature-fm': config.FEATURE_FM_SECRET_TOKEN} def get_secret(token_type): """Get shared token. Args: token_type (str): the secret type Returns: string: with the shared token or None """ secret_name = TOKEN_TYPES[token_type] session = boto3.session.Session() client = session.client( service_name='secretsmanager', region_name=config.AWS_REGION, endpoint_url=config.AWS_SECRET_MANAGER_URL, ) get_secret_value_response = client.get_secret_value(SecretId=secret_name) # Decrypted secret using the associated KMS CMK # Depending on whether the secret was a string or binary, # one of these fields will be populated if 'SecretString' in get_secret_value_response: return json.loads(get_secret_value_response['SecretString']) else: return json.loads(get_secret_value_response['SecretBinary'])