Skip to main content

An Infisical secrets source for pydantic-settings.

Project description

pydantic-settings-infisical

An Infisical secrets source for pydantic-settings.

Declare your configuration as a typed BaseSettings class and load values from an Infisical project — alongside (and with the usual precedence over) environment variables and dotenv files.

Install

pip install pydantic-settings-infisical

Usage

from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic_settings_infisical import InfisicalSettingsSource


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        infisical_project_id="<your-infisical-project-id>",
        infisical_environment="prod",   # default: "prod"
        # infisical_host="https://app.infisical.com",  # or self-hosted
        # infisical_secret_path="/",
    )

    DB_HOST: str
    DB_PASSWORD: str

    @classmethod
    def settings_customise_sources(cls, settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings):
        return (
            init_settings,
            InfisicalSettingsSource(settings_cls),  # Infisical wins over env/dotenv here
            env_settings,
            dotenv_settings,
            file_secret_settings,
        )


settings = Settings()

Authentication

Set one of the following (via env var, model_config, or a source kwarg):

  • Service token: INFISICAL_TOKEN
  • Machine identity (Universal Auth): INFISICAL_CLIENT_ID + INFISICAL_CLIENT_SECRET

Configuration

Every connection parameter resolves in this order: explicit source kwarg → model_config key → environment variable.

Parameter model_config key Environment variable Default
host infisical_host INFISICAL_HOST https://app.infisical.com
project id infisical_project_id INFISICAL_PROJECT_ID (required)
environment infisical_environment INFISICAL_ENVIRONMENT prod
secret path infisical_secret_path INFISICAL_SECRET_PATH /

Mapping fields to secret names

Each field is looked up by its Infisical secret name, resolved from the field's validation_alias (the first string of an AliasChoices), else its alias, else the field name. This lets you map Python-friendly field names to Infisical's naming:

from pydantic import Field, AliasChoices

class Settings(BaseSettings):
    # reads the Infisical secret "POSTGRES--HOST", falls back to the DB_HOST env var
    db_host: str = Field(validation_alias=AliasChoices("POSTGRES--HOST", "DB_HOST"))

Error handling

By default a missing secret or any Infisical error is non-fatal — the value simply falls through to the remaining settings sources (env, dotenv, …). Pass InfisicalSettingsSource(settings_cls, raise_on_error=True) to fail loudly instead.

License

MIT

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

pydantic_settings_infisical-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

pydantic_settings_infisical-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_settings_infisical-0.1.1.tar.gz.

File metadata

File hashes

Hashes for pydantic_settings_infisical-0.1.1.tar.gz
Algorithm Hash digest
SHA256 12d37d567b1071741a99ebb3ac695306938c79ab96eb3958e271f458c9101be1
MD5 e68d8ab4206792e098c949481c8d5724
BLAKE2b-256 8cd5d48946245bb573e508f6dc3eaca5795cd3fc0eb11a9847fbb0d743c23df7

See more details on using hashes here.

File details

Details for the file pydantic_settings_infisical-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_settings_infisical-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37baa3e69a9eb5ea43d610d51cba64f87826f4f34465b5d0bafe8a52758cd35f
MD5 69adfd7876bd4dcc50f8e311ba3e1999
BLAKE2b-256 56287d2704832ff3c51da649de46ba4e01d60bf30e8fb3d9571a0c662ec46a9c

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