The pieces / weirdnet-vault

weirdnet-vault

An encrypted secret and credential store: sealed values, scopes, per-identity grants, rotation and a hash-chained audit log.

v0.1.0 GitHub crates.io docs.rs weirdnet-vault = "0.1.0"

What it brings

Use weirdnet-vault when a server holds other people's credentials: exchange or provider API keys for many accounts, tokens, passwords. It keeps them sealed at rest, lets code use a credential without ever handing it back, lets several approved identities retrieve one when they need to, rotates without downtime, keeps dry-run callers away from live credentials, and records every attempt in an audit log you can verify.

The API is generic: scopes, environments, roles and grants are yours to name. Key management comes from weirdnet-keys and the database from weirdnet-storage.

What it is

A secret and credential store for servers and apps that keep API credentials, tokens and passwords for many tenants and identities. Plaintext stays in the process unless an approved identity retrieves it.

Values sit in a SQLCipher database (weirdnet-storage) and each version is sealed again on its own with XChaCha20-Poly1305 (weirdnet-keys), bound to its secret, version, environment and scope. Every call is checked and recorded in an audit log that never holds a value.

How it works

Keys and sealing

The vault is a SQLCipher database keyed by a data key. Inside it, a random entry key is wrapped by a key derived from the data key, and every version of every value is sealed with XChaCha20-Poly1305 under the entry key, with the secret id, version, environment and scope as associated data, so a sealed value copied into another row won't open. A rekey wraps the entry key again; values aren't resealed. The data key comes from a password keyring (Vault::init, then unlock or unlock_with_master, with recovery words and shards), a KeyStore (open_with_keystore), or a key you hold (open_with_key). open_in_memory is for tests. Plaintext is a zeroizing Plaintext that can't be cloned, printed or serialized; SecretMeta (name, kind, labels, scope, versions) is the only secret type that serializes.

Checks on every call

  1. Environment: a caller bound to Environment::Test may only list and read metadata of a live secret.
  2. Grant: Use and Reveal need an unexpired grant for that identity on the secret or a scope above it.
  3. Authorize: your hook (any Fn(&AccessRequest) -> Decision, or RolePolicy).
  4. Options: VaultOptions can turn Reveal off for the whole vault.
  5. Audit: allowed, denied and failed calls are all recorded.

RolePolicy::with_example_roles() defines viewer (list and metadata), trader (viewer plus Use), auditor (viewer plus Audit) and admin (every action), assigned per scope; define your own with define_role.

Using and retrieving

use_secret passes the value to a closure inside the process and returns only the closure's result. reveal returns it to an approved identity, and reveal_sealed seals it to the recipient's X25519 key. Admins add and remove grants with grant and revoke; several identities can hold Reveal on the same secret, and a revoke takes effect at once.

Rotation

stage_rotation adds a pending version while readers keep the active one. Try it with use_version(…, Which::Pending, …), then promote it (the old value stays as previous) or discard_pending; after promoting, revert rolls back and retire_previous removes the old value. Every step is audited.

Audit log

Each record holds the actor, scope, secret id and version, action, outcome and time, and never a value. Records are chained with BLAKE3 and the table refuses updates and deletes. audit queries by time range, scope, actor, action and outcome; verify_audit checks the whole chain and returns its head.

Backups

snapshot_to writes an online copy keyed with the same or a new key.

Key types and modules

  • Vault: create, list, use, reveal, rotate, delete, grant and revoke, with the audit log and snapshots. AsyncVault runs each call on Tokio’s blocking pool.
  • Scope (hierarchical, such as tenant-7/provider-a) and Environment (Test or Live): a dry-run caller never reaches a live secret.
  • Authorize, RolePolicy, Caller, Action: your policy hook on every call, with example roles you assign per scope.
  • Grant, GrantTarget, GrantAction: Use and Reveal approvals per identity on a secret or scope, with an optional expiry; RecipientSealed seals a retrieved value to the recipient’s X25519 key.
  • AuditRecord, AuditQuery, verify_chain: the hash-chained, append-only log and its queries.
  • KeyStore, VaultHome: where the database key comes from (a password keyring, an OS keychain, a weirdnet-sync secret store or your own).

Feature flags

  • tokio (default): AsyncVault.
  • vendored-openssl (default): builds SQLCipher's OpenSSL from source (weirdnet-storage).
  • keychain: KeychainStore for the macOS Keychain, Windows Credential Manager and Secret Service.
  • sync: SyncSecrets over any weirdnet-sync SecretStore; the vault's key stores implement SecretStore.
  • access: access::AccessAuthorizer over weirdnet-access and its vault.* permissions.
  • mcp: mcp::VaultTools, weirdnet-mcp tools for metadata, grants and rotation that never take or return a value.

Usage

In Cargo.toml:

[dependencies]
weirdnet-vault = "0.1.0"

Then (built and run against the published crate when this page was made):

// Keep a credential sealed, let a bot use it without seeing it, approve one operator to
// retrieve it, refuse a dry-run caller, and check the audit log.
use std::sync::Arc;
use weirdnet_vault::*;

fn main() -> Result<()> {
    let policy = Arc::new(RolePolicy::with_example_roles());
    policy.define_role("operator", &[Action::List, Action::ReadMeta, Action::Reveal]);
    let tenant = Scope::new(["tenant-7"])?;
    let account = tenant.child("provider-a")?;
    policy.assign("admin", &Scope::root(), "admin")?;
    policy.assign("bot", &tenant, "trader")?;
    policy.assign("ops", &tenant, "operator")?;

    // A real server opens a file: Vault::unlock (password), open_with_keystore or open_with_key.
    let vault = Vault::open_in_memory(policy, VaultOptions::new())?;
    let admin = Caller::live("admin");
    let key = vault.create(&admin, NewSecret::new(account.clone(), "api", Environment::Live).kind("api-key"), b"key:secret")?;

    // The bot may use the credential; it never gets the plaintext back.
    vault.grant(&admin, GrantTarget::Scope(account), "bot", &[GrantAction::Use], None)?;
    let len = vault.use_secret(&Caller::live("bot"), &key.id, |s| s.len())?;
    println!("signed with {len} bytes of credentials");
    println!("dry-run: {}", vault.use_secret(&Caller::test("bot"), &key.id, |_| ()).unwrap_err());
    println!("bot reveal: {}", vault.reveal(&Caller::live("bot"), &key.id).unwrap_err());

    // An approved operator may retrieve it.
    vault.grant(&admin, GrantTarget::Secret(key.id.clone()), "ops", &[GrantAction::Reveal], None)?;
    println!("ops retrieved {} bytes", vault.reveal(&Caller::live("ops"), &key.id)?.len());

    let head = vault.verify_audit()?;
    println!("audit log verified: {} records", head.seq);
    for r in vault.audit(&admin, &AuditQuery::new().action(Action::Reveal))? {
        println!("#{} {} {} {}", r.seq, r.actor, r.action, r.outcome.as_str());
    }
    Ok(())
}

Where it fits