A Rust Client for the HashiCorp Vault HTTP API
Dual async and blocking Rust client for the HashiCorp Vault HTTP API.
Covers KV v1/v2, Transit, PKI, Database, SSH, Identity, TOTP, Cubbyhole, sys operations (seal/unseal, mounts, policies, leases, audit, Raft, rekey, namespaces, quotas), and authentication via Token, AppRole, Userpass, LDAP, Kubernetes, TLS certificates, GitHub, OIDC/JWT, AWS, Azure, GCP, Kerberos, and RADIUS.
Every API handler implements a trait (Kv2Operations, TransitOperations, PkiOperations, ...)
so you can swap in a mock for tests without a running Vault server.
Project Maturity
This library is young. Before 1.0, breaking API changes likely, including major changes.
Requirements
- Rust 1.93+
- Tokio
Dependency
= "0.7"
With Blocking Client
= { = "0.7", = ["blocking"] }
With Automatic Token Renewal
= { = "0.7", = ["auto-renew"] }
Async Client
Create a Client
use VaultClient;
let client = new?;
Using ClientBuilder
use VaultClient;
let client = builder
.address
.token_str
.max_retries
.build?;
Circuit Breaker
Circuit Breaking is a feature that monitors consecutive failures and short-circuits (avoids) subsequent requests until a certain period of time passes:
use ;
let client = builder
.address
.token_str
.circuit_breaker
.build?;
CLI Mode
For short-lived sessions in CLI tools, cli_mode disables retries and sealed-Vault retry loops:
use VaultClient;
let client = builder
.address
.token_str
.cli_mode
.build?;
From Environment Variables
Reads VAULT_ADDR, VAULT_TOKEN, VAULT_NAMESPACE, and other VAULT_* variables.
When VAULT_TOKEN is not set, falls back to ~/.vault-token (written by vault login):
use VaultClient;
let client = from_env?;
KV v2 Secrets
let kv = client.kv2;
// Write a secret
kv.write.await?;
// Read into a typed struct or a HashMap
let data: = kv.read_data.await?;
// Read a single field
let host: String = kv.read_field.await?;
// List keys
let keys: = kv.list.await?;
// Delete
kv.delete.await?;
KV v2 Version Management
let kv = client.kv2;
// Read a specific version
let v1: = kv.read_version.await?;
// Soft-delete versions
kv.delete_versions.await?;
// Undelete
kv.undelete_versions.await?;
// Permanently destroy versions
kv.destroy_versions.await?;
// Check-and-set write (optimistic locking)
kv.write_cas.await?;
// Patch (merge fields into existing secret)
kv.patch.await?;
KV v1 Secrets
let kv = client.kv1;
// Read into a typed struct or a HashMap
let data: = kv.read_data.await?;
// Read a single field
let host: String = kv.read_field.await?;
// Write, list, delete work the same way as in the KV v2 interface
kv.write.await?;
Transit Encryption
use SecretString;
let transit = client.transit;
// Encrypt, decrypt
let ciphertext = transit.encrypt.await?;
let plaintext = transit.decrypt.await?;
// Sign, verify
let signature = transit.sign.await?;
let valid = transit.verify.await?;
// Key management
transit.create_key.await?;
transit.rotate_key.await?;
let keys = transit.list_keys.await?;
PKI Certificates
use PkiIssueParams;
let pki = client.pki;
// Issue a certificate
let cert = pki.issue.await?;
// Sign a CSR
let signed = pki.sign.await?;
// Revoke by serial
pki.revoke.await?;
Database Dynamic Credentials
let db = client.database;
// Get dynamic credentials for a role
let creds = db.get_credentials.await?;
println!;
SSH Certificate Signing
use SshSignRequest;
let ssh = client.ssh;
let signed = ssh.sign_key.await?;
TOTP (Time-Based One-Time Passwords)
use TotpKeyRequest;
let totp = client.totp;
// Create a key (Vault generates the secret)
totp.create_key.await?;
// Generate a code
let code = totp.generate_code.await?;
// Validate a code from a user
let result = totp.validate_code.await?;
assert!;
Lease Management
let sys = client.sys;
// Look up a lease
let info = sys.read_lease.await?;
// Renew a lease with an optional increment
sys.renew_lease.await?;
// Revoke a specific lease
sys.revoke_lease.await?;
// Revoke all leases under a prefix
sys.revoke_prefix.await?;
Automatic Token and Lease Renewal
Requires the auto-renew feature. The daemon renews the client token at ~2/3 of its
remaining TTL and falls back to re-authentication if renewal fails:
// Renew the client token in the background
let daemon = client.start_token_renewal;
// Watch a dynamic lease (e.g. database credentials)
use Duration;
let watcher = client.watch_lease;
// Or get programmatic events on each renewal or failure
let mut watcher = client.watch_lease_events;
while let Some = watcher.recv.await
// Both handles cancel their background task on drop
Authentication
use SecretString;
use VaultClient;
let client = builder
.address
.build?;
// Userpass
let auth = client.auth.userpass.login.await?;
// AppRole
let auth = client.auth.approle.login.await?;
// Kubernetes (in-cluster)
let auth = client.auth.kubernetes.login.await?;
// LDAP
let auth = client.auth.ldap.login.await?;
// GitHub
let auth = client.auth.github.login.await?;
// OIDC/JWT
let auth = client.auth.oidc.login_jwt.await?;
Token Management
let token = client.auth.token;
let info = token.lookup_self.await?;
token.renew_self.await?;
let child = token.create.await?;
System Operations
let sys = client.sys;
let health = sys.health.await?;
let status = sys.seal_status.await?;
let policies = sys.list_policies.await?;
// Mounts
let mounts = sys.list_mounts.await?;
sys.mount.await?;
// Policies
sys.write_policy.await?;
// Response wrapping
let wrapped: MyStruct = sys.unwrap.await?;
Namespaces (Enterprise)
// Work in a specific namespace
let ns_client = client.with_namespace;
let kv = ns_client.kv2;
Response Wrapping
// Wrap the next response with a TTL
let wrapping_client = client.with_wrap_ttl;
Blocking Client
The blocking client has the same API as the async client but without async/await.
Create a Client
use BlockingVaultClient;
let client = new?;
Read a Secret
let data: HashMap =
client.kv2.read_data?;
System Operations
let health = client.sys.health?;
let policies = client.sys.list_policies?;
Mocking in Tests
Every handler implements a trait (Kv2Operations, TransitOperations, PkiOperations, ...)
that can be used for mocking in tests:
use ;
use KvReadResponse;
;
Copyright
(c) 2025-2026 Michael S. Klishin and Contributors.
License
This crate, vault-client-rs, is dual-licensed under
the Apache Software License 2.0 and the MIT license.
SPDX-License-Identifier: Apache-2.0 OR MIT