Skip to main content

Azure App Configuration setting source for pydantic-settings

Project description

pydantic-settings-azure-app-configuration

pypi license

Introduction

https://learn.microsoft.com/en-us/azure/azure-app-configuration/overview

Installation

uv add 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 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


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

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

File details

Details for the file pydantic_settings_azure_app_configuration-1.1.0.tar.gz.

File metadata

File hashes

Hashes for pydantic_settings_azure_app_configuration-1.1.0.tar.gz
Algorithm Hash digest
SHA256 cf5450c01cfb2b7a5e204e425c86b15b4eac6e8c38f16d9d0c7b614eda7ef1ee
MD5 45fa6e13d39fd6b08177899a98d8889d
BLAKE2b-256 9578514e5dae72a5c5f45ef0b8c455651e87f9ac3c7d6a8841f515aa91adf5f1

See more details on using hashes here.

File details

Details for the file pydantic_settings_azure_app_configuration-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_settings_azure_app_configuration-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1cc3b7c8e661eb8cdbd85eee6eda036152d83c808d6cb7be5f1224fc403948fa
MD5 9b496b10ba1a79c2c62c12c106d90352
BLAKE2b-256 dc2a81d5f3af5cca2193faa528af7daf21f410174e09c6bf474b4f9b5838b515

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