Skip to main content

OpenBao/Vault secrets source for pydantic-settings with AppRole authentication

Project description

openbao-pydantic-settings-adapter

PyPI version Python 3.11+ License: PolyForm Noncommercial

OpenBao/Vault secrets source for pydantic-settings with AppRole authentication, automatic token renewal, and graceful fallback to environment variables.

Features

  • AppRole Authentication - Secure machine-to-machine authentication
  • Automatic Token Renewal - TokenManager handles TTL-based token lifecycle
  • Namespace Support - Multi-tenant isolation (team/project level)
  • Two-Path Architecture - Separate secrets and supersecrets paths with deep merge
  • SecretStr Validation - Enforces SecretStr for supersecrets to prevent log exposure
  • Graceful Degradation - Falls back to .env when OpenBao is unavailable
  • Type Safety - Full type annotations with py.typed marker (PEP 561)

Installation

pip install openbao-pydantic-settings-adapter

Note: The import name is openbao_settings:

from openbao_settings import OpenBaoSettingsSource

Quick Start

from pydantic import SecretStr
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource

from openbao_settings import OpenBaoSettingsSource


class Settings(BaseSettings):
    database_url: str
    api_key: SecretStr  # Fields from supersecrets MUST use SecretStr

    @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, ...]:
        return (
            init_settings,
            OpenBaoSettingsSource(settings_cls),  # OpenBao has priority
            env_settings,
            dotenv_settings,
        )


settings = Settings()

Configuration

Configure via environment variables:

Variable Description Required
BAO_ADDR OpenBao server URL (e.g., http://localhost:8200) Yes
BAO_SECRET_PATH Base path to secrets (e.g., myapp) Yes
BAO_APPROLE_ROLE_ID AppRole Role ID Yes
BAO_APPROLE_SECRET_ID AppRole Secret ID Yes
BAO_NAMESPACE OpenBao namespace for multi-tenant isolation No
BAO_MOUNT_POINT KV engine mount point (default: kv) No
BAO_TIMEOUT Connection timeout in seconds (default: 30) No
BAO_TOKEN_RENEWAL_THRESHOLD When to renew token (default: 0.75 = at 75% of TTL) No

For Docker/Kubernetes, you can use file-based credentials:

  • BAO_APPROLE_ROLE_ID_FILE
  • BAO_APPROLE_SECRET_ID_FILE
  • BAO_NAMESPACE_FILE

Secrets Architecture

The library reads from two paths and merges them:

kv/{BAO_SECRET_PATH}/secrets      - Regular secrets (developers can view/edit)
kv/{BAO_SECRET_PATH}/supersecrets - Sensitive secrets (admin only)

Important: Fields loaded from supersecrets MUST use SecretStr type annotation. The library validates this at runtime and raises SecurityMisconfigurationError if violated.

# Wrong - will raise SecurityMisconfigurationError
class Settings(BaseSettings):
    api_key: str  # Loaded from supersecrets but not SecretStr!

# Correct
class Settings(BaseSettings):
    api_key: SecretStr  # Properly protected

Token Lifecycle

TokenManager automatically handles token renewal:

  1. Caches tokens per (namespace, role_id) combination
  2. Renews at 75% of TTL (configurable via BAO_TOKEN_RENEWAL_THRESHOLD)
  3. Thread-safe for multi-threaded applications
  4. Graceful degradation if OpenBao becomes unavailable
from openbao_settings import TokenManager

# Manual token invalidation (e.g., for credential rotation)
TokenManager().invalidate()

# Check token health
TokenManager().is_healthy(namespace="myapp", role_id="...")

Diagnostics

Track where settings were loaded from:

from openbao_settings import get_last_source_info

info = get_last_source_info()
print(info["source"])  # "openbao" or "env"
print(info["details"])  # Human-readable description
print(info["openbao_keys_loaded"])  # Number of keys from OpenBao

API Reference

Classes

  • OpenBaoSettingsSource - Main settings source for pydantic-settings
  • OpenBaoClient - Low-level HTTP client for OpenBao API
  • TokenManager - Singleton for token lifecycle management

Exceptions

  • OpenBaoError - Base exception for all OpenBao errors
  • InvalidPathError - Path not found (HTTP 404)
  • InvalidRequestError - Invalid request/credentials (HTTP 400)
  • ForbiddenError - Access denied (HTTP 403)
  • InvalidResponseError - Unexpected API response structure
  • SecurityMisconfigurationError - SecretStr validation failed

Response Models

  • AppRoleLoginResponse - AppRole authentication response
  • KvV2ReadResponse - KV v2 read response

License

This project is licensed under the PolyForm Noncommercial License 1.0.0.

You may use this software for noncommercial purposes only.

See LICENSE for the full license text, or visit polyformproject.org.

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

openbao_pydantic_settings_adapter-1.0.3.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file openbao_pydantic_settings_adapter-1.0.3.tar.gz.

File metadata

File hashes

Hashes for openbao_pydantic_settings_adapter-1.0.3.tar.gz
Algorithm Hash digest
SHA256 8a6ab67243bbec7f0feb7ca73dedb35be974b68d238bd067d6b055f660383041
MD5 7837c2da22399f1d1b09de8e44cdefd7
BLAKE2b-256 df59d95a5e5487a20c3daf1cbd524bd5463e9b2d0a8af7ee5fbd15e4d83bfa16

See more details on using hashes here.

Provenance

The following attestation bundles were made for openbao_pydantic_settings_adapter-1.0.3.tar.gz:

Publisher: publish.yml on itstandart/openbao-pydantic-settings-adapter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openbao_pydantic_settings_adapter-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for openbao_pydantic_settings_adapter-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1c8fae42794f99b2afb0c38183930b4aefa1342babed9a4adc61942074d54f0a
MD5 0f3d504618d47c5dcb92d66717589e56
BLAKE2b-256 9ede6a47abd2ea23511c29a1bcbf43381a486019d558a89534edcbf00bbe4782

See more details on using hashes here.

Provenance

The following attestation bundles were made for openbao_pydantic_settings_adapter-1.0.3-py3-none-any.whl:

Publisher: publish.yml on itstandart/openbao-pydantic-settings-adapter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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