gate ssh signing per agent: ssh permission, signing policy on the login role, ca key endpoint
This commit is contained in:
parent
aeb9317ad1
commit
910f9a6f50
6 changed files with 805 additions and 57 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
362
swarm-controller/src/agent_ssh.rs
Normal file
362
swarm-controller/src/agent_ssh.rs
Normal file
|
|
@ -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-<name>` 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<String> {
|
||||
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<bool, swarm_secret_client::Error> {
|
||||
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<String, bool>) -> 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<Option<bool>, 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<bool> {
|
||||
let (record, _, _) = read_or_default::<AgentPermissions>(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<BTreeMap<String, bool>> {
|
||||
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<AppState>,
|
||||
) -> Result<Json<SshCa>, 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<String, bool> {
|
||||
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::<Vec<_>>();
|
||||
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
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -65,6 +65,21 @@ pub(crate) trait AccessStore {
|
|||
name: &str,
|
||||
) -> impl Future<Output = Result<(), Error>> + Send;
|
||||
|
||||
/// As [`SecretStore::cert_role_policies`].
|
||||
fn cert_role_policies(
|
||||
&self,
|
||||
mount: &str,
|
||||
name: &str,
|
||||
) -> impl Future<Output = Result<Option<Vec<String>>, Error>> + Send;
|
||||
|
||||
/// As [`SecretStore::set_cert_role_policies`].
|
||||
fn set_cert_role_policies(
|
||||
&self,
|
||||
mount: &str,
|
||||
name: &str,
|
||||
policies: &[&str],
|
||||
) -> impl Future<Output = Result<(), Error>> + 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<Option<Vec<String>>, 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<String, Error> {
|
||||
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<Option<Vec<String>>, 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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue