diff --git a/docs/swarm/credentials.md b/docs/swarm/credentials.md index 13597a341..3f269fa62 100644 --- a/docs/swarm/credentials.md +++ b/docs/swarm/credentials.md @@ -369,41 +369,35 @@ that mount's one role for a `hive-agent-` client certificate at agent creation. Host roles never pin that CA and agent roles pin only it, so an agent's certificate opens that agent's own `swarm/agents//*` and nothing else: `read` on the values there and `list` on their names. +`swarm-controller` writes that policy at agent creation and rewrites every +agent's at its own start, so a change to it reaches existing agents with the +next controller restart. -All agents share one policy, `hive-agent`, which the agent's cert-auth role -attaches and no other. It states the rule with `{{identity.entity.name}}` -where an agent's name would go, so the rule is one document and a change to it -is one write. `swarm-controller` writes it at agent creation and at its own -start. +The agent's cert-auth role attaches a second policy, `hive-agent`, shared by +all agents. It states the same rule with `{{identity.entity.name}}` where the +per-agent policy has the agent's name, so the rule is one document. For that +the controller names each agent's identity entity after the agent: at agent +creation, and for every agent at its own start, it reads the cert mount's +accessor and creates the entity `` with the alias `hive-agent-`, +or renames the entity a first login created under a generated name. When the alias +sits on any other entity, the controller logs an error and changes nothing. It needs +the `identity/` and `sys/auth/cert` grants in `swarm-bao.nix`'s +`controllerPolicyText` for that. Without them it logs a warning and the agent +keeps working through its own policy, which stays attached until every agent +has its entity. An agent's role gains `hive-agent` at its next certificate +mint. -For that the controller names each agent's identity entity after the agent: at -each certificate mint or renewal, before it writes the agent's cert-auth role, -and for every agent at its own start. It reads the cert mount's accessor and -creates the entity `` with the alias `hive-agent-`, or renames the -entity a first login created under a generated name. At a mint or renewal this is -a precondition: when the store refuses a call, or the alias sits on any other -entity, the mint fails and the role stays as it was, so a live agent keeps -working until an operator resolves it. The start pass only logs the same -failures, because it writes no role. The controller needs the `identity/` and -`sys/auth/cert` grants in `swarm-bao.nix`'s `controllerPolicyText` for that. - -A cert-auth role that predates the shared policy may still name a policy -`hive-agent-`. The controller no longer writes or attaches these. The -objects stay in the store and keep granting what they did, to a token that -already carries them and to a role not yet rewritten. A live agent's role is -rewritten at its next certificate renewal. - -A hive's cert-auth role `hive-` attaches one policy, `hive-hives`, shared -by all hives. A hive's certificate carries its bare name as +A hive's cert-auth role `hive-` likewise attaches a second policy, +`hive-hives`, shared by all hives. A hive's certificate carries its bare name as the common name, which is the login's alias name, so hives need no entity: the policy states the hive's own path as `swarm/hives/{{identity.entity.aliases..name}}/*`, where `` is the cert mount's accessor. The controller reads the accessor at its own start, -writes the policy, and rewrites the roles to attach it. No hive name can +writes the policy, and attaches it when it rewrites the roles. No hive name can spell `hive-hives`: it would be a hive called `hives`, and `reserved-hive-fragments.nix` refuses the substring `hive`. When the controller -can't read the accessor or write the policy it logs a warning and leaves the -roles untouched, so a role that names a `hive-` policy keeps it. +can't read the accessor or write the policy it logs a warning and the role +attaches the hive's own policy alone, which stays attached. ### Per-principal identities diff --git a/hive-matrix-mcp/src/credential.rs b/hive-matrix-mcp/src/credential.rs index c63fb7d69..cc7b2ce2e 100644 --- a/hive-matrix-mcp/src/credential.rs +++ b/hive-matrix-mcp/src/credential.rs @@ -17,7 +17,7 @@ //! //! Which accounts exist is read the same way: [`linked_accounts`] lists this //! agent's `matrix/` directory, which its policy grants `list` on -//! (`swarm_secret_client::policy::render_shared_agent`). +//! (`swarm_secret_client::policy::render_agent`). //! //! The file arm is a `tokenFile` an operator declared in `matrixAccounts`, or //! the `main` token a hive minted before the swarm did. diff --git a/nix/host-modules/otel.nix b/nix/host-modules/otel.nix index 62081e81e..6d57db0f2 100644 --- a/nix/host-modules/otel.nix +++ b/nix/host-modules/otel.nix @@ -38,7 +38,7 @@ let # The hive's own store identity. The collector authenticates to the swarm # as the hive, so it reads its secret as the hive: the hive leaf's policy - # (`swarm-secret-client`'s `policy::render_shared_hives`) grants `swarm/hives//*`. + # (`swarm-secret-client`'s `policy::render`) grants `swarm/hives//*`. runsHive = config.services.hyperhive.otel.collector.enable; hiveLeaf = diff --git a/nix/host-modules/swarm-bao.nix b/nix/host-modules/swarm-bao.nix index 270e78cf9..93a48bcb0 100644 --- a/nix/host-modules/swarm-bao.nix +++ b/nix/host-modules/swarm-bao.nix @@ -872,8 +872,8 @@ let agentPkiRoleName = baoDeploy.agentPkiRoleName; # Every common name the agent role issues for: `swarm_secret_client`'s - # `policy::AGENT_PREFIX`, which names each agent's cert-auth role too. - # Outside it the role refuses, so the controller cannot obtain + # `policy::AGENT_PREFIX`, which names each agent's cert-auth role and + # policy too. Outside it the role refuses, so the controller cannot obtain # a certificate for any other name. agentCnGlob = "hive-agent-*"; @@ -938,7 +938,7 @@ let # # 🩸 Each of these reads exactly ONE path in the store, and does it holding # its own leaf rather than `deploy.bao.clientCertFile` — the hive's own leaf, - # whose policy (`swarm-secret-client`'s `policy::render_shared_hives`) grants read on + # whose policy (`swarm-secret-client`'s `policy::render`) grants read on # `swarm/agents/*` AND `swarm/hives//*`, and nothing under # `swarm/services/`. # Principals presenting one certificate are ONE principal to bao, so sharing diff --git a/swarm-controller/src/agent_entity.rs b/swarm-controller/src/agent_entity.rs index 5c698630c..0bbc0d2d0 100644 --- a/swarm-controller/src/agent_entity.rs +++ b/swarm-controller/src/agent_entity.rs @@ -13,22 +13,19 @@ //! is ever moved. Any other arrangement is a [`Plan::Conflict`]: nothing is //! written and an operator decides. //! -//! An agent whose entity is not named after it reads nothing under the shared -//! policy, so what a failure costs depends on the caller. [`ensure_agent`], -//! which runs before a cert-auth role is narrowed to that policy, returns the -//! error and leaves the role as it was. [`ensure_agents`], the pass at every -//! start, writes no role and so narrows nothing: it logs at `warn` and goes on. -//! The grants are the `identity/` stanzas of `swarm-bao.nix`'s -//! `controllerPolicyText`. +//! Everything else here is best effort. An agent's own policy still grants it +//! its paths, so a store that refuses these calls costs the shared policy and +//! nothing else: the failure is logged at `warn` and the caller goes on. The +//! grants are the `identity/` stanzas of `swarm-bao.nix`'s +//! `controllerPolicyText`, and a controller can start before they deploy. use anyhow::{Context, Result, bail}; use swarm_secret_client::{ + SecretStore, client::{DEFAULT_CERT_MOUNT, EntityRef}, policy, }; -use crate::store::AccessStore; - /// What the store holds for one agent's entity. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Seen { @@ -94,13 +91,13 @@ pub fn plan(agent: &str, seen: &Seen) -> Plan { /// Make sure every agent in `agents` has its entity, logging what could not be /// done. Never fails: see the module header. -pub async fn ensure_agents(store: &impl AccessStore, agents: &[String]) { +pub async fn ensure_agents(store: &SecretStore, agents: &[String]) { let accessor = match store.auth_mount_accessor(DEFAULT_CERT_MOUNT).await { Ok(accessor) => accessor, Err(e) => { tracing::warn!( error = %e, - "agent entities are not managed: the cert auth mount's accessor could not be read; an agent whose role attaches the shared policy alone reads nothing until its entity is named after it" + "agent entities are not managed: the cert auth mount's accessor could not be read; agents keep their own policy" ); return; } @@ -122,30 +119,13 @@ pub async fn ensure_agents(store: &impl AccessStore, agents: &[String]) { ); } -/// Make sure `agent` has its entity, or say why not. -/// -/// For a caller that must not go on without it, such as the one that narrows a -/// cert-auth role to the shared policy: an agent whose entity is not named after -/// it reads nothing under that policy. -/// -/// # Errors -/// When the cert mount's accessor cannot be read, when a store call fails, and -/// on a [`Plan::Conflict`], which [`ensure_agents`] only logs. -pub async fn ensure_agent(store: &impl AccessStore, agent: &str) -> Result<()> { - let accessor = store - .auth_mount_accessor(DEFAULT_CERT_MOUNT) - .await - .context("reading the cert auth mount's accessor")?; - match ensure_one(store, &accessor, agent).await? { - Plan::Conflict { alias, entity_name } => bail!( - "the alias {alias} is on the entity {entity_name}, which is not {agent}; nothing was changed" - ), - _ => Ok(()), - } +/// [`ensure_agents`] for one agent. +pub async fn ensure_agent(store: &SecretStore, agent: &str) { + ensure_agents(store, &[agent.to_owned()]).await; } /// Observe, plan and apply for one agent. -async fn ensure_one(store: &impl AccessStore, accessor: &str, agent: &str) -> Result { +async fn ensure_one(store: &SecretStore, accessor: &str, agent: &str) -> Result { let alias = policy::agent_object_name(agent)?; let seen = Seen { by_alias: store @@ -193,7 +173,7 @@ async fn ensure_one(store: &impl AccessStore, accessor: &str, agent: &str) -> Re agent, alias, entity_name, - "the agent's alias is on an entity that is not the agent's; nothing was changed; under the shared policy alone the agent reads nothing until an operator resolves it" + "the agent's alias is on an entity that is not the agent's; nothing was changed and the agent keeps its own policy" ); } } diff --git a/swarm-controller/src/agent_identity.rs b/swarm-controller/src/agent_identity.rs index 13eec673c..aead0378e 100644 --- a/swarm-controller/src/agent_identity.rs +++ b/swarm-controller/src/agent_identity.rs @@ -17,10 +17,10 @@ //! not derived from the first, so either renews without reference to the other. //! //! Four separate strings have to agree before an agent can authenticate: the -//! identity entity's name, the cert-auth role's name, the certificate's common -//! name, and the authority the role pins. That is the kind of agreement that -//! holds in review and fails in production — a mismatch is a 403 naming none -//! of the four — so [`mint_and_verify`] does not finish on a write. See +//! policy's name, the cert-auth role's name, the certificate's common name, +//! and the authority the role pins. That is the kind of agreement that holds +//! in review and fails in production — a mismatch is a 403 naming none of the +//! four — so [`mint_and_verify`] does not finish on a write. See //! [`read_back_as_agent`]. //! //! The authority is the store's own agent CA, generated inside its agent PKI @@ -29,7 +29,6 @@ //! [`crate::agent_renewal`] re-issues a live agent's leaf once it is past half //! its validity (the role's `ttl`), by queueing the same node as creation. -use crate::store::AccessStore; use anyhow::{Context, Result, bail}; use swarm_secret_client::{ SecretStore, @@ -63,12 +62,15 @@ fn agent_pki(get: impl Fn(&str) -> Option) -> Result<(String, String)> { /// What the cert-auth role for `agent` is written from. struct RoleInputs<'a> { - /// Role name and the common name the role matches: one string. + /// Role name, policy name and the common name the role matches: one string. name: String, /// The authority the role pins: the one that signed this very leaf. ca: &'a str, - /// The policies the role attaches by name: the one every agent shares. - attached: Vec, + /// The policy document the role attaches. + policy: String, + /// The policies the role attaches by name: the agent's own, and the one every + /// agent shares. + attached: [String; 2], } fn role_inputs<'a>(agent: &str, credential: &'a mtls::Credential) -> Result> { @@ -76,52 +78,11 @@ fn role_inputs<'a>(agent: &str, credential: &'a mtls::Credential) -> Result, -) -> Result<()> { - store - .write_policy(policy::SHARED_AGENT_POLICY, &policy::render_shared_agent()) - .await - .with_context(|| { - format!( - "writing the shared agent policy {}", - policy::SHARED_AGENT_POLICY - ) - })?; - crate::agent_entity::ensure_agent(store, agent) - .await - .with_context(|| format!("naming {agent}'s identity entity; the role is not changed"))?; - store - .write_cert_role( - DEFAULT_CERT_MOUNT, - &inputs.name, - inputs.ca, - &inputs.name, - &inputs - .attached - .iter() - .map(String::as_str) - .collect::>(), - ) - .await - .with_context(|| format!("writing the cert-auth role {}", inputs.name)) -} - /// How many bytes of kernel randomness a queue secret is before encoding. /// /// Thirty-two because the secret is a bearer token compared for equality and @@ -154,7 +115,7 @@ pub(crate) fn generate_queue_secret() -> Result { /// Give `agent` an identity at the store, and prove it works. /// -/// Six store calls' worth of agreement, then the login that checks it: +/// Five store calls' worth of agreement, then the login that checks it: /// /// 1. have the agent PKI role issue a leaf whose common name is /// [`policy::agent_object_name`]; the store generates its key; @@ -163,13 +124,14 @@ pub(crate) fn generate_queue_secret() -> Result { /// 3. publish a queue secret at [`queue::agent_queue_path`] — the agent's own /// identity at the swarm queue, minted here so that the credential an agent /// presents names *it* rather than its hive; -/// 4. write the shared ACL document [`policy::render_shared_agent`] renders; -/// 5. name the agent's identity entity after the agent ([`crate::agent_entity`]), -/// which the policy reads; -/// 6. write the cert-auth role that ties the three together, attaching that -/// policy and pinning the CA the store named as this leaf's issuer. +/// 4. write the ACL document [`policy::render_agent`] renders — read and list on +/// this one agent's paths — and the shared [`policy::render_shared_agent`]; +/// 5. write the cert-auth role that ties the three together, attaching both +/// policies and pinning the CA the store named as this leaf's issuer. /// -/// Policy and entity before role: see [`publish_access`]. +/// Policy before role, for the reason `read_policy::provision` gives: the role +/// names the policy, so the other order leaves a window in which it points at +/// nothing. /// /// ⚠️ **Step 3 is idempotent and steps 1–2 are not.** Re-running issues a /// fresh leaf, picked up on the agent's next boot, but leaves an existing @@ -178,10 +140,9 @@ pub(crate) fn generate_queue_secret() -> Result { /// replacing one by age is [`crate::agent_renewal`]'s. /// /// # Errors -/// Anything that stops one of those six steps, with the step named, or the -/// read-back login. A failure here fails the job node and nothing else — a new -/// agent is still created, without a store identity, and a live agent keeps the -/// role it had. +/// Anything that stops one of those five steps, with the step named. A +/// failure here fails the job node and nothing else — the agent is still +/// created, without a store identity. pub async fn mint_and_verify(agent: &str) -> Result<()> { let (mount, pki_role) = agent_pki(|k| std::env::var(k).ok())?; let name = policy::agent_object_name(agent)?; @@ -240,8 +201,33 @@ pub async fn mint_and_verify(agent: &str) -> Result<()> { let queue_credential = wanted; let inputs = role_inputs(agent, &credential)?; - publish_access(&store, agent, &inputs).await?; + store + .write_policy(&inputs.name, &inputs.policy) + .await + .with_context(|| format!("writing the read policy {}", inputs.name))?; + store + .write_policy(policy::SHARED_AGENT_POLICY, &policy::render_shared_agent()) + .await + .with_context(|| { + format!( + "writing the shared agent policy {}", + policy::SHARED_AGENT_POLICY + ) + })?; + store + .write_cert_role( + DEFAULT_CERT_MOUNT, + &inputs.name, + inputs.ca, + &inputs.name, + &inputs.attached.each_ref().map(String::as_str), + ) + .await + .with_context(|| format!("writing the cert-auth role {}", inputs.name))?; tracing::info!(agent, %path, role = %name, "agent store identity published"); + // Before the read-back below, whose login would otherwise create the + // entity under a generated name. + crate::agent_entity::ensure_agent(&store, agent).await; read_back_as_agent(&credential, &name, &path, &queue_path, &queue_credential).await?; tracing::info!( @@ -311,7 +297,7 @@ pub async fn revoke_queue_credential(agent: &str) -> Result<()> { /// /// Both paths, not just the certificate's, for the reason this function /// exists at all: the queue credential is readable only because it sits inside -/// the stanza [`policy::render_shared_agent`] grants, and a policy that +/// the stanza [`policy::render_agent`] already grants, and a policy that /// drifted from that path would otherwise fail at the agent's first connection /// — far from here, as a denial naming neither the policy nor the path. /// @@ -370,9 +356,8 @@ async fn read_back_as_agent( mod tests { use super::{ ENV_AGENT_PKI_MOUNT, ENV_AGENT_PKI_ROLE, QUEUE_SECRET_BYTES, agent_pki, - generate_queue_secret, publish_access, role_inputs, + generate_queue_secret, role_inputs, }; - use crate::store::fake::FakeAccess; use swarm_secret_client::{mtls, policy}; /// The alphabet claim the token format rests on: the secret is carried in @@ -465,9 +450,9 @@ mod tests { } /// Three of the four strings that must agree, checked where this process - /// sets them: role and matched CN are one name; the pinned CA is the issuer - /// the store reported for this leaf; the role attaches the shared policy - /// and no policy of the agent's own. + /// sets them: role, policy and matched CN are one name; the pinned CA is + /// the issuer the store reported for this leaf; the policy is the agent's + /// own single stanza. #[test] fn the_role_pins_the_leafs_own_issuer_under_the_agents_name() { let credential = mtls::Credential { @@ -483,90 +468,17 @@ mod tests { "the CN the leaf is issued for" ); assert_eq!(inputs.ca, "AGENT-CA"); - // Only the shared policy: a policy named for the agent is not attached. assert_eq!( inputs.attached, - [policy::SHARED_AGENT_POLICY], - "the role attaches the shared policy and nothing else" - ); - assert!( - !inputs.attached.contains(&inputs.name), - "the role must not attach a policy named for the agent" + ["hive-agent-atlas", policy::SHARED_AGENT_POLICY], + "the role attaches the agent's own policy and the shared one" ); + assert_eq!(inputs.policy, policy::render_agent("atlas").expect("legal")); + assert!(!inputs.policy.contains("swarm/hives/"), "{}", inputs.policy); - // 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. + // The control: another agent's inputs differ in both name and grant. let other = role_inputs("argus", &credential).expect("legal"); assert_ne!(other.name, inputs.name); - assert_eq!(other.attached, inputs.attached); - } - - fn credential() -> mtls::Credential { - mtls::Credential { - cert: "LEAF".to_owned(), - key: "KEY".to_owned(), - ca: "AGENT-CA".to_owned(), - } - } - - /// The control: with a working entity step the role is narrowed, after the - /// entity is named, so the absence checks below can see a role write. - #[tokio::test] - 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"); - publish_access(&store, "atlas", &inputs) - .await - .expect("a store that accepts everything"); - assert_eq!( - store.roles_written(), - ["write_cert_role hive-agent-atlas hive-agent"] - ); - let calls = store.calls(); - let at = |call: &str| calls.iter().position(|c| c == call).expect(call); - assert!( - at("create_entity_alias hive-agent-atlas") - < at("write_cert_role hive-agent-atlas hive-agent"), - "the entity is named before the role is written: {calls:?}" - ); - } - - #[tokio::test] - 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 err = publish_access(&store, "atlas", &inputs) - .await - .expect_err("the entity step failed"); - assert!( - format!("{err:#}").contains("the role is not changed"), - "{err:#}" - ); - assert!(store.roles_written().is_empty(), "{:?}", store.calls()); - // The control: the call log sees writes, and the shared policy was one. - assert!( - store - .calls() - .contains(&"write_policy hive-agent".to_owned()) - ); - } - - #[tokio::test] - 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 err = publish_access(&store, "atlas", &inputs) - .await - .expect_err("a conflict is an error"); - assert!(format!("{err:#}").contains("somebody"), "{err:#}"); - assert!(store.roles_written().is_empty(), "{:?}", store.calls()); - assert!( - store - .calls() - .contains(&"entity_by_alias hive-agent-atlas".to_owned()) - ); + assert!(!other.policy.contains("agents/atlas/"), "{}", other.policy); } } diff --git a/swarm-controller/src/main.rs b/swarm-controller/src/main.rs index 49817605f..ad7141990 100644 --- a/swarm-controller/src/main.rs +++ b/swarm-controller/src/main.rs @@ -217,8 +217,8 @@ async fn main() -> Result<()> { // abandoned, since the two of them boot together. let hive_names: Vec = hives.iter().map(|h| h.name.clone()).collect(); read_policy::ensure_hive_access(hive_names).await; - // The one policy every agent's role attaches, and the entity each agent's - // login is read under, are checked here at every start. + // Agents keep the policy they were minted with until this rewrites it, so + // what `policy::render_agent` grants today reaches them only from here. read_policy::ensure_agent_policies().await; let state = AppState { diff --git a/swarm-controller/src/read_policy.rs b/swarm-controller/src/read_policy.rs index c651d189e..dea5eff1d 100644 --- a/swarm-controller/src/read_policy.rs +++ b/swarm-controller/src/read_policy.rs @@ -14,24 +14,24 @@ //! take down a daemon that serves everything else, and a store that answers is //! answering the same way next time, so the next start is the retry for those. //! -//! The rule is written once for all hives, [`policy::SHARED_HIVE_POLICY`]: one -//! stanza is the same everywhere, the other is scoped to the reader's own name -//! ([`swarm_secret_client::policy`] explains the asymmetry), taken from its -//! login's alias on the cert mount instead of written into the text. A hive's -//! role attaches that policy and no other. The hive list is loaded once because -//! a config change means a redeploy. +//! The document names the hive it is written for: one stanza is the same +//! everywhere, the other is scoped to the reader's own name +//! ([`swarm_secret_client::policy`] explains the asymmetry). The same rule is +//! also written once for all hives, [`policy::SHARED_HIVE_POLICY`], with the +//! reader's own name taken from its login's alias on the cert mount instead of +//! written into the text; a hive's role attaches both. The hive list is loaded +//! once because a config change means a redeploy. //! -//! The shared agent policy is written by the same kind of pass -//! ([`ensure_agent_policies`]), together with each agent's identity entity. -//! Its roster is the store's `hive-agent-*` cert-auth roles. +//! Every agent's policy is rewritten by the same kind of pass +//! ([`ensure_agent_policies`]), so a change to `policy::render_agent` reaches +//! agents minted before it. Its roster is the store's `hive-agent-*` cert-auth +//! roles. use std::{future::Future, time::Duration}; use anyhow::{Context, Result}; use backon::{BackoffBuilder as _, ConstantBuilder}; -use swarm_secret_client::{Error, client::DEFAULT_CERT_MOUNT, policy}; - -use crate::store::AccessStore; +use swarm_secret_client::{Error, SecretStore, client::DEFAULT_CERT_MOUNT, policy}; /// File holding the authority hives are issued from, as /// `swarm-controller.nix` names it. @@ -128,12 +128,10 @@ where None } -/// One pass: log in once, write the shared hive policy, then every hive's role. +/// One pass: log in once, write every hive's policy, then every hive's role. /// /// Policy first, because the role names the policy — the other order leaves a -/// window in which the role points at nothing. Without the policy the roles are -/// left as they are: a role written to name a policy that does not exist logs -/// its hive in able to read nothing. +/// window in which the role points at nothing. /// /// # Errors /// [`Unreachable`] for the states a later attempt can resolve: a store that is @@ -156,56 +154,55 @@ async fn provision(hives: &[String]) -> Result<(), Unreachable> { } Err(e) => return Err(Unreachable(e.to_string())), }; - provision_with(&store, hives, read_hive_authority).await -} + for hive in hives { + if let Err(e) = write_policy_for(&store, hive).await { + tracing::warn!(hive, error = %format!("{e:#}"), "writing this hive's read policy failed"); + } + } + let shared = write_shared_policy(&store).await; -/// The hive authority from the file `ENV_HIVE_CLIENT_CA` names, or `None` when -/// none is configured. -fn read_hive_authority() -> Result, Unreachable> { let Some(path) = std::env::var_os(ENV_HIVE_CLIENT_CA) else { - return Ok(None); + tracing::info!("no hive certificate authority configured; hive roles are not managed here"); + return Ok(()); }; let path = path.to_string_lossy().into_owned(); // Retryable rather than fatal: the unit that places this file and this // daemon start together, so "not there yet" is a moment, not a verdict. - std::fs::read_to_string(&path) - .map(Some) - .map_err(|e| Unreachable(format!("reading the hive authority {path}: {e}"))) -} - -/// [`provision`] against `store`, with the authority read by `read_authority`. -async fn provision_with( - store: &impl AccessStore, - hives: &[String], - read_authority: impl FnOnce() -> Result, Unreachable>, -) -> Result<(), Unreachable> { - if !write_shared_policy(store).await { - return Ok(()); - } - let Some(ca) = read_authority()? else { - tracing::info!("no hive certificate authority configured; hive roles are not managed here"); - return Ok(()); - }; + let ca = std::fs::read_to_string(&path) + .map_err(|e| Unreachable(format!("reading the hive authority {path}: {e}")))?; for hive in hives { - if let Err(e) = write_role(store, hive, &ca).await { + if let Err(e) = write_role(&store, hive, &ca, shared).await { tracing::warn!(hive, error = %format!("{e:#}"), "creating this hive's store role failed"); } } Ok(()) } +/// One hive's policy: its own name, its own document. +async fn write_policy_for(store: &SecretStore, hive: &str) -> Result<()> { + let name = policy::hive_object_name(hive)?; + store + .write_policy(&name, &policy::render(hive)?) + .await + .with_context(|| format!("writing the read policy {name}"))?; + tracing::info!(hive, policy = %name, "hive read policy in place"); + Ok(()) +} + /// Write [`policy::SHARED_HIVE_POLICY`], rendered with the cert mount's -/// accessor. Answers whether it is in place: hive roles are written only then. +/// accessor. Answers whether it is in place: a hive's role attaches it only +/// then. /// -/// A store that refuses the accessor read (the controller's policy may not -/// carry it yet) or the write leaves the roles already in the store as they are. -async fn write_shared_policy(store: &impl AccessStore) -> bool { +/// Best effort. A hive's own policy grants it the same paths, so a store that +/// refuses the accessor read (the controller's policy may not carry it yet) or +/// the write costs the shared policy and nothing else. +async fn write_shared_policy(store: &SecretStore) -> bool { let accessor = match store.auth_mount_accessor(DEFAULT_CERT_MOUNT).await { Ok(accessor) => accessor, Err(e) => { tracing::warn!( error = %e, - "the shared hive policy is not managed: the cert auth mount's accessor could not be read; hive roles are left as they are" + "the shared hive policy is not managed: the cert auth mount's accessor could not be read; hives keep their own policy" ); return false; } @@ -213,7 +210,7 @@ async fn write_shared_policy(store: &impl AccessStore) -> bool { let document = match policy::render_shared_hives(&accessor) { Ok(document) => document, Err(e) => { - tracing::warn!(error = %e, "the shared hive policy is not managed: the cert auth mount's accessor is not usable in a policy; hive roles are left as they are"); + tracing::warn!(error = %e, "the shared hive policy is not managed: the cert auth mount's accessor is not usable in a policy; hives keep their own policy"); return false; } }; @@ -232,23 +229,28 @@ async fn write_shared_policy(store: &impl AccessStore) -> bool { tracing::warn!( policy = policy::SHARED_HIVE_POLICY, error = %e, - "writing the shared hive policy failed; hive roles are left as they are" + "writing the shared hive policy failed; hives keep their own policy" ); false } } } -/// The policies a hive's role attaches: the shared one, and no policy named for -/// the hive. -fn attached_policies() -> Vec { - vec![policy::SHARED_HIVE_POLICY.to_owned()] +/// The policies `hive`'s role attaches: its own, and the shared one when that +/// is in place. +fn attached_policies(hive: &str, shared: bool) -> Result, Error> { + let mut attached = vec![policy::hive_object_name(hive)?]; + if shared { + attached.push(policy::SHARED_HIVE_POLICY.to_owned()); + } + Ok(attached) } -/// The role for one hive: its own certificate's subject and the shared policy. -async fn write_role(store: &impl AccessStore, hive: &str, ca: &str) -> Result<()> { +/// The role for one hive: its own certificate's subject, its own policy, and +/// the shared one when `shared`. +async fn write_role(store: &SecretStore, hive: &str, ca: &str, shared: bool) -> Result<()> { let name = policy::hive_object_name(hive)?; - let attached = attached_policies(); + let attached = attached_policies(hive, shared)?; store // The subject is the hive's name: `glue-bao-tls.nix` mints a hive's // client leaf with its name as the CN, and cert auth matches on that. @@ -265,8 +267,8 @@ async fn write_role(store: &impl AccessStore, hive: &str, ca: &str) -> Result<() Ok(()) } -/// Write the shared agent policy and give every agent its identity entity, with -/// the retry [`ensure_hive_access`] has and for the same reason. +/// Rewrite every agent's policy from [`policy::render_agent`], with the retry +/// [`ensure_hive_access`] has and for the same reason. /// /// Returns once the first pass is done. When that pass cannot reach the store, /// it repeats in the background for up to [`RETRY_WINDOW`]. @@ -292,7 +294,7 @@ pub async fn ensure_agent_policies() { tracing::warn!( attempts = RETRY_ATTEMPTS, window = ?RETRY_WINDOW, - "giving up on the agent policy rewrite; the shared agent policy and the agents' identity entities are not checked until this daemon is restarted" + "giving up on the agent policy rewrite; agents keep the policy they were minted with until this daemon is restarted" ); } }); @@ -300,9 +302,9 @@ pub async fn ensure_agent_policies() { } /// One pass: log in once, list the agents, write the shared agent policy and -/// give each agent its identity entity. +/// each one's own, and give each its identity entity. /// -/// The policy and entities only. An agent's cert-auth role and certificate are +/// Policies and entities only. An agent's cert-auth role and certificate are /// `agent_identity::mint_and_verify`'s, and that reissues the certificate, so /// it is not what a pass on every start may call. /// @@ -341,19 +343,46 @@ async fn rewrite_agent_policies() -> Result<(), Unreachable> { "writing the shared agent policy failed" ), } + let policies = agent_policies(&roles); + let mut written = 0_usize; + for (name, document) in &policies { + match store.write_policy(name, document).await { + Ok(()) => written += 1, + Err(e) => { + tracing::warn!(policy = %name, error = %e, "rewriting this agent's policy failed"); + } + } + } + tracing::info!(written, agents = policies.len(), "agent policies rewritten"); crate::agent_entity::ensure_agents(&store, &policy::agents_from_role_names(&roles)).await; Ok(()) } +/// The policy to write for each agent among `roles`, a listing of cert-auth +/// roles: its name, which is the role's own, and its document. +/// +/// A role that is not an agent's — a hive's `hive-`, a host's — gets +/// nothing: the same `write_policy` call with an agent's document would +/// narrow that principal to one agent's subtree. +fn agent_policies(roles: &[String]) -> Vec<(String, String)> { + policy::agents_from_role_names(roles) + .iter() + .filter_map(|agent| { + Some(( + policy::agent_object_name(agent).ok()?, + policy::render_agent(agent).ok()?, + )) + }) + .collect() +} + #[cfg(test)] mod tests { use std::{cell::Cell, time::Duration}; - use crate::store::fake::FakeAccess; - use super::{ - RETRY_ATTEMPTS, RETRY_INTERVAL, RETRY_WINDOW, Unreachable, attached_policies, policy, - provision_with, retry_until_ok, + RETRY_ATTEMPTS, RETRY_INTERVAL, RETRY_WINDOW, Unreachable, agent_policies, + attached_policies, policy, retry_until_ok, }; #[test] @@ -395,95 +424,54 @@ mod tests { } #[test] - fn a_hives_role_attaches_the_shared_policy_and_none_named_for_the_hive() { - let attached = attached_policies(); - assert_eq!(attached, [policy::SHARED_HIVE_POLICY]); - let own = policy::hive_object_name("pr1ma").expect("legal"); - assert!( - !attached.contains(&own), - "the role must not attach a policy named for the hive" - ); - // The control: the same lookup finds a policy that is attached. - assert!(attached.contains(&policy::SHARED_HIVE_POLICY.to_owned())); - } - - fn hives() -> Vec { - ["pr1ma", "beta"].map(str::to_owned).to_vec() - } - - #[tokio::test] - async fn every_hives_role_is_written_after_the_shared_policy_and_attaches_it_alone() { - let store = FakeAccess::default(); - provision_with(&store, &hives(), || Ok(Some("HIVE-CA".to_owned()))) - .await - .unwrap_or_else(|_| panic!("a store that accepts everything")); + fn the_pass_writes_agents_policies_under_their_role_names_and_no_one_elses() { + let roles: Vec = [ + "hive-agent-atlas", + "hive-pr1ma", + "swarm-controller", + "hive-agent-argus", + ] + .map(str::to_owned) + .to_vec(); + let written = agent_policies(&roles); + let names: Vec<&str> = written.iter().map(|(name, _)| name.as_str()).collect(); + // The role names an agent's policy by its own name, so a policy written + // under any other name is one the role does not attach. + assert_eq!(names, ["hive-agent-atlas", "hive-agent-argus"]); assert_eq!( - store.roles_written(), - [ - "write_cert_role hive-pr1ma hive-hives", - "write_cert_role hive-beta hive-hives" - ] + written[0].1, + policy::render_agent("atlas").expect("legal"), + "the prefix is stripped before rendering, or the document names `agent-atlas`" ); - let calls = store.calls(); - let policy_at = calls - .iter() - .position(|c| c == "write_policy hive-hives") - .expect("the shared policy was written"); - let first_role_at = calls - .iter() - .position(|c| c.starts_with("write_cert_role ")) - .expect("a role was written"); - assert!(policy_at < first_role_at, "{calls:?}"); + assert_eq!(written[1].1, policy::render_agent("argus").expect("legal")); } - #[tokio::test] - async fn an_unreadable_accessor_leaves_every_hive_role_as_it_was() { - let store = FakeAccess::without_accessor(); - provision_with(&store, &hives(), || Ok(Some("HIVE-CA".to_owned()))) - .await - .unwrap_or_else(|_| panic!("a policy that cannot be written is not a retry")); - assert!(store.roles_written().is_empty(), "{:?}", store.calls()); - // The control: the accessor read was attempted, and the same fake - // writes roles when it works (the test above). - assert_eq!(store.calls(), ["auth_mount_accessor"]); - } - - #[tokio::test] - async fn a_refused_shared_policy_leaves_every_hive_role_as_it_was() { - let store = FakeAccess::refusing_policies(); - provision_with(&store, &hives(), || Ok(Some("HIVE-CA".to_owned()))) - .await - .unwrap_or_else(|_| panic!("a refused write is not a retry")); - assert!(store.roles_written().is_empty(), "{:?}", store.calls()); - assert!( - store - .calls() - .contains(&"write_policy hive-hives".to_owned()) + #[test] + fn a_hives_role_attaches_its_own_policy_and_the_shared_one() { + assert_eq!( + attached_policies("pr1ma", true).expect("legal"), + ["hive-pr1ma", policy::SHARED_HIVE_POLICY] ); } - #[tokio::test] - async fn without_an_authority_the_policy_is_written_and_no_role_is() { - let store = FakeAccess::default(); - provision_with(&store, &hives(), || Ok(None)) - .await - .unwrap_or_else(|_| panic!("no authority configured is not a retry")); - assert!(store.roles_written().is_empty(), "{:?}", store.calls()); - assert!( - store - .calls() - .contains(&"write_policy hive-hives".to_owned()) + #[test] + fn without_the_shared_policy_the_role_keeps_the_hives_own() { + assert_eq!( + attached_policies("pr1ma", false).expect("legal"), + ["hive-pr1ma"] ); } - #[tokio::test] - async fn an_authority_that_cannot_be_read_is_retried_and_writes_no_role() { - let store = FakeAccess::default(); - let outcome = provision_with(&store, &hives(), || { - Err(Unreachable("not placed yet".to_owned())) - }) - .await; - assert!(outcome.is_err()); - assert!(store.roles_written().is_empty(), "{:?}", store.calls()); + #[test] + fn the_shared_text_serves_every_hive_what_its_own_policy_does() { + let shared = policy::render_shared_hives("auth_cert_0a1b2c3d").expect("legal accessor"); + let template = "{{identity.entity.aliases.auth_cert_0a1b2c3d.name}}"; + for hive in ["pr1ma", "beta", "gamma-2", "delta_3"] { + assert_eq!( + shared.replace(template, hive), + policy::render(hive).expect("legal"), + "{hive}" + ); + } } } diff --git a/swarm-controller/src/store.rs b/swarm-controller/src/store.rs index eff037d21..6d94a4005 100644 --- a/swarm-controller/src/store.rs +++ b/swarm-controller/src/store.rs @@ -3,12 +3,8 @@ //! Two paths log in: writing an agent's credential, and writing the read //! grant that lets a hive fetch one back. Both present the same certificate //! under the same role, so the role is named here rather than at each caller. -//! [`AccessStore`] is the subset of calls that give a principal its access, so -//! a test can stand in for the store. -use std::future::Future; - -use swarm_secret_client::{Error, SecretStore, client::EntityRef}; +use swarm_secret_client::{Error, SecretStore}; /// The cert-auth role the controller logs in under. /// @@ -27,256 +23,3 @@ pub const CERT_ROLE: &str = "swarm-controller"; pub async fn connect() -> Result { SecretStore::from_env(CERT_ROLE).await } - -/// The store calls that give a principal its access: policies, cert-auth roles -/// and identity entities. A test stands in for the store through this trait. -pub(crate) trait AccessStore { - /// As [`SecretStore::write_policy`]. - fn write_policy( - &self, - name: &str, - policy: &str, - ) -> impl Future> + Send; - - /// As [`SecretStore::write_cert_role`]. - fn write_cert_role( - &self, - mount: &str, - name: &str, - certificate: &str, - common_name: &str, - policies: &[&str], - ) -> impl Future> + Send; - - /// As [`SecretStore::auth_mount_accessor`]. - fn auth_mount_accessor( - &self, - mount: &str, - ) -> impl Future> + Send; - - /// As [`SecretStore::entity_by_alias`]. - fn entity_by_alias( - &self, - alias_name: &str, - mount_accessor: &str, - ) -> impl Future, Error>> + Send; - - /// As [`SecretStore::entity_id_by_name`]. - fn entity_id_by_name( - &self, - name: &str, - ) -> impl Future, Error>> + Send; - - /// As [`SecretStore::create_entity`]. - fn create_entity(&self, name: &str) -> impl Future> + Send; - - /// As [`SecretStore::rename_entity`]. - fn rename_entity(&self, id: &str, name: &str) - -> impl Future> + Send; - - /// As [`SecretStore::create_entity_alias`]. - fn create_entity_alias( - &self, - alias_name: &str, - mount_accessor: &str, - entity_id: &str, - ) -> impl Future> + Send; -} - -impl AccessStore for SecretStore { - async fn write_policy(&self, name: &str, policy: &str) -> Result<(), Error> { - SecretStore::write_policy(self, name, policy).await - } - - async fn write_cert_role( - &self, - mount: &str, - name: &str, - certificate: &str, - common_name: &str, - policies: &[&str], - ) -> Result<(), Error> { - SecretStore::write_cert_role(self, mount, name, certificate, common_name, policies).await - } - - async fn auth_mount_accessor(&self, mount: &str) -> Result { - SecretStore::auth_mount_accessor(self, mount).await - } - - async fn entity_by_alias( - &self, - alias_name: &str, - mount_accessor: &str, - ) -> Result, Error> { - SecretStore::entity_by_alias(self, alias_name, mount_accessor).await - } - - async fn entity_id_by_name(&self, name: &str) -> Result, Error> { - SecretStore::entity_id_by_name(self, name).await - } - - async fn create_entity(&self, name: &str) -> Result<(), Error> { - SecretStore::create_entity(self, name).await - } - - async fn rename_entity(&self, id: &str, name: &str) -> Result<(), Error> { - SecretStore::rename_entity(self, id, name).await - } - - async fn create_entity_alias( - &self, - alias_name: &str, - mount_accessor: &str, - entity_id: &str, - ) -> Result<(), Error> { - SecretStore::create_entity_alias(self, alias_name, mount_accessor, entity_id).await - } -} - -/// A store that records what it is asked to do, for tests of the order and the -/// outcome of the access calls. -#[cfg(test)] -pub(crate) mod fake { - use std::sync::Mutex; - - use swarm_secret_client::{Error, client::EntityRef}; - - use super::AccessStore; - - /// What the fake answers; the default is a store with a readable cert mount - /// and no entities, which accepts every write. - #[derive(Default)] - pub(crate) struct FakeAccess { - /// The accessor read fails. - no_accessor: bool, - /// Every policy write is refused. - refuse_policy: bool, - /// The entity that owns each alias, as `(id, name)`. - alias_owner: Option<(String, String)>, - entities: Mutex>, - calls: Mutex>, - } - - impl FakeAccess { - /// A store whose cert mount accessor cannot be read. - pub(crate) fn without_accessor() -> Self { - Self { - no_accessor: true, - ..Self::default() - } - } - - /// A store that refuses every policy write. - pub(crate) fn refusing_policies() -> Self { - Self { - refuse_policy: true, - ..Self::default() - } - } - - /// A store where the agent alias is already on the entity `name`. - pub(crate) fn with_alias_on(id: &str, name: &str) -> Self { - Self { - alias_owner: Some((id.to_owned(), name.to_owned())), - ..Self::default() - } - } - - /// The calls so far, as `verb name` strings, in order. - pub(crate) fn calls(&self) -> Vec { - self.calls.lock().expect("not poisoned").clone() - } - - /// The `write_cert_role` calls so far, as `write_cert_role `. - pub(crate) fn roles_written(&self) -> Vec { - self.calls() - .into_iter() - .filter(|c| c.starts_with("write_cert_role ")) - .collect() - } - - fn log(&self, call: String) { - self.calls.lock().expect("not poisoned").push(call); - } - - fn denied() -> Error { - Error::Settings("fake store: refused".to_owned()) - } - } - - impl AccessStore for FakeAccess { - async fn write_policy(&self, name: &str, _policy: &str) -> Result<(), Error> { - self.log(format!("write_policy {name}")); - if self.refuse_policy { - return Err(Self::denied()); - } - Ok(()) - } - - async fn write_cert_role( - &self, - _mount: &str, - name: &str, - _certificate: &str, - _common_name: &str, - policies: &[&str], - ) -> Result<(), Error> { - self.log(format!("write_cert_role {name} {}", policies.join(","))); - Ok(()) - } - - async fn auth_mount_accessor(&self, _mount: &str) -> Result { - self.log("auth_mount_accessor".to_owned()); - if self.no_accessor { - return Err(Self::denied()); - } - Ok("auth_cert_test".to_owned()) - } - - async fn entity_by_alias( - &self, - alias_name: &str, - _mount_accessor: &str, - ) -> Result, Error> { - self.log(format!("entity_by_alias {alias_name}")); - Ok(self.alias_owner.as_ref().map(|(id, name)| EntityRef { - id: id.clone(), - name: name.clone(), - })) - } - - async fn entity_id_by_name(&self, name: &str) -> Result, Error> { - self.log(format!("entity_id_by_name {name}")); - let named = self - .entities - .lock() - .expect("not poisoned") - .contains(&name.to_owned()); - Ok(named.then(|| format!("id-{name}"))) - } - - async fn create_entity(&self, name: &str) -> Result<(), Error> { - self.log(format!("create_entity {name}")); - self.entities - .lock() - .expect("not poisoned") - .push(name.to_owned()); - Ok(()) - } - - async fn rename_entity(&self, id: &str, name: &str) -> Result<(), Error> { - self.log(format!("rename_entity {id} {name}")); - Ok(()) - } - - async fn create_entity_alias( - &self, - alias_name: &str, - _mount_accessor: &str, - _entity_id: &str, - ) -> Result<(), Error> { - self.log(format!("create_entity_alias {alias_name}")); - Ok(()) - } - } -} diff --git a/swarm-secret-client/src/acp.rs b/swarm-secret-client/src/acp.rs index 49c23d363..065325bd9 100644 --- a/swarm-secret-client/src/acp.rs +++ b/swarm-secret-client/src/acp.rs @@ -14,7 +14,7 @@ use crate::{ /// The path holding `agent`'s ACP provider API key. /// /// A flat leaf under the agent's prefix, like [`crate::forge::agent_token_path`], -/// so the agent's own read stanza ([`crate::policy::render_shared_agent`]) already +/// so the agent's own read stanza ([`crate::policy::render_agent`]) already /// covers it. /// /// # Errors diff --git a/swarm-secret-client/src/claude_login.rs b/swarm-secret-client/src/claude_login.rs index 3cfcda4b6..782822fab 100644 --- a/swarm-secret-client/src/claude_login.rs +++ b/swarm-secret-client/src/claude_login.rs @@ -18,7 +18,7 @@ use crate::{ /// /// A flat leaf under the agent's prefix, like /// [`crate::github::account_path`], so the agent's own read stanza -/// ([`crate::policy::render_shared_agent`]) already covers it. +/// ([`crate::policy::render_agent`]) already covers it. /// /// # Errors /// [`Error::PathSegment`] when `agent` contains anything but `[A-Za-z0-9_-]`, @@ -88,12 +88,12 @@ mod tests { #[test] fn the_agents_own_read_grant_covers_the_path() { let path = credentials_path("atlas").expect("legal"); - let own = crate::policy::effective_agent("atlas"); + let own = crate::policy::render_agent("atlas").expect("legal"); assert!( covered_by(&own, &path), "no stanza in\n{own}\ncovers {path}" ); - let other = crate::policy::effective_agent("argus"); + let other = crate::policy::render_agent("argus").expect("legal"); assert!(!covered_by(&other, &path), "argus's policy reaches {path}"); } diff --git a/swarm-secret-client/src/client.rs b/swarm-secret-client/src/client.rs index 5fb4592d7..829b4b561 100644 --- a/swarm-secret-client/src/client.rs +++ b/swarm-secret-client/src/client.rs @@ -780,7 +780,7 @@ mod tests { let request = WriteAclPolicy { name: "hive-pr1ma".to_owned(), - policy: crate::policy::effective_hive("pr1ma"), + policy: crate::policy::render("pr1ma").expect("a plain name is legal"), }; assert_eq!(request.path(), "sys/policies/acl/hive-pr1ma"); } @@ -791,7 +791,7 @@ mod tests { let request = WriteAclPolicy { name: "hive-pr1ma".to_owned(), - policy: crate::policy::effective_hive("pr1ma"), + policy: crate::policy::render("pr1ma").expect("a plain name is legal"), }; let body = request .body() @@ -799,7 +799,10 @@ mod tests { .expect("a policy write sends one"); let sent: serde_json::Value = serde_json::from_slice(&body).expect("the body is the JSON the store parses"); - assert_eq!(sent["policy"], crate::policy::effective_hive("pr1ma")); + assert_eq!( + sent["policy"], + crate::policy::render("pr1ma").expect("legal") + ); assert!( sent.get("name").is_none(), "`name` addresses the policy in the path; sending it too would make \ diff --git a/swarm-secret-client/src/forge.rs b/swarm-secret-client/src/forge.rs index 438a9333e..f7656f43b 100644 --- a/swarm-secret-client/src/forge.rs +++ b/swarm-secret-client/src/forge.rs @@ -28,7 +28,7 @@ use crate::{ /// /// A flat leaf under the agent's prefix, like its controller-minted siblings /// [`crate::queue::agent_queue_path`] and [`crate::mtls::identity_path`], so -/// the agent's own read stanza ([`crate::policy::render_shared_agent`]) +/// the agent's own read stanza ([`crate::policy::render_agent`]) /// already covers it. /// /// # Errors @@ -190,7 +190,7 @@ mod tests { #[test] fn no_hive_policy_reaches_the_controller_token() { - let policy = crate::policy::effective_hive("alpha"); + let policy = crate::policy::render("alpha").expect("legal"); let covers = |path: &str| { policy.lines().any(|line| { line.strip_prefix("path \"") @@ -232,7 +232,7 @@ mod tests { #[test] fn the_agents_own_grants_list_its_accounts_and_read_each() { - let policy = crate::policy::effective_agent("atlas"); + let policy = crate::policy::render_agent("atlas").expect("legal"); let covers = |path: &str| { policy.lines().any(|line| { line.strip_prefix("path \"") @@ -309,7 +309,7 @@ mod tests { // policy is rendered elsewhere. If the path ever moved out from under // it, the agent's fetch would 403 at boot, naming neither. let path = agent_token_path("atlas").expect("legal"); - let policy = crate::policy::effective_agent("atlas"); + let policy = crate::policy::render_agent("atlas").expect("legal"); let covered = policy.lines().any(|line| { line.strip_prefix("path \"") .and_then(|rest| rest.split_once("\" {")) @@ -324,7 +324,7 @@ mod tests { // Control for the test above: the prefix match must actually be // discriminating, or it proves nothing. let path = agent_token_path("atlas").expect("legal"); - let policy = crate::policy::effective_agent("argus"); + let policy = crate::policy::render_agent("argus").expect("legal"); let covered = policy.lines().any(|line| { line.strip_prefix("path \"") .and_then(|rest| rest.split_once("\" {")) @@ -357,12 +357,15 @@ mod tests { #[test] fn a_hive_reaches_its_own_forge_token_and_no_other_hives() { let covered_by = |hive: &str, path: &str| { - crate::policy::effective_hive(hive).lines().any(|line| { - line.strip_prefix("path \"") - .and_then(|rest| rest.split_once("\" {")) - .and_then(|(p, _)| p.strip_suffix('*')) - .is_some_and(|prefix| format!("secret/data/{path}").starts_with(prefix)) - }) + crate::policy::render(hive) + .expect("legal") + .lines() + .any(|line| { + line.strip_prefix("path \"") + .and_then(|rest| rest.split_once("\" {")) + .and_then(|(p, _)| p.strip_suffix('*')) + .is_some_and(|prefix| format!("secret/data/{path}").starts_with(prefix)) + }) }; let alpha = hive_token_path("alpha").expect("legal"); assert!(covered_by("alpha", &alpha), "alpha cannot read {alpha}"); diff --git a/swarm-secret-client/src/github.rs b/swarm-secret-client/src/github.rs index 885c4998c..0bd920169 100644 --- a/swarm-secret-client/src/github.rs +++ b/swarm-secret-client/src/github.rs @@ -18,7 +18,7 @@ use crate::{ /// /// A flat leaf under the agent's prefix, like /// [`crate::forge::agent_token_path`], so the agent's own read stanza -/// ([`crate::policy::render_shared_agent`]) already covers it. +/// ([`crate::policy::render_agent`]) already covers it. /// /// # Errors /// [`Error::PathSegment`] when `agent` contains anything but `[A-Za-z0-9_-]`, @@ -84,13 +84,13 @@ mod tests { #[test] fn the_agents_own_read_grant_covers_the_path() { let path = account_path("atlas").expect("legal"); - let own = crate::policy::effective_agent("atlas"); + let own = crate::policy::render_agent("atlas").expect("legal"); assert!( covered_by(&own, &path), "no stanza in\n{own}\ncovers {path}" ); // The control: another agent's grant does not reach it. - let other = crate::policy::effective_agent("argus"); + let other = crate::policy::render_agent("argus").expect("legal"); assert!(!covered_by(&other, &path), "argus's policy reaches {path}"); } diff --git a/swarm-secret-client/src/matrix.rs b/swarm-secret-client/src/matrix.rs index e02de8dc7..5eebcaba7 100644 --- a/swarm-secret-client/src/matrix.rs +++ b/swarm-secret-client/src/matrix.rs @@ -118,7 +118,7 @@ pub(crate) const CONTROLLER: &str = "swarm-controller"; /// /// The matrix container mints it and publishes it here; `swarm-controller` /// reads it to create agents' accounts. Under [`Kind::Controller`] because -/// [`crate::policy::render_shared_hives`] grants no hive that kind: every hive reads +/// [`crate::policy::render`] grants no hive that kind: every hive reads /// `agents/*` and its own `hives//*`, and nothing else. /// /// # Errors @@ -186,7 +186,7 @@ mod tests { fn the_agent_reads_its_own_recovery_passphrase_and_no_other() { // The daemon reads this under the agent's own policy, so the rendered // document is what has to cover it. - let doc = crate::policy::effective_agent("atlas"); + let doc = crate::policy::render_agent("atlas").expect("a plain name is legal"); let granted: Vec<&str> = doc .lines() .filter_map(|l| l.strip_prefix("path \"secret/data/")) @@ -272,7 +272,7 @@ mod tests { // be under any stanza a hive's policy renders. Checked against the // rendered document rather than against the prefix, because the // document is what the store enforces. - let doc = crate::policy::effective_hive("pr1ma"); + let doc = crate::policy::render("pr1ma").expect("a plain name is legal"); // Every stanza is `path "secret/data/*" { … }`. let granted: Vec<&str> = doc .lines() diff --git a/swarm-secret-client/src/mtls.rs b/swarm-secret-client/src/mtls.rs index f60135822..062692acd 100644 --- a/swarm-secret-client/src/mtls.rs +++ b/swarm-secret-client/src/mtls.rs @@ -106,12 +106,12 @@ mod tests { } /// The path sits under the same prefix an agent's policy grants - /// (`policy::render_shared_agent`'s stanza is `swarm/agents//*`), which is + /// (`policy::render_agent`'s stanza is `swarm/agents//*`), which is /// what makes an agent able to read its own identity back. #[test] fn an_agents_identity_is_inside_its_own_policy_stanza() { let path = identity_path("atlas").expect("legal"); - let document = crate::policy::effective_agent("atlas"); + let document = crate::policy::render_agent("atlas").expect("legal"); let prefix = format!("{}/data/swarm/agents/atlas/", crate::path::MOUNT); assert!( path.starts_with("swarm/agents/atlas/"), diff --git a/swarm-secret-client/src/policy.rs b/swarm-secret-client/src/policy.rs index 232e5b496..d3a99688e 100644 --- a/swarm-secret-client/src/policy.rs +++ b/swarm-secret-client/src/policy.rs @@ -5,9 +5,8 @@ //! two have to agree on the same path or a delivery fails with a 403 that names //! nothing. //! -//! The hive document ([`render_shared_hives`]) has one stanza per kind a hive -//! reads, and they are not all scoped alike — which is the point rather than -//! an inconsistency: +//! The document has one stanza per kind a hive reads, and they are not all +//! scoped alike — which is the point rather than an inconsistency: //! //! ⚠️ The **agent** stanza grants read on *every* agent's credentials rather //! than on the ones that hive hosts. That is a decision, not an oversight: an @@ -34,14 +33,15 @@ use crate::{ path::{Kind, MOUNT, ROOT, checked_segment}, }; -/// Namespace for a hive's cert-auth role and the policies the controller writes. +/// Namespace for a hive's own policy and cert-auth role. /// /// The controller's own grant is scoped to `hive-*` for both, so this prefix is /// the difference between a hive the controller may provision and a policy it /// must not be able to rewrite — including its own. pub const HIVE_PREFIX: &str = "hive-"; -/// The cert-auth role name for `hive`. +/// The policy and cert-auth role name for `hive`. One name, both objects: the +/// role attaches the policy by spelling it identically. /// /// # Errors /// [`Error::PathSegment`] when `hive` holds anything but `[A-Za-z0-9_-]`. @@ -50,7 +50,7 @@ pub fn hive_object_name(hive: &str) -> Result { Ok(format!("{HIVE_PREFIX}{hive}")) } -/// Namespace for an agent's cert-auth role and certificate common name. +/// Namespace for an agent's own policy and cert-auth role. /// /// Deliberately *inside* [`HIVE_PREFIX`] rather than beside it. /// `sys/policies/acl/hive-*` and `auth/cert/certs/hive-*` are the whole of what @@ -59,7 +59,7 @@ pub fn hive_object_name(hive: &str) -> Result { /// learns an agent exists — a name outside that prefix is one nothing can /// write. It costs no authority: the controller already holds `create`/`update` /// on `secret/data/swarm/agents/*`, so it can already replace every credential -/// the shared policies grant read on. +/// this policy grants read on. /// /// It still cannot collide with a hive's. [`hive_object_name`] renders /// `hive-`, so a collision needs a hive named `agent-` — and @@ -75,9 +75,9 @@ pub fn hive_object_name(hive: &str) -> Result { /// agent leaf. A CN match alone logs nobody in. pub const AGENT_PREFIX: &str = "hive-agent-"; -/// The cert-auth role name for `agent`, and the common name of the certificate -/// that authenticates it. One string, two objects: the role matches the subject -/// by spelling both the same. +/// The policy and cert-auth role name for `agent`, and the common name of the +/// certificate that authenticates it. One string, three objects: the role +/// attaches the policy and matches the subject by spelling all three the same. /// /// Injective in `agent` — the prefix is fixed and [`checked_segment`] has /// already refused anything that could re-punctuate the suffix. Agent names @@ -124,43 +124,39 @@ fn list_stanza(path: &str) -> String { format!("path \"{path}\" {{\n capabilities = [\"list\"]\n}}\n") } -/// The one policy every hive's cert-auth role attaches. +/// Render `hive`'s policy document: read on every agent's credentials and on +/// this hive's own. /// -/// Inside [`HIVE_PREFIX`] so that the controller may write it. It is the object -/// name of a hive called `hives`, and `nix/reserved-hive-fragments.nix` forbids -/// the substring `hive` in every hive name, so no hive spells it. -pub const SHARED_HIVE_POLICY: &str = "hive-hives"; - -/// Render the policy [`SHARED_HIVE_POLICY`] holds: read on every agent's -/// credentials and on this hive's own, with the hive's name taken from the alias -/// of its certificate login on the cert auth mount `mount_accessor`. -/// -/// A hive's certificate carries its bare name as the common name, and that is -/// the alias name, so the own-path stanza reads `swarm/hives//` for -/// whichever hive presents the token. A login whose alias is not a hive's name -/// reads a path where nothing is stored. The `agents/` and `hives/` segments -/// are fixed text, never taken from the identity. -/// -/// The controller kind and the `swarm/services/` tree are deliberately absent: -/// a hive has no business reading the credentials of the thing that provisions -/// it, and a swarm service's OIDC client secret is read under a certificate that -/// is not a hive's. Adding either is a boundary decision, not a consequence of -/// the namespace growing. -/// -/// The hive's *own* kind is granted, and only that one: a hive holds the queue -/// credential its own agents authenticate with, so it reads the one principal -/// named after itself, which is why the path takes the name from the alias -/// instead of widening to the whole kind. +/// The name is the only input, and it is deploy-time — so the document is +/// still a deploy-time object rather than derived state with a re-emission to +/// get wrong. Nothing about which agents exist changes the text. /// /// Read-only: the controller mints these and never reads one back. /// +/// ⚠️ The controller kind and the `swarm/services/` tree are deliberately +/// absent: a hive has no business reading the credentials of the thing that +/// provisions it, and a swarm service's OIDC client secret is read under a +/// certificate that is not a hive's. Adding either is a boundary decision, +/// not a consequence of the namespace growing. +/// +/// The hive's *own* kind is granted, and that is the decision the agent-only +/// version of this grant said had to be made rather than assumed: a hive holds +/// the queue credential its own agents authenticate with, so it has to read the +/// one principal named after itself — and only that one, which is why the path +/// interpolates the name instead of widening to the whole kind. +/// /// # Errors -/// [`Error::PathSegment`] when `mount_accessor` holds anything but -/// `[A-Za-z0-9_-]`: it is interpolated into the policy text, where it could -/// close the stanza. -pub fn render_shared_hives(mount_accessor: &str) -> Result { - checked_segment("mount accessor", mount_accessor)?; - let own = format!("{{{{identity.entity.aliases.{mount_accessor}.name}}}}"); +/// [`Error::PathSegment`] when `hive` holds anything but `[A-Za-z0-9_-]` — the +/// name is interpolated into a policy path, so a name that could close the +/// stanza could grant itself anything. +pub fn render(hive: &str) -> Result { + checked_segment("hive", hive)?; + Ok(hive_document(hive)) +} + +/// The text of [`render`] with `own` as the hive segment: a checked hive name, +/// or the template [`render_shared_hives`] puts there. +fn hive_document(own: &str) -> String { let agents = read_stanza(&format!( "{MOUNT}/data/{ROOT}/{}/*", <&str>::from(Kind::Agent) @@ -169,10 +165,76 @@ pub fn render_shared_hives(mount_accessor: &str) -> Result { "{MOUNT}/data/{ROOT}/{}/{own}/*", <&str>::from(Kind::Hive) )); - Ok(format!("{agents}{own}")) + format!("{agents}{own}") } -/// The one policy every agent's cert-auth role attaches. +/// The one policy every hive's cert-auth role attaches next to its own. +/// +/// Inside [`HIVE_PREFIX`] so that the controller may write it. It is the object +/// name of a hive called `hives`, and `nix/reserved-hive-fragments.nix` forbids +/// the substring `hive` in every hive name, so no hive spells it. +pub const SHARED_HIVE_POLICY: &str = "hive-hives"; + +/// Render the policy [`SHARED_HIVE_POLICY`] holds: the text of [`render`], with +/// the hive's name taken from the alias of its certificate login on the cert +/// auth mount `mount_accessor`. +/// +/// A hive's certificate carries its bare name as the common name, and that is +/// the alias name, so the own-path stanza reads `swarm/hives//` for +/// whichever hive presents the token. A login whose alias is not a hive's name +/// reads a path where nothing is stored. The `agents/` and `hives/` segments +/// are fixed text, never taken from the identity. +/// +/// # Errors +/// [`Error::PathSegment`] when `mount_accessor` holds anything but +/// `[A-Za-z0-9_-]`: it is interpolated into the policy text, where it could +/// close the stanza. +pub fn render_shared_hives(mount_accessor: &str) -> Result { + checked_segment("mount accessor", mount_accessor)?; + Ok(hive_document(&format!( + "{{{{identity.entity.aliases.{mount_accessor}.name}}}}" + ))) +} + +/// Render `agent`'s policy document: read on that one agent's credentials, list +/// on their names (how `hive-matrix-daemon` finds its linked accounts), and +/// nothing else at all. +/// +/// Two stanzas over one subtree, and the single interpolated name is the whole +/// document: an agent authenticating with its own certificate gets a token that +/// fetches `swarm/agents//…`, lists the keys under it, and is refused +/// every other path in the store. That is the point — the reason to give an +/// agent an identity is that it then depends on its hive for one file (the +/// certificate) rather than for every credential it uses. +/// +/// ⚠️ **None of [`render`]'s breadth is inherited.** A hive's document grants +/// read on `swarm/agents/*` — *every* agent's credentials, not the ones that +/// hive hosts; the module header says why that is a decision rather than an +/// oversight. The reason does not transfer: an agent's path names the agent, +/// so scoping costs nothing and drifts nowhere (the argument that keeps +/// [`render`]'s hive stanza narrow), and an agent has no business with another +/// agent's credentials, a hive's, a service's, or the controller's. Narrow +/// here does **not** narrow the hive's: a hive still reads this agent's +/// secrets, and closing that is its own decision. +/// Nothing here writes, for the reason the hive's does not: an agent that could +/// write its own credentials could hand itself an identity it was never issued. +/// +/// ⚠️ Not deploy-time: hive-c0re creates agents at runtime (`hive_c0re::meta`). +/// swarm-controller rewrites every agent's document at its own start, so an +/// edit here reaches existing agents with the next controller restart. +/// +/// # Errors +/// [`Error::PathSegment`] when `agent` holds anything but `[A-Za-z0-9_-]`: the +/// name is interpolated into the policy text, where it could close the stanza. +pub fn render_agent(agent: &str) -> Result { + checked_segment("agent", agent)?; + let kind = <&str>::from(Kind::Agent); + let read = read_stanza(&format!("{MOUNT}/data/{ROOT}/{kind}/{agent}/*")); + let list = list_stanza(&format!("{MOUNT}/metadata/{ROOT}/{kind}/{agent}/*")); + Ok(format!("{read}{list}")) +} + +/// The one policy every agent's cert-auth role attaches next to its own. /// /// Inside [`HIVE_PREFIX`] so that the controller may write it, and without an /// agent's suffix so that it is never an agent's object name: @@ -186,21 +248,14 @@ pub const SHARED_AGENT_POLICY: &str = "hive-agent"; /// agent's name. const ENTITY_NAME_TEMPLATE: &str = "{{identity.entity.name}}"; -/// Render the policy [`SHARED_AGENT_POLICY`] holds: read on one agent's -/// credentials and list on their names (how `hive-matrix-daemon` finds its -/// linked accounts), with the agent's name taken from the identity entity of -/// whoever presents the token. +/// Render the policy [`SHARED_AGENT_POLICY`] holds: the stanzas of +/// [`render_agent`], with the agent's name taken from the identity entity of +/// whoever presents the token instead of written into the text. /// /// The text is the same for every agent, so a change to the rule is one write. /// An entity whose name is not an agent's grants nothing: the controller names /// each agent's entity after the agent, and a login that no entity of that /// name owns reads `swarm/agents//`, where nothing is stored. -/// -/// Nothing here writes: an agent that could write its own credentials could -/// hand itself an identity it was never issued. An agent has no business with -/// another agent's credentials, a hive's, a service's, or the controller's, so -/// none of [`render_shared_hives`]'s breadth applies. A hive still reads this -/// agent's secrets, and closing that is its own decision. #[must_use] pub fn render_shared_agent() -> String { let kind = <&str>::from(Kind::Agent); @@ -213,36 +268,23 @@ pub fn render_shared_agent() -> String { format!("{read}{list}") } -/// What an agent's token grants under [`SHARED_AGENT_POLICY`]: the document -/// with the store's template filled in for `agent`. The oracle tests use to ask -/// whether the policy covers a path. -#[cfg(test)] -pub(crate) fn effective_agent(agent: &str) -> String { - checked_segment("agent", agent).expect("a legal agent name"); - render_shared_agent().replace(ENTITY_NAME_TEMPLATE, agent) -} - -/// What a hive's token grants under [`SHARED_HIVE_POLICY`]: the document with -/// the store's template filled in for `hive`. -#[cfg(test)] -pub(crate) fn effective_hive(hive: &str) -> String { - checked_segment("hive", hive).expect("a legal hive name"); - render_shared_hives("auth_cert_test") - .expect("a legal accessor") - .replace("{{identity.entity.aliases.auth_cert_test.name}}", hive) -} - #[cfg(test)] mod tests { use super::*; #[test] - fn the_shared_agent_document_reads_the_entity_name_where_the_agent_goes() { + fn the_shared_document_is_the_per_agent_one_with_the_entity_name_for_the_agent() { + let shared = render_shared_agent(); assert_eq!( - render_shared_agent(), + shared, "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" ); + // Same shape as the per-agent document, so the rule is one rule. + assert_eq!( + shared.replace("{{identity.entity.name}}", "atlas"), + render_agent("atlas").expect("legal") + ); } #[test] @@ -272,26 +314,28 @@ mod tests { #[test] fn the_document_grants_every_agent_and_this_hive_alone() { assert_eq!( - effective_hive("pr1ma"), + render("pr1ma").expect("a plain name is legal"), "path \"secret/data/swarm/agents/*\" {\n capabilities = [\"read\"]\n}\n\ path \"secret/data/swarm/hives/pr1ma/*\" {\n capabilities = [\"read\"]\n}\n" ); } #[test] - fn the_shared_hive_document_takes_the_hive_from_the_alias_name() { + fn the_shared_hive_document_is_each_hives_own_with_the_alias_name_for_the_hive() { let shared = render_shared_hives("auth_cert_1a2b3c4d").expect("legal accessor"); assert_eq!( shared, "path \"secret/data/swarm/agents/*\" {\n capabilities = [\"read\"]\n}\n\ path \"secret/data/swarm/hives/{{identity.entity.aliases.auth_cert_1a2b3c4d.name}}/*\" {\n capabilities = [\"read\"]\n}\n" ); - // The template fills in for hives that differ in length, case and - // punctuation, and each then reads exactly its own path. + // Same text as every hive's own document once the template is filled + // in, for hives that differ in length, case and punctuation. for hive in ["pr1ma", "a", "Mixed_Case-1", "x".repeat(63).as_str()] { - let own = effective_hive(hive); - assert!(own.contains(&format!("swarm/hives/{hive}/*")), "{own}"); - assert!(!own.contains("{{"), "{own}"); + assert_eq!( + shared.replace("{{identity.entity.aliases.auth_cert_1a2b3c4d.name}}", hive), + render(hive).expect("legal"), + "{hive}" + ); } } @@ -343,7 +387,7 @@ mod tests { // A swarm service's OIDC client secret is read under a certificate that // is not a hive's. Checked by walking the stanzas, so a wider grant // that happened to cover the tree (`swarm/*`, say) fails too. - let p = effective_hive("pr1ma"); + let p = render("pr1ma").expect("legal"); assert!(!p.contains("swarm/services"), "{p}"); let service = "swarm/services/swarm-grafana/oidc/client"; assert!( @@ -364,7 +408,7 @@ mod tests { /// means something if the stanza that reaches it is too. #[test] fn a_hive_reaches_its_own_matrix_sender_token_and_no_other_hives() { - let alpha = effective_hive("alpha"); + let alpha = render("alpha").expect("legal"); let own = crate::matrix::sender_token_path("alpha").expect("legal"); let other = crate::matrix::sender_token_path("beta").expect("legal"); @@ -412,7 +456,7 @@ mod tests { fn the_grant_is_read_only() { // A hive reads credentials; a hive that could write one could hand // itself an agent's identity. - let p = effective_hive("pr1ma"); + let p = render("pr1ma").expect("legal"); assert!(!p.contains("create")); assert!(!p.contains("update")); assert!(!p.contains("delete")); @@ -421,16 +465,32 @@ mod tests { #[test] fn one_hives_document_does_not_reach_another_hives_path() { - let a = effective_hive("alpha"); + // Replaces `every_hive_gets_a_byte_identical_document`: the hive stanza + // is per-reader now, so identical text is no longer the property. This + // is what that test was protecting — that a document says only what the + // deploy-time name puts in it. + let a = render("alpha").expect("legal"); assert!(a.contains("swarm/hives/alpha/*")); assert!(!a.contains("beta")); assert!(!a.contains("swarm/hives/*"), "the hive stanza stays narrow"); } + #[test] + fn the_same_name_still_renders_byte_identically() { + // The half of the old property that survives: the text is a function of + // the deploy-time name alone, so a re-emission cannot drift. + assert_eq!( + render("pr1ma").expect("legal"), + render("pr1ma").expect("legal") + ); + } + #[test] fn a_name_that_could_close_the_stanza_is_refused() { - // The name reaches a role name and a certificate subject, so a name that - // could re-punctuate either is refused. + // The name reaches the document now, which it did not before — so the + // injection case is live again in the policy TEXT, not just in the + // policy's identifier. + assert!(render("alpha/*\" { capabilities = [\"root\"] }").is_err()); assert!( hive_object_name("atlas/*\" { capabilities = [\"root\"] }").is_err(), "the object NAME is a place a name can do damage too" @@ -442,6 +502,7 @@ mod tests { // The control for the case above: if every name were refused, that // assertion would pass while proving nothing. assert!(hive_object_name("a-b_C9").is_ok()); + assert!(render("a-b_C9").is_ok()); } #[test] @@ -454,13 +515,16 @@ mod tests { assert!(n.starts_with(HIVE_PREFIX)); } - /// What `effective_agent("atlas")` must be, byte for byte. + /// What `render_agent("atlas")` must render, 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"; #[test] fn an_agents_document_is_two_stanzas_naming_that_agent() { - assert_eq!(effective_agent("atlas"), ATLAS_DOCUMENT); + assert_eq!( + render_agent("atlas").expect("a plain name is legal"), + ATLAS_DOCUMENT + ); } #[test] @@ -468,7 +532,7 @@ mod tests { // `render`'s agent stanza is `agents/*` on purpose, and inheriting one // character of that here would give every agent every other agent's // credentials while the document still read as per-agent. - let p = effective_agent("atlas"); + let p = render_agent("atlas").expect("legal"); assert!( !p.contains("swarm/agents/*"), "the agent stanza must not widen to the kind: {p}" @@ -482,7 +546,7 @@ mod tests { // Stated as an exhaustive check over the kinds rather than as a list of // paths, so a kind added to `path::Kind` cannot be granted here by a // renderer nobody re-read. - let p = effective_agent("atlas"); + let p = render_agent("atlas").expect("legal"); for kind in Kind::ALL { let segment = <&str>::from(kind); let expected = kind == Kind::Agent; @@ -504,9 +568,13 @@ mod tests { fn an_agents_document_is_exactly_its_own_two_stanzas() { // Pinned byte for byte: an added stanza (a hive's queue credential, a // second agent) is exactly what a presence check misses. - assert_eq!(effective_agent("atlas"), ATLAS_DOCUMENT); + assert_eq!(render_agent("atlas").expect("legal"), ATLAS_DOCUMENT); // The control: the pin discriminates between agents. - assert!(!effective_agent("other").contains("agents/atlas/")); + assert!( + !render_agent("other") + .expect("legal") + .contains("agents/atlas/") + ); } #[test] @@ -514,7 +582,7 @@ mod tests { // An agent that could write its own credentials could hand itself an // identity it was never issued — and `create`/`update` on that path is // exactly what the controller holds, so the wall is the capability. - let p = effective_agent("atlas"); + let p = render_agent("atlas").expect("legal"); for capability in ["create", "update", "delete", "sudo", "patch"] { assert!( !p.contains(capability), @@ -528,7 +596,7 @@ mod tests { // `read` on `data/` is the values; `list` on `metadata/` is the key // names. `read` on `metadata/` (version history, deletion times) is not // granted, nor is `list` anywhere but `metadata/`. - let p = effective_agent("atlas"); + let p = render_agent("atlas").expect("legal"); let stanzas: Vec<&str> = p.split_inclusive("}\n").collect(); assert_eq!(stanzas.len(), 2, "{p}"); for stanza in stanzas { @@ -550,19 +618,22 @@ mod tests { #[test] fn one_agents_document_does_not_reach_another_agents_path() { - let a = effective_agent("atlas"); + let a = render_agent("atlas").expect("legal"); assert!(a.contains("swarm/agents/atlas/*")); assert!(!a.contains("argus")); + assert_eq!(a, render_agent("atlas").expect("legal")); } #[test] fn an_agent_name_that_could_close_the_stanza_is_refused() { - // The name reaches a role name and a certificate subject, so a name that - // could re-punctuate either is refused. + // Same live injection surface as the hive renderer's: the name reaches + // the document text, not just an identifier. + assert!(render_agent("atlas/*\" { capabilities = [\"root\"] }").is_err()); + assert!(render_agent("").is_err()); assert!(agent_object_name("atlas/*\" { capabilities = [\"root\"] }").is_err()); - assert!(agent_object_name("").is_err()); // The control: if everything were refused the arms above would pass for // the wrong reason. + assert!(render_agent("a-b_C9").is_ok()); assert!(agent_object_name("a-b_C9").is_ok()); } @@ -602,14 +673,18 @@ mod tests { #[test] fn an_agents_document_is_not_the_hives() { - // A later edit that made one shared document delegate to the other - // would hand an agent the hive's `agents/*` stanza. - let agent = effective_agent("atlas"); - let hive = effective_hive("atlas"); + // The pin the two renderers need against each other: a later edit that + // made `render_agent` delegate to `render`, or vice versa, would hand + // an agent the hive's `agents/*` stanza. + let agent = render_agent("atlas").expect("legal"); + let hive = render("atlas").expect("legal"); assert_ne!(agent, hive); assert!(!agent.contains("swarm/services/")); assert!(!agent.contains("swarm/hives/")); - // The control: the hive's document does carry that stanza. + // And the hive's is untouched by this module's growth: still the two + // stanzas `the_document_grants_every_agent_and_this_hive_alone` pins + // byte for byte, neither narrowed to make the agent's look consistent + // with it. assert!(hive.contains("path \"secret/data/swarm/agents/*\"")); } diff --git a/swarm-secret-client/src/queue.rs b/swarm-secret-client/src/queue.rs index 0c32251d2..a10ed2443 100644 --- a/swarm-secret-client/src/queue.rs +++ b/swarm-secret-client/src/queue.rs @@ -27,8 +27,8 @@ use crate::{ /// Under the agent's own principal prefix, and that is the whole reason for /// this spelling rather than a new top-level one: every agent's ACL document /// already grants read on `swarm/agents//*` -/// ([`crate::policy::render_shared_agent`]), so a credential here needs no new grant -/// and no policy rewrite. +/// ([`crate::policy::render_agent`]), so a credential here needs no new grant, +/// no policy re-render, and no rewrite of any existing agent's document. /// /// # Errors /// [`Error::PathSegment`] when `agent` contains anything but `[A-Za-z0-9_-]`, @@ -68,7 +68,7 @@ pub struct ControllerCredential { /// identity hive-c0re publishes the hive's status to the swarm queue with. /// /// Under the hive's own prefix, which the hive's policy reads -/// ([`crate::policy::render_shared_hives`]). +/// ([`crate::policy::render`]). /// /// The nix half is `swarm-secret-publisher.nix`, which writes it. /// @@ -129,7 +129,7 @@ mod tests { #[test] fn no_hive_policy_reaches_the_controller_client() { - let doc = crate::policy::effective_hive("alpha"); + let doc = crate::policy::render("alpha").expect("a plain name is legal"); // Every stanza is `path "secret/data/*" { … }`. let granted: Vec<&str> = doc .lines() @@ -157,7 +157,7 @@ mod tests { #[test] fn a_hives_own_policy_reads_its_client_and_no_other_hives() { - let doc = crate::policy::effective_hive("alpha"); + let doc = crate::policy::render("alpha").expect("a plain name is legal"); let granted: Vec<&str> = doc .lines() .filter_map(|l| l.strip_prefix("path \"secret/data/")) @@ -216,7 +216,7 @@ mod tests { /// it belongs to. #[test] fn the_agents_existing_policy_already_covers_its_queue_path() { - let document = crate::policy::effective_agent("atlas"); + let document = crate::policy::render_agent("atlas").expect("legal"); let path = agent_queue_path("atlas").expect("legal"); let stanza = path .rsplit_once('/')