Watch
0
0
Fork
You've already forked hyperhive
0

add an ssh ca to bao, agent ssh certificates and a target module

This commit is contained in:
atlas 2026-11-16 16:24:37 +01:00 • committed by mara
commit 0afb111862

View file

@ -111,9 +111,9 @@ pub fn agents_from_role_names(names: &[String]) -> Vec<String> {
.collect()
}
/// One read stanza. With [`list_stanza`] and [`create_stanza`], the only shapes
/// this module emits, so what an agent may write is a property of the renderers
/// rather than of each call site.
/// One read stanza. With [`list_stanza`], [`create_stanza`] and [`sign_stanza`],
/// the only shapes this module emits, so what an agent may write is a property
/// of the renderers rather than of each call site.
fn read_stanza(path: &str) -> String {
format!("path \"{path}\" {{\n capabilities = [\"read\"]\n}}\n")
}
@ -131,6 +131,12 @@ fn create_stanza(path: &str) -> String {
format!("path \"{path}\" {{\n capabilities = [\"create\", \"read\"]\n}}\n")
}
/// One sign stanza: `update`, which is what a signing endpoint takes, and
/// nothing that reads or rewrites the role behind it.
fn sign_stanza(path: &str) -> String {
format!("path \"{path}\" {{\n capabilities = [\"update\"]\n}}\n")
}
/// The one policy every hive's cert-auth role attaches.
///
/// Inside [`HIVE_PREFIX`] so that the controller may write it. It is the object
@ -193,6 +199,16 @@ pub const SHARED_AGENT_POLICY: &str = "hive-agent";
/// agent's name.
const ENTITY_NAME_TEMPLATE: &str = "{{identity.entity.name}}";
/// The SSH secrets engine mount whose CA signs the agents' short-lived user
/// certificates. `nix/host-modules/swarm-bao.nix` enables it and writes the one
/// role below; `nix/agent-modules/ssh.nix` signs through it.
pub const SSH_MOUNT: &str = "ssh-agents";
/// The one role on [`SSH_MOUNT`] an agent signs through. It templates the
/// certificate's principal from the identity entity's name, so the role is the
/// same for every agent and each can only be signed for its own name.
pub const SSH_ROLE: &str = "swarm-agent";
/// Render the policy [`SHARED_AGENT_POLICY`] holds: read on one agent's
/// credentials and list on their names (how `hive-matrix-daemon` finds its
/// linked accounts), with the agent's name taken from the identity entity of
@ -216,6 +232,12 @@ const ENTITY_NAME_TEMPLATE: &str = "{{identity.entity.name}}";
/// another agent's credentials, a hive's, a service's, or the controller's, so
/// none of [`render_shared_hives`]'s breadth applies. A hive still reads this
/// agent's secrets, and closing that is its own decision.
///
/// The last stanza is `update` on [`SSH_MOUNT`]'s sign endpoint for [`SSH_ROLE`]:
/// the agent submits a public key it generated and gets a short-lived user
/// certificate back. It names no agent. What a certificate may say is the
/// role's: the principal is the identity entity's name, so an agent can be
/// signed for its own name and no other.
#[must_use]
pub fn render_shared_agent() -> String {
let kind = <&str>::from(Kind::Agent);
@ -229,7 +251,8 @@ pub fn render_shared_agent() -> String {
"{MOUNT}/data/{ROOT}/{kind}/{ENTITY_NAME_TEMPLATE}/{}",
backend_env::LEAF
));
format!("{read}{list}{seed}")
let sign = sign_stanza(&format!("{SSH_MOUNT}/sign/{SSH_ROLE}"));
format!("{read}{list}{seed}{sign}")
}
/// What an agent's token grants under [`SHARED_AGENT_POLICY`]: the document
@ -261,7 +284,8 @@ mod tests {
render_shared_agent(),
"path \"secret/data/swarm/agents/{{identity.entity.name}}/*\" {\n capabilities = [\"read\"]\n}\n\
path \"secret/metadata/swarm/agents/{{identity.entity.name}}/*\" {\n capabilities = [\"list\"]\n}\n\
path \"secret/data/swarm/agents/{{identity.entity.name}}/backend-env\" {\n capabilities = [\"create\", \"read\"]\n}\n"
path \"secret/data/swarm/agents/{{identity.entity.name}}/backend-env\" {\n capabilities = [\"create\", \"read\"]\n}\n\
path \"ssh-agents/sign/swarm-agent\" {\n capabilities = [\"update\"]\n}\n"
);
}
@ -270,8 +294,33 @@ mod tests {
let shared = render_shared_agent();
assert!(!shared.contains("atlas"));
assert!(!shared.contains("agents/*"), "{shared}");
assert!(!shared.contains("update"));
assert!(!shared.contains("delete"));
// `update` is the signing stanza's and no KV path's: a KV leaf the
// agent could rewrite would let it replace a credential it only seeds.
let secret_only = shared
.split("path \"ssh-agents/")
.next()
.unwrap_or_default();
assert!(!secret_only.contains("update"), "{shared}");
}
#[test]
fn the_agent_may_sign_through_one_role_and_do_nothing_else_on_the_ssh_mount() {
let shared = render_shared_agent();
let ssh: Vec<&str> = shared
.split("path \"")
.filter(|s| s.starts_with(SSH_MOUNT))
.collect();
assert_eq!(
ssh,
["ssh-agents/sign/swarm-agent\" {\n capabilities = [\"update\"]\n}\n"]
);
// The control: the filter finds a stanza that is there.
assert!(shared.split("path \"").any(|s| s.starts_with(MOUNT)));
assert_eq!(
format!("{SSH_MOUNT}/sign/{SSH_ROLE}"),
"ssh-agents/sign/swarm-agent"
);
}
#[test]
@ -476,7 +525,8 @@ mod tests {
/// What `effective_agent("atlas")` must be, byte for byte.
const ATLAS_DOCUMENT: &str = "path \"secret/data/swarm/agents/atlas/*\" {\n capabilities = [\"read\"]\n}\n\
path \"secret/metadata/swarm/agents/atlas/*\" {\n capabilities = [\"list\"]\n}\n\
path \"secret/data/swarm/agents/atlas/backend-env\" {\n capabilities = [\"create\", \"read\"]\n}\n";
path \"secret/data/swarm/agents/atlas/backend-env\" {\n capabilities = [\"create\", \"read\"]\n}\n\
path \"ssh-agents/sign/swarm-agent\" {\n capabilities = [\"update\"]\n}\n";
#[test]
fn an_agents_document_is_its_stanzas_naming_that_agent() {
@ -515,8 +565,8 @@ mod tests {
assert!(!p.contains("argus"), "no other principal is named");
assert_eq!(
p.matches("path \"").count(),
3,
"three stanzas, or the document grants something unaccounted for: {p}"
4,
"four stanzas, or the document grants something unaccounted for: {p}"
);
}
@ -560,7 +610,12 @@ mod tests {
["secret/data/swarm/agents/atlas/*", leaf.as_str()]
);
assert_eq!(paths_granting(&p, "create"), [leaf.as_str()]);
for capability in ["update", "delete", "sudo", "patch"] {
// The one `update` is the SSH signing endpoint's: no KV path takes it.
assert_eq!(
paths_granting(&p, "update"),
["ssh-agents/sign/swarm-agent"]
);
for capability in ["delete", "sudo", "patch"] {
assert!(
!p.contains(capability),
"an agent's document must not grant {capability}: {p}"
@ -581,8 +636,9 @@ mod tests {
// granted, nor is `list` anywhere but `metadata/`.
let p = effective_agent("atlas");
let stanzas: Vec<&str> = p.split_inclusive("}\n").collect();
assert_eq!(stanzas.len(), 3, "{p}");
// The third is the create stanza, pinned by the test above.
assert_eq!(stanzas.len(), 4, "{p}");
// The third is the create stanza and the fourth the signing stanza,
// each pinned by a test above.
for stanza in &stanzas[..2] {
let on_data = stanza.starts_with("path \"secret/data/");
let on_metadata = stanza.starts_with("path \"secret/metadata/");