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 SettingsConfigDict
from pydantic_settings_infisical import InfisicalBaseSettings


class Settings(InfisicalBaseSettings):
    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


settings = Settings()

InfisicalBaseSettings wires Infisical into the source chain for you, with precedence init kwargs → Infisical → env → dotenv → file secrets.

Custom source ordering

Need a different precedence, or source kwargs like raise_on_error=True? Use the source directly and override settings_customise_sources yourself:

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>")

    DB_HOST: str

    @classmethod
    def settings_customise_sources(cls, settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings):
        return (
            init_settings,
            InfisicalSettingsSource(settings_cls, raise_on_error=True),
            env_settings,
            dotenv_settings,
            file_secret_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.2.0.tar.gz (6.5 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.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pydantic_settings_infisical-0.2.0.tar.gz
Algorithm Hash digest
SHA256 238f301fe843430a804eaeb66eb5dce489c167aeae3265137513f40dea752fed
MD5 eb316d7b9233f19f2c869f23adf90788
BLAKE2b-256 e694dd5d9edc0be69bda2271af5a8352911e265e6af958bbd4204443af50b09c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_settings_infisical-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 089733271190da898bb44c703eb3951dafe952893cc7c9dfabd740086e3c52de
MD5 117852f3c8fc0c3aa5a6faaba006a069
BLAKE2b-256 84f97bf0b72db2bea3e78fe28c8f0eff9e46b1d6c856abdcadf4616eb3237dc6

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