credstash

1.17.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically store and retrieve a secret using the `credstash` Python API. It assumes AWS credentials are configured (e.g., via environment variables or an IAM role) and that a KMS key aliased 'credstash' and a DynamoDB table named 'credential-store' have been created. It uses explicit `boto3` clients for clarity, though `credstash` can often infer them from the environment.

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'.")

view raw JSON →