diff --git a/docs/design.md b/docs/design.md index b628e70..993dae2 100644 --- a/docs/design.md +++ b/docs/design.md @@ -511,7 +511,9 @@ only people should be able to push to, and the approval shows what that branch s in the rules, added or removed; or that the path isn't in the rules. - Only the protected branch's Nix is evaluated, never a requester's commit, so this doesn't need the builder approval. - - `secrets.names` labels well-known keys (such as people's); other keys are shown abbreviated. + - Keys are labelled by name from `secrets.names` (well-known keys, such as people's), then from + SSH keys' comments anywhere in the branch's rules (`ssh-ed25519 AAAA… shill`, which age + ignores). Other keys are shown abbreviated. - **The broker's own key.** The broker always adds it to the recipients, and says so, so it can change the file later. - **Sensitive requests.** Requests that read a secret, change who can read one or name content as diff --git a/src/builder.rs b/src/builder.rs index 7e040cd..a19ca3f 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -103,6 +103,9 @@ pub struct SecretContext { pub paths: Vec, /// The path's recipients in the branch's rules file, or `None` if it has no entry. pub recipients: Option>, + /// Keys by name, from SSH keys' comments anywhere in the rules file. + #[serde(default)] + pub names: BTreeMap, } /// A commit as approvers see it before anything is built. Everything but `on_branch` is @@ -524,7 +527,19 @@ async fn secret_context(config: &BuilderConfig, request: &SecretRequest) -> Resu let rules = rules(config, &commit, &request.rules).await?; let recipients = rules.get(&*request.path).cloned(); - Ok(SecretContext { branch: config.branch.clone(), commit, paths, recipients }) + let names = commented(rules.values().flatten()); + Ok(SecretContext { branch: config.branch.clone(), commit, paths, recipients, names }) +} + +/// SSH keys by their comments, like `ssh-ed25519 AAAA… shill`; age ignores the comment. +fn commented<'a>(keys: impl Iterator) -> BTreeMap { + keys.filter_map(|key| match key.split_whitespace().collect::>().as_slice() { + [kind, key, comment @ ..] if kind.starts_with("ssh-") && !comment.is_empty() => { + Some((comment.join(" "), format!("{kind} {key}"))) + } + _ => None, + }) + .collect() } /// Paths in `git ls-tree -r -z` output whose object is `blob`. diff --git a/src/ops/secret.rs b/src/ops/secret.rs index b0d1a34..ab227fc 100644 --- a/src/ops/secret.rs +++ b/src/ops/secret.rs @@ -152,7 +152,8 @@ fn describe(op: &SecretOp, context: &SecretContext, config: &SecretsConfig, own: if let Some(requested) = op.recipients() { let (recipients, added) = with_own(requested, own); let rules = &config.rules; - let label = |r: &Recipient| label(r, &config.names); + let names = names(config, context); + let label = |r: &Recipient| label(r, &names); let rows: Vec = match context.recipients.as_ref().map(|keys| expected(keys)) { Some(expected) => { @@ -216,6 +217,17 @@ fn same(a: &[Recipient], b: &[Recipient]) -> bool { a.iter().all(|r| b.contains(r)) && b.iter().all(|r| a.contains(r)) } +/// The configured names, then the comments on the main branch's keys. +fn names(config: &SecretsConfig, context: &SecretContext) -> BTreeMap { + let mut names = config.names.clone(); + for (name, key) in &context.names { + if let Ok(key) = Recipient::normalize(key) { + names.entry(name.clone()).or_insert(key); + } + } + names +} + fn label(recipient: &Recipient, names: &BTreeMap) -> String { names.iter().find(|(_, r)| *r == recipient).map_or_else(|| recipient.abbreviated(), |(name, _)| name.clone()) } @@ -412,6 +424,7 @@ mod tests { commit: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(), paths: paths.iter().map(|p| p.to_string()).collect(), recipients, + names: BTreeMap::new(), } } @@ -528,6 +541,14 @@ mod tests { let (detail, sensitive) = describe(&set(vec![dev.clone()]), &context(&[], None), &config, &own); assert!(sensitive && detail.to_text().contains("isn't in secrets.nix"), "{}", detail.to_text()); + + // The main branch's key comments name recipients, though not over the configured names. + let mut named = context(&[], Some(vec![dev.to_string()])); + named.names = BTreeMap::from([("box".into(), host.to_string()), ("not-dev".into(), dev.to_string())]); + named.names.insert("bad".into(), "nonsense".into()); + let (detail, _) = describe(&set(vec![host.clone(), dev.clone()]), &named, &config, &own); + let labels: Vec<_> = statuses(&detail).into_iter().map(|(label, ..)| label).collect(); + assert_eq!(labels, ["box".to_owned(), "dev".to_owned(), own.abbreviated()]); } /// Each recipient row's key, status and tone. diff --git a/tests/builder.rs b/tests/builder.rs index 3994ecc..965aaf4 100644 --- a/tests/builder.rs +++ b/tests/builder.rs @@ -122,13 +122,16 @@ fn secrets_are_looked_up_on_the_branch() { git(&work, &["init", "--quiet", "--initial-branch=master"]); std::fs::write(work.join("secrets/a.age"), "age-encryption.org/v1\nreal\n").unwrap(); std::fs::write(work.join("secrets/copy.age"), "age-encryption.org/v1\nreal\n").unwrap(); - std::fs::write(work.join("secrets.nix"), "{ \"secrets/a.age\".publicKeys = [ \"age1dev\" ]; }\n").unwrap(); + let rules = |keys: &str| { + format!("{{ \"secrets/a.age\".publicKeys = [ \"age1dev\" ]; \"secrets/b.age\".publicKeys = [ {keys} ]; }}\n") + }; + std::fs::write(work.join("secrets.nix"), rules("\"ssh-ed25519 AAAA box\"")).unwrap(); git(&work, &["add", "."]); git(&work, &["commit", "--quiet", "-m", "Secrets"]); // An agent's branch can't change what the main branch says. git(&work, &["checkout", "--quiet", "-b", "agent"]); - std::fs::write(work.join("secrets.nix"), "{ \"secrets/a.age\".publicKeys = [ \"age1agent\" ]; }\n").unwrap(); + std::fs::write(work.join("secrets.nix"), rules("\"ssh-ed25519 AAAA box\" \"ssh-ed25519 BBBB box\"")).unwrap(); git(&work, &["commit", "--quiet", "-am", "Change the rules"]); let state = directory.path().join("state"); @@ -154,6 +157,8 @@ fn secrets_are_looked_up_on_the_branch() { let context: SecretContext = ask(&config, &BuilderRequest::Secret(request.clone())); assert_eq!(context.paths, ["secrets/a.age", "secrets/copy.age"]); assert_eq!(context.recipients, Some(vec!["age1dev".to_owned()])); + // Names come from comments on every path's keys. + assert_eq!(context.names, std::collections::BTreeMap::from([("box".to_owned(), "ssh-ed25519 AAAA".to_owned())])); assert_eq!(&*context.commit, git(&work, &["rev-parse", "master"])); let unknown = SecretRequest { blob: None, path: "secrets/new.age".parse().unwrap(), ..request };