Skip to main content

Simplify retrieval of secrets from AWS SecretsManager. Optionally auto-memoize secrets in environment variables to improve performance and reduce costs.

Project description

Get AWS Secret

Simplify retrieval of secrets from AWS SecretsManager. Optionally auto-memoize secrets in environment variables to improve performance and reduce costs.

Test Coverage PyPI License Requirements Status Code Style Code Formatter Test Framework

We built this library in Hackt to support local development of internal projects and public apps in our catalog. Learn more about other open-source libraries on lib.hackt.app.


Runtime support

Python Logo

This is the Python runtime library, compatible with Python3.6+. Currently there isn't support for other runtimes. A Javascript/nodejs version is planned, but unscheduled.


Installation and Usage

Install with pip: pip install get-aws-secret

from get_aws_secret import get_secret

secret_val = get_secret('MY_SECRET_DATA')

The get_secret method also accepts a secret ARN:

secret_val = get_secret('arn:aws:secretsmanager:us-east-1:123456789012:secret:MY_SECRET_DATA')

Auto-load JSON strings

Secrets with JSON-like strings can be automatically loaded by setting load_json argument to True. The default behavior is False (i.e. always returns a string, without attempting to load any JSON).

from get_aws_secret import get_secret

# MY_SECRET_JSON_DATA = '{"data": "foobar"}' (str)

secret_val = get_secret('MY_SECRET_JSON_DATA', load_json=True)

# Returns a dict, instead of str: {'data': 'foobar'}

If load_json=True and the secret value isn't a JSON-compatible string, it returns the string without raising exceptions:

from get_aws_secret import get_secret

# MY_SECRET_PLAIN_DATA = 'just plain text' (str)

secret_val = get_secret('MY_SECRET_PLAIN_DATA', load_json=True)

# Returns a string: 'just plain text'

Memoization

The library can automatically set the secret as environment variable and retrieve from there in subsequent requests.

from get_aws_secret import get_secret

secret_val = get_secret('MY_SECRET_DATA', memoize=True)

In the first run, setting memoize=True is equivalent to running os.environ['MY_SECRET_DATA'] = secret_val after retrieving the secret.

In subsequent calls with memoize=True, the function will find MY_SECRET_DATA in os.environ and retrieve it locally . In other words, it won't hit the AWS endpoints (saves a few milliseconds and cents).

It's possible to set the behavior of memoize=True (in fact, any other get_value argument) as the default for all requests without explicit argument:

from get_aws_secret import get_secret_fix_args

get_secret = get_secret_fix_args(memoize=True)

secret_val = get_secret('MY_SECRET_DATA')

Custom boto3.client

Optionally, set a custom boto3.client with:

import boto3
from get_aws_secret import get_secret_fix_args

client = boto3.client('secretsmanager', region_name='my-region-1')

get_secret = get_secret_fix_args(client=client)

secret_val = get_secret('MY_SECRET_DATA')

License

This library is licensed under Apache 2.0.


Contributor guide

Please check out guidelines in the repository wiki.


Acknowledgements

Published & supported by Hackt App

Logos provided by Clearbit

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

get_aws_secret-0.2.0b3-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file get_aws_secret-0.2.0b3-py3-none-any.whl.

File metadata

  • Download URL: get_aws_secret-0.2.0b3-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2

File hashes

Hashes for get_aws_secret-0.2.0b3-py3-none-any.whl
Algorithm Hash digest
SHA256 913624759102ae0900b278fe2cc87ba8886bc7460458ca67f3cc7c4f3bf977e5
MD5 a0b3fdf6ba12f8cb03f0a02f9028faa8
BLAKE2b-256 b9eb78f1e60a10489e4032a06b77074f64d521a13cdc29857e3e92ab33905cce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page