Watch
0
0
Fork
You've already forked hyperhive
0

gate ssh signing per agent: ssh permission, signing policy on the login role, ca key endpoint

This commit is contained in:
atlas 2026-11-16 18:29:06 +01:00 • committed by mara
commit 910f9a6f50

View file

@ -71,12 +71,16 @@ struct RoleInputs<'a> {
attached: Vec<String>,
}
fn role_inputs<'a>(agent: &str, credential: &'a mtls::Credential) -> Result<RoleInputs<'a>> {
fn role_inputs<'a>(
agent: &str,
credential: &'a mtls::Credential,
ssh: bool,
) -> Result<RoleInputs<'a>> {
let name = policy::agent_object_name(agent)?;
Ok(RoleInputs {
name: name.clone(),
ca: &credential.ca,
attached: vec![policy::SHARED_AGENT_POLICY.to_owned()],
attached: crate::agent_ssh::attached(ssh),
})
}
@ -107,6 +111,11 @@ async fn publish_access(
)
})
.at(MintStep::Policy)?;
store
.write_policy(policy::SSH_POLICY, &policy::render_ssh_signer())
.await
.with_context(|| format!("writing the SSH signing policy {}", policy::SSH_POLICY))
.at(MintStep::Policy)?;
crate::agent_entity::ensure_agent(store, agent)
.await
.with_context(|| format!("naming {agent}'s identity entity; the role is not changed"))
@ -322,7 +331,13 @@ pub async fn mint_and_verify(agent: &str) -> Result<()> {
}
let queue_credential = wanted;
let inputs = role_inputs(agent, &credential).at(MintStep::Role)?;
// The role is where the agent's SSH switch lives, so a rewrite keeps what it
// has (`crate::agent_ssh`).
let ssh = crate::agent_ssh::role_has_ssh(&store, &name)
.await
.with_context(|| format!("reading the policies of the cert-auth role {name}"))
.at(MintStep::Role)?;
let inputs = role_inputs(agent, &credential, ssh).at(MintStep::Role)?;
publish_access(&store, agent, &inputs).await?;
tracing::info!(agent, %path, role = %name, "agent store identity published");
@ -560,7 +575,7 @@ mod tests {
key: "KEY".to_owned(),
ca: "AGENT-CA".to_owned(),
};
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
assert_eq!(inputs.name, "hive-agent-atlas");
assert_eq!(
inputs.name,
@ -581,7 +596,7 @@ mod tests {
// The control: another agent's inputs differ in name, and attach the
// same shared policy, so the check above can see a policy that is there.
let other = role_inputs("argus", &credential).expect("legal");
let other = role_inputs("argus", &credential, false).expect("legal");
assert_ne!(other.name, inputs.name);
assert_eq!(other.attached, inputs.attached);
}
@ -600,7 +615,7 @@ mod tests {
async fn the_role_is_narrowed_after_the_entity_is_named() {
let store = FakeAccess::default();
let credential = credential();
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
publish_access(&store, "atlas", &inputs)
.await
.expect("a store that accepts everything");
@ -617,6 +632,54 @@ mod tests {
);
}
/// Switch on: the role attaches the signing policy as well, and the policy
/// itself is written before the role names it. Off: the shared policy alone.
/// A rewrite for a new certificate keeps what the role had.
#[tokio::test]
async fn the_signing_policy_is_attached_only_when_ssh_is_on_and_a_rewrite_keeps_it() {
let credential = credential();
let on = FakeAccess::default();
let inputs = role_inputs("atlas", &credential, true).expect("legal");
publish_access(&on, "atlas", &inputs)
.await
.expect("accepted");
assert_eq!(
on.roles_written(),
["write_cert_role hive-agent-atlas hive-agent,hive-agent-ssh"]
);
let calls = on.calls();
let at = |call: &str| calls.iter().position(|c| c == call).expect(call);
assert!(
at("write_policy hive-agent-ssh")
< at("write_cert_role hive-agent-atlas hive-agent,hive-agent-ssh"),
"the policy exists before the role names it: {calls:?}"
);
// The rewrite reads the role it replaces, and keeps the grant.
let ssh = crate::agent_ssh::role_has_ssh(&on, "hive-agent-atlas")
.await
.expect("reads");
assert!(ssh);
let again = role_inputs("atlas", &credential, ssh).expect("legal");
assert_eq!(again.attached, ["hive-agent", "hive-agent-ssh"]);
// The control: a role that never had it does not gain it.
let off = FakeAccess::default();
let inputs = role_inputs("atlas", &credential, false).expect("legal");
publish_access(&off, "atlas", &inputs)
.await
.expect("accepted");
assert_eq!(
off.roles_written(),
["write_cert_role hive-agent-atlas hive-agent"]
);
assert!(
!crate::agent_ssh::role_has_ssh(&off, "hive-agent-atlas")
.await
.expect("reads")
);
}
/// An agent created under the name of a destroyed one gets its entity
/// enabled and its role back, the enable first.
#[tokio::test]
@ -629,7 +692,7 @@ mod tests {
assert_eq!(store.role("hive-agent-atlas"), None);
let credential = credential();
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
publish_access(&store, "atlas", &inputs)
.await
.expect("a store that accepts everything");
@ -648,7 +711,7 @@ mod tests {
async fn a_mint_whose_enable_is_refused_leaves_the_role_alone() {
let store = FakeAccess::default().refusing_disable();
let credential = credential();
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
let err = publish_access(&store, "atlas", &inputs)
.await
.expect_err("the enable is refused");
@ -660,7 +723,7 @@ mod tests {
async fn a_renewal_whose_entity_step_fails_leaves_the_role_alone() {
let store = FakeAccess::without_accessor();
let credential = credential();
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
let err = publish_access(&store, "atlas", &inputs)
.await
.expect_err("the entity step failed");
@ -681,7 +744,7 @@ mod tests {
async fn a_renewal_whose_alias_is_on_another_entity_leaves_the_role_alone() {
let store = FakeAccess::with_alias_on("id-1", "somebody");
let credential = credential();
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
let err = publish_access(&store, "atlas", &inputs)
.await
.expect_err("a conflict is an error");
@ -700,7 +763,7 @@ mod tests {
#[tokio::test]
async fn a_refused_step_is_named_in_the_message_and_by_failed_step() {
let credential = credential();
let inputs = role_inputs("atlas", &credential).expect("legal");
let inputs = role_inputs("atlas", &credential, false).expect("legal");
let err = publish_access(&FakeAccess::refusing_policies(), "atlas", &inputs)
.await