//! Mandatory: policy evaluation, every combination of team size, environment and the author/approver rule //! (docs/TEAM.md, "Evaluation"). use custos_team::policy::{EnvRule, may_approve}; use custos_team::*; const TEAM: &str = "0123456789abcdef0123456789abcdef"; fn roster(n: usize) -> Vec { (0..n) .map(|i| RosterMember { id: format!("{i:032x}"), name: format!("Member {i}"), // Member 0 is the author; Fabrica names them by handle, in another case. handles: if i == 0 { vec!["MNovak".into()] } else { vec![format!("dev{i}")] }, ed25519: [i as u8; 32], }) .collect() } fn id(i: usize) -> String { format!("{i:032x}") } const OUTSIDER: &str = "ffffffffffffffffffffffffffffffff"; /// The approver sets every size is tried with. fn approver_sets(n: usize) -> Vec<(&'static str, Vec)> { let mut v = vec![("nobody", vec![]), ("the author", vec![id(0)]), ("an outsider", vec![OUTSIDER.into()])]; if n >= 2 { v.push(("one other", vec![id(1)])); v.push(("the author and one other", vec![id(0), id(1)])); v.push(("one other twice", vec![id(1), id(1)])); } if n >= 3 { v.push(("two others", vec![id(1), id(2)])); } v } /// The rule in the doc's own words, for the default policy. fn expected(n: usize, env: &str, author_is_member: bool, set: &[String]) -> Verdict { let members: Vec<&String> = set.iter().filter(|a| a.as_str() != OUTSIDER).collect(); let mut distinct: Vec<&String> = Vec::new(); for m in members { if !distinct.contains(&m) { distinct.push(m); } } match env { "preview" => Verdict::Allowed(KeyRole::Test), "staging" => { if distinct.is_empty() { Verdict::Need(1) } else { Verdict::Allowed(KeyRole::Release) } } "production" => { // "1 by someone other than the release author when the team has 2+ members" // ... and the rule must know the author: someone no member is (or nobody named) cannot be excluded. if n >= 2 && !author_is_member { return Verdict::Unsatisfiable( "the release's author is not a member the team knows, so the rule cannot exclude them", ); } let author_excluded = n >= 2 && author_is_member; let counted = distinct.iter().filter(|m| !(author_excluded && m.as_str() == id(0))).count(); if counted >= 1 { Verdict::Allowed(KeyRole::Release) } else { Verdict::Need(1) } } _ => Verdict::UnknownEnv, } } #[test] fn every_size_environment_author_and_approver_under_the_default_policy() { let policy = Policy::default_for(TEAM); let mut cases = 0; for n in [1usize, 2, 3, 4, 10] { 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 (what, set) in approver_sets(n) { let got = evaluate(&policy, &r, env, author, &set); let want = expected(n, env, is_member, &set); assert_eq!(got.verdict, want, "size {n}, {env}, author {author:?}, approved by {what}"); cases += 1; } } } } assert!(cases > 400, "{cases}"); } #[test] fn a_team_of_one_approves_its_own_production_release_and_a_second_member_ends_that() { let p = Policy::default_for(TEAM); let solo = roster(1); assert_eq!(evaluate(&p, &solo, "production", "mnovak", &[id(0)]).verdict, Verdict::Allowed(KeyRole::Release)); assert!(may_approve(&p, &solo, "production", "mnovak", &id(0)).is_ok()); let two = roster(2); let e = evaluate(&p, &two, "production", "mnovak", &[id(0)]); assert_eq!(e.verdict, Verdict::Need(1)); assert_eq!(e.ignored, vec![id(0)]); assert_eq!(e.eligible, vec![id(1)]); assert_eq!( may_approve(&p, &two, "production", "mnovak", &id(0)), Err(TeamError::NotEligible("the release's author may not approve it here")) ); assert!(may_approve(&p, &two, "production", "mnovak", &id(1)).is_ok()); assert_eq!(may_approve(&p, &two, "production", "mnovak", OUTSIDER), Err(TeamError::NotAMember)); assert_eq!(may_approve(&p, &two, "qa", "mnovak", &id(1)), Err(TeamError::UnknownEnv)); } #[test] fn several_approvals_others_and_approver_lists() { let mut p = Policy::default_for(TEAM); p.set_rule(EnvRule { env: "production".into(), approvals: 2, others: true, approvers: vec![] }).unwrap(); assert_eq!(p.rev, 2); // Two members, the author excluded: one left for two approvals, which can never be met. assert!(matches!(evaluate(&p, &roster(2), "production", "mnovak", &[]).verdict, Verdict::Unsatisfiable(_))); // A team of one: `others` does not apply, but two approvals from one person cannot be. assert!(matches!(evaluate(&p, &roster(1), "production", "mnovak", &[id(0)]).verdict, Verdict::Unsatisfiable(_))); 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) ); // Someone no member is, or nobody named: the rule cannot exclude the author, so it refuses (core 0.18.1). for author in ["stranger", ""] { assert!(matches!( evaluate(&p, &three, "production", author, &[id(0), id(1)]).verdict, Verdict::Unsatisfiable(_) )); assert!(matches!(may_approve(&p, &three, "production", author, &id(1)), Err(TeamError::NotEligible(_)))); } // Only member 1 may approve staging. p.set_rule(EnvRule { env: "staging".into(), approvals: 1, others: false, approvers: vec![id(1)] }).unwrap(); let four = roster(4); assert_eq!(evaluate(&p, &four, "staging", "", &[id(2)]).verdict, Verdict::Need(1)); assert_eq!(evaluate(&p, &four, "staging", "", &[id(2), id(1)]).verdict, Verdict::Allowed(KeyRole::Release)); assert!(matches!(may_approve(&p, &four, "staging", "", &id(2)), Err(TeamError::NotEligible(_)))); // The only listed approver is the author, and the rule wants someone else. p.set_rule(EnvRule { env: "staging".into(), approvals: 1, others: true, approvers: vec![id(0)] }).unwrap(); assert!(matches!(evaluate(&p, &four, "staging", "mnovak", &[id(0)]).verdict, Verdict::Unsatisfiable(_))); // ... but in a team of one the author is the only member and approves. let solo_policy = { let mut q = Policy::default_for(TEAM); 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) ); // 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)); assert_eq!(evaluate(&p, &four, "preview", "mnovak", &[id(0)]).verdict, Verdict::Allowed(KeyRole::Release)); } #[test] fn the_policy_text_is_canonical_and_its_rules_are_checked() { let p = Policy::default_for(TEAM); let text = p.canonical(); assert_eq!( text, "{\"v\":1,\"team\":\"0123456789abcdef0123456789abcdef\",\"rev\":1,\"envs\":[{\"env\":\"preview\",\"approvals\":0,\"others\":false,\"approvers\":[]},{\"env\":\"staging\",\"approvals\":1,\"others\":false,\"approvers\":[]},{\"env\":\"production\",\"approvals\":1,\"others\":true,\"approvers\":[]}]}" ); assert_eq!(Policy::parse(&text).unwrap(), p); for bad in [ text.replace("{\"v\":1,", "{ \"v\":1,"), text.replace("\"rev\":1", "\"rev\":0"), text.replace("\"approvals\":0", "\"approvals\":11"), text.replace("\"staging\"", "\"preview\""), text.replace("\"staging\"", "\"Staging\""), 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\"", ), ] { assert!(Policy::parse(&bad).is_err(), "{bad}"); } let mut q = p.clone(); assert!(q.set_rule(EnvRule { env: "prod uction".into(), approvals: 1, others: true, approvers: vec![] }).is_err()); assert_eq!(q, p, "a refused change leaves the policy as it was"); } #[test] fn the_identity_signs_the_policy_and_nothing_else_passes_for_it() { let identity = TeamIdentity::generate(); 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 )); // 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)); assert_eq!(format!("{identity:?}"), "[redacted]"); }