Skip to main content

Python bindings for dotenvage - encrypt secrets in .env files using age encryption

Project description

dotenvage

Python bindings for dotenvage — encrypt secrets in .env files using age encryption (X25519).

Installation

pip install dotenvage
# or with uv
uv add dotenvage

Quick start

import dotenvage

# Generate a new identity (key pair)
manager = dotenvage.SecretManager.generate()
print(f"Public key: {manager.public_key_string()}")

# Encrypt a secret
encrypted = manager.encrypt_value("my-secret-password")
print(f"Encrypted: {encrypted}")
# Output: ENC[AGE:b64:...]

# Decrypt it back
decrypted = manager.decrypt_value(encrypted)
print(f"Decrypted: {decrypted}")
# Output: my-secret-password

# Check if a value is encrypted
print(dotenvage.SecretManager.is_encrypted(encrypted))  # True
print(dotenvage.SecretManager.is_encrypted("plain"))    # False

Loading .env files

import dotenvage

# Load .env files (decrypts encrypted values automatically)
loader = dotenvage.EnvLoader()
loaded_files = loader.load()
print(f"Loaded: {loaded_files}")

# Or get all variables as a dict
variables = loader.get_all_variables()
print(variables)

Auto-detection

dotenvage can detect which keys should be encrypted based on naming patterns:

import dotenvage

# Keys containing PASSWORD, SECRET, KEY, TOKEN, etc. are detected
dotenvage.should_encrypt("API_KEY")         # True
dotenvage.should_encrypt("DATABASE_URL")    # False
dotenvage.should_encrypt("SECRET_TOKEN")    # True

Key discovery

The SecretManager automatically discovers keys in this order:

  1. Auto-discover AGE_KEY_NAME from .env or .env.local files
  2. DOTENVAGE_AGE_KEY environment variable (full identity string)
  3. AGE_KEY environment variable
  4. EKG_AGE_KEY environment variable
  5. OS keychain entry (service: dotenvage or DOTENVAGE_KEYCHAIN_SERVICE; account: AGE_KEY_NAME or {CARGO_PKG_NAME}/dotenvage)
  6. Key file from AGE_KEY_NAME~/.local/state/{namespace}/{keyname}.key
  7. Default: ~/.local/state/dotenvage/dotenvage.key

OS keychain lookup currently uses:

  • macOS: Keychain via security
  • Linux/Unix: Secret Service via secret-tool
  • Windows: lookup falls back to file/env sources (no keychain lookup yet); keygen --store os|both stores using cmdkey

AGE_KEY_NAME format is {namespace}/{keyname}, e.g., myapp/production~/.local/state/myapp/production.key

API reference

SecretManager

Manager for encrypting and decrypting secrets using age encryption.

class SecretManager:
    def __init__(self) -> None:
        """Create from discovered key file."""

    @staticmethod
    def generate() -> SecretManager:
        """Generate a new random identity (key pair)."""

    @staticmethod
    def from_identity_string(identity: str) -> SecretManager:
        """Create from an age identity string (AGE-SECRET-KEY-...)."""

    @staticmethod
    def is_encrypted(value: str) -> bool:
        """Check if a value is in encrypted format."""

    def public_key_string(self) -> str:
        """Get public key as age1... string."""

    def encrypt_value(self, plaintext: str) -> str:
        """Encrypt to ENC[AGE:b64:...] format."""

    def decrypt_value(self, value: str) -> str:
        """Decrypt if encrypted, otherwise return unchanged."""

EnvLoader

Loader for .env files with automatic decryption.

class EnvLoader:
    def __init__(self) -> None:
        """Create with default SecretManager."""

    @staticmethod
    def with_manager(manager: SecretManager) -> EnvLoader:
        """Create with a specific SecretManager."""

    def load(self) -> list[str]:
        """Load .env files from current directory. Returns loaded paths."""

    def load_from_dir(self, dir: str) -> list[str]:
        """Load .env files from specific directory."""

    def get_all_variable_names(self) -> list[str]:
        """Get all variable names from .env files."""

    def get_all_variable_names_from_dir(self, dir: str) -> list[str]:
        """Get all variable names from .env files in directory."""

    def get_all_variables(self) -> dict[str, str]:
        """Load and return all variables as dict (decrypted)."""

    def get_all_variables_from_dir(self, dir: str) -> dict[str, str]:
        """Load and return all variables from directory (decrypted)."""

    def resolve_env_paths(self, dir: str) -> list[str]:
        """Get ordered list of .env file paths that would be loaded."""

Functions

def should_encrypt(key: str) -> bool:
    """Check if key name should be encrypted based on patterns."""

License

CC-BY-SA-4.0

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 Distributions

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

dotenvage-0.4.1-cp310-abi3-win_amd64.whl (519.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

dotenvage-0.4.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (641.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

dotenvage-0.4.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (601.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

dotenvage-0.4.1-cp310-abi3-macosx_11_0_arm64.whl (638.0 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

dotenvage-0.4.1-cp310-abi3-macosx_10_12_x86_64.whl (659.3 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file dotenvage-0.4.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: dotenvage-0.4.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 519.8 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dotenvage-0.4.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b771869f55c2edb4fd33b3b00c37e7effbf3b258e076ce6060e9d569caa1d50a
MD5 7f09b8dd521bdaa248957fc1bccf46a3
BLAKE2b-256 ffe0e1bc790095ca46d9e0881220059c6009c108e7685b3770052ce083ab6826

See more details on using hashes here.

Provenance

The following attestation bundles were made for dotenvage-0.4.1-cp310-abi3-win_amd64.whl:

Publisher: ci.yml on dataroadinc/dotenvage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dotenvage-0.4.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dotenvage-0.4.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 598559036ca66338361adb1892344e5098fb21ccd39c6c6d6a48c702fed50d3a
MD5 ec2ededae9b35b02a80d003cb247db23
BLAKE2b-256 653ad6f21670fa9cc046e6e0568ae8769f7729380846d3aafa29b694759d97da

See more details on using hashes here.

Provenance

The following attestation bundles were made for dotenvage-0.4.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on dataroadinc/dotenvage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dotenvage-0.4.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for dotenvage-0.4.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d291ae82192a956f42bd2941a26bc10fe2b12dc761e41a4b29e2b86485f7b0d5
MD5 ccb367abeca883114f17789429de3bfa
BLAKE2b-256 b3f3e9867e4f97079286a334b13aa35996aa9d1c7022ca5564897ac1a78c7a9a

See more details on using hashes here.

Provenance

The following attestation bundles were made for dotenvage-0.4.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: ci.yml on dataroadinc/dotenvage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dotenvage-0.4.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dotenvage-0.4.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 44ce5fb28654d6c63c232619d0d3f4193a88c8e0efb17a8759c93ccb1347e9ac
MD5 cbb792c4996e4acbd2e28e494bf5d0f7
BLAKE2b-256 55c43bbe8943d80d087e9f78f758033f6210f5d49e2c75f05a9e33752121e03c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dotenvage-0.4.1-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: ci.yml on dataroadinc/dotenvage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dotenvage-0.4.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dotenvage-0.4.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dfa71b6f0d1de1d93be925b800ad938369b20fb2b07c89f8fc4b23573d26a680
MD5 1e05cacd88c42bc63e0c1c37ec25b82f
BLAKE2b-256 37657000a803464e2c6e4d31ec75058386b811a38f1b61c7bba7a60ad07ae479

See more details on using hashes here.

Provenance

The following attestation bundles were made for dotenvage-0.4.1-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: ci.yml on dataroadinc/dotenvage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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