From 910f9a6f50c5ad47663e22045e5606c7d834a4e1 Mon Sep 17 00:00:00 2001 From: atlas Date: Mon, 16 Nov 2026 18:29:06 +0100 Subject: [PATCH] gate ssh signing per agent: ssh permission, signing policy on the login role, ca key endpoint --- swarm-controller-api/src/lib.rs | 1 + swarm-controller-api/src/permissions.rs | 18 ++ swarm-controller-api/src/ssh.rs | 29 ++ swarm-controller/src/agent_identity.rs | 85 ++++- swarm-controller/src/agent_permissions.rs | 92 +++++- swarm-controller/src/agent_ssh.rs | 362 ++++++++++++++++++++++ swarm-controller/src/main.rs | 1 + swarm-controller/src/startup.rs | 12 +- swarm-controller/src/store.rs | 61 ++++ swarm-secret-client/src/client.rs | 113 +++++++ swarm-secret-client/src/policy.rs | 82 ++--- swarm-wire/src/agent_permissions.rs | 5 + swarmctl/src/permissions.rs | 1 + 13 files changed, 805 insertions(+), 57 deletions(-) create mode 100644 swarm-controller-api/src/ssh.rs create mode 100644 swarm-controller/src/agent_ssh.rs diff --git a/swarm-controller-api/src/lib.rs b/swarm-controller-api/src/lib.rs index b5585c45a..3a0e71255 100644 --- a/swarm-controller-api/src/lib.rs +++ b/swarm-controller-api/src/lib.rs @@ -13,3 +13,4 @@ pub mod limits; pub mod messages; pub mod permissions; pub mod schedules; +pub mod ssh; diff --git a/swarm-controller-api/src/permissions.rs b/swarm-controller-api/src/permissions.rs index 4a6d67452..51e5ed96a 100644 --- a/swarm-controller-api/src/permissions.rs +++ b/swarm-controller-api/src/permissions.rs @@ -39,6 +39,13 @@ pub struct Permissions { #[serde(default, skip_serializing_if = "Option::is_none")] #[cfg_attr(feature = "schema", schema(example = 0, minimum = 0, maximum = 100))] pub max_repos: Option, + /// Whether the agent may get short-lived SSH certificates from the swarm's SSH + /// CA. Absent in a `GET` means the swarm never set it, which counts as off. + /// Absent in a `PUT` keeps the stored value. The agent's store login role + /// takes the change within five minutes; a certificate already signed stays + /// valid until it expires, at most three hours. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, } #[cfg(test)] @@ -56,6 +63,7 @@ mod tests { unrestricted_internet: None, internet_log: None, max_repos: None, + ssh: None, }, r#"{"permissions":[]}"#, ), @@ -65,6 +73,7 @@ mod tests { unrestricted_internet: Some(false), internet_log: None, max_repos: None, + ssh: None, }, r#"{"unrestricted_internet":false}"#, ), @@ -74,6 +83,7 @@ mod tests { unrestricted_internet: Some(false), internet_log: Some(true), max_repos: None, + ssh: None, }, r#"{"unrestricted_internet":false,"internet_log":true}"#, ), @@ -83,6 +93,7 @@ mod tests { unrestricted_internet: None, internet_log: None, max_repos: None, + ssh: None, }, r#"{"permissions":["messaging","manage_root_agent"]}"#, ), @@ -93,6 +104,13 @@ mod tests { }, r#"{"max_repos":3}"#, ), + ( + Permissions { + ssh: Some(true), + ..Permissions::default() + }, + r#"{"ssh":true}"#, + ), ] { assert_eq!(serde_json::to_string(&value).unwrap(), wire); assert_eq!(serde_json::from_str::(wire).unwrap(), value); diff --git a/swarm-controller-api/src/ssh.rs b/swarm-controller-api/src/ssh.rs new file mode 100644 index 000000000..2014f193d --- /dev/null +++ b/swarm-controller-api/src/ssh.rs @@ -0,0 +1,29 @@ +//! `/api/ssh/ca` — the public key of the SSH CA that signs the agents' +//! certificates. + +use serde::{Deserialize, Serialize}; + +/// The CA's public key, one `authorized_keys`-style line. A public value: a +/// machine puts it in its sshd config to trust the certificates the swarm +/// signs. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))] +pub struct SshCa { + /// For example `ssh-ed25519 AAAA…`, with no trailing newline. + pub public_key: String, +} + +#[cfg(test)] +mod tests { + use super::SshCa; + + #[test] + fn the_ca_round_trips() { + let wire = r#"{"public_key":"ssh-ed25519 AAAA"}"#; + let value = SshCa { + public_key: "ssh-ed25519 AAAA".to_owned(), + }; + assert_eq!(serde_json::to_string(&value).unwrap(), wire); + assert_eq!(serde_json::from_str::(wire).unwrap(), value); + } +} diff --git a/swarm-controller/src/agent_identity.rs b/swarm-controller/src/agent_identity.rs index 0577ecbcc..be22011f5 100644 --- a/swarm-controller/src/agent_identity.rs +++ b/swarm-controller/src/agent_identity.rs @@ -71,12 +71,16 @@ struct RoleInputs<'a> { attached: Vec, } -fn role_inputs<'a>(agent: &str, credential: &'a mtls::Credential) -> Result> { +fn role_inputs<'a>( + agent: &str, + credential: &'a mtls::Credential, + ssh: bool, +) -> Result> { 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 diff --git a/swarm-controller/src/agent_permissions.rs b/swarm-controller/src/agent_permissions.rs index 27d9dea9f..af97ba7bc 100644 --- a/swarm-controller/src/agent_permissions.rs +++ b/swarm-controller/src/agent_permissions.rs @@ -42,8 +42,8 @@ use crate::wanted::WantedWriter; const MAX_ATTEMPTS: usize = 5; /// The permissions to store for `req`, whose list is already checked. The list -/// replaces the stored one; `unrestricted_internet` and `max_repos` left out -/// keep the ones in `current`. `internet_log` is stored only as `true`, and a request that sets +/// replaces the stored one; `unrestricted_internet`, `max_repos` and `ssh` left +/// out keep the ones in `current`. `internet_log` is stored only as `true`, and a request that sets /// `unrestricted_internet` without it turns it off: an agent that moves to /// unrestricted and back to restricted does not come back logging. fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions { @@ -57,6 +57,7 @@ fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions unrestricted_internet: req.unrestricted_internet.or(current.unrestricted_internet), internet_log, max_repos: req.max_repos.or(current.max_repos), + ssh: req.ssh.or(current.ssh), } } @@ -123,6 +124,7 @@ fn view(v: AgentPermissions) -> Permissions { unrestricted_internet: v.unrestricted_internet, internet_log: v.internet_log, max_repos: v.max_repos, + ssh: v.ssh, } } @@ -331,6 +333,9 @@ async fn put_with( if value.max_repos != current.max_repos { MAX_REPOS_CHANGED.notify_one(); } + if value.ssh != current.ssh { + crate::agent_ssh::SSH_CHANGED.notify_one(); + } if !revoking { mirror(leaves, &agent, &before, &after) .await @@ -390,6 +395,7 @@ async fn delete_with( unrestricted_internet: current.unrestricted_internet, internet_log: current.internet_log, max_repos: current.max_repos, + ssh: current.ssh, ..AgentPermissions::default() }; mirror(leaves, &agent, &held(¤t), &held(&kept)) @@ -490,6 +496,7 @@ pub(crate) fn at_create( unrestricted_internet, internet_log: None, max_repos: None, + ssh: None, }) } @@ -520,6 +527,7 @@ fn merged_at_create( current.internet_log }, max_repos: value.max_repos.or(current.max_repos), + ssh: value.ssh.or(current.ssh), } } @@ -875,6 +883,7 @@ pub(crate) mod tests { unrestricted_internet, internet_log, max_repos: None, + ssh: None, } } @@ -1012,6 +1021,80 @@ pub(crate) mod tests { assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), MAX_REPOS); } + /// The SSH switch is the agent's own and off by default: a `PUT` sets it and + /// wakes the role pass, a `PUT` or `DELETE` that leaves it out keeps it, and + /// another agent is not changed. + #[tokio::test] + async fn an_agents_ssh_switch_is_its_own_default_off_and_survives_other_changes() { + use crate::wanted::WantedWriter; + use swarm_wire::wanted::AgentState; + + let nats = TestNats::start(); + let client = nats.client().await; + let state = crate::agent_limits::tests::declared(&client).await; + WantedWriter::new(client.clone()) + .set("pr1ma", "iris", AgentState::Up) + .await + .expect("declare iris"); + let ssh = |on| Permissions { + ssh: Some(on), + ..Permissions::default() + }; + + assert_eq!(get(&state, "atlas").await.ssh, None, "off until set"); + while tokio::time::timeout( + std::time::Duration::from_millis(1), + crate::agent_ssh::SSH_CHANGED.notified(), + ) + .await + .is_ok() + {} + assert_eq!( + put(&state, "atlas", ssh(true)).await.expect("set").0.ssh, + Some(true) + ); + tokio::time::timeout( + std::time::Duration::from_secs(5), + crate::agent_ssh::SSH_CHANGED.notified(), + ) + .await + .expect("the change wakes the role pass"); + assert_eq!( + get(&state, "iris").await.ssh, + None, + "another agent is not changed" + ); + + let _ = put(&state, "atlas", granted()).await.expect("set"); + assert_eq!(get(&state, "atlas").await.ssh, Some(true)); + delete_agent_permissions(State(state.clone()), Path("atlas".to_owned())) + .await + .expect("cleared"); + assert_eq!(get(&state, "atlas").await.ssh, Some(true)); + + assert_eq!( + put(&state, "atlas", ssh(false)).await.expect("set").0.ssh, + Some(false) + ); + tokio::time::timeout( + std::time::Duration::from_secs(5), + crate::agent_ssh::SSH_CHANGED.notified(), + ) + .await + .expect("switching off wakes the role pass too"); + } + + /// A create keeps the stored SSH switch and does not set one. + #[test] + fn a_create_keeps_the_ssh_switch() { + let current = stored(r#"{"ssh":true}"#); + assert_eq!( + super::merged_at_create(&stored("{}"), ¤t, true).ssh, + Some(true) + ); + assert_eq!(super::at_create(None, None).expect("valid").ssh, None); + } + /// A stored limit above the range, as a hand edit could leave it, reads as /// none rather than as that many repositories. #[test] @@ -1050,6 +1133,7 @@ pub(crate) mod tests { unrestricted_internet: None, internet_log: None, max_repos: None, + ssh: None, } } @@ -1096,6 +1180,7 @@ pub(crate) mod tests { unrestricted_internet: None, internet_log: None, max_repos: None, + ssh: None, }; let _ = put(&state, "atlas", none.clone()).await.expect("set"); assert_eq!( @@ -1125,6 +1210,7 @@ pub(crate) mod tests { unrestricted_internet: Some(false), internet_log: Some(true), max_repos: None, + ssh: None, }; let stored = put(&state, "atlas", log.clone()).await.expect("set").0; assert_eq!(stored, log); @@ -1148,6 +1234,7 @@ pub(crate) mod tests { unrestricted_internet: Some(true), internet_log: None, max_repos: None, + ssh: None, }; let _ = put(&state, "atlas", on.clone()).await.expect("set"); let off = Permissions { @@ -1296,6 +1383,7 @@ pub(crate) mod tests { unrestricted_internet: None, internet_log: None, max_repos: None, + ssh: None, } } diff --git a/swarm-controller/src/agent_ssh.rs b/swarm-controller/src/agent_ssh.rs new file mode 100644 index 000000000..ceaeb747a --- /dev/null +++ b/swarm-controller/src/agent_ssh.rs @@ -0,0 +1,362 @@ +//! SSH certificates for agents: the per-agent switch, and the public key of +//! the CA that signs them. +//! +//! An agent may have its SSH key signed only while its `ssh` field in +//! `agent-permissions` (`swarm_wire::agent_permissions`) is on. The grant is +//! the policy [`policy::SSH_POLICY`], which holds `update` on the one sign path +//! and nothing else. It is attached to the agent's cert-auth role +//! `hive-agent-` next to the shared policy while the field is on, and +//! left off otherwise. The role is the state: [`crate::agent_identity`] keeps +//! whatever the role has when it rewrites it for a new certificate, and +//! [`reconcile`] is the only code that changes it. +//! +//! **When a change takes effect.** The role is rewritten when the field +//! changes ([`SSH_CHANGED`]) and by a pass every [`RECONCILE_INTERVAL`]. A token +//! carries the policies of the role it logged in under and keeps them until it +//! expires, which is at most [`swarm_secret_client::client::CERT_ROLE_TOKEN_TTL`] +//! after login, so a new login gets the new policies at once and an agent that +//! was signed in when the switch went off can still sign for up to an hour. +//! Switching on takes effect at the agent's next login, which its certificate +//! unit makes on every run (every 20 minutes). A certificate already signed +//! stays valid until it expires, at most three hours. + +use std::collections::BTreeMap; +use std::time::Duration; + +use axum::Json; +use axum::extract::State; +use axum::http::StatusCode; +use problem_details::ProblemDetails; +use swarm_controller_api::ssh::SshCa; +use swarm_queue_client::agent_permissions; +use swarm_secret_client::{client::DEFAULT_CERT_MOUNT, policy}; +use swarm_wire::agent_permissions::AgentPermissions; + +use crate::routes::{AppState, error_problem}; +use crate::schedules::read_or_default; +use crate::store::AccessStore; + +/// How often [`spawn`] re-checks every agent's role. +const RECONCILE_INTERVAL: Duration = Duration::from_mins(5); + +/// Woken when an agent's `ssh` field changes, so [`spawn`]'s pass applies it at +/// once instead of at its next tick. A wake with no pass waiting is kept for +/// the next one. +pub(crate) static SSH_CHANGED: tokio::sync::Notify = tokio::sync::Notify::const_new(); + +/// The policies an agent's cert-auth role attaches: the shared one, and the +/// signing grant when `ssh` is on. +#[must_use] +pub fn attached(ssh: bool) -> Vec { + let mut policies = vec![policy::SHARED_AGENT_POLICY.to_owned()]; + if ssh { + policies.push(policy::SSH_POLICY.to_owned()); + } + policies +} + +/// Whether `role`'s current policies include the signing grant, which is how +/// [`crate::agent_identity`] knows to keep it when it rewrites the role. +/// +/// # Errors +/// The store could not be read. +pub async fn role_has_ssh( + store: &impl AccessStore, + role: &str, +) -> Result { + Ok(store + .cert_role_policies(DEFAULT_CERT_MOUNT, role) + .await? + .is_some_and(|p| p.iter().any(|p| p == policy::SSH_POLICY))) +} + +/// What one pass did, one count per role. +#[derive(Debug, Default, PartialEq, Eq)] +pub struct Counts { + pub rewritten: usize, + pub already_current: usize, + pub failed: usize, +} + +/// Give every agent of `wanted` the policies its `ssh` field says, writing the +/// signing policy first. An agent with no role is skipped: the mint that makes +/// one keeps nothing, and the next pass attaches the grant. +pub async fn reconcile(store: &impl AccessStore, wanted: &BTreeMap) -> Counts { + let mut counts = Counts::default(); + if let Err(e) = store + .write_policy(policy::SSH_POLICY, &policy::render_ssh_signer()) + .await + { + tracing::warn!(error = %e, "agent ssh: the signing policy was not written; retrying next pass"); + counts.failed = wanted.len(); + return counts; + } + for (agent, ssh) in wanted { + let Ok(role) = policy::agent_object_name(agent) else { + continue; + }; + match reconcile_one(store, &role, *ssh).await { + Ok(Some(true)) => counts.rewritten += 1, + Ok(Some(false)) => counts.already_current += 1, + Ok(None) => {} + Err(e) => { + counts.failed += 1; + tracing::warn!(role, error = %e, "agent ssh: this role's policies were not brought current"); + } + } + } + if counts.rewritten > 0 || counts.failed > 0 { + tracing::info!( + rewritten = counts.rewritten, + already_current = counts.already_current, + failed = counts.failed, + "agent ssh roles checked" + ); + } + counts +} + +/// `Some(true)` when the role was rewritten, `Some(false)` when it was already +/// right, `None` when it does not exist. +async fn reconcile_one( + store: &impl AccessStore, + role: &str, + ssh: bool, +) -> Result, swarm_secret_client::Error> { + let Some(mut current) = store.cert_role_policies(DEFAULT_CERT_MOUNT, role).await? else { + return Ok(None); + }; + let mut wanted = attached(ssh); + current.sort(); + wanted.sort(); + if current == wanted { + return Ok(Some(false)); + } + let policies: Vec<&str> = wanted.iter().map(String::as_str).collect(); + store + .set_cert_role_policies(DEFAULT_CERT_MOUNT, role, &policies) + .await?; + tracing::info!(role, ssh, "agent ssh: role policies set"); + Ok(Some(true)) +} + +/// `agent`'s `ssh` field: on only when its record says so. +async fn ssh_of(store: &async_nats::jetstream::kv::Store, agent: &str) -> anyhow::Result { + let (record, _, _) = read_or_default::(store, agent).await?; + Ok(record.ssh == Some(true)) +} + +/// One pass: every agent holding a cert-auth role, with its field. An agent +/// whose record cannot be read is left out, so its role stays as it is. +async fn desired( + queue: &async_nats::Client, + roster: &[String], +) -> anyhow::Result> { + let store = agent_permissions::open_or_create(queue).await?; + let mut wanted = BTreeMap::new(); + for agent in roster { + match ssh_of(&store, agent).await { + Ok(on) => { + wanted.insert(agent.clone(), on); + } + Err(e) => tracing::warn!( + %agent, + error = %format!("{e:#}"), + "agent ssh: the agent's record was not read; leaving its role as it is" + ), + } + } + Ok(wanted) +} + +/// Apply every agent's field now, again on each wake and on every tick. A pass +/// that fails is logged and retried at the next tick. +pub fn spawn(queue: async_nats::Client) { + tokio::spawn(async move { + let mut ticker = tokio::time::interval(RECONCILE_INTERVAL); + loop { + tokio::select! { + _ = ticker.tick() => {} + () = SSH_CHANGED.notified() => {} + } + if let Err(e) = pass(&queue).await { + tracing::warn!(error = %format!("{e:#}"), "agent ssh: pass failed; retrying next tick"); + } + } + }); +} + +async fn pass(queue: &async_nats::Client) -> anyhow::Result<()> { + use anyhow::Context as _; + let store = crate::store::connect() + .await + .context("logging in to the swarm secret store")?; + let roles = store + .list_cert_roles(DEFAULT_CERT_MOUNT) + .await + .context("listing the store's cert-auth roles")?; + let roster = policy::agents_from_role_names(&roles); + let wanted = desired(queue, &roster).await?; + reconcile(&store, &wanted).await; + Ok(()) +} + +#[utoipa::path( + get, + path = "/api/ssh/ca", + responses( + (status = 200, description = "the public key of the SSH CA that signs agents' \ + certificates", body = SshCa), + (status = 404, description = "the store holds no SSH CA yet (problem+json)", + body = String), + (status = 502, description = "the store could not be read (problem+json)", + body = String), + ), + tag = "agents" +)] +pub(crate) async fn get_ssh_ca( + State(_state): State, +) -> Result, ProblemDetails> { + let store = crate::store::connect().await.map_err(|e| { + error_problem( + StatusCode::BAD_GATEWAY, + &format!("logging in to the swarm secret store: {e}"), + ) + })?; + match store.ssh_ca_public_key(policy::SSH_MOUNT).await { + Ok(Some(public_key)) => Ok(Json(SshCa { public_key })), + Ok(None) => Err(error_problem( + StatusCode::NOT_FOUND, + "the swarm secret store holds no SSH CA yet", + )), + Err(e) => Err(error_problem( + StatusCode::BAD_GATEWAY, + &format!("reading the SSH CA: {e}"), + )), + } +} + +#[cfg(test)] +mod tests { + use std::collections::BTreeMap; + + use swarm_secret_client::policy; + + use crate::store::fake::{FakeAccess, FakeRole}; + + use super::{Counts, attached, reconcile, role_has_ssh}; + + fn role(policies: &[&str]) -> FakeRole { + FakeRole { + certificate: "CA".to_owned(), + common_name: "hive-agent-a".to_owned(), + policies: policies.iter().map(|p| (*p).to_owned()).collect(), + ttls: swarm_secret_client::client::CertRoleTokenTtls { + token_ttl: swarm_secret_client::client::CERT_ROLE_TOKEN_TTL_SECS, + token_max_ttl: swarm_secret_client::client::CERT_ROLE_TOKEN_TTL_SECS, + }, + } + } + + fn wanted(of: &[(&str, bool)]) -> BTreeMap { + of.iter().map(|(a, on)| ((*a).to_owned(), *on)).collect() + } + + #[test] + fn the_signing_policy_is_attached_only_while_on() { + assert_eq!(attached(false), ["hive-agent"]); + assert_eq!(attached(true), ["hive-agent", "hive-agent-ssh"]); + } + + #[tokio::test] + async fn switching_on_attaches_the_policy_and_keeps_the_rest_of_the_role() { + let before = role(&["hive-agent"]); + let store = FakeAccess::default().with_role("hive-agent-a", before.clone()); + let counts = reconcile(&store, &wanted(&[("a", true)])).await; + assert_eq!( + counts, + Counts { + rewritten: 1, + ..Counts::default() + } + ); + assert_eq!( + store.role("hive-agent-a"), + Some(FakeRole { + policies: vec!["hive-agent".to_owned(), policy::SSH_POLICY.to_owned()], + ..before + }) + ); + assert!( + store + .calls() + .contains(&"write_policy hive-agent-ssh".to_owned()) + ); + } + + #[tokio::test] + async fn switching_off_takes_the_policy_away_and_a_current_role_is_left_alone() { + let store = FakeAccess::default() + .with_role("hive-agent-a", role(&["hive-agent", "hive-agent-ssh"])) + .with_role("hive-agent-b", role(&["hive-agent"])); + let counts = reconcile(&store, &wanted(&[("a", false), ("b", false)])).await; + assert_eq!( + counts, + Counts { + rewritten: 1, + already_current: 1, + failed: 0 + } + ); + assert_eq!( + store.role("hive-agent-a").map(|r| r.policies), + Some(vec!["hive-agent".to_owned()]) + ); + // The role already right is not written. + let writes = store + .calls() + .into_iter() + .filter(|c| c.starts_with("set_cert_role_policies")) + .collect::>(); + assert_eq!(writes, ["set_cert_role_policies hive-agent-a hive-agent"]); + } + + #[tokio::test] + async fn flipping_twice_ends_where_it_started() { + let store = FakeAccess::default().with_role("hive-agent-a", role(&["hive-agent"])); + reconcile(&store, &wanted(&[("a", true)])).await; + assert!(role_has_ssh(&store, "hive-agent-a").await.expect("reads")); + reconcile(&store, &wanted(&[("a", false)])).await; + assert!(!role_has_ssh(&store, "hive-agent-a").await.expect("reads")); + } + + #[tokio::test] + async fn an_agent_with_no_role_is_not_given_one() { + let store = FakeAccess::default(); + let counts = reconcile(&store, &wanted(&[("gone", true)])).await; + assert_eq!(counts, Counts::default()); + assert_eq!(store.role("hive-agent-gone"), None); + assert!( + !role_has_ssh(&store, "hive-agent-gone") + .await + .expect("reads") + ); + } + + #[tokio::test] + async fn a_role_the_store_refuses_to_write_is_counted_and_the_rest_go_on() { + let store = FakeAccess::default() + .with_role("hive-agent-a", role(&["hive-agent"])) + .with_role("hive-agent-b", role(&["hive-agent"])) + .with_unwritable_role("hive-agent-a"); + let counts = reconcile(&store, &wanted(&[("a", true), ("b", true)])).await; + assert_eq!( + counts, + Counts { + rewritten: 1, + already_current: 0, + failed: 1 + } + ); + } +} diff --git a/swarm-controller/src/main.rs b/swarm-controller/src/main.rs index 6c46944ed..3074451d6 100644 --- a/swarm-controller/src/main.rs +++ b/swarm-controller/src/main.rs @@ -36,6 +36,7 @@ mod agent_login; mod agent_permissions; mod agent_renewal; mod agent_role_ttl; +mod agent_ssh; mod agent_state_stream; mod agent_status; mod agent_todos; diff --git a/swarm-controller/src/startup.rs b/swarm-controller/src/startup.rs index 1b96b0109..b638f9aa4 100644 --- a/swarm-controller/src/startup.rs +++ b/swarm-controller/src/startup.rs @@ -27,11 +27,11 @@ use crate::routes::hives::{HiveEntry, ServiceLink}; use crate::routes::{self, AppState, agents, config_prs, forge_admin, hives, jobq_graph}; use crate::{ agent_commands, agent_control, agent_custom, agent_icon, agent_input, agent_limits, - agent_login, agent_permissions, agent_renewal, agent_state_stream, agent_status, agent_todos, - agents_md, alerts, archetypes, auth, backend_env, bash_task, builder, claude_login, - config_drift, config_pr, egress_allow, egress_deny, forge, forge_account, github_account, - gitlab_account, inference, issue_report, linked_accounts, matrix_account, message_stream, - notify_flows, notify_router_api, pause_hold, pr_report, pulls, queue_identity, + agent_login, agent_permissions, agent_renewal, agent_ssh, agent_state_stream, agent_status, + agent_todos, agents_md, alerts, archetypes, auth, backend_env, bash_task, builder, + claude_login, config_drift, config_pr, egress_allow, egress_deny, forge, forge_account, + github_account, gitlab_account, inference, issue_report, linked_accounts, matrix_account, + message_stream, notify_flows, notify_router_api, pause_hold, pr_report, pulls, queue_identity, review_classifier, schedules, status, subagent_term, term_stream, wanted, webhook, }; @@ -278,6 +278,7 @@ fn spawn_forge_workers( forge::runner_token::spawn(Arc::clone(&client)); if let Some(queue) = queue { forge::agent_max_repos::spawn(Arc::clone(&client), queue.clone()); + crate::agent_ssh::spawn(queue.clone()); crate::cache_miss::spawn( queue.clone(), Arc::new(wanted::WantedWriter::new(queue)), @@ -644,6 +645,7 @@ pub(crate) fn build_app(state: AppState) -> axum::Router { agent_permissions::delete_agent_permissions )) .routes(routes!(agent_permissions::get_permission_catalogue)) + .routes(routes!(agent_ssh::get_ssh_ca)) .routes(routes!( egress_allow::get_allowed_domains, egress_allow::add_allowed_domain, diff --git a/swarm-controller/src/store.rs b/swarm-controller/src/store.rs index 164763ec5..7bbe280f3 100644 --- a/swarm-controller/src/store.rs +++ b/swarm-controller/src/store.rs @@ -65,6 +65,21 @@ pub(crate) trait AccessStore { name: &str, ) -> impl Future> + Send; + /// As [`SecretStore::cert_role_policies`]. + fn cert_role_policies( + &self, + mount: &str, + name: &str, + ) -> impl Future>, Error>> + Send; + + /// As [`SecretStore::set_cert_role_policies`]. + fn set_cert_role_policies( + &self, + mount: &str, + name: &str, + policies: &[&str], + ) -> impl Future> + Send; + /// As [`SecretStore::auth_mount_accessor`]. fn auth_mount_accessor( &self, @@ -142,6 +157,23 @@ impl AccessStore for SecretStore { SecretStore::set_cert_role_token_ttls(self, mount, name).await } + async fn cert_role_policies( + &self, + mount: &str, + name: &str, + ) -> Result>, Error> { + SecretStore::cert_role_policies(self, mount, name).await + } + + async fn set_cert_role_policies( + &self, + mount: &str, + name: &str, + policies: &[&str], + ) -> Result<(), Error> { + SecretStore::set_cert_role_policies(self, mount, name, policies).await + } + async fn auth_mount_accessor(&self, mount: &str) -> Result { SecretStore::auth_mount_accessor(self, mount).await } @@ -385,6 +417,35 @@ pub(crate) mod fake { Ok(self.role(name).map(|r| r.ttls)) } + async fn cert_role_policies( + &self, + _mount: &str, + name: &str, + ) -> Result>, Error> { + self.log(format!("cert_role_policies {name}")); + Ok(self.role(name).map(|r| r.policies)) + } + + async fn set_cert_role_policies( + &self, + _mount: &str, + name: &str, + policies: &[&str], + ) -> Result<(), Error> { + self.log(format!( + "set_cert_role_policies {name} {}", + policies.join(",") + )); + if self.unwritable_roles.iter().any(|r| r == name) { + return Err(Self::denied()); + } + // Only the policies: the store keeps the fields a write omits. + if let Some(role) = self.roles.lock().expect("not poisoned").get_mut(name) { + role.policies = policies.iter().map(|p| (*p).to_owned()).collect(); + } + Ok(()) + } + async fn set_cert_role_token_ttls(&self, _mount: &str, name: &str) -> Result<(), Error> { self.log(format!("set_cert_role_token_ttls {name}")); if self.unwritable_roles.iter().any(|r| r == name) { diff --git a/swarm-secret-client/src/client.rs b/swarm-secret-client/src/client.rs index bfb283576..81510b00b 100644 --- a/swarm-secret-client/src/client.rs +++ b/swarm-secret-client/src/client.rs @@ -474,6 +474,67 @@ impl SecretStore { Ok(()) } + /// The public key of the SSH secrets engine mounted at `mount`, one + /// `authorized_keys`-style line, or `None` when the mount holds no CA yet. + /// + /// # Errors + /// [`Error::Vault`] for anything that is not a 400 or 404: a denial, or an + /// unreachable store. + pub async fn ssh_ca_public_key(&self, mount: &str) -> Result, Error> { + let request = ReadSshCa { + mount: mount.to_owned(), + }; + match vaultrs::api::exec_with_result(&self.inner, request).await { + Ok(ca) => Ok(Some(ca.public_key.trim().to_owned()).filter(|k| !k.is_empty())), + Err(e) if is_absent(&e) || is_bad_request(&e) => Ok(None), + Err(e) => Err(e.into()), + } + } + + /// The policies the cert-auth role `name` attaches to its tokens, or `None` + /// when the role does not exist. + /// + /// # Errors + /// [`Error::Vault`] for anything that is not a 404. + pub async fn cert_role_policies( + &self, + mount: &str, + name: &str, + ) -> Result>, Error> { + let request = ReadCertRolePolicies { + mount: mount.to_owned(), + name: name.to_owned(), + }; + match vaultrs::api::exec_with_result(&self.inner, request).await { + Ok(role) => Ok(Some(role.token_policies)), + Err(e) if is_absent(&e) => Ok(None), + Err(e) => Err(e.into()), + } + } + + /// Replace the policies the existing cert-auth role `name` attaches, leaving + /// the rest of the role as it is. The write names no other field, for the + /// reason [`Self::set_cert_role_token_ttls`] gives. Tokens already issued + /// keep the policies they were issued with until they expire or are renewed. + /// + /// # Errors + /// [`Error::Vault`] when the token's policy does not cover + /// `auth//certs/`, or the store refuses the write. + pub async fn set_cert_role_policies( + &self, + mount: &str, + name: &str, + policies: &[&str], + ) -> Result<(), Error> { + let request = SetCertRolePolicies { + mount: mount.to_owned(), + name: name.to_owned(), + token_policies: policies.iter().map(|p| (*p).to_owned()).collect(), + }; + vaultrs::api::exec_with_empty(&self.inner, request).await?; + Ok(()) + } + /// Have the PKI role `role` on `mount` issue a client certificate for /// `common_name`, returning it with its key and the issuing CA. /// @@ -768,6 +829,11 @@ fn is_absent(e: &vaultrs::error::ClientError) -> bool { matches!(e, vaultrs::error::ClientError::APIError { code: 404, .. }) } +/// A 400, which the SSH engine answers a CA read with while it holds no CA. +fn is_bad_request(e: &vaultrs::error::ClientError) -> bool { + matches!(e, vaultrs::error::ClientError::APIError { code: 400, .. }) +} + /// Write an ACL policy, spelled out here because [`vaultrs`] does not have it: /// its `sys::policy` module targets `sys/policy/`, the deprecated alias, /// and the store ACLs that path separately from `sys/policies/acl/`. A @@ -828,6 +894,53 @@ struct SetCertRoleTtls { token_max_ttl: String, } +/// Read an SSH engine mount's CA. +#[derive(Endpoint)] +#[endpoint(path = "{self.mount}/config/ca", method = "GET", response = "SshCa")] +struct ReadSshCa { + #[endpoint(skip)] + mount: String, +} + +/// The public half of the CA, the one field read. +#[derive(Debug, serde::Deserialize)] +struct SshCa { + public_key: String, +} + +/// Read the policies of a cert-auth role. +#[derive(Endpoint)] +#[endpoint( + path = "auth/{self.mount}/certs/{self.name}", + method = "GET", + response = "CertRolePolicies" +)] +struct ReadCertRolePolicies { + #[endpoint(skip)] + mount: String, + #[endpoint(skip)] + name: String, +} + +/// The one field of a cert-auth role [`SecretStore::cert_role_policies`] reads. +#[derive(Debug, serde::Deserialize)] +struct CertRolePolicies { + #[serde(default)] + token_policies: Vec, +} + +/// Write the policies of a cert-auth role and nothing else, as [`SetCertRoleTtls`] +/// does for the lifetimes. +#[derive(Endpoint)] +#[endpoint(path = "auth/{self.mount}/certs/{self.name}", method = "POST")] +struct SetCertRolePolicies { + #[endpoint(skip)] + mount: String, + #[endpoint(skip)] + name: String, + token_policies: Vec, +} + /// The request [`SecretStore::set_cert_role_token_ttls`] sends. fn set_cert_role_token_ttls_request(mount: &str, name: &str) -> SetCertRoleTtls { SetCertRoleTtls { diff --git a/swarm-secret-client/src/policy.rs b/swarm-secret-client/src/policy.rs index d74c866a3..755f8e2d5 100644 --- a/swarm-secret-client/src/policy.rs +++ b/swarm-secret-client/src/policy.rs @@ -194,6 +194,11 @@ pub fn render_shared_hives(mount_accessor: &str) -> Result { /// forbids. pub const SHARED_AGENT_POLICY: &str = "hive-agent"; +/// The policy that holds the SSH signing grant ([`render_ssh_signer`]). Inside +/// [`HIVE_PREFIX`] so the controller may write it. It is a policy name; the cert-auth role `hive-agent-ssh` would be an agent +/// called `ssh`'s, a different namespace. +pub const SSH_POLICY: &str = "hive-agent-ssh"; + /// The template that stands for the agent's name in [`render_shared_agent`]: /// the name of the login's identity entity, which swarm-controller sets to the /// agent's name. @@ -233,11 +238,8 @@ pub const SSH_ROLE: &str = "swarm-agent"; /// 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. +/// No SSH stanza: signing is [`SSH_POLICY`]'s, which an agent's role attaches +/// only while the operator has switched SSH on for it. #[must_use] pub fn render_shared_agent() -> String { let kind = <&str>::from(Kind::Agent); @@ -251,8 +253,21 @@ pub fn render_shared_agent() -> String { "{MOUNT}/data/{ROOT}/{kind}/{ENTITY_NAME_TEMPLATE}/{}", backend_env::LEAF )); - let sign = sign_stanza(&format!("{SSH_MOUNT}/sign/{SSH_ROLE}")); - format!("{read}{list}{seed}{sign}") + format!("{read}{list}{seed}") +} + +/// The policy that lets an agent have its SSH key signed: `update` on +/// [`SSH_MOUNT`]'s sign endpoint for [`SSH_ROLE`], and nothing else. The same +/// text for every agent. The agent submits a public key it generated and gets a +/// short-lived user certificate back. What the certificate may say is the +/// role's: the principal is the identity entity's name, so an agent is signed +/// for its own name and no other. +/// +/// Attached to an agent's cert-auth role only while its `ssh` permission is on +/// (`swarm-controller`'s `agent_ssh`). +#[must_use] +pub fn render_ssh_signer() -> String { + sign_stanza(&format!("{SSH_MOUNT}/sign/{SSH_ROLE}")) } /// What an agent's token grants under [`SHARED_AGENT_POLICY`]: the document @@ -284,8 +299,7 @@ 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 \"ssh-agents/sign/swarm-agent\" {\n capabilities = [\"update\"]\n}\n" + path \"secret/data/swarm/agents/{{identity.entity.name}}/backend-env\" {\n capabilities = [\"create\", \"read\"]\n}\n" ); } @@ -294,33 +308,30 @@ 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}"); } + /// What the SSH policy holds, byte for byte: one update stanza on one role's + /// sign path, the same text for every agent. #[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(); + fn the_ssh_policy_is_update_on_one_sign_path_and_nothing_else() { assert_eq!( - ssh, - ["ssh-agents/sign/swarm-agent\" {\n capabilities = [\"update\"]\n}\n"] + render_ssh_signer(), + "path \"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" ); + assert!(SSH_POLICY.starts_with(HIVE_PREFIX)); + assert_ne!(SSH_POLICY, SHARED_AGENT_POLICY); + } + + #[test] + fn the_shared_agent_policy_names_nothing_on_the_ssh_mount() { + assert!(!render_shared_agent().contains(SSH_MOUNT)); + assert!(render_ssh_signer().contains(SSH_MOUNT)); } #[test] @@ -525,8 +536,7 @@ 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 \"ssh-agents/sign/swarm-agent\" {\n capabilities = [\"update\"]\n}\n"; + path \"secret/data/swarm/agents/atlas/backend-env\" {\n capabilities = [\"create\", \"read\"]\n}\n"; #[test] fn an_agents_document_is_its_stanzas_naming_that_agent() { @@ -565,8 +575,8 @@ mod tests { assert!(!p.contains("argus"), "no other principal is named"); assert_eq!( p.matches("path \"").count(), - 4, - "four stanzas, or the document grants something unaccounted for: {p}" + 3, + "three stanzas, or the document grants something unaccounted for: {p}" ); } @@ -610,12 +620,7 @@ mod tests { ["secret/data/swarm/agents/atlas/*", leaf.as_str()] ); assert_eq!(paths_granting(&p, "create"), [leaf.as_str()]); - // 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"] { + for capability in ["update", "delete", "sudo", "patch"] { assert!( !p.contains(capability), "an agent's document must not grant {capability}: {p}" @@ -636,9 +641,8 @@ 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(), 4, "{p}"); - // The third is the create stanza and the fourth the signing stanza, - // each pinned by a test above. + assert_eq!(stanzas.len(), 3, "{p}"); + // The third is the create stanza, pinned by the 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/"); diff --git a/swarm-wire/src/agent_permissions.rs b/swarm-wire/src/agent_permissions.rs index 201805038..37a98721b 100644 --- a/swarm-wire/src/agent_permissions.rs +++ b/swarm-wire/src/agent_permissions.rs @@ -97,6 +97,11 @@ pub struct AgentPermissions { /// or agent acts on it. #[serde(default, skip_serializing_if = "Option::is_none")] pub max_repos: Option, + /// Whether the agent may have its SSH key signed by the swarm's SSH CA. Absent + /// is off. Only the controller reads it: it attaches the SSH signing policy + /// to the agent's store login role while this is `true`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ssh: Option, } /// What an agent whose record has no `max_repos` may create: no repositories. diff --git a/swarmctl/src/permissions.rs b/swarmctl/src/permissions.rs index 075392443..8a88228bc 100644 --- a/swarmctl/src/permissions.rs +++ b/swarmctl/src/permissions.rs @@ -102,6 +102,7 @@ fn with_permissions( unrestricted_internet, internet_log: None, max_repos: None, + ssh: None, }) }