Reusable HashiCorp Vault helper and secret refresher utilities for Python services.
Project description
jh-vault-helper
Reusable HashiCorp Vault helpers for Python services.
This package provides:
VaultKVClientfor Vault KV v2 readsload_env_from_vault()to load secrets intoos.environVaultSecretRefresherfor background refreshstart_vault_auto_refresh()andstop_vault_auto_refresh()convenience helpers
Installation
pip install jh-vault-helper
Quick Start
from jh_vault_helper import load_env_from_vault
load_env_from_vault(
mount="mentors",
secret_path="staging",
override_existing=False,
)
config.py Integration
import logging
import os
import sys
from dotenv import load_dotenv
from jh_vault_helper import load_env_from_vault
load_dotenv(override=True)
logger = logging.getLogger("config")
def is_production_environment() -> bool:
return (os.getenv("APP_ENV") or "").strip().lower() == "production"
def load_vault_configuration() -> None:
errors = []
env_mount = os.getenv("VAULT_KV_MOUNT")
env_path = os.getenv("VAULT_KV_PATH")
if not env_path:
env_path = "production" if is_production_environment() else "staging"
shared_mount = os.getenv("VAULT_SHARED_MOUNT", "shared")
shared_path = os.getenv("VAULT_SHARED_PATH", "services/common")
def fetch(label: str, mount: str | None, secret_path: str | None) -> None:
if not mount or not secret_path:
return
try:
load_env_from_vault(
mount=mount,
secret_path=secret_path,
override_existing=False,
)
except Exception as exc:
errors.append((label, mount, secret_path, exc))
fetch("environment", env_mount, env_path)
fetch("shared", shared_mount, shared_path)
if errors:
for label, mount, secret_path, exc in errors:
logger.error(
"Vault load failed for %s secrets (mount=%s path=%s): %s",
label,
mount,
secret_path,
exc,
)
if is_production_environment():
sys.exit(1)
Auto Refresh
from jh_vault_helper import start_vault_auto_refresh, stop_vault_auto_refresh
start_vault_auto_refresh(refresh_interval=300)
# On shutdown
stop_vault_auto_refresh()
Environment Variables
Required:
VAULT_ADDRVAULT_TOKENorVAULT_ROLE_ID+VAULT_SECRET_IDVAULT_KV_MOUNTunless passed explicitly
Optional:
VAULT_KV_PATHVAULT_NAMESPACEVAULT_VERIFYVAULT_TIMEOUTVAULT_AUTO_REFRESHVAULT_REFRESH_INTERVALVAULT_SHARED_MOUNTVAULT_SHARED_PATHAPP_ENV
If VAULT_KV_PATH is unset, the package defaults to production when APP_ENV=production and staging otherwise.
Release Workflow
python -m build
twine check dist/*
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ jh-vault-helper==0.1.0
twine upload dist/*
If a version is already published, bump the patch version in pyproject.toml, rebuild, and publish the new version.
Project details
Release history Release notifications | RSS feed
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 jh_vault_helper-0.1.0.tar.gz.
File metadata
- Download URL: jh_vault_helper-0.1.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbff2aa4bab39f674a771d84beec5de802f9cc99596c8b5056447327a39248c9
|
|
| MD5 |
9487121f28e540207c7e23bbf419d155
|
|
| BLAKE2b-256 |
7ff9329eaee05e56e01ee7fe6ac0994623e199ca48ae5113835e2b9a8e68b77a
|
File details
Details for the file jh_vault_helper-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jh_vault_helper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52a8b4d91459961b2409d94c0a468663732152863fd525d550a916709cd5b939
|
|
| MD5 |
22429b9249a35ded4c76f99aa692eb17
|
|
| BLAKE2b-256 |
42b007249d6da2be19717fa38858d33e720ce7fa239f61a0a8cb7b2a63c9738a
|