[vault] Provision: the provision audit body member (checked entries, no value member, append-only, newest 5 000; docs/FORMAT.md)
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
//! Mandatory tests for the Provisio audit (docs/FORMAT.md, "The provision audit"): it survives a reopen, takes
|
||||
//! only entries that fit, is append-only and bounded, and has no member that could hold a secret. That an older
|
||||
//! reader still opens such a vault is a unit test in src/provision.rs, which can see the decrypted body.
|
||||
|
||||
use custos_vault::provision::{AUDIT_LIMIT, Direction, Outcome, ProvisionEntry, Store};
|
||||
use custos_vault::{Error, KdfParams, SecretString, Vault};
|
||||
|
||||
const FAST: KdfParams = KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 };
|
||||
const NOW: i64 = 1_793_750_000_000;
|
||||
|
||||
/// One way to spoil an entry.
|
||||
type Change = Box<dyn Fn(&mut ProvisionEntry)>;
|
||||
|
||||
fn entry(i: i64, outcome: Outcome) -> ProvisionEntry {
|
||||
ProvisionEntry {
|
||||
at: NOW + i,
|
||||
direction: Direction::Offer,
|
||||
host: "nixos-vm".into(),
|
||||
item: "fabrica/gitea-read-token".into(),
|
||||
store: Store::Vault,
|
||||
run: Some("deploy-7".into()),
|
||||
kind: Some("Gitea token, read:repository".into()),
|
||||
code: "684361".into(),
|
||||
outcome,
|
||||
replaced: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_provision_audit_records_every_outcome_and_survives_a_reopen() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("v.custos");
|
||||
let mut v = Vault::create(&SecretString::from("pw"), FAST).unwrap();
|
||||
assert!(v.provision_audit().is_empty());
|
||||
let all = [
|
||||
Outcome::Approved,
|
||||
Outcome::Generated,
|
||||
Outcome::Accepted,
|
||||
Outcome::Refused,
|
||||
Outcome::OutOfScope,
|
||||
Outcome::Invalid,
|
||||
Outcome::Expired,
|
||||
];
|
||||
for (i, o) in all.iter().enumerate() {
|
||||
let mut e = entry(i as i64, *o);
|
||||
if i % 2 == 0 {
|
||||
e.direction = Direction::Ask;
|
||||
e.store = Store::Vault;
|
||||
e.kind = None;
|
||||
e.run = None;
|
||||
} else {
|
||||
e.store = if i % 3 == 0 { Store::Claude } else { Store::Cloud };
|
||||
e.item = "fabrica-hub-token".into();
|
||||
}
|
||||
e.replaced = *o == Outcome::Accepted;
|
||||
v.provision_record(e).unwrap();
|
||||
}
|
||||
v.save(&path).unwrap();
|
||||
|
||||
let back = Vault::open_file(&path, &SecretString::from("pw")).unwrap();
|
||||
let audit: &[ProvisionEntry] = back.provision_audit();
|
||||
assert_eq!(audit, v.provision_audit());
|
||||
assert_eq!(audit.iter().map(|e| e.outcome).collect::<Vec<_>>(), all);
|
||||
// Newest last.
|
||||
assert!(audit.windows(2).all(|w| w[0].at < w[1].at));
|
||||
assert_eq!(audit[0].direction, Direction::Ask);
|
||||
assert_eq!(audit[1].store, Store::Cloud);
|
||||
assert_eq!(audit[3].store, Store::Claude);
|
||||
assert!(audit[2].replaced && !audit[1].replaced);
|
||||
// The signing audit is a separate log and stays empty.
|
||||
assert!(back.signing_audit().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entries_that_do_not_fit_are_refused_and_not_stored() {
|
||||
let mut v = Vault::create(&SecretString::from("pw"), FAST).unwrap();
|
||||
v.provision_record(entry(0, Outcome::Approved)).unwrap();
|
||||
let secretish = "ghp_s3cr3tV4lu3ThatMustNeverBeInAnError";
|
||||
let bad: Vec<Change> = vec![
|
||||
Box::new(|e| e.code = "68436".into()),
|
||||
Box::new(|e| e.code = "68436a".into()),
|
||||
Box::new(|e| e.code = "6843611".into()),
|
||||
Box::new(|e| e.item = "Arca/x".into()),
|
||||
Box::new(|e| e.item = "../x".into()),
|
||||
Box::new(|e| e.item = "a/b/c/d/e".into()),
|
||||
Box::new(|e| e.item = String::new()),
|
||||
Box::new(|e| e.item = format!("arca/{}", "x".repeat(65))),
|
||||
Box::new(|e| e.run = Some(String::new())),
|
||||
Box::new(|e| e.run = Some("run 1".into())),
|
||||
Box::new(|e| e.run = Some("r".repeat(65))),
|
||||
Box::new(|e| e.host = String::new()),
|
||||
Box::new(|e| e.host = "nixos vm".into()),
|
||||
Box::new(|e| e.host = "h".repeat(254)),
|
||||
Box::new(|e| e.kind = Some("x".repeat(201))),
|
||||
Box::new(move |e| e.kind = Some(format!("token\n{secretish}"))),
|
||||
];
|
||||
for (i, change) in bad.iter().enumerate() {
|
||||
let mut e = entry(1, Outcome::Accepted);
|
||||
change(&mut e);
|
||||
let err = v.provision_record(e).unwrap_err();
|
||||
assert!(matches!(err, Error::Provision(_)), "case {i}: {err:?}");
|
||||
assert!(!format!("{err}{err:?}").contains(secretish), "case {i}: an error repeats a value");
|
||||
}
|
||||
assert_eq!(v.provision_audit(), [entry(0, Outcome::Approved)]);
|
||||
// The limits themselves fit.
|
||||
let mut e = entry(2, Outcome::Accepted);
|
||||
e.item = format!("a{0}/b{0}/c{0}/d{0}", "x".repeat(63));
|
||||
e.run = Some("R".repeat(64));
|
||||
e.host = "h".repeat(253);
|
||||
e.kind = Some("k".repeat(200));
|
||||
v.provision_record(e).unwrap();
|
||||
assert_eq!(v.provision_audit().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_audit_is_append_only_and_keeps_the_newest_entries() {
|
||||
let mut v = Vault::create(&SecretString::from("pw"), FAST).unwrap();
|
||||
v.provision_record(entry(0, Outcome::Refused)).unwrap();
|
||||
let before = v.provision_audit().to_vec();
|
||||
v.provision_record(entry(1, Outcome::Accepted)).unwrap();
|
||||
// Recording appends; what was there is unchanged.
|
||||
assert_eq!(&v.provision_audit()[..1], before.as_slice());
|
||||
assert_eq!(v.provision_audit()[1], entry(1, Outcome::Accepted));
|
||||
|
||||
assert_eq!(AUDIT_LIMIT, 5_000);
|
||||
for i in 2..(AUDIT_LIMIT as i64 + 3) {
|
||||
v.provision_record(entry(i, Outcome::Expired)).unwrap();
|
||||
}
|
||||
let audit = v.provision_audit();
|
||||
assert_eq!(audit.len(), AUDIT_LIMIT);
|
||||
// The three oldest went; the newest is last.
|
||||
assert_eq!(audit[0].at, NOW + 3);
|
||||
assert_eq!(audit.last().unwrap().at, NOW + AUDIT_LIMIT as i64 + 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_entry_has_no_member_that_could_hold_a_secret() {
|
||||
// Every member, with every option set: the JSON has exactly these names and nothing like a value.
|
||||
let mut e = entry(0, Outcome::Accepted);
|
||||
e.replaced = true;
|
||||
let json: serde_json::Value = serde_json::to_value(&e).unwrap();
|
||||
let mut names: Vec<&str> = json.as_object().unwrap().keys().map(String::as_str).collect();
|
||||
names.sort();
|
||||
assert_eq!(names, ["at", "code", "direction", "host", "item", "kind", "outcome", "replaced", "run", "store"]);
|
||||
for v in json.as_object().unwrap().values() {
|
||||
assert!(!v.is_object() && !v.is_array(), "no nested member to hide a value in");
|
||||
}
|
||||
assert_eq!(
|
||||
serde_json::to_string(&e).unwrap(),
|
||||
r#"{"at":1793750000000,"direction":"offer","host":"nixos-vm","item":"fabrica/gitea-read-token","store":"vault","run":"deploy-7","kind":"Gitea token, read:repository","code":"684361","outcome":"accepted","replaced":true}"#
|
||||
);
|
||||
// The options are left out when absent; the outcomes are spelled as docs/FORMAT.md says.
|
||||
let mut e = entry(0, Outcome::OutOfScope);
|
||||
e.run = None;
|
||||
e.kind = None;
|
||||
let s = serde_json::to_string(&e).unwrap();
|
||||
assert!(!s.contains("\"run\"") && !s.contains("\"kind\"") && s.contains("\"out-of-scope\""), "{s}");
|
||||
}
|
||||
Reference in New Issue
Block a user