[vault] Signing: the Android grant, one answer for every Domi app's release APK and its key on first use; v0.19.0
signing.android: patterns owner/name or owner/*, the certificate's O and C, uses, last used and the keys it made. A request fits when it is an apk from this PC's pipe with a run, pipeline release, a covered repository, and that repository's own app key (<name>-android, scoped to exactly it and release, role release), while card-signing is not ticked; a key's first signature fits. android_grant_make_key makes that key for a covered repository (android-key-made in the security audit). Granting and revoking write standing-granted and standing-revoked. A vault whose grant made a key needs core 0.19.0; older cores drop the grant itself.
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
//! The Android grant (core 0.19.0, docs/SIGNING.md "The Android grant"): one answer for every Domi app's release
|
||||
//! APK. It signs a request for a repository's own app key (`<name>-android`, scoped to exactly that repository and
|
||||
//! `release`) from this PC's pipe, its first signature included, and makes that key when the app's first release
|
||||
//! asks. Everything else still needs a card.
|
||||
|
||||
use custos_vault::security::{Action, Auth, Item};
|
||||
use custos_vault::signing::{
|
||||
AndroidKey, Approval, Context, KeyKind, Outcome, Refusal, Request, Role, SignKind, Via, android_key_id,
|
||||
};
|
||||
use custos_vault::{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;
|
||||
const COMMIT: &str = "6925d0cb08b9e0c5c3a4a7a2c37b7f2a9f7ed3b1";
|
||||
|
||||
fn s(v: &[&str]) -> Vec<String> {
|
||||
v.iter().map(|x| x.to_string()).collect()
|
||||
}
|
||||
|
||||
fn vault() -> Vault {
|
||||
let mut v = Vault::create(&SecretString::from("pw"), FAST).unwrap();
|
||||
v.signing_create(Role::Release, &s(&["release"]), &s(&["mnovak/Culina"]), NOW).unwrap();
|
||||
v
|
||||
}
|
||||
|
||||
fn granted(repos: &[&str]) -> Vault {
|
||||
let mut v = vault();
|
||||
v.android_grant_set(&s(repos), "Marek Novak", "CZ", NOW).unwrap();
|
||||
v
|
||||
}
|
||||
|
||||
fn apk(key: &str, repo: &str, pipeline: &str, run: Option<&str>) -> Request {
|
||||
Request {
|
||||
key: key.into(),
|
||||
kind: SignKind::Apk,
|
||||
digest: vec![5; 64],
|
||||
manifest: None,
|
||||
approval: None,
|
||||
run: run.map(Into::into),
|
||||
context: Context {
|
||||
repo: repo.into(),
|
||||
commit: COMMIT.into(),
|
||||
pipeline: pipeline.into(),
|
||||
step: "sign-apk".into(),
|
||||
trigger: "tag".into(),
|
||||
file: "Culina-1.2.0.apk".into(),
|
||||
size: 9_400_000,
|
||||
sha256: "c4995c6e2e0c20fec7e8dcb05d70144da58f42d379c6705df66edb42abafb8a3".into(),
|
||||
version: "1.2.0".into(),
|
||||
},
|
||||
via: Via::Local,
|
||||
}
|
||||
}
|
||||
|
||||
fn culina(run: &str) -> Request {
|
||||
apk("culina-android", "mnovak/Culina", "release", Some(run))
|
||||
}
|
||||
|
||||
fn notes(v: &Vault, action: Action) -> Vec<String> {
|
||||
v.security_view(NOW + 1_000)
|
||||
.audit
|
||||
.into_iter()
|
||||
.filter(|e| e.action == action)
|
||||
.map(|e| e.detail.unwrap_or_default())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_app_key_is_named_by_its_repository() {
|
||||
assert_eq!(android_key_id("mnovak/Culina").as_deref(), Some("culina-android"));
|
||||
assert_eq!(android_key_id("mnovak/domi-office").as_deref(), Some("domi-office-android"));
|
||||
assert_eq!(android_key_id("mnovak/Domi.Office_2").as_deref(), Some("domi-office-2-android"));
|
||||
for bad in ["Culina", "mnovak/", "/Culina", "mnovak/*", "a/b/c", "mnovak/Cu lina", "mnovak/Čulina", ""] {
|
||||
assert_eq!(android_key_id(bad), None, "{bad}");
|
||||
}
|
||||
assert_eq!(android_key_id(&format!("mnovak/{}", "x".repeat(57))), None, "the id would be longer than 64");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_grant_makes_an_apps_key_and_signs_its_first_apk_without_a_card() {
|
||||
let mut v = granted(&["mnovak/*"]);
|
||||
assert!(!v.android_grant_fits(&culina("r-1"), NOW + 1), "no key yet");
|
||||
|
||||
let id = v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW + 1).unwrap();
|
||||
assert_eq!(id, "culina-android");
|
||||
let k = v.signing_keys().into_iter().find(|k| k.id == id).unwrap();
|
||||
assert_eq!((k.kind, k.role, k.name.as_str()), (KeyKind::Android, Role::Release, "Culina Android"));
|
||||
assert_eq!((k.pipelines.clone(), k.repos.clone()), (s(&["release"]), s(&["mnovak/Culina"])));
|
||||
let cert = String::from_utf8_lossy(k.cert.as_ref().unwrap()).into_owned();
|
||||
assert!(cert.contains("Culina") && cert.contains("Marek Novak"), "the certificate names the app and Mark");
|
||||
|
||||
// Its first signature: no card, recorded as `standing`, counted on the grant.
|
||||
assert!(v.android_grant_fits(&culina("r-1"), NOW + 2));
|
||||
assert!(v.standing_rule_for(&culina("r-1"), NOW + 2).is_none(), "the grant is not a per-key rule");
|
||||
let signed = v.signing_sign(&culina("r-1"), Approval::Standing, NOW + 2);
|
||||
assert!(signed.is_ok(), "{signed:?}");
|
||||
assert_eq!(v.signing_audit().last().unwrap().outcome, Outcome::Standing);
|
||||
assert_eq!(v.signing_audit().last().unwrap().context.file, "Culina-1.2.0.apk");
|
||||
v.signing_sign(&culina("r-2"), Approval::Standing, NOW + 3).unwrap();
|
||||
let g = v.android_grant().unwrap();
|
||||
assert_eq!((g.uses, g.last_used, g.made.clone()), (2, Some(NOW + 3), s(&["culina-android"])));
|
||||
|
||||
assert_eq!(notes(&v, Action::AndroidKeyMade), ["culina-android for mnovak/Culina pipeline release"]);
|
||||
let granted_note = notes(&v, Action::StandingGranted);
|
||||
assert_eq!(granted_note.len(), 1);
|
||||
assert!(granted_note[0].contains("Android releases") && granted_note[0].contains("mnovak/*"), "{granted_note:?}");
|
||||
// The public note call cannot write it.
|
||||
assert!(v.security_note(Action::AndroidKeyMade, "x", NOW).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_grant_never_makes_a_key_it_does_not_cover() {
|
||||
let mut v = granted(&["mnovak/Culina", "mnovak/Liber"]);
|
||||
for (repo, pipeline, via, why) in [
|
||||
("mnovak/Postea", "release", Via::Local, "a repository no pattern matches"),
|
||||
("someone/Culina", "release", Via::Local, "another owner"),
|
||||
("mnovak/Culina", "ci", Via::Local, "another pipeline"),
|
||||
("mnovak/Culina", "release", Via::Remote, "the server runner"),
|
||||
("Culina", "release", Via::Local, "not owner/name"),
|
||||
] {
|
||||
assert!(v.android_grant_make_key(repo, pipeline, via, NOW + 1).is_err(), "{why}");
|
||||
}
|
||||
assert!(v.signing_keys().iter().all(|k| k.kind != KeyKind::Android), "nothing was made");
|
||||
v.android_grant_make_key("mnovak/Liber", "release", Via::Local, NOW + 2).unwrap();
|
||||
assert!(v.android_grant_make_key("mnovak/Liber", "release", Via::Local, NOW + 3).is_err(), "made once");
|
||||
|
||||
v.security_tick(Item::CardSigning, NOW + 4);
|
||||
assert!(v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW + 5).is_err(), "paused");
|
||||
v.security_untick(Item::CardSigning, Auth::Hello, NOW + 6);
|
||||
assert!(v.android_grant_revoke(NOW + 7));
|
||||
assert!(v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW + 8).is_err(), "no grant");
|
||||
assert_eq!(v.android_grant().map(|g| g.made), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_repositorys_own_app_key_in_its_own_scope_from_this_pc_with_a_run_fits() {
|
||||
let mut v = granted(&["mnovak/*"]);
|
||||
v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW).unwrap();
|
||||
// A key of the right name with a wider scope, and one for the wrong repository, made by hand.
|
||||
let wide = AndroidKey {
|
||||
id: "liber-android".into(),
|
||||
name: "Liber Android".into(),
|
||||
cn: "Liber".into(),
|
||||
o: "Marek Novak".into(),
|
||||
c: "CZ".into(),
|
||||
role: Role::Release,
|
||||
pipelines: s(&["release"]),
|
||||
repos: s(&["*"]),
|
||||
};
|
||||
v.signing_create_android(&wide, NOW).unwrap();
|
||||
v.signing_create_android(
|
||||
&AndroidKey { id: "other-android".into(), repos: s(&["mnovak/Rosea"]), ..wide.clone() },
|
||||
NOW,
|
||||
)
|
||||
.unwrap();
|
||||
v.signing_create_android(&AndroidKey { id: "domi-test-android".into(), role: Role::Test, ..wide.clone() }, NOW)
|
||||
.unwrap();
|
||||
|
||||
let mut remote = culina("r-1");
|
||||
remote.via = Via::Remote;
|
||||
let mut minisign = culina("r-1");
|
||||
minisign.kind = SignKind::Minisign;
|
||||
minisign.key = "domi-release".into();
|
||||
minisign.digest = vec![7; 64];
|
||||
for (req, why) in [
|
||||
(remote, "the server runner"),
|
||||
(apk("culina-android", "mnovak/Culina", "release", None), "no run"),
|
||||
(apk("liber-android", "mnovak/Liber", "release", Some("r-1")), "a key with a wider scope"),
|
||||
(apk("other-android", "mnovak/Rosea", "release", Some("r-1")), "not the repository's app key"),
|
||||
(apk("domi-test-android", "mnovak/Liber", "release", Some("r-1")), "a test key"),
|
||||
(minisign, "another kind"),
|
||||
] {
|
||||
assert!(!v.android_grant_fits(&req, NOW + 1), "{why}");
|
||||
assert!(v.signing_sign(&req, Approval::Standing, NOW + 1).is_err(), "{why}: never signed without a card");
|
||||
assert_ne!(v.signing_audit().last().unwrap().outcome, Outcome::Standing, "{why}");
|
||||
}
|
||||
assert_eq!(v.android_grant().unwrap().uses, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_explicit_list_covers_only_its_repositories_and_bad_patterns_are_refused() {
|
||||
let mut v = granted(&["mnovak/Culina"]);
|
||||
v.signing_create_android(
|
||||
&AndroidKey {
|
||||
id: "liber-android".into(),
|
||||
name: "Liber Android".into(),
|
||||
cn: "Liber".into(),
|
||||
o: "Marek Novak".into(),
|
||||
c: "CZ".into(),
|
||||
role: Role::Release,
|
||||
pipelines: s(&["release"]),
|
||||
repos: s(&["mnovak/Liber"]),
|
||||
},
|
||||
NOW,
|
||||
)
|
||||
.unwrap();
|
||||
let liber = apk("liber-android", "mnovak/Liber", "release", Some("r-1"));
|
||||
assert!(!v.android_grant_fits(&liber, NOW + 1), "not in the list");
|
||||
// Editing the list keeps the counts and covers the new repository; a key made by hand in that shape is covered.
|
||||
v.android_grant_set(&s(&["mnovak/Culina", "mnovak/Liber", "mnovak/Liber"]), "Marek Novak", "CZ", NOW + 2).unwrap();
|
||||
assert_eq!(v.android_grant().unwrap().repos, s(&["mnovak/Culina", "mnovak/Liber"]));
|
||||
assert!(v.android_grant_fits(&liber, NOW + 3));
|
||||
|
||||
for bad in [&[][..], &["mnovak"], &["*/*"], &["*"], &["mnovak/*/x"], &["mn ovak/*"], &["mnovak/Cu*"]] {
|
||||
assert!(v.android_grant_set(&s(bad), "Marek Novak", "CZ", NOW + 4).is_err(), "{bad:?}");
|
||||
}
|
||||
let many: Vec<String> = (0..65).map(|i| format!("mnovak/app{i}")).collect();
|
||||
assert!(v.android_grant_set(&many, "Marek Novak", "CZ", NOW + 4).is_err(), "at most 64");
|
||||
assert!(v.android_grant_set(&s(&["mnovak/*"]), "Marek Novak", "Czech", NOW + 4).is_err(), "a two-letter country");
|
||||
assert!(v.android_grant_set(&s(&["mnovak/*"]), "", "CZ", NOW + 4).is_err(), "an organisation");
|
||||
assert_eq!(
|
||||
v.android_grant().unwrap().repos,
|
||||
s(&["mnovak/Culina", "mnovak/Liber"]),
|
||||
"a refused edit changes nothing"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sign_only_with_a_card_pauses_the_grant_and_revoking_ends_it() {
|
||||
let mut v = granted(&["mnovak/*"]);
|
||||
v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW).unwrap();
|
||||
v.security_tick(Item::CardSigning, NOW + 1);
|
||||
assert!(!v.android_grant_fits(&culina("r-1"), NOW + 2));
|
||||
assert!(matches!(v.signing_sign(&culina("r-1"), Approval::Standing, NOW + 2), Err(Refusal::NeedsApproval(_))));
|
||||
assert!(v.android_grant_set(&s(&["mnovak/*"]), "Marek Novak", "CZ", NOW + 2).is_err(), "no grant while ticked");
|
||||
assert!(v.android_grant().is_some(), "kept, paused");
|
||||
v.security_untick(Item::CardSigning, Auth::Hello, NOW + 3);
|
||||
assert!(v.android_grant_fits(&culina("r-1"), NOW + 4));
|
||||
|
||||
assert!(v.android_grant_revoke(NOW + 5));
|
||||
assert!(!v.android_grant_revoke(NOW + 6), "once");
|
||||
assert!(!v.android_grant_fits(&culina("r-2"), NOW + 7));
|
||||
assert!(matches!(v.signing_sign(&culina("r-2"), Approval::Standing, NOW + 7), Err(Refusal::NeedsApproval(_))));
|
||||
// Mark still signs it on a card, and the key stays.
|
||||
assert!(v.signing_sign(&culina("r-2"), Approval::Mark, NOW + 8).is_ok());
|
||||
let revoked = notes(&v, Action::StandingRevoked);
|
||||
assert_eq!(revoked.len(), 1);
|
||||
assert!(revoked[0].contains("Android releases"), "{revoked:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_standing_rule_for_the_same_request_is_used_first() {
|
||||
let mut v = granted(&["mnovak/*"]);
|
||||
v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW).unwrap();
|
||||
v.signing_sign(&culina("r-1"), Approval::Mark, NOW + 1).unwrap();
|
||||
v.standing_grant(&culina("r-2"), NOW + 2).unwrap();
|
||||
v.signing_sign(&culina("r-2"), Approval::Standing, NOW + 2).unwrap();
|
||||
assert_eq!(v.standing_rules()[0].uses, 1);
|
||||
assert_eq!(v.android_grant().unwrap().uses, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_grant_and_its_counts_survive_a_reopen() {
|
||||
let mut v = granted(&["mnovak/*", "domi/*"]);
|
||||
v.android_grant_make_key("mnovak/Culina", "release", Via::Local, NOW).unwrap();
|
||||
v.signing_sign(&culina("r-1"), Approval::Standing, NOW + 1).unwrap();
|
||||
|
||||
let mut back = Vault::open(&v.to_bytes().unwrap(), &SecretString::from("pw")).unwrap();
|
||||
assert_eq!(back.android_grant(), v.android_grant());
|
||||
assert!(back.signing_sign(&culina("r-2"), Approval::Standing, NOW + 2).is_ok());
|
||||
assert_eq!(back.android_grant().unwrap().uses, 2);
|
||||
assert!(back.android_grant_revoke(NOW + 3));
|
||||
let again = Vault::open(&back.to_bytes().unwrap(), &SecretString::from("pw")).unwrap();
|
||||
assert_eq!(again.android_grant(), None);
|
||||
}
|
||||
Reference in New Issue
Block a user