Skip to main content

Decouple config with support for secrets directory (e.g., Docker secrets)

Project description

decouplet

A tiny wrapper around python-decouple that adds first-class support for reading secrets from a directory (e.g. Docker secrets mounted at /run/secrets/).

Drop-in replacement for python-decouple's config — same API, extra secrets support.

Source code: https://gitlab.com/mdigital-public/backend/decouplet

Documentation

Installation

pip install decouplet

Install directly from the public GitLab repository:

pip install "git+https://gitlab.com/mdigital-public/backend/decouplet.git"

Usage

from decouplet import config

# Read a value — checked in secrets dir first, then env vars / .env or settings.ini
DATABASE_PASSWORD = config("DATABASE_PASSWORD")

# With a default
DEBUG = config("DEBUG", default="False")

# With type casting (inherited from python-decouple)
PORT = config("PORT", default=8000, cast=int)
IS_ENABLED = config("IS_ENABLED", default=False, cast=bool)

Priority order

When looking up a key, decouplet checks sources in this order:

  1. Secrets directory — files in /run/secrets/ (or the path set via SECRETS_PATH)
  2. Environment variables
  3. settings.ini or .env file (standard python-decouple behaviour)
  4. Default value (if provided)

Secrets always win. This matches the expected behaviour when deploying with Docker Swarm or Kubernetes secrets.

Secrets directory

Each secret is stored as a plain-text file whose filename (uppercased) becomes the key and whose contents become the value. Trailing whitespace/newlines are stripped automatically.

/run/secrets/
    database_password   →  key: DATABASE_PASSWORD
    api_key             →  key: API_KEY

Changing the secrets path

Set the SECRETS_PATH environment variable to point to a different directory:

SECRETS_PATH=/my/secrets python app.py

Or in your .env file:

SECRETS_PATH=/my/secrets

Or in settings.ini:

[settings]
SECRETS_PATH=/my/secrets

If the directory does not exist, decouplet silently skips it and falls back to env vars / settings.ini / .env.

Encoding

decouplet follows python-decouple's lazy loading behavior, so you can set file encoding before the first lookup:

from decouplet import config

config.encoding = "cp1251"
SECRET_KEY = config("SECRET_KEY")

Docker example

See the runnable Docker Compose example in examples/docker-secrets-example.

# docker-compose.yml
services:
  app:
    image: myapp
    secrets:
      - database_password

secrets:
  database_password:
    file: ./secrets/database_password.txt
# app.py
from decouplet import config

DB_PASSWORD = config("DATABASE_PASSWORD")  # read from /run/secrets/database_password

Requirements

  • Python >= 3.9
  • python-decouple >= 3.8

Links

License

MIT

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

decouplet-0.2.4.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

decouplet-0.2.4-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file decouplet-0.2.4.tar.gz.

File metadata

  • Download URL: decouplet-0.2.4.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for decouplet-0.2.4.tar.gz
Algorithm Hash digest
SHA256 d1724299db0f614e8451a64a8e8887f0c789aba8357fbb7454bcb543659954e1
MD5 b6e405bf6e68ba44a54c060cfc04ffa1
BLAKE2b-256 a037f8ddd447cf705233e86723819f2c06abeda63c9a720b0d68c7fdd2221831

See more details on using hashes here.

File details

Details for the file decouplet-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: decouplet-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for decouplet-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7105da8c26910d40d5c8721415570d47ce88e6d8a7cae9d3dd35e755fa5cf654
MD5 92317a9f3be26e82bf776a34988bbf8c
BLAKE2b-256 7f003ecfd6eb349d79c084290b5d896b09c733588ef57753657063c0c575890e

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