[team] Team vaults: member keys, member slots (kind 6), policies per environment, the canonical sign.request, approval statements, policy proofs; cosign and Tauri updater envelopes; v0.18.0
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
//! 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<RosterMember> {
|
||||
(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<String>)> {
|
||||
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"
|
||||
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 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));
|
||||
|
||||
// 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]");
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
//! The canonical sign.request, match codes, approval statements and the policy proof (docs/TEAM.md).
|
||||
|
||||
use custos_team::policy::Policy;
|
||||
use custos_team::proof::ProofBody;
|
||||
use custos_team::statement::{approve, verified_members};
|
||||
use custos_team::*;
|
||||
|
||||
const NOW: u64 = 1_794_000_000;
|
||||
|
||||
pub fn request(team: &str, env: &str, author: &str, public: [u8; 32]) -> SignRequest {
|
||||
SignRequest {
|
||||
team: team.into(),
|
||||
id: "aa".repeat(16),
|
||||
project: "mnovak/Arca".into(),
|
||||
release: "0.7.0".into(),
|
||||
env: env.into(),
|
||||
author: author.into(),
|
||||
format: Format::Minisign,
|
||||
artifact: custos_team::request::Artifact {
|
||||
name: "Arca_0.7.0_x64-setup.exe".into(),
|
||||
size: 25_145_977,
|
||||
sha256: "c4".repeat(32),
|
||||
},
|
||||
artifact_digest: vec![9; 64],
|
||||
image: String::new(),
|
||||
public,
|
||||
expires: NOW + 3600,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_request_is_read_only_in_canonical_form() {
|
||||
let fabrica = custos_seal::Recipient::generate();
|
||||
let r = request(&"01".repeat(16), "production", "mnovak", fabrica.public());
|
||||
let text = r.canonical();
|
||||
assert!(text.starts_with("{\"v\":1,\"op\":\"sign.request\",\"team\":\"0101"));
|
||||
assert_eq!(SignRequest::check(&text, NOW).unwrap(), r);
|
||||
assert_eq!(r.match_code(), fabrica.match_code());
|
||||
for bad in [
|
||||
text.replace(",\"id\"", ", \"id\""),
|
||||
text.replace("\"op\":\"sign.request\"", "\"op\":\"sign.reply\""),
|
||||
text.replace("\"format\":\"minisign\"", "\"format\":\"gpg\""),
|
||||
text.replace("mnovak/Arca", "mnovak/Arca\\u0041"),
|
||||
text.replace("Arca_0.7.0_x64-setup.exe", "../Arca.exe"),
|
||||
text.replace("\"env\":\"production\"", "\"env\":\"Production\""),
|
||||
format!("{text}\n"),
|
||||
text.replace("\"image\":\"\"", "\"image\":\"arca\""),
|
||||
] {
|
||||
assert!(SignRequest::check(&bad, NOW).is_err(), "{bad}");
|
||||
}
|
||||
assert_eq!(SignRequest::check(&text, r.expires), Err(TeamError::Expired));
|
||||
let mut far = r.clone();
|
||||
far.expires = NOW + 25 * 3600 + 1;
|
||||
assert!(SignRequest::check(&far.canonical(), NOW).is_err());
|
||||
// Digest lengths per format.
|
||||
let mut apk = r.clone();
|
||||
apk.format = Format::Apk;
|
||||
apk.artifact_digest = vec![1; 128];
|
||||
assert!(SignRequest::check(&apk.canonical(), NOW).is_ok());
|
||||
apk.artifact_digest = vec![1; 64 * 5];
|
||||
assert!(SignRequest::check(&apk.canonical(), NOW).is_err());
|
||||
let mut cosign = r.clone();
|
||||
cosign.format = Format::Cosign;
|
||||
cosign.artifact_digest = vec![1; 32];
|
||||
assert!(SignRequest::check(&cosign.canonical(), NOW).is_err(), "cosign needs an image");
|
||||
cosign.image = "registry.example/team/arca".into();
|
||||
assert!(SignRequest::check(&cosign.canonical(), NOW).is_ok());
|
||||
}
|
||||
|
||||
fn roster_of(keys: &[&MemberKey]) -> Vec<RosterMember> {
|
||||
keys.iter()
|
||||
.enumerate()
|
||||
.map(|(i, k)| RosterMember {
|
||||
id: k.id(),
|
||||
name: format!("M{i}"),
|
||||
handles: if i == 0 { vec!["mnovak".into()] } else { vec![] },
|
||||
ed25519: k.public().ed25519,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wrong_match_code_is_refused_and_never_counts() {
|
||||
let (a, b) = (MemberKey::generate(), MemberKey::generate());
|
||||
let roster = roster_of(&[&a, &b]);
|
||||
let fabrica = custos_seal::Recipient::generate();
|
||||
let req = request(&"01".repeat(16), "production", "mnovak", fabrica.public());
|
||||
// B's card showed another code: B approves nothing.
|
||||
let wrong = if req.match_code() == "000000" { "000001" } else { "000000" };
|
||||
assert_eq!(approve(&b, &req, wrong, NOW), Err(TeamError::WrongMatch));
|
||||
// A request whose pub was swapped after B's card was drawn: the code B saw is the old request's.
|
||||
let shown = req.match_code();
|
||||
let mut swapped = req.clone();
|
||||
swapped.public = custos_seal::Recipient::generate().public();
|
||||
assert_eq!(approve(&b, &swapped, &shown, NOW), Err(TeamError::WrongMatch));
|
||||
// A statement made for one request does not count for the swapped one.
|
||||
let s = approve(&b, &req, &shown, NOW).unwrap();
|
||||
s.verify(&req, &roster).unwrap();
|
||||
assert_eq!(s.verify(&swapped, &roster), Err(TeamError::WrongMatch));
|
||||
assert!(verified_members(&[s.clone()], &swapped, &roster).is_empty());
|
||||
// A forged code inside a statement fails its signature or its code.
|
||||
let mut forged = s.clone();
|
||||
forged.code = wrong.into();
|
||||
assert_eq!(forged.verify(&req, &roster), Err(TeamError::WrongMatch));
|
||||
let mut other_member = s.clone();
|
||||
other_member.member = a.id();
|
||||
assert_eq!(other_member.verify(&req, &roster), Err(TeamError::BadSignature));
|
||||
// Not on the roster.
|
||||
let c = MemberKey::generate();
|
||||
let sc = approve(&c, &req, &shown, NOW).unwrap();
|
||||
assert_eq!(sc.verify(&req, &roster), Err(TeamError::NotAMember));
|
||||
assert_eq!(verified_members(&[s.clone(), s.clone(), sc], &req, &roster), vec![b.id()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn member_keys_seal_the_team_key_to_one_member_team_and_epoch() {
|
||||
let (a, b) = (MemberKey::generate(), MemberKey::generate());
|
||||
let team = "02".repeat(16);
|
||||
let key = [7u8; 32];
|
||||
let sealed = custos_team::member::seal_team_key(&key, &a.public(), &team, 3).unwrap();
|
||||
assert_eq!(sealed.len(), custos_team::member::SEALED_KEY_LEN);
|
||||
assert_eq!(*a.open_team_key(&sealed, &team, 3).unwrap(), key);
|
||||
assert_eq!(a.open_team_key(&sealed, &team, 4), Err(TeamError::NotAMember));
|
||||
assert_eq!(a.open_team_key(&sealed, &"03".repeat(16), 3), Err(TeamError::NotAMember));
|
||||
assert_eq!(b.open_team_key(&sealed, &team, 3), Err(TeamError::NotAMember));
|
||||
// The key round-trips through its secrets; ids and codes are stable.
|
||||
let again = MemberKey::from_secrets(a.x25519_secret(), a.ed25519_secret()).unwrap();
|
||||
assert_eq!(again.public(), a.public());
|
||||
assert_eq!(a.public().join_code(), custos_seal::match_code(&a.public().x25519));
|
||||
assert_eq!(a.id().len(), 32);
|
||||
assert_ne!(a.id(), b.id());
|
||||
assert_eq!(format!("{a:?}"), "[redacted]");
|
||||
a.public().check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_proof_verifies_only_for_its_request_signature_policy_and_identity() {
|
||||
let identity = TeamIdentity::generate();
|
||||
let team = "04".repeat(16);
|
||||
let (a, b) = (MemberKey::generate(), MemberKey::generate());
|
||||
let roster = roster_of(&[&a, &b]);
|
||||
let policy = Policy::default_for(&team);
|
||||
let policy_text = policy.canonical();
|
||||
let fabrica = custos_seal::Recipient::generate();
|
||||
let req = request(&team, "production", "mnovak", fabrica.public());
|
||||
let req_text = req.canonical();
|
||||
let s = approve(&b, &req, &req.match_code(), NOW).unwrap();
|
||||
let envelope = b"untrusted comment: ...".to_vec();
|
||||
let body = |approvals: Vec<Statement>, signer: String, role: KeyRole| ProofBody {
|
||||
team: team.clone(),
|
||||
request: req.hash_hex(),
|
||||
env: req.env.clone(),
|
||||
author: req.author.clone(),
|
||||
policy: policy_text.clone(),
|
||||
policy_sig: identity.sign_policy(&policy_text),
|
||||
roster: roster.clone(),
|
||||
approvals,
|
||||
signer,
|
||||
role,
|
||||
signature: custos_sign::sha256_hex(&envelope),
|
||||
at: NOW,
|
||||
};
|
||||
let good = body(vec![s.clone()], b.id(), KeyRole::Release).sign(&identity);
|
||||
assert_eq!(ProofBody::parse(&good.text).unwrap(), body(vec![s.clone()], b.id(), KeyRole::Release));
|
||||
let v = verify_proof(&good, &identity.public(), &req_text, &envelope).unwrap();
|
||||
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!(verify_proof(&good, &identity.public(), &req_text, b"another signature").is_err());
|
||||
let mut other = req.clone();
|
||||
other.release = "0.7.1".into();
|
||||
assert!(verify_proof(&good, &identity.public(), &other.canonical(), &envelope).is_err());
|
||||
// The author's own approval does not make a production proof.
|
||||
let sa = approve(&a, &req, &req.match_code(), NOW).unwrap();
|
||||
let selfish = body(vec![sa], a.id(), KeyRole::Release).sign(&identity);
|
||||
assert_eq!(verify_proof(&selfish, &identity.public(), &req_text, &envelope), Err(TeamError::NeedMore(1)));
|
||||
// A proof claiming the test role for production, or signed by someone who did not approve.
|
||||
let wrong_role = body(vec![s.clone()], b.id(), KeyRole::Test).sign(&identity);
|
||||
assert!(verify_proof(&wrong_role, &identity.public(), &req_text, &envelope).is_err());
|
||||
let not_approver = body(vec![s.clone()], a.id(), KeyRole::Release).sign(&identity);
|
||||
assert!(verify_proof(¬_approver, &identity.public(), &req_text, &envelope).is_err());
|
||||
// An edited text breaks the identity's signature.
|
||||
let mut edited = good.clone();
|
||||
edited.text = edited.text.replace("\"production\"", "\"staging\"");
|
||||
assert_eq!(verify_proof(&edited, &identity.public(), &req_text, &envelope), Err(TeamError::BadSignature));
|
||||
}
|
||||
Reference in New Issue
Block a user