Skip to main content

Upload secrets from a .env file to Azure Key Vault and fetch them easily.

Project description

keyvault-env

Prerequisites

  • An Azure account and access to an Azure Key Vault .
  • Azure CLI installed and logged in (az login).

🔑 Authentication

Before using the library, ensure you are authenticated with Azure:

az login

The library uses DefaultAzureCredential, which supports multiple authentication methods (CLI, Managed Identity, Service Principal, etc.).


🛠️ Usage

1. Initialize the Client

from keyvault_env import KeyVaultEnv # Initialize with Key Vault URI kv = KeyVaultEnv(kv_uri="https://your-vault-name.vault.azure.net") # Or with just the vault name kv = KeyVaultEnv(vault_name="your-vault-name")

2. Upload Secrets from .env

# Upload all secrets from .env kv.set_secrets_from_env() # Upload only secrets with a specific prefix kv.set_secrets_from_env(prefix="SECRET_") # Exclude specific keys kv.set_secrets_from_env(exclude_keys=["DB_PASSWORD","API_KEY"])

3. Fetch Secrets

# Get a single secret secret_value = kv.get_secret("my-secret-name") # Get multiple secrets secrets = kv.get_secrets(["secret1","secret2"]) # List all secret names secret_names = kv.list_secret_names() # Get all secrets (optionally filtered by prefix) all_secrets = kv.get_all_secrets(prefix_filter="SECRET_")

📖 Function Reference

KeyVaultEnv(vault_name=None, kv_uri=None)

  • Parameters :
  • vault_name: Name of the Azure Key Vault (e.g., my-vault).
  • kv_uri: Full URI of the Key Vault (e.g., https://my-vault.vault.azure.net).
  • Raises : ValueError if neither vault_name nor kv_uri is provided.

set_secrets_from_env(env_path=".env", prefix="SECRET_", exclude_keys=None, normalize_names=True)

  • Parameters :
  • env_path: Path to the .env file.
  • prefix: Only upload keys starting with this prefix.
  • exclude_keys: List of keys to exclude from upload.
  • normalize_names: If True, normalizes secret names for Key Vault compatibility.
  • Returns : Dictionary of uploaded secrets.

set_secret(secret_name, value)

  • Parameters :
  • secret_name: Name of the secret.
  • value: Value of the secret.
  • Returns : None

get_secret(secret_name)

  • Parameters :
  • secret_name: Name of the secret to fetch.
  • Returns : Value of the secret, or None if not found.

get_secrets(secret_names)

  • Parameters :
  • secret_names: List of secret names to fetch.
  • Returns : Dictionary of secret names and values.

list_secret_names(prefix_filter=None)

  • Parameters :
  • prefix_filter: Only list secrets starting with this prefix.
  • Returns : List of secret names.

get_all_secrets(prefix_filter=None)

  • Parameters :
  • prefix_filter: Only fetch secrets starting with this prefix.
  • Returns : Dictionary of all secrets.

📝 Notes

  • Environment Variables : The library respects KEYVAULT_URI and KEYVAULT_NAME environment variables for configuration.
  • Normalization : Secret names are normalized to be Key Vault-compatible (lowercase, alphanumeric, hyphens).
  • Error Handling : Errors are logged and skipped where possible.

💡 Example

from keyvault_env import KeyVaultEnv # Initialize kv = KeyVaultEnv(vault_name="my-vault") # Upload secrets kv.set_secrets_from_env(prefix="SECRET_") # Fetch secrets secrets = kv.get_all_secrets(prefix_filter="SECRET_") print(secrets)

📄 License

MIT

--- You can **[download the README.md file here](sandbox/README.md)**.

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

keyvault_env-1.0.4.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

keyvault_env-1.0.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file keyvault_env-1.0.4.tar.gz.

File metadata

  • Download URL: keyvault_env-1.0.4.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for keyvault_env-1.0.4.tar.gz
Algorithm Hash digest
SHA256 1565b68c8a0f2f056884fa2ad5a0a6a07f19fbc094aedb584dc14bd730bc2d65
MD5 c5a67b96f3aea5087ea7fcdbcdd9984e
BLAKE2b-256 764e3b03afea354109989f0b6282437011e9570cfcac92a62aae5ce726bfa4cb

See more details on using hashes here.

File details

Details for the file keyvault_env-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: keyvault_env-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for keyvault_env-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4dd9a19a031093834b958a067edbe691cc875f5156fed25ed2ea00839de61585
MD5 69c6c26ed6b95a1b03757696bb0c8ce7
BLAKE2b-256 7ff635d41908c13fdb22a0fd86324503d04cc16f844e60c59f04a17c9d07072c

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