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.core 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 :
ValueErrorif neithervault_namenorkv_uriis provided.
set_secrets_from_env(env_path=".env", prefix="SECRET_", exclude_keys=None, normalize_names=True)
- Parameters :
env_path: Path to the.envfile.prefix: Only upload keys starting with this prefix.exclude_keys: List of keys to exclude from upload.normalize_names: IfTrue, 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
Noneif 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_URIandKEYVAULT_NAMEenvironment 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
<pre><div class="relative m-0 rounded-md border border-default bg-[#1e1e1e] whitespace-break-spaces dark:bg-subtle">``<div class="rounded-b-md select-text dark:bg-subtle!"><code class="language-python">``<span class="token">from <span> keyvault_env.core <span class="token">import <span> KeyVaultEnv
<span><span class="token"># Initialize <span>
<span>kv <span class="token">=<span> KeyVaultEnv <span class="token">(<span>vault_name <span class="token">=<span class="token">"my-vault"<span class="token">)<span>
<span><span class="token"># Upload secrets <span>
<span>kv <span class="token">.<span>set_secrets_from_env <span class="token">(<span>prefix <span class="token">=<span class="token">"SECRET_"<span class="token">)<span>
<span><span class="token"># Fetch secrets <span>
<span>secrets <span class="token">=<span> kv <span class="token">.<span>get_all_secrets <span class="token">(<span>prefix_filter <span class="token">=<span class="token">"SECRET_"<span class="token">)<span>
<span><span class="token">print <span class="token">(<span>secrets <span class="token">)</code></div>``</div></pre>
📄 License
MIT
--- You can **[download the README.md file here](sandbox/README.md)**.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file keyvault_env-1.0.5.tar.gz.
File metadata
- Download URL: keyvault_env-1.0.5.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5eec7e058d75cf8e44a004516b9ada73e741dce7c7580790381335361f0cb9c
|
|
| MD5 |
507692f04228bcd96678fe8298a243dd
|
|
| BLAKE2b-256 |
c34f18b98f03391b8af360ed0b7c178c16bce2aa5bef6797aa1e4754753c3cea
|
File details
Details for the file keyvault_env-1.0.5-py3-none-any.whl.
File metadata
- Download URL: keyvault_env-1.0.5-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa51f5880e67e7364ee01f55e163b91c590e73ba1d333d3733d0854e6e28f924
|
|
| MD5 |
4d74880c4d96c8d0911cde12b1f119e3
|
|
| BLAKE2b-256 |
c9b695be0e55c0b00d7bdee1f485e2789243a51c95d8de6be302d1c96313644a
|