From 3bb05bf6f156ff31f0f4a8ed7e664e37f5cebcfc Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 17 Nov 2026 12:15:18 +0100 Subject: [PATCH] [team] Team vaults: rustfmt on the files this change touches --- crates/custos-team/src/policy.rs | 7 ++- crates/custos-team/src/proof.rs | 7 ++- crates/custos-team/src/request.rs | 9 ++-- crates/custos-team/src/statement.rs | 3 +- crates/custos-team/tests/policy.rs | 30 ++++++++++--- crates/custos-team/tests/request.rs | 5 ++- crates/custos-vault/src/team.rs | 55 +++++++++++++++++------ crates/custos-vault/tests/team.rs | 67 +++++++++++++++++++++-------- 8 files changed, 138 insertions(+), 45 deletions(-) diff --git a/crates/custos-team/src/policy.rs b/crates/custos-team/src/policy.rs index 7c0b7c7..7e9e408 100644 --- a/crates/custos-team/src/policy.rs +++ b/crates/custos-team/src/policy.rs @@ -227,7 +227,12 @@ pub fn eligible(policy: &Policy, roster: &[RosterMember], env: &str, author: &st /// statements were already checked. pub fn evaluate(policy: &Policy, roster: &[RosterMember], env: &str, author: &str, approvers: &[String]) -> Evaluation { let Some(rule) = policy.rule(env) else { - return Evaluation { verdict: Verdict::UnknownEnv, eligible: vec![], counted: vec![], ignored: approvers.to_vec() }; + return Evaluation { + verdict: Verdict::UnknownEnv, + eligible: vec![], + counted: vec![], + ignored: approvers.to_vec(), + }; }; let eligible = eligible(policy, roster, env, author).unwrap_or_default(); let mut counted: Vec = Vec::new(); diff --git a/crates/custos-team/src/proof.rs b/crates/custos-team/src/proof.rs index c84d9e7..9a711df 100644 --- a/crates/custos-team/src/proof.rs +++ b/crates/custos-team/src/proof.rs @@ -180,7 +180,12 @@ pub struct Verified { /// with it: both signatures, every approval (signature, roster, the request's own match code), the request and /// envelope hashes, and the policy evaluated again. The clock is not consulted: a proof stays checkable after its /// request expired. -pub fn verify_proof(proof: &Proof, identity: &[u8; 32], request_text: &str, envelope: &[u8]) -> Result { +pub fn verify_proof( + proof: &Proof, + identity: &[u8; 32], + request_text: &str, + envelope: &[u8], +) -> Result { let sig: [u8; 64] = unb64(&proof.sig).and_then(|s| s.try_into().ok()).ok_or(TeamError::BadSignature)?; if !verify_proof_sig(identity, &proof.text, &sig) { return Err(TeamError::BadSignature); diff --git a/crates/custos-team/src/request.rs b/crates/custos-team/src/request.rs index f8f2bc0..c11bfb2 100644 --- a/crates/custos-team/src/request.rs +++ b/crates/custos-team/src/request.rs @@ -100,9 +100,8 @@ fn lower_hex(s: &str, len: usize) -> bool { } pub(crate) fn pub_key(s: &str) -> Result<[u8; 32], TeamError> { - let k: [u8; 32] = unb64(s) - .and_then(|v| v.try_into().ok()) - .ok_or(TeamError::Invalid("pub is not base64 of 32 bytes"))?; + let k: [u8; 32] = + unb64(s).and_then(|v| v.try_into().ok()).ok_or(TeamError::Invalid("pub is not base64 of 32 bytes"))?; if k == [0; 32] { return Err(TeamError::Invalid("pub is all zeros")); } @@ -176,7 +175,9 @@ impl SignRequest { return bad("artifact_digest has the wrong length for its format"); } match self.format { - Format::Cosign => custos_sign::image_ok(&self.image).map_err(|_| TeamError::Invalid("image is not a repository"))?, + Format::Cosign => { + custos_sign::image_ok(&self.image).map_err(|_| TeamError::Invalid("image is not a repository"))? + } _ if !self.image.is_empty() => return bad("image is for cosign only"), _ => {} } diff --git a/crates/custos-team/src/statement.rs b/crates/custos-team/src/statement.rs index 0606692..6d93071 100644 --- a/crates/custos-team/src/statement.rs +++ b/crates/custos-team/src/statement.rs @@ -55,8 +55,7 @@ impl Statement { } let m = roster.iter().find(|m| m.id == self.member).ok_or(TeamError::NotAMember)?; let id: [u8; 16] = unhex(&self.member).ok_or(TeamError::NotAMember)?; - let sig: [u8; 64] = - unb64(&self.sig).and_then(|s| s.try_into().ok()).ok_or(TeamError::BadSignature)?; + let sig: [u8; 64] = unb64(&self.sig).and_then(|s| s.try_into().ok()).ok_or(TeamError::BadSignature)?; if !crate::member::verify(&m.ed25519, &message(&req.hash(), &self.code, &id, self.at), &sig) { return Err(TeamError::BadSignature); } diff --git a/crates/custos-team/tests/policy.rs b/crates/custos-team/tests/policy.rs index 399a5e9..eddbe78 100644 --- a/crates/custos-team/tests/policy.rs +++ b/crates/custos-team/tests/policy.rs @@ -74,7 +74,9 @@ fn every_size_environment_author_and_approver_under_the_default_policy() { let r = roster(n); for env in ["preview", "staging", "production", "qa"] { // The author by handle (any case), by member id, someone outside the team, or nobody named. - for (author, is_member) in [("mnovak", true), ("MNOVAK", true), (id(0).as_str(), true), ("stranger", false), ("", false)] { + for (author, is_member) in + [("mnovak", true), ("MNOVAK", true), (id(0).as_str(), true), ("stranger", false), ("", false)] + { for (what, set) in approver_sets(n) { let got = evaluate(&policy, &r, env, author, &set); let want = expected(n, env, is_member, &set); @@ -119,9 +121,15 @@ fn several_approvals_others_and_approver_lists() { let three = roster(3); assert_eq!(evaluate(&p, &three, "production", "mnovak", &[id(1)]).verdict, Verdict::Need(1)); assert_eq!(evaluate(&p, &three, "production", "mnovak", &[id(0), id(1)]).verdict, Verdict::Need(1)); - assert_eq!(evaluate(&p, &three, "production", "mnovak", &[id(1), id(2)]).verdict, Verdict::Allowed(KeyRole::Release)); + assert_eq!( + evaluate(&p, &three, "production", "mnovak", &[id(1), id(2)]).verdict, + Verdict::Allowed(KeyRole::Release) + ); // Someone not named as the author (a release by a non-member): every member counts. - assert_eq!(evaluate(&p, &three, "production", "stranger", &[id(0), id(1)]).verdict, Verdict::Allowed(KeyRole::Release)); + assert_eq!( + evaluate(&p, &three, "production", "stranger", &[id(0), id(1)]).verdict, + Verdict::Allowed(KeyRole::Release) + ); // Only member 1 may approve staging. p.set_rule(EnvRule { env: "staging".into(), approvals: 1, others: false, approvers: vec![id(1)] }).unwrap(); @@ -138,7 +146,10 @@ fn several_approvals_others_and_approver_lists() { q.set_rule(EnvRule { env: "staging".into(), approvals: 1, others: true, approvers: vec![id(0)] }).unwrap(); q }; - assert_eq!(evaluate(&solo_policy, &roster(1), "staging", "mnovak", &[id(0)]).verdict, Verdict::Allowed(KeyRole::Release)); + assert_eq!( + evaluate(&solo_policy, &roster(1), "staging", "mnovak", &[id(0)]).verdict, + Verdict::Allowed(KeyRole::Release) + ); // A preview made strict. p.set_rule(EnvRule { env: "preview".into(), approvals: 1, others: false, approvers: vec![] }).unwrap(); assert_eq!(evaluate(&p, &four, "preview", "mnovak", &[]).verdict, Verdict::Need(1)); @@ -163,7 +174,10 @@ fn the_policy_text_is_canonical_and_its_rules_are_checked() { text.replace("\"approvers\":[]}]", "\"approvers\":[\"nope\"]}]"), text.replace("{\"v\":1,", "{\"v\":2,"), text.clone() + "\n", - text.replace("\"others\":false,\"approvers\":[]},{\"env\":\"staging\"", "\"others\":false,\"approvers\":[],\"x\":1},{\"env\":\"staging\""), + text.replace( + "\"others\":false,\"approvers\":[]},{\"env\":\"staging\"", + "\"others\":false,\"approvers\":[],\"x\":1},{\"env\":\"staging\"", + ), ] { assert!(Policy::parse(&bad).is_err(), "{bad}"); } @@ -178,7 +192,11 @@ fn the_identity_signs_the_policy_and_nothing_else_passes_for_it() { let text = Policy::default_for(TEAM).canonical(); let sig = identity.sign_policy(&text); assert!(custos_team::member::verify_policy_sig(&identity.public(), &text, &sig)); - assert!(!custos_team::member::verify_policy_sig(&identity.public(), &text.replace("\"approvals\":1,\"others\":true", "\"approvals\":0,\"others\":true"), &sig)); + assert!(!custos_team::member::verify_policy_sig( + &identity.public(), + &text.replace("\"approvals\":1,\"others\":true", "\"approvals\":0,\"others\":true"), + &sig + )); // A proof signature is not a policy signature. let proof_sig = identity.sign_proof(&text); assert!(!custos_team::member::verify_policy_sig(&identity.public(), &text, &proof_sig)); diff --git a/crates/custos-team/tests/request.rs b/crates/custos-team/tests/request.rs index a764744..1851ee4 100644 --- a/crates/custos-team/tests/request.rs +++ b/crates/custos-team/tests/request.rs @@ -166,7 +166,10 @@ fn a_proof_verifies_only_for_its_request_signature_policy_and_identity() { assert_eq!((v.role, v.signer.as_str(), v.approvers.clone()), (KeyRole::Release, b.id().as_str(), vec![b.id()])); // Another identity, another envelope, another request. - assert_eq!(verify_proof(&good, &TeamIdentity::generate().public(), &req_text, &envelope), Err(TeamError::BadSignature)); + assert_eq!( + verify_proof(&good, &TeamIdentity::generate().public(), &req_text, &envelope), + Err(TeamError::BadSignature) + ); assert!(verify_proof(&good, &identity.public(), &req_text, b"another signature").is_err()); let mut other = req.clone(); other.release = "0.7.1".into(); diff --git a/crates/custos-vault/src/team.rs b/crates/custos-vault/src/team.rs index f27f6df..354c090 100644 --- a/crates/custos-vault/src/team.rs +++ b/crates/custos-vault/src/team.rs @@ -565,7 +565,13 @@ impl Vault { /// A new team vault with `founder` as its only member, the default policy, and no keys yet /// ([`Vault::team_create_keys`]). The caller saves it where the team keeps it. - pub fn create_team(name: &str, founder: &MemberKey, founder_name: &str, handles: &[String], now: i64) -> Result { + pub fn create_team( + name: &str, + founder: &MemberKey, + founder_name: &str, + handles: &[String], + now: i64, + ) -> Result { let name = name.trim(); if !canon::text_ok(name, 64) || !canon::text_ok(founder_name.trim(), 64) || !handles_ok(handles) { return Err(Error::Team("a team or member name is 1 to 64 printable characters".into())); @@ -749,7 +755,13 @@ impl Vault { } let gone = t.members.remove(at); t.epoch = t.epoch.checked_add(1).ok_or(Error::Corrupted)?; - t.removed.push(RemovedMember { id: gone.id.clone(), name: gone.name.clone(), at: now, by: by.into(), epoch: t.epoch }); + t.removed.push(RemovedMember { + id: gone.id.clone(), + name: gone.name.clone(), + at: now, + by: by.into(), + epoch: t.epoch, + }); if t.policy.envs.iter().any(|r| r.approvers.contains(&gone.id)) { for r in t.policy.envs.iter_mut() { r.approvers.retain(|a| *a != gone.id); @@ -793,7 +805,8 @@ impl Vault { return Err(Error::Team("an approver is not a member".into())); } let env = rule.env.clone(); - let detail = format!("{env}: {} approval(s){}", rule.approvals, if rule.others { ", not the author" } else { "" }); + let detail = + format!("{env}: {} approval(s){}", rule.approvals, if rule.others { ", not the author" } else { "" }); t.policy.set_rule(rule).map_err(team_err)?; t.sign_policy(); t.log(now, TeamAction::PolicySet, by, Some(detail)); @@ -854,7 +867,12 @@ impl Vault { } /// An Android key for one project (`apk`), scoped to it. Slow: RSA-4096. The caller saves. - pub fn team_create_android(&mut self, spec: &crate::signing::AndroidKey, by: &str, now: i64) -> Result { + pub fn team_create_android( + &mut self, + spec: &crate::signing::AndroidKey, + by: &str, + now: i64, + ) -> Result { self.team()?; let id = self.signing_create_android(spec, now)?; self.team_mut()?.log(now, TeamAction::KeysCreated, by, Some(id.clone())); @@ -876,7 +894,9 @@ impl Vault { || new.secret.is_empty() || new.secret.expose().len() > MAX_SECRET { - return Err(Error::Team("a target needs a name, a project, an environment and a credential (≤ 64 KiB)".into())); + return Err(Error::Team( + "a target needs a name, a project, an environment and a credential (≤ 64 KiB)".into(), + )); } let id = short_id(); let name = new.name.trim().to_owned(); @@ -927,7 +947,12 @@ impl Vault { /// request this team vault signed for the target's project and environment within 25 hours, and this /// (target, deployment) was never released before. Returns the plaintext to seal to the request's key. /// The caller saves. - pub fn team_target_release(&mut self, request: &str, by: &str, now: i64) -> Result<(Zeroizing, TargetRequest), Error> { + pub fn team_target_release( + &mut self, + request: &str, + by: &str, + now: i64, + ) -> Result<(Zeroizing, TargetRequest), Error> { let now_s = seconds(now); let req = TargetRequest::check(request, now_s).map_err(team_err)?; let signed: Vec<(String, String, i64)> = self @@ -1068,7 +1093,13 @@ impl Vault { /// A member approves without signing (the policy needs more): the statement to post. `shown_code` is the code /// the card showed. - pub fn team_approve(&self, text: &str, key: &MemberKey, shown_code: &str, now: i64) -> Result { + pub fn team_approve( + &self, + text: &str, + key: &MemberKey, + shown_code: &str, + now: i64, + ) -> Result { let req = self.team_check_request(text, now)?; self.team_may_approve(&req, &key.id())?; approve(key, &req, shown_code, seconds(now)) @@ -1077,7 +1108,8 @@ impl Vault { /// Records a refusal by `by`; the request is never signed here afterwards. The caller saves. pub fn team_refuse(&mut self, text: &str, by: &str, reason: &str, now: i64) -> Result<(), Error> { let req = SignRequest::parse(text).map_err(team_err)?; - let entry = self.team_audit_entry(&req, Outcome::Refused, by, Some(reason.chars().take(200).collect()), None, &[], now); + let entry = + self.team_audit_entry(&req, Outcome::Refused, by, Some(reason.chars().take(200).collect()), None, &[], now); self.signing.audit.push(entry); let hash = req.hash_hex(); self.team_mut()?.mark_seen(hash, seconds(now)); @@ -1149,11 +1181,8 @@ impl Vault { let me = key.id(); let me_member = roster.iter().find(|m| m.id == me).ok_or(TeamError::NotAMember)?; let rule = t.policy.rule(&req.env).ok_or(TeamError::UnknownEnv)?; - let mut approvals: Vec = statements - .iter() - .filter(|s| s.member != me && s.verify(&req, &roster).is_ok()) - .cloned() - .collect(); + let mut approvals: Vec = + statements.iter().filter(|s| s.member != me && s.verify(&req, &roster).is_ok()).cloned().collect(); if rule.approvals > 0 { let code = shown_code.ok_or(TeamError::WrongMatch)?; may_approve(&t.policy, &roster, &req.env, &req.author, &me)?; diff --git a/crates/custos-vault/tests/team.rs b/crates/custos-vault/tests/team.rs index 6d0202e..74d22aa 100644 --- a/crates/custos-vault/tests/team.rs +++ b/crates/custos-vault/tests/team.rs @@ -43,7 +43,11 @@ fn request(vault: &Vault, env: &str, author: &str, format: Format, fabrica: &cus env: env.into(), author: author.into(), format, - artifact: Artifact { name: "Arca_0.7.0_x64-setup.exe".into(), size: 13, sha256: custos_sign::sha256_hex(b"the installer") }, + artifact: Artifact { + name: "Arca_0.7.0_x64-setup.exe".into(), + size: 13, + sha256: custos_sign::sha256_hex(b"the installer"), + }, artifact_digest: digest.to_vec(), image: image.into(), public: fabrica.public(), @@ -67,7 +71,9 @@ fn members_open_the_team_vault_and_outsiders_do_not() { assert!(matches!(Vault::open_team(&bytes, &MemberKey::generate()), Err(Error::NotAMember))); // A team vault is not a two-factor vault, and a personal vault is not a team vault. assert!(Vault::factor_kinds(&bytes).unwrap().is_empty()); - let mut personal = Vault::create(&SecretString::from("pw"), custos_vault::KdfParams { m_cost_kib: 8, t_cost: 1, p_cost: 1 }).unwrap(); + let mut personal = + Vault::create(&SecretString::from("pw"), custos_vault::KdfParams { m_cost_kib: 8, t_cost: 1, p_cost: 1 }) + .unwrap(); assert!(matches!(Vault::open_team(&personal.to_bytes().unwrap(), &t.a), Err(Error::NotATeam))); // A changed header byte (a member slot moved to another epoch) does not open. let mut moved = bytes.clone(); @@ -171,7 +177,9 @@ fn contains(hay: &[u8], needle: &[u8]) -> bool { #[test] fn a_member_refuses_an_older_copy_or_another_identity() { let mut t = team(); - let mut own = Vault::create(&SecretString::from("pw"), custos_vault::KdfParams { m_cost_kib: 8, t_cost: 1, p_cost: 1 }).unwrap(); + let mut own = + Vault::create(&SecretString::from("pw"), custos_vault::KdfParams { m_cost_kib: 8, t_cost: 1, p_cost: 1 }) + .unwrap(); // Mark's own vault holds his member key; the team was made with the same key. let c = MemberKey::generate(); let code = c.public().join_code(); @@ -192,15 +200,29 @@ fn the_policy_is_set_per_environment_signed_and_kept() { let mut t = team(); let p = t .vault - .team_set_rule(EnvRule { env: "staging".into(), approvals: 1, others: true, approvers: vec![t.b.id()] }, &t.a.id(), NOW) + .team_set_rule( + EnvRule { env: "staging".into(), approvals: 1, others: true, approvers: vec![t.b.id()] }, + &t.a.id(), + NOW, + ) .unwrap(); assert_eq!(p.rev, 2); - assert!(t.vault.team_set_rule(EnvRule { env: "staging".into(), approvals: 1, others: true, approvers: vec!["ab".repeat(16)] }, &t.a.id(), NOW).is_err()); + assert!( + t.vault + .team_set_rule( + EnvRule { env: "staging".into(), approvals: 1, others: true, approvers: vec!["ab".repeat(16)] }, + &t.a.id(), + NOW + ) + .is_err() + ); let v = Vault::open_team(&t.vault.to_bytes().unwrap(), &t.b).unwrap(); let (text, sig) = v.team_policy_signed().unwrap(); assert!(custos_team::member::verify_policy_sig(&v.team_identity().unwrap(), &text, &sig)); assert_eq!(v.team_policy().unwrap().rule("staging").unwrap().approvers, vec![t.b.id()]); - assert!(v.team_audit().unwrap().iter().any(|e| e.detail.as_deref() == Some("staging: 1 approval(s), not the author"))); + assert!( + v.team_audit().unwrap().iter().any(|e| e.detail.as_deref() == Some("staging: 1 approval(s), not the author")) + ); } /// Mandatory: a signing request with a wrong or replayed match code is refused. @@ -232,7 +254,10 @@ fn wrong_and_replayed_match_codes_are_refused() { assert!(eval.counted.is_empty()); // An expired request. let late = request(&t.vault, "staging", "mnovak", Format::Minisign, &fabrica); - assert_eq!(t.vault.team_sign(&late, &[], &t.a, Some(&code), None, NOW + 3_601_000).unwrap_err(), TeamError::Expired); + assert_eq!( + t.vault.team_sign(&late, &[], &t.a, Some(&code), None, NOW + 3_601_000).unwrap_err(), + TeamError::Expired + ); } #[test] @@ -241,7 +266,13 @@ fn two_approvals_collect_statements_and_the_last_approver_signs() { let c = MemberKey::generate(); let code = c.public().join_code(); t.vault.team_add_member(&c.public(), "Jan", &[], &t.a.id(), &code, NOW).unwrap(); - t.vault.team_set_rule(EnvRule { env: "production".into(), approvals: 2, others: true, approvers: vec![] }, &t.a.id(), NOW).unwrap(); + t.vault + .team_set_rule( + EnvRule { env: "production".into(), approvals: 2, others: true, approvers: vec![] }, + &t.a.id(), + NOW, + ) + .unwrap(); let fabrica = custos_seal::Recipient::generate(); let text = request(&t.vault, "production", "mnovak", Format::Minisign, &fabrica); let code = fabrica.match_code(); @@ -382,15 +413,17 @@ fn a_target_credential_is_released_once_for_a_signed_deployment_of_its_project_a assert!(!format!("{:?}", t.vault.team_targets().unwrap()).contains("apiVersion")); let team_id = t.vault.team_view().unwrap().id; let fabrica = custos_seal::Recipient::generate(); - let ask = |deployment: &str| TargetRequest { - team: team_id.clone(), - id: custos_team::new_id(), - target: target.clone(), - deployment: deployment.into(), - public: fabrica.public(), - expires: NOW_S + 600, - } - .canonical(); + let ask = |deployment: &str| { + TargetRequest { + team: team_id.clone(), + id: custos_team::new_id(), + target: target.clone(), + deployment: deployment.into(), + public: fabrica.public(), + expires: NOW_S + 600, + } + .canonical() + }; // No signed deployment yet. let unsigned = "ab".repeat(32); assert!(t.vault.team_target_release(&ask(&unsigned), &t.b.id(), NOW).is_err());