From df3fc02279cf7170cfdb52fe4542585fa76884bf Mon Sep 17 00:00:00 2001 From: atlas Date: Mon, 16 Nov 2026 19:15:11 +0100 Subject: [PATCH] keep the signing policy on a role rewrite through one tested helper; route helper for build_app --- swarm-controller/src/agent_identity.rs | 34 +++-- swarm-controller/src/startup.rs | 198 +++++++++++++------------ 2 files changed, 126 insertions(+), 106 deletions(-) diff --git a/swarm-controller/src/agent_identity.rs b/swarm-controller/src/agent_identity.rs index be22011f5..d17b45bab 100644 --- a/swarm-controller/src/agent_identity.rs +++ b/swarm-controller/src/agent_identity.rs @@ -84,6 +84,22 @@ fn role_inputs<'a>( }) } +/// The inputs for `agent`'s role, with the signing policy attached when the role +/// it replaces has it. The role is where the agent's SSH switch lives, so a +/// rewrite for a new certificate keeps what it has (`crate::agent_ssh`). +async fn inputs_keeping_ssh<'a>( + store: &impl AccessStore, + agent: &str, + credential: &'a mtls::Credential, +) -> Result> { + let name = policy::agent_object_name(agent).at(MintStep::Role)?; + 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)?; + role_inputs(agent, credential, ssh).at(MintStep::Role) +} + /// Write the shared policy, name the agent's entity and enable it, then write /// the cert-auth role that attaches the policy. /// @@ -331,13 +347,7 @@ pub async fn mint_and_verify(agent: &str) -> Result<()> { } let queue_credential = wanted; - // 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)?; + let inputs = inputs_keeping_ssh(&store, agent, &credential).await?; publish_access(&store, agent, &inputs).await?; tracing::info!(agent, %path, role = %name, "agent store identity published"); @@ -470,7 +480,7 @@ async fn read_back_as_agent( mod tests { use super::{ ENV_AGENT_PKI_MOUNT, ENV_AGENT_PKI_ROLE, MintStep, QUEUE_SECRET_BYTES, agent_pki, - failed_step, generate_queue_secret, publish_access, role_inputs, + failed_step, generate_queue_secret, inputs_keeping_ssh, publish_access, role_inputs, }; use crate::store::fake::FakeAccess; use swarm_secret_client::{mtls, policy}; @@ -656,14 +666,16 @@ mod tests { ); // The rewrite reads the role it replaces, and keeps the grant. - let ssh = crate::agent_ssh::role_has_ssh(&on, "hive-agent-atlas") + let again = inputs_keeping_ssh(&on, "atlas", &credential) .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 fresh = inputs_keeping_ssh(&FakeAccess::default(), "atlas", &credential) + .await + .expect("reads"); + assert_eq!(fresh.attached, ["hive-agent"]); let off = FakeAccess::default(); let inputs = role_inputs("atlas", &credential, false).expect("legal"); publish_access(&off, "atlas", &inputs) diff --git a/swarm-controller/src/startup.rs b/swarm-controller/src/startup.rs index b638f9aa4..b1896cd54 100644 --- a/swarm-controller/src/startup.rs +++ b/swarm-controller/src/startup.rs @@ -574,6 +574,20 @@ pub(crate) async fn start_forge( .await; } +/// The routes of an agent's permissions, the permission catalogue and the SSH +/// CA key the permissions card shows, kept apart so `build_app` stays under the +/// line limit. +fn permission_routes(router: OpenApiRouter) -> OpenApiRouter { + router + .routes(routes!( + agent_permissions::get_agent_permissions, + agent_permissions::put_agent_permissions, + agent_permissions::delete_agent_permissions + )) + .routes(routes!(agent_permissions::get_permission_catalogue)) + .routes(routes!(agent_ssh::get_ssh_ca)) +} + /// The routes that write, delete and list an agent's linked accounts and /// backend environment, and those of its claude login, kept apart so /// `build_app` stays under the line limit. @@ -616,101 +630,95 @@ fn linked_account_routes(router: OpenApiRouter) -> OpenApiRouter axum::Router { - let (router, api) = - linked_account_routes(OpenApiRouter::::with_openapi(ApiDoc::openapi())) - .routes(routes!(routes::health)) - .routes(routes!(alerts::get_alerts)) - .routes(routes!(hives::get_hives)) - .routes(routes!(hives::get_hives_status)) - .routes(routes!(hives::get_links)) - .routes(routes!(hives::get_swarm_info)) - .routes(routes!(jobq_graph::get_jobq_graph)) - .routes(routes!(jobq_graph::get_jobq_rollup)) - .routes(routes!(config_prs::get_agent_config_pr)) - .routes(routes!(agents::get_agent_icon)) - .routes(routes!(agents_md::get_agents_md, agents_md::put_agents_md)) - .routes(routes!( - inference::get_agent_inference, - inference::put_agent_inference, - inference::delete_agent_inference - )) - .routes(routes!( - agent_limits::get_agent_limits, - agent_limits::put_agent_limits, - agent_limits::delete_agent_limits - )) - .routes(routes!( - agent_permissions::get_agent_permissions, - agent_permissions::put_agent_permissions, - 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, - egress_allow::remove_allowed_domain - )) - .routes(routes!( - egress_deny::get_denied_domains, - egress_deny::add_denied_domain, - egress_deny::remove_denied_domain - )) - .routes(routes!(archetypes::get_archetypes)) - .routes(routes!(config_prs::get_config_prs)) - .routes(routes!(agents::create_agent)) - .routes(routes!(forge_admin::mint_agent_forge_token)) - .routes(routes!(forge_admin::make_forge_admin)) - .routes(routes!(agents::get_agents)) - .routes(routes!(agents::get_agents_status)) - .routes(routes!(agents::set_agent_state)) - .routes(routes!(agents::get_hive_wanted)) - .routes(routes!(term_stream::stream_agent_term)) - .routes(routes!(message_stream::stream_messages)) - .routes(routes!(subagent_term::list_subagents)) - .routes(routes!(bash_task::list_bash_tasks)) - .routes(routes!(agent_commands::list_commands)) - .routes(routes!(agent_todos::list_todos)) - .routes(routes!(subagent_term::stream_subagent_term)) - .routes(routes!(agent_state_stream::stream_agent_state)) - .routes(routes!(agent_input::post_agent_input)) - .routes(routes!(agent_input::post_agents_broadcast)) - .routes(routes!(agent_control::post_agent_cancel)) - .routes(routes!(agent_control::post_agent_new_session)) - .routes(routes!(agent_custom::post_agent_custom)) - .routes(routes!(schedules::list_schedules, schedules::add_schedule)) - .routes(routes!( - schedules::edit_schedule, - schedules::remove_schedule - )) - .routes(routes!(issue_report::get_repos)) - .routes(routes!(issue_report::get_issue_report_all)) - .routes(routes!(issue_report::get_issue_report)) - .routes(routes!(pr_report::get_pr_report_all)) - .routes(routes!(pr_report::get_pr_report)) - .routes(routes!(pulls::get_pull_repos)) - .routes(routes!(pulls::get_pulls)) - .routes(routes!(review_classifier::post_try_out)) - .routes(routes!(review_classifier::get_try_out)) - .routes(routes!(review_classifier::get_rules)) - .routes(routes!(review_classifier::open_pr::post_open_pr)) - .routes(routes!(review_classifier::runs::get_runs)) - .routes(routes!(builder::runs::get_runs)) - .routes(routes!(builder::runs::get_run)) - .routes(routes!( - review_classifier::settings::get_settings, - review_classifier::settings::put_settings - )) - .routes(routes!(review_classifier::keys::get_keys)) - .routes(routes!(review_classifier::keys::put_key)) - .routes(routes!(notify_flows::get_flows, notify_flows::put_flows)) - .routes(routes!(notify_router_api::get_deliveries)) - .routes(routes!( - notify_router_api::get_dry_run, - notify_router_api::post_dry_run - )) - .routes(routes!(webhook::post_webhook_forge)) - .split_for_parts(); + let (router, api) = permission_routes(linked_account_routes( + OpenApiRouter::::with_openapi(ApiDoc::openapi()), + )) + .routes(routes!(routes::health)) + .routes(routes!(alerts::get_alerts)) + .routes(routes!(hives::get_hives)) + .routes(routes!(hives::get_hives_status)) + .routes(routes!(hives::get_links)) + .routes(routes!(hives::get_swarm_info)) + .routes(routes!(jobq_graph::get_jobq_graph)) + .routes(routes!(jobq_graph::get_jobq_rollup)) + .routes(routes!(config_prs::get_agent_config_pr)) + .routes(routes!(agents::get_agent_icon)) + .routes(routes!(agents_md::get_agents_md, agents_md::put_agents_md)) + .routes(routes!( + inference::get_agent_inference, + inference::put_agent_inference, + inference::delete_agent_inference + )) + .routes(routes!( + agent_limits::get_agent_limits, + agent_limits::put_agent_limits, + agent_limits::delete_agent_limits + )) + .routes(routes!( + egress_allow::get_allowed_domains, + egress_allow::add_allowed_domain, + egress_allow::remove_allowed_domain + )) + .routes(routes!( + egress_deny::get_denied_domains, + egress_deny::add_denied_domain, + egress_deny::remove_denied_domain + )) + .routes(routes!(archetypes::get_archetypes)) + .routes(routes!(config_prs::get_config_prs)) + .routes(routes!(agents::create_agent)) + .routes(routes!(forge_admin::mint_agent_forge_token)) + .routes(routes!(forge_admin::make_forge_admin)) + .routes(routes!(agents::get_agents)) + .routes(routes!(agents::get_agents_status)) + .routes(routes!(agents::set_agent_state)) + .routes(routes!(agents::get_hive_wanted)) + .routes(routes!(term_stream::stream_agent_term)) + .routes(routes!(message_stream::stream_messages)) + .routes(routes!(subagent_term::list_subagents)) + .routes(routes!(bash_task::list_bash_tasks)) + .routes(routes!(agent_commands::list_commands)) + .routes(routes!(agent_todos::list_todos)) + .routes(routes!(subagent_term::stream_subagent_term)) + .routes(routes!(agent_state_stream::stream_agent_state)) + .routes(routes!(agent_input::post_agent_input)) + .routes(routes!(agent_input::post_agents_broadcast)) + .routes(routes!(agent_control::post_agent_cancel)) + .routes(routes!(agent_control::post_agent_new_session)) + .routes(routes!(agent_custom::post_agent_custom)) + .routes(routes!(schedules::list_schedules, schedules::add_schedule)) + .routes(routes!( + schedules::edit_schedule, + schedules::remove_schedule + )) + .routes(routes!(issue_report::get_repos)) + .routes(routes!(issue_report::get_issue_report_all)) + .routes(routes!(issue_report::get_issue_report)) + .routes(routes!(pr_report::get_pr_report_all)) + .routes(routes!(pr_report::get_pr_report)) + .routes(routes!(pulls::get_pull_repos)) + .routes(routes!(pulls::get_pulls)) + .routes(routes!(review_classifier::post_try_out)) + .routes(routes!(review_classifier::get_try_out)) + .routes(routes!(review_classifier::get_rules)) + .routes(routes!(review_classifier::open_pr::post_open_pr)) + .routes(routes!(review_classifier::runs::get_runs)) + .routes(routes!(builder::runs::get_runs)) + .routes(routes!(builder::runs::get_run)) + .routes(routes!( + review_classifier::settings::get_settings, + review_classifier::settings::put_settings + )) + .routes(routes!(review_classifier::keys::get_keys)) + .routes(routes!(review_classifier::keys::put_key)) + .routes(routes!(notify_flows::get_flows, notify_flows::put_flows)) + .routes(routes!(notify_router_api::get_deliveries)) + .routes(routes!( + notify_router_api::get_dry_run, + notify_router_api::post_dry_run + )) + .routes(routes!(webhook::post_webhook_forge)) + .split_for_parts(); // Just the JSON, not the UI — Swagger UI itself is nginx-hosted from // the nix store (see the module doc comment above). `api` is // `Clone`; each request gets its own owned copy for `Json` to