Skip to main content

A secure encrypted credentials system for Django and FastAPI, inspired by Rails credentials

Project description

Secure Credentials Kit

A secure, encrypted credentials system for Django and FastAPI, inspired by Rails credentials.

Features

  • Environment-specific encrypted credentials
  • Framework-neutral CLI for generating and editing encrypted credentials
  • Master keys for editing credentials and read-only keys for application runtime access
  • Django management commands
  • FastAPI helpers for loading credentials into application state

Installation

The PyPI distribution, Python package, and CLI are all named for Secure Credentials Kit:

  • Distribution: secure-credentials-kit
  • Python package: secure_credentials_kit
  • CLI: secure-credentials-kit

Supported versions:

  • Python 3.10, 3.11, 3.12, 3.13, and 3.14
  • Django 5.2 LTS and Django 6.0

For Django:

pip install "secure-credentials-kit[django]"

For FastAPI:

pip install "secure-credentials-kit[fastapi]"

Local Development

This project uses pyproject.toml for package metadata and uv for local dependency management.

Install uv, then create a development environment:

uv sync

Install framework extras when you need to test integrations:

uv sync --extra django
uv sync --extra fastapi

Run tests:

uv run python -m unittest discover -v

Build the package:

uv run python -m build

Credentials Files

Add secret keys to .gitignore:

echo "secrets/*.key" >> .gitignore

Generate a new key:

secure-credentials-kit generate-key <environment>

This creates two keys:

  • secrets/<environment>.master.key can decrypt, edit, encrypt, and sign credentials.
  • secrets/<environment>.readonly.key can decrypt and verify credentials, but cannot produce accepted credential updates.

You can regenerate a read-only key from an existing master key:

secure-credentials-kit generate-key <environment> --role readonly

Edit encrypted credentials:

secure-credentials-kit edit <environment>

Editing requires secrets/<environment>.master.key. Applications should normally run with only secrets/<environment>.readonly.key.

The editor opens the decrypted YAML. The YAML root must be a mapping:

SOME_ENV_VAR: secret-value
database:
  url: postgres://user:password@localhost:5432/app
api:
  token: token-value

Credentials are stored in secrets/<environment>.yml.enc, and keys are stored in secrets/<environment>.master.key and secrets/<environment>.readonly.key. The encrypted file is generated by the tool and should not be edited by hand. It contains a signed encrypted payload similar to:

{
  "version": 2,
  "payload": "gAAAAAB...",
  "signature": "..."
}

Django Usage

Add secure_credentials_kit to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'secure_credentials_kit',
    ...
]

You can also use Django management commands:

python manage.py credentials_generate_key <environment>
python manage.py credentials_generate_key <environment> --role readonly
python manage.py credentials_edit <environment>

To load the credentials in your Django app:

from secure_credentials_kit.secrets_loader import decrypt_credentials
credentials = decrypt_credentials("environment")

Where credentials is an instance of class CredentialsContainer containing the decrypted credentials.

FastAPI Usage

Load credentials into FastAPI application state:

from fastapi import Depends, FastAPI
from secure_credentials_kit.fastapi import (
    credentials_dependency,
    setup_secure_credentials_kit,
)

app = FastAPI()
setup_secure_credentials_kit(app, "production")


@app.get("/settings")
def settings(credentials=Depends(credentials_dependency())):
    return {"api_host": credentials.get("api_host")}

If no environment is passed to setup_secure_credentials_kit, the helper checks SECURE_CREDENTIALS_KIT_ENV, FASTAPI_ENV, ENV, then falls back to development.

Accessing Credentials

To access a credential:

credentials.get('key')

or

credentials.dig('key', 'subkey')

for complex nested credentials.

Project details


Download files

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

Source Distribution

secure_credentials_kit-0.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

secure_credentials_kit-0.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file secure_credentials_kit-0.1.0.tar.gz.

File metadata

  • Download URL: secure_credentials_kit-0.1.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for secure_credentials_kit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d8134fb83a96ac52a63cacfa568452d8a87f36056804f3fb289ff12b00b57b05
MD5 cd8a7a992e4ca6b2d88dbc0203126e20
BLAKE2b-256 6d0aa76a60752c6b3153ca1fa56a971b693dd4ed95a1d59b22cdc3e744144fbe

See more details on using hashes here.

File details

Details for the file secure_credentials_kit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for secure_credentials_kit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dff7cfbfd238c1f98489c5020254dfb9112a617e0bee4bc07dffb92d52da715c
MD5 c05cb10aacc85e078e78a6051f1cdaf2
BLAKE2b-256 61c54f26728b792f94d8735a6a01268872b9e65fb83f6373db95b105bc4e819a

See more details on using hashes here.

Supported by

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