repositories
azure_bootstrap.repositories
¶
Repository implementations for Azure bootstrap library.
This module contains concrete implementations of configuration and secrets repositories.
Modules:
| Name | Description |
|---|---|
enhanced_config_repository |
Enhanced Config Repository for hierarchical configuration management. |
interfaces |
Repository interfaces for Azure bootstrap library. |
secrets_repository |
Secrets Repository implementation for Azure Key Vault integration. |
Classes:
| Name | Description |
|---|---|
EnhancedConfigRepository |
Enhanced configuration repository with hierarchical lookup. |
SecretsRepository |
Implementation of secrets repository for Azure Key Vault. |
EnhancedConfigRepository
¶
EnhancedConfigRepository(app_config_connection_string: str | None = None, secrets_repository: SecretsRepositoryInterface | None = None, auto_load_to_environ: bool = False)
Bases: EnhancedConfigRepositoryInterface
Enhanced configuration repository with hierarchical lookup.
This repository provides unified configuration access with automatic precedence: 1. Environment variables (highest priority) 2. Azure App Configuration with automatic Key Vault reference resolution 3. Key Vault secrets (via secrets repository, fallback only) 4. Default values (lowest priority)
Features: - Hierarchical configuration lookup - Azure App Configuration integration with built-in Key Vault resolution - Automatic Key Vault reference resolution when secrets are stored as references - Fallback to direct Key Vault access for non-referenced secrets - Automatic loading to os.environ - Configuration caching for performance - Comprehensive logging
Key Vault Integration: - When secrets are stored in App Config as Key Vault references (JSON format), they are automatically resolved by the Azure SDK - Reference format: {"uri": "https://vault.vault.azure.net/secrets/secretname"} - Requires Managed Identity with "App Configuration Data Reader" and "Key Vault Secrets User" RBAC roles
Usage
With App Config (Key Vault references auto-resolved)¶
config_repo = EnhancedConfigRepository( app_config_connection_string="...", auto_load_to_environ=True )
Without App Config (environment only)¶
config_repo = EnhancedConfigRepository() db_host = config_repo.get_value("DATABASE_HOST", "localhost")
Initialize the enhanced configuration repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_config_connection_string
|
str | None
|
Azure App Configuration connection string |
None
|
secrets_repository
|
SecretsRepositoryInterface | None
|
Optional secrets repository for direct Key Vault fallback |
None
|
auto_load_to_environ
|
bool
|
If True, automatically load configs to os.environ on init |
False
|
Methods:
| Name | Description |
|---|---|
get_value |
Get a configuration value with hierarchical lookup. |
get_secret_value |
Get a secret value from Key Vault. |
get_all_values |
Get all configuration values from all sources. |
load_to_environ |
Load configuration values to os.environ. |
refresh |
Refresh configuration from all sources. |
get_repository_metrics |
Get metrics about the configuration repository. |
clear_cache |
Clear the configuration cache. |
is_available |
Check if the configuration repository is available. |
is_app_config_available |
Check if Azure App Configuration is available and accessible. |
is_key_vault_available |
Check if Azure Key Vault is available and accessible. |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
get_value
¶
Get a configuration value with hierarchical lookup.
Lookup order: 1. Environment variables (highest priority) 2. Cache (if previously retrieved) 3. Azure App Configuration with automatic Key Vault reference resolution 4. Key Vault secrets (via secrets repository, fallback) 5. Default value (lowest priority)
Note: If a value is stored in App Config as a Key Vault reference, it will be automatically resolved by the provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key name |
required |
default
|
str | None
|
Default value if key not found |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: Configuration value or default |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
get_secret_value
¶
Get a secret value from Key Vault.
This method specifically targets secrets and bypasses the standard configuration hierarchy to directly access Key Vault.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Secret key name |
required |
default
|
str | None
|
Default value if secret not found |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: Secret value or default |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
get_all_values
¶
Get all configuration values from all sources.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict[str, str]: All configuration key-value pairs with Key Vault references resolved |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
load_to_environ
¶
load_to_environ() -> int
Load configuration values to os.environ.
Precedence Logic: 1. Values already in os.environ are PRESERVED (local.settings.json wins) 2. Missing values are added from App Configuration (fill gaps) 3. This allows local development overrides while providing defaults
Example
local.settings.json sets:¶
os.environ["USE_MOCK_SHAREPOINT"] = "true"
App Config has:¶
config["USE_MOCK_SHAREPOINT"] = "false" config["NEW_CONFIG"] = "value"
After load_to_environ():¶
os.environ["USE_MOCK_SHAREPOINT"] = "true" # ✅ Local preserved os.environ["NEW_CONFIG"] = "value" # ✅ Remote added
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of NEW values added to os.environ (excludes skipped) |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
refresh
¶
Refresh configuration from all sources.
This method clears the cache and reloads configuration from App Configuration (with Key Vault references resolved) to pick up any changes.
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
get_repository_metrics
¶
Get metrics about the configuration repository.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict[str, Any]: Metrics including source counts, cache hits, etc. |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
clear_cache
¶
Clear the configuration cache.
This method clears all cached configuration values, forcing the next get_value call to retrieve fresh data from sources.
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
is_available
¶
is_available() -> bool
Check if the configuration repository is available.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if at least one configuration source is available |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
is_app_config_available
¶
is_app_config_available() -> bool
Check if Azure App Configuration is available and accessible.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if App Config is accessible, False otherwise |
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
SecretsRepository
¶
SecretsRepository(vault_url: str | None = None)
Bases: SecretsRepositoryInterface
Implementation of secrets repository for Azure Key Vault.
This repository provides access to secrets from Azure Key Vault with graceful fallback to environment variables when Key Vault is not available.
Features: - Azure Key Vault integration (when available) - Environment variable fallback for local development - Caching for performance - Comprehensive logging and error handling
Usage
With Key Vault¶
secrets_repo = SecretsRepository(vault_url="https://myvault.vault.azure.net/") db_password = secrets_repo.get_secret("database-password")
Without Key Vault (environment variables only)¶
secrets_repo = SecretsRepository() db_password = secrets_repo.get_secret("DATABASE_PASSWORD")
Initialize the secrets repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vault_url
|
str | None
|
Optional Azure Key Vault URL (e.g., "https://myvault.vault.azure.net/") If not provided, uses AZURE_KEY_VAULT_URL from environment |
None
|
Methods:
| Name | Description |
|---|---|
get_secret |
Retrieve a secret value by name. |
set_secret |
Store a secret value (Key Vault only, not environment). |
delete_secret |
Delete a secret from Key Vault. |
list_secrets |
List all available secrets (names only for security). |
is_available |
Check if secrets repository is available and accessible. |
clear_cache |
Clear the secrets cache. |
Source code in azure_bootstrap/repositories/secrets_repository.py
get_secret
¶
Retrieve a secret value by name.
Lookup order: 1. Cache (if previously retrieved) 2. Azure Key Vault (if available) 3. Environment variables (fallback)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret_name
|
str
|
Name of the secret to retrieve |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: Secret value if found, None otherwise |
Source code in azure_bootstrap/repositories/secrets_repository.py
set_secret
¶
Store a secret value (Key Vault only, not environment).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret_name
|
str
|
Name of the secret |
required |
secret_value
|
str
|
Value to store |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise |
Source code in azure_bootstrap/repositories/secrets_repository.py
delete_secret
¶
Delete a secret from Key Vault.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret_name
|
str
|
Name of the secret to delete |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise |
Source code in azure_bootstrap/repositories/secrets_repository.py
list_secrets
¶
List all available secrets (names only for security).
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict[str, str]: Dictionary mapping secret names to metadata (not values) |