Skip to main content

Crate weirdnet_vault

Crate weirdnet_vault 

Source
Expand description

An encrypted secret and credential store for Weirdnet servers and apps.

  • Per-entry sealed values in a SQLCipher database: each version of each value is sealed on its own (XChaCha20-Poly1305, bound to its secret, version, environment and scope), with metadata (name, kind, labels, scope) beside it. Plaintext only lives in Plaintext, which is wiped on drop and never serialized.
  • Scoped access: every call goes through an environment check, per-identity grants for Use and Reveal, and the app’s Authorize hook.
  • Use without revealing: Vault::use_secret hands the value to a closure inside the process; Vault::reveal retrieves it for approved identities, also sealed to a recipient key.
  • Environments: a caller bound to Environment::Test can never touch a live secret.
  • Rotation without downtime: stage, try, promote, roll back, retire.
  • A hash-chained, append-only audit log with time-range queries.
  • Keys from a password (weirdnet-keys keyring, VaultHome), an OS keychain (KeychainStore, feature keychain), a weirdnet-sync secret store (feature sync) or the caller.
  • AsyncVault for Tokio (feature tokio, on by default), an Authorize over weirdnet-access (feature access) and metadata-only MCP tools (feature mcp).
use std::sync::Arc;
use weirdnet_vault::*;

let policy = Arc::new(RolePolicy::with_example_roles());
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("alice", &tenant, "viewer")?;

let vault = Vault::open_in_memory(policy.clone(), 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")?;
vault.grant(&admin, GrantTarget::Scope(account.clone()), "bot", &[GrantAction::Use], None)?;

// The bot signs with the key; the key never comes back.
let signed = vault.use_secret(&Caller::live("bot"), &key.id, |s| s.len())?;
assert_eq!(signed, 10);
// It may not retrieve it, and a viewer may only see metadata.
assert!(vault.reveal(&Caller::live("bot"), &key.id).is_err());
assert_eq!(vault.list(&Caller::live("alice"), &tenant)?.len(), 1);
assert!(vault.use_secret(&Caller::live("alice"), &key.id, |_| ()).is_err());
// A dry-run context never reaches a live secret.
assert!(matches!(vault.use_secret(&Caller::test("bot"), &key.id, |_| ()), Err(VaultError::EnvironmentMismatch { .. })));
vault.verify_audit()?;

See the design.

Modules§

access
An Authorize over weirdnet-access’s host-side enforcement (feature access).
mcp
Metadata-only MCP tools over weirdnet-mcp (feature mcp).

Structs§

AccessRequest
One request, as the Authorize hook sees it.
AsyncVault
A cheap-to-clone handle that runs every vault call on Tokio’s blocking pool, so database and crypto work never stalls the runtime.
AuditHead
The newest record’s position and hash. Publish it somewhere else (another node, a signed note) to make a rewritten log detectable.
AuditQuery
Filters for Vault::audit. Everything is optional; records come back oldest first.
AuditRecord
One audit record. It says who did what, where and when, and how it ended; it never holds a secret value.
Caller
Who is asking: an identity the app has authenticated (a Weirdnet user id, a device peer id, an API-key id, …) bound to an environment.
Clock
The time source (ms since the Unix epoch). Tests pass their own.
ExchangeSecret
An X25519 secret for one exchange (wiped on drop).
Grant
An approval for one identity.
GrantId
A grant’s id (a random UUID).
InitOutput
Result of init. Show once, then drop.
Key32
A 256-bit secret key, wiped on drop and never printed.
KeychainStore
The OS keychain: macOS Keychain, Windows Credential Manager, or the Secret Service on Linux and the BSDs. Entries are (service, name).
MemoryKeyStore
An in-memory key store (tests, examples).
NewSecret
What to create.
Plaintext
A secret value in memory: wiped on drop, never cloned, printed or serialized.
RecipientSealed
A retrieved value sealed to one recipient: an ephemeral X25519 key agreement, HKDF over the shared secret bound to both public keys, the secret id and version, then XChaCha20-Poly1305. Only the holder of the recipient’s secret key can open it, so it can travel over any channel.
RolePolicy
A small role policy: roles are named sets of actions, and identities hold roles on scopes (a role on a scope covers everything below it). It can change while the vault runs.
Scope
A hierarchical key such as tenant-7/provider-a. A scope contains itself and every scope below it; the empty scope is the root and contains everything.
SecretId
A secret’s id (a random UUID).
SecretMeta
A secret’s metadata: the only secret type that is serializable, and so the only one safe to send to clients or MCP. It never holds a value.
Vault
An open vault. It is Send + Sync; share it with Arc (or use AsyncVault from Tokio).
VaultHome
A vault folder: envelope.toml, service/ (the keyring’s files) and vault.db.
VaultOptions
Vault-wide settings.
VersionMeta
One version’s metadata (never its value).
Zeroizing
Zeroizing is a wrapper for any Z: Zeroize type which implements a Drop handler which zeroizes dropped values.

Enums§

Action
What a caller wants to do.
Decision
The hook’s answer.
Environment
Which world a secret belongs to, enforced on every call.
GrantAction
What a grant approves.
GrantTarget
What a grant covers: one secret, or every secret in a scope (and below it).
Outcome
How a request ended.
VaultError
Everything that can go wrong.
VersionState
Where a version stands.
Which
Which version to use or retrieve.

Constants§

GENESIS
The prev_hash of the first record: 64 zeros.
MAX_DEPTH
The most segments a scope may have.
MAX_DETAIL
The longest detail text kept, in bytes.
MAX_LABELS
The most labels a secret may have.
MAX_LIMIT
The most records one query returns.
MAX_SEGMENT
The longest scope segment, in bytes.

Traits§

Authorize
The app’s access policy. Every vault call asks it, after the environment check and (for Action::Use and Action::Reveal) the grant check.
KeyStore
Keeps secret bytes by name.

Functions§

load_or_create_key
The 32-byte key stored under name, or a new random one, stored and read back.
verify_chain
Checks a contiguous run of records: each is intact, follows the one before (or prev_hash, when given) and has the next sequence number. Returns the failing seq.

Type Aliases§

Labels
Free-form labels.
Result
Result with VaultError.
SecretString
Secret string type.