HVAC: HashiCorp Vault API Client
HVAC is a Python client library for interacting with HashiCorp Vault. It provides a programmatic interface to manage secrets, policies, and authentication methods within a Vault instance. Currently at version 2.4.0, the library maintains an active development status with regular minor releases addressing new Vault features, bug fixes, and dependency updates, alongside occasional major releases for significant breaking changes.
Warnings
- breaking hvac v2.0.0 dropped support for Python 3.6 and 3.7. It also removed support for Vault versions 1.6.x through 1.10.x and previously deprecated methods and code paths. [cite: 2.0.0 release notes]
- breaking The `Client.write_data` method's default behavior changed in `v2.1.0` related to a bug fix for a 'potentially dangerous default.' This could alter behavior for users relying on previous default parameters. [cite: 2.1.0 release notes]
- deprecated The `certificate` parameter for `create_ca_certificate_role` will no longer accept file paths in `v3.0.0`. [cite: v1.1.0 release notes]
- deprecated The default value of `raise_on_deleted_version` is planned to change from `True` to `False` in a future major release, impacting how deleted secret versions are handled during reads. [cite: v1.1.0 release notes]
- gotcha For KV v2 secret engines, the generic `client.list()` method does not work as expected for listing paths/folders. You must use `client.secrets.kv.v2.list_secrets()`.
- gotcha Vault currently defaults the `secret/` path to KV secrets engine version 2 in 'dev' mode. Outside of 'dev' mode (from Vault v1.1.0 onwards), no KV secrets engine is mounted by default at `secret/` and must be explicitly enabled.
- gotcha New exception types (`hvac.exceptions.VaultPermissionDenied` for 405 and `hvac.exceptions.VaultPreconditionFailed` for 412) were added in v2.2.0. If you were catching generic exceptions for these HTTP status codes, your error handling might need updates. [cite: v2.2.0 release notes, 18]
Install
-
pip install hvac -
pip install "hvac[parser]"
Imports
- Client
import hvac client = hvac.Client(...)
Quickstart
import os
import hvac
# Configure these environment variables or replace with direct values
VAULT_ADDR = os.environ.get('VAULT_ADDR', 'http://127.0.0.1:8200')
VAULT_TOKEN = os.environ.get('VAULT_TOKEN', 'root-token-for-dev')
try:
client = hvac.Client(url=VAULT_ADDR, token=VAULT_TOKEN)
if not client.is_authenticated():
raise Exception("HVAC client not authenticated. Check VAULT_ADDR and VAULT_TOKEN.")
print("Client authenticated successfully.")
# Example: Write a secret to KV v2 engine
mount_point = 'secret'
path = 'my-app/config'
secret_data = {'api_key': 'super-secret-key-123', 'environment': 'dev'}
print(f"\nWriting secret to {mount_point}/{path}...")
client.secrets.kv.v2.create_or_update_secret(
mount_point=mount_point,
path=path,
secret=secret_data,
)
print("Secret written.")
# Example: Read the secret from KV v2 engine
print(f"\nReading secret from {mount_point}/{path}...")
read_response = client.secrets.kv.v2.read_secret_version(
mount_point=mount_point,
path=path
)
retrieved_data = read_response['data']['data']
print(f"Retrieved secret: {retrieved_data}")
# Example: Delete the secret
print(f"\nDeleting secret {mount_point}/{path}...")
client.secrets.kv.v2.delete_metadata_and_all_versions(
mount_point=mount_point,
path=path
)
print("Secret deleted.")
except Exception as e:
print(f"An error occurred: {e}")