import envresolve
from envresolve.providers.azure_kv import AzureKVProvider
from azure.identity import ManagedIdentityCredential
# Create custom provider with specific credential
custom_provider = AzureKVProvider(
credential=ManagedIdentityCredential(client_id="your-client-id")
)
# Register the custom provider
envresolve.register_azure_kv_provider(provider=custom_provider)
# Now use envresolve as normal
secret = envresolve.resolve_secret("akv://vault/secret")
This is particularly useful for:
Testing: Inject mock providers without patching internal implementation details
Custom authentication: Use specific Azure credentials (service principal, managed identity, etc.)
Provider configuration: Pre-configure providers with custom settings
Resolve Existing Environment Variables
Resolve secret URIs already set in os.environ (useful for containerized applications):
import os
import envresolve
# Environment variables set by container orchestrator or parent process
os.environ["API_KEY"] = "akv://prod-vault/api-key"
os.environ["DB_PASSWORD"] = "akv://prod-vault/db-password"
# Requires: pip install envresolve[azure]
envresolve.register_azure_kv_provider()
# Resolve all environment variables containing secret URIs
resolved = envresolve.resolve_os_environ()
# Resolve only specific keys
resolved = envresolve.resolve_os_environ(keys=["API_KEY"])
# Resolve variables with prefix and strip the prefix
# DEV_API_KEY -> API_KEY, DEV_DB_URL -> DB_URL
os.environ["DEV_API_KEY"] = "akv://dev-vault/api-key"
os.environ["DEV_DB_URL"] = "akv://dev-vault/db-url"
resolved = envresolve.resolve_os_environ(prefix="DEV_")
# Ignore specific variables (exact match)
os.environ["PS1"] = "${USER}@${HOST}$ " # Should not be expanded
os.environ["API_KEY"] = "akv://vault/api-key"
resolved = envresolve.resolve_os_environ(ignore_keys=["PS1"])
# Ignore variables by pattern (glob matching)
os.environ["PS1"] = "${USER}@${HOST}$ "
os.environ["PS2"] = "> "
os.environ["PROMPT"] = "${PWD}$ "
os.environ["API_KEY"] = "akv://vault/api-key"
resolved = envresolve.resolve_os_environ(ignore_patterns=["PS*", "PROMPT*"])
# Combine exact match and patterns
resolved = envresolve.resolve_os_environ(
ignore_keys=["SPECIFIC_VAR"],
ignore_patterns=["TEMP_*", "DEBUG_*"]
)
Error Handling
Resolution errors include context about which environment variable failed:
# Skip variables with expansion errors
resolved = envresolve.load_env(stop_on_expansion_error=False)
# Skip variables with secret resolution errors
resolved = envresolve.load_env(stop_on_resolution_error=False)
stop_on_expansion_error=False also skips variables whose values use
${...} syntax that envresolve does not support (for example bash's
${VAR:+word}, as found in the default Debian/Ubuntu PS1). This is
useful when resolving os.environ, which may contain shell-provided
variables that were never meant to be expanded:
import os
import envresolve
# Default Debian/Ubuntu PS1 uses unsupported ${VAR:+word} syntax
os.environ["PS1"] = "${debian_chroot:+($debian_chroot)}"
# PS1 is skipped; the remaining variables are still resolved
resolved = envresolve.resolve_os_environ(stop_on_expansion_error=False)
Installation
# Basic installation (variable expansion only)
pip install envresolve
# With Azure Key Vault support
pip install envresolve[azure]
This project uses uv for dependency management and nox for task automation:
# Install uv (if not already installed)
pip install uv
# Clone the repository
git clone https://github.com/osoekawaitlab/envresolve.git
cd envresolve
# Install dependencies and create virtual environment
uv sync --all-extras --all-groups
# Activate virtual environment
source .venv/bin/activate # On Unix/macOS
# .venv\Scripts\activate # On Windows
Running Tests
# Quick test during development
nox -s tests_unit # Unit tests only (fast)
nox -s tests_e2e # E2E tests with mocked Azure SDK
# Full test suite
nox -s tests # All tests with coverage report (HTML in htmlcov/)
# Test across Python versions
nox -s tests_all_versions # Test on Python 3.10-3.14
# Test without Azure SDK
nox -s tests_without_azure # For environments without Azure dependencies
Code Quality
# Run all quality checks
nox -s quality # Type checking (mypy) + linting (ruff)
# Individual checks
nox -s mypy # Type checking only
nox -s lint # Linting only
nox -s format_code # Auto-format code
# Run everything
nox -s check_all # Tests + quality checks
Live Azure Tests
Optional integration tests against real Azure Key Vault:
# One-time setup
cd infra/terraform
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your Azure credentials
terraform init
terraform apply
# Before running tests
az login
source scripts/setup_live_tests.sh
# Run live tests
nox -s tests_live