OpenBao/Vault secrets source for pydantic-settings with AppRole authentication
Project description
openbao-pydantic-settings-adapter
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
secretsandsupersecretspaths with deep merge - SecretStr Validation - Enforces
SecretStrfor supersecrets to prevent log exposure - Graceful Degradation - Falls back to
.envwhen OpenBao is unavailable - Type Safety - Full type annotations with
py.typedmarker (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_FILEBAO_APPROLE_SECRET_ID_FILEBAO_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:
- Caches tokens per
(namespace, role_id)combination - Renews at 75% of TTL (configurable via
BAO_TOKEN_RENEWAL_THRESHOLD) - Thread-safe for multi-threaded applications
- 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, SourceInfo
# Basic info (only OpenBao fields in field_sources)
info: SourceInfo = get_last_source_info()
print(info.status) # True if loaded from OpenBao
print(info.source) # "openbao" or "env"
print(info.details) # Human-readable description
print(info.openbao_keys_loaded) # Number of keys from OpenBao
print(info.timestamp) # When the check was performed
# Full field tracking (pass settings instance)
settings = Settings()
info = get_last_source_info(settings)
print(info.field_sources)
# {
# "db.host": "openbao/secrets",
# "db.port": "openbao/secrets",
# "db.password": "openbao/supersecrets",
# "services.otlp.domain": "openbao/secrets",
# "services.otlp.password": "openbao/supersecrets",
# "log_level": "env",
# "debug": "default"
# }
Field source values (FieldSourceType) with dot notation for nested fields:
"openbao/secrets"— loaded from secrets/ path (e.g.,"db.host")"openbao/supersecrets"— loaded from supersecrets/ path (e.g.,"db.password")"openbao/merged"— same leaf field in BOTH paths (supersecrets wins)"env"— loaded from environment variables or .env file"default"— uses field's default value
API Reference
Classes
OpenBaoSettingsSource- Main settings source for pydantic-settingsOpenBaoClient- Low-level HTTP client for OpenBao APITokenManager- Singleton for token lifecycle managementTokenEntry- Dataclass for cached token metadata (token, expires_at, ttl)
Exceptions
OpenBaoError- Base exception for all OpenBao errorsInvalidPathError- Path not found (HTTP 404)InvalidRequestError- Invalid request/credentials (HTTP 400)ForbiddenError- Access denied (HTTP 403)InvalidResponseError- Unexpected API response structureSecurityMisconfigurationError- SecretStr validation failed
Response Models
AppRoleLoginResponse- AppRole authentication responseAuthInfo- Auth block with token, TTL, policies (part ofAppRoleLoginResponse.auth)KvV2ReadResponse- KV v2 read responseSecretDataWrapper- Inner data wrapper for KV v2 secrets (part ofKvV2ReadResponse.data)SourceInfo- Settings source diagnostic infoFieldSourceType- Type alias for field source values (Literal["openbao/secrets", "openbao/supersecrets", "openbao/merged", "env", "default"])
Utilities
get_last_source_info(settings=None)- Get info about last settings load sourcedeep_merge(base, override)- Recursively merge dictionaries (override wins)
Development
Syncing with Remote
# Fetch commits and tags
git pull origin main --tags
# If local branch is behind remote
git reset --hard origin/main
Verify sync status
git log --oneline origin/main -5
git diff origin/main
CI/CD
The pipeline consists of two stages:
-
auto_tag — on push to
main, reads__version__from__init__.pyand automatically creates a tag (if it doesn't exist) -
mirror_to_github — on tag creation, mirrors the repository to GitHub (removing
.gitlab-ci.yml)
Release flow
- Update
__version__insrc/openbao_settings/__init__.py - Push to main
- CI creates tag → mirrors to GitHub → publishes to PyPI
Versioning
This project follows Semantic Versioning (MAJOR.MINOR.PATCH):
- PATCH — bug fixes, documentation, metadata
- MINOR — new features (backwards compatible)
- MAJOR — breaking changes
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openbao_pydantic_settings_adapter-1.0.6.tar.gz.
File metadata
- Download URL: openbao_pydantic_settings_adapter-1.0.6.tar.gz
- Upload date:
- Size: 23.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5336806e320b13453f75156a54980e9046e404a9cbe53863905490c5e947ac18
|
|
| MD5 |
d4fcc5f4afe2880f847f9b5183099783
|
|
| BLAKE2b-256 |
69c0425ee92376ffb1a3d2d14cf577e1e56ef748ccded05f87e59342e6e41c46
|
Provenance
The following attestation bundles were made for openbao_pydantic_settings_adapter-1.0.6.tar.gz:
Publisher:
publish.yml on itstandart/openbao-pydantic-settings-adapter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openbao_pydantic_settings_adapter-1.0.6.tar.gz -
Subject digest:
5336806e320b13453f75156a54980e9046e404a9cbe53863905490c5e947ac18 - Sigstore transparency entry: 927183814
- Sigstore integration time:
-
Permalink:
itstandart/openbao-pydantic-settings-adapter@f186b16b45d1beda62b4c10f81ef5a72269a9bbc -
Branch / Tag:
refs/tags/v1.0.6 - Owner: https://github.com/itstandart
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f186b16b45d1beda62b4c10f81ef5a72269a9bbc -
Trigger Event:
push
-
Statement type:
File details
Details for the file openbao_pydantic_settings_adapter-1.0.6-py3-none-any.whl.
File metadata
- Download URL: openbao_pydantic_settings_adapter-1.0.6-py3-none-any.whl
- Upload date:
- Size: 25.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2340c9e8a7fb99b9706e2c2b646ba759634f244b77a209211df2dcbf8d1881fb
|
|
| MD5 |
7fa3c6e4cd8de9fb7450fe74738ace99
|
|
| BLAKE2b-256 |
6efd499bcfa1da156ef16787785b824e72f398cd0f7b3ea56560384d624086cd
|
Provenance
The following attestation bundles were made for openbao_pydantic_settings_adapter-1.0.6-py3-none-any.whl:
Publisher:
publish.yml on itstandart/openbao-pydantic-settings-adapter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openbao_pydantic_settings_adapter-1.0.6-py3-none-any.whl -
Subject digest:
2340c9e8a7fb99b9706e2c2b646ba759634f244b77a209211df2dcbf8d1881fb - Sigstore transparency entry: 927183820
- Sigstore integration time:
-
Permalink:
itstandart/openbao-pydantic-settings-adapter@f186b16b45d1beda62b4c10f81ef5a72269a9bbc -
Branch / Tag:
refs/tags/v1.0.6 - Owner: https://github.com/itstandart
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f186b16b45d1beda62b4c10f81ef5a72269a9bbc -
Trigger Event:
push
-
Statement type: