Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the recommended way to access settings loaded from previous sources? #319

Closed
AndreuCodina opened this issue Jun 23, 2024 · 1 comment
Assignees

Comments

@AndreuCodina
Copy link
Contributor

AndreuCodina commented Jun 23, 2024

My BaseSettings uses 2 settings sources: env_settings and Azure Key Vault.

class ApplicationSettings(BaseSettings):
    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Any,
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource]:
        return (
            env_settings,
            AzureKeyVaultSettingsSource(
                settings_cls,
                os.environ['AZURE_KEY_VAULT__URL'],
                DefaultAzureCredential()
            ),
        )

In a .env file I have the URL to access to Azure Key Vault:

AZURE_KEY_VAULT="https://my-resource.vault.azure.net/"

And then in ApplicationSettings I need to use the URL provided by env_settings to configure the Azure Key Vault setting source.

In ASP.NET Core we can do this because each setting source loads the settings in a common place, and they're loaded one by one. For example:

builder.Configuration.AddJsonFile("...");

var keyVaultUrl = builder.Configuration.GetValue<string>("AzureKeyVault");

builder.Configuration.AddAzureKeyVault(keyVaultUrl, ...);
@AndreuCodina AndreuCodina changed the title What's the recommended way access settings loaded from previous sources? What's the recommended way to access settings loaded from previous sources? Jun 23, 2024
@hramezani
Copy link
Member

In pydantic-settings V2(current major version), you can't access the values loaded by other settings sources.
There is an open issue for that.

You can have a different settings class for this and load it before.

class AzureKeyVaultSettings(BaseSettings):
    url: str


class ApplicationSettings(BaseSettings):
    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Any,
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource]:
        s = AzureKeyVaultSettings()
        return (
            env_settings,
            AzureKeyVaultSettingsSource(
                settings_cls,
                s.url,
                DefaultAzureCredential()
            ),
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants