weirdnet_vault/lib.rs
1//! An encrypted secret and credential store for Weirdnet servers and apps.
2//!
3//! - **Per-entry sealed values** in a SQLCipher database: each version of each value is sealed
4//! on its own (XChaCha20-Poly1305, bound to its secret, version, environment and scope), with
5//! metadata (name, kind, labels, scope) beside it. Plaintext only lives in [`Plaintext`],
6//! which is wiped on drop and never serialized.
7//! - **Scoped access**: every call goes through an environment check, per-identity
8//! [grants](Grant) for `Use` and `Reveal`, and the app's [`Authorize`] hook.
9//! - **Use without revealing**: [`Vault::use_secret`] hands the value to a closure inside the
10//! process; [`Vault::reveal`] retrieves it for approved identities, also
11//! [sealed to a recipient key](Vault::reveal_sealed).
12//! - **Environments**: a caller bound to [`Environment::Test`] can never touch a live secret.
13//! - **Rotation without downtime**: stage, try, promote, roll back, retire.
14//! - **A hash-chained, append-only audit log** with time-range queries.
15//! - **Keys** from a password (weirdnet-keys keyring, [`VaultHome`]), an OS keychain
16//! ([`KeychainStore`], feature `keychain`), a weirdnet-sync secret store (feature `sync`) or
17//! the caller.
18//! - [`AsyncVault`] for Tokio (feature `tokio`, on by default), an `Authorize` over
19//! weirdnet-access (feature `access`) and metadata-only MCP tools (feature `mcp`).
20//!
21//! ```
22//! use std::sync::Arc;
23//! use weirdnet_vault::*;
24//!
25//! # fn main() -> Result<()> {
26//! let policy = Arc::new(RolePolicy::with_example_roles());
27//! let tenant = Scope::new(["tenant-7"])?;
28//! let account = tenant.child("provider-a")?;
29//! policy.assign("admin", &Scope::root(), "admin")?;
30//! policy.assign("bot", &tenant, "trader")?;
31//! policy.assign("alice", &tenant, "viewer")?;
32//!
33//! let vault = Vault::open_in_memory(policy.clone(), VaultOptions::new())?;
34//! let admin = Caller::live("admin");
35//! let key = vault.create(&admin, NewSecret::new(account.clone(), "api", Environment::Live).kind("api-key"), b"key:secret")?;
36//! vault.grant(&admin, GrantTarget::Scope(account.clone()), "bot", &[GrantAction::Use], None)?;
37//!
38//! // The bot signs with the key; the key never comes back.
39//! let signed = vault.use_secret(&Caller::live("bot"), &key.id, |s| s.len())?;
40//! assert_eq!(signed, 10);
41//! // It may not retrieve it, and a viewer may only see metadata.
42//! assert!(vault.reveal(&Caller::live("bot"), &key.id).is_err());
43//! assert_eq!(vault.list(&Caller::live("alice"), &tenant)?.len(), 1);
44//! assert!(vault.use_secret(&Caller::live("alice"), &key.id, |_| ()).is_err());
45//! // A dry-run context never reaches a live secret.
46//! assert!(matches!(vault.use_secret(&Caller::test("bot"), &key.id, |_| ()), Err(VaultError::EnvironmentMismatch { .. })));
47//! vault.verify_audit()?;
48//! # Ok(()) }
49//! ```
50//!
51//! See the [design](https://github.com/WeirdBrains/weirdnet-vault/blob/main/docs/design/vault.md).
52#![forbid(unsafe_code)]
53#![warn(missing_docs)]
54
55#[cfg(feature = "access")]
56pub mod access;
57#[cfg(feature = "tokio")]
58mod asynch;
59mod audit;
60mod crypto;
61mod error;
62mod grant;
63mod home;
64mod keystore;
65#[cfg(feature = "mcp")]
66pub mod mcp;
67mod policy;
68mod recipient;
69mod scope;
70mod secret;
71mod store;
72mod vault;
73
74#[cfg(feature = "tokio")]
75pub use asynch::AsyncVault;
76pub use audit::{AuditHead, AuditQuery, AuditRecord, GENESIS, MAX_DETAIL, MAX_LIMIT, Outcome, verify_chain};
77pub use error::{Result, VaultError};
78pub use grant::{Grant, GrantAction, GrantId, GrantTarget};
79pub use home::VaultHome;
80#[cfg(feature = "keychain")]
81pub use keystore::KeychainStore;
82#[cfg(feature = "sync")]
83pub use keystore::SyncSecrets;
84pub use keystore::{KeyStore, MemoryKeyStore, load_or_create_key};
85pub use policy::{AccessRequest, Action, Authorize, Caller, Decision, RolePolicy};
86pub use recipient::RecipientSealed;
87pub use scope::{Environment, MAX_DEPTH, MAX_SEGMENT, Scope};
88pub use secret::{Labels, MAX_LABELS, NewSecret, Plaintext, SecretId, SecretMeta, VersionMeta, VersionState, Which};
89pub use vault::{Clock, Vault, VaultOptions};
90pub use weirdnet_keys::exchange::ExchangeSecret;
91pub use weirdnet_keys::keyring::InitOutput;
92pub use weirdnet_keys::{Key32, SecretString, Zeroizing};