Azure App Configuration setting source for pydantic-settings
Project description
pydantic-settings-azure-app-configuration
Introduction
https://learn.microsoft.com/en-us/azure/azure-app-configuration/overview
Installation
pip install pydantic-settings-azure-app-configuration
Usage
By default, it loads all the values from Azure App Configuration, but you can assign a prefix to your application (e.g. my_api__
) and load only the values for it using the select_key
method. You can also use the trim_key_prefix
method to remove the prefix from the value names.
Furthermore, if some value references a secret stored in Azure Key Vault, you can use the configure_key_vault
method to retrieve it.
If you use Entra ID authentication, you can use the role App Configuration Data Reader
to access the configurations and Key Vault Secrets User
to access the secrets.
To nest models you have to define a env_nested_delimiter
(e.g. __
), either in the source constructor or in the model_config
class.
The configuration of this settings source is almost idental to the the provided by ASP.NET Core, in case you want to read the official documentation to inform you about more complex uses, best practices, etc.
import os
from azure.identity import DefaultAzureCredential
from pydantic import BaseModel
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource
from src.pydantic_settings_azure_app_configuration.source import (
AzureAppConfigurationSettingsSource,
)
import os
from azure.identity import DefaultAzureCredential
from pydantic import BaseModel
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource
from src.pydantic_settings_azure_app_configuration.source import (
AzureAppConfigurationSettingsSource,
)
class SqlServer(BaseModel):
password: str
host: str
class AzureKeyVaultSettings(BaseSettings):
logging_level: str
sql_server: SqlServer
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
azure_credential = DefaultAzureCredential()
azure_app_configuration = AzureAppConfigurationSettingsSource(
settings_cls,
lambda app_configuration_options: app_configuration_options.connect_with_url(
os.environ["AZURE_APP_CONFIGURATION_URL"], azure_credential
)
.select_key("my_api__*")
.trim_key_prefix("my_api__")
.configure_key_vault(
lambda key_vault_options: key_vault_options.set_credential(
azure_credential
)
),
env_nested_delimiter="__",
)
return (
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
azure_app_configuration,
)
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
File details
Details for the file pydantic_settings_azure_app_configuration-1.0.1.tar.gz
.
File metadata
- Download URL: pydantic_settings_azure_app_configuration-1.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96904e007b750d7336152c0611c893c5d4054fa654f28bd5f57f34e8355f5ac0 |
|
MD5 | 480bacda7c07221516d569fe65926a69 |
|
BLAKE2b-256 | 874c5af775b1e89224633972fcda2d21f00f70174288cc53e8cea5d78951c796 |
File details
Details for the file pydantic_settings_azure_app_configuration-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_settings_azure_app_configuration-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22c417674167514ff979b2ee8ed341412f5b51d957c8cd4f22f7a12171111285 |
|
MD5 | ae4005df9e7229f29781d2442b7c9ccd |
|
BLAKE2b-256 | 8ca474a4d184dfd65d7d5ed8c2128b4d00b9e936e07edf0fee0773bce31e686b |