credstash
credstash is a Python utility for securely managing secrets in the cloud by leveraging AWS Key Management Service (KMS) for encryption and Amazon DynamoDB for storage. It provides a simple command-line interface and a Python API to store, retrieve, and version secrets such as database passwords or API keys. The library is actively maintained, with version 1.17.1 being the latest, and receives regular updates for bug fixes and new features.
Warnings
- breaking Credstash migrated from PyCrypto to Cryptography, and in v1.15.0, unsupported hashing methods were removed. Users with older secret stores (pre-v1.15.0) or custom hashing methods may experience decryption failures. Additionally, v1.13.4 introduced an upper bound on `cryptography` due to incompatibilities, which might cause installation issues with newer `cryptography` versions.
- gotcha Prior to v1.17.0, `credstash` might have logged sensitive information to local disk when imported as a library, potentially exposing secrets or causing issues in read-only environments. As of v1.17.0, logging is disabled by default when used as a library.
- gotcha In `v1.17.1`, a bug was fixed where `kms_region` as an optional parameter could cause issues when other parameters were passed positionally. While fixed, relying solely on positional arguments for optional parameters can lead to unexpected behavior.
- breaking Older versions of `credstash` (prior to December 2015) used unpadded integers for auto-versioning secrets, which could lead to incorrect sorting and retrieval of the latest secret once versions reached 10 or more. This is a significant issue for legacy secret stores.
- gotcha Credstash requires an initial setup: a KMS master key (default alias `credstash`) must be created manually in AWS KMS, and the `credstash setup` command must be run to create the default DynamoDB table (`credential-store`). The library will not function without these prerequisites.
- gotcha The default security model for `credstash` assumes the EC2 instance boundary as the security boundary. If an attacker gains sufficient access to an EC2 instance (e.g., to the instance metadata service or process memory), they may be able to retrieve credentials.
Install
-
pip install credstash -
pip install credstash[YAML]
Imports
- credstash
import credstash
Quickstart
import os
import credstash
import boto3
# Ensure AWS credentials are set up (e.g., via environment variables like AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION)
# Or configure a Boto3 session explicitly
# For demonstration, assume credentials are in env or IAM role is attached
# Set a specific region if not relying on AWS_DEFAULT_REGION or instance metadata
aws_region = os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
# Initialize Boto3 clients if custom sessions or specific clients are needed
kms_client = boto3.client('kms', region_name=aws_region)
dynamodb_client = boto3.client('dynamodb', region_name=aws_region)
# Instantiate Credstash (optional, can also call functions directly)
stash = credstash.Credstash(table='credential-store', region=aws_region)
secret_name = "my_test_secret"
secret_value = "supersecretpassword123"
try:
# Put a secret
# By default, uses the 'credential-store' table and 'alias/credstash' KMS key
# ensure these are set up (credstash setup and KMS key creation)
stash.putSecret(name=secret_name, secret=secret_value, version='1', kms_key='alias/credstash', kms_client=kms_client)
print(f"Secret '{secret_name}' version 1 stored successfully.")
# Get the secret
retrieved_secret = stash.getSecret(name=secret_name, kms_client=kms_client)
print(f"Retrieved secret '{secret_name}': {retrieved_secret}")
# Update the secret with a new version (auto-increment example)
stash.putSecret(name=secret_name, secret='new_supersecret_value', autoversion=True, kms_key='alias/credstash', kms_client=kms_client)
print(f"Secret '{secret_name}' updated with new version.")
updated_secret = stash.getSecret(name=secret_name, kms_client=kms_client)
print(f"Retrieved updated secret '{secret_name}': {updated_secret}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you have configured AWS credentials and run `credstash setup` and created a KMS key 'alias/credstash'.")