Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: move the secrets concern into its own module

Refs #6897
This commit is contained in:
atlas 2026-11-27 16:31:21 +01:00
commit 6d0c0dc391

View file

@ -1,797 +0,0 @@
//! One agent's own identity at the swarm's secret store: issued by the store,
//! published here, granted here — and, before the job node reports success,
//! **used** here.
//!
//! The swarm obtains the agent's certificate so that no hive ever needs the
//! capability to obtain one; the hive only carries it down. The controller is
//! the swarm-level service that asks because it already logs in to the store,
//! and its grant covers exactly the calls made here (`swarm-bao.nix`'s
//! `controllerPolicyText`: `update` on the agent PKI role's `issue` path, and
//! `create/update` on `secret/data/swarm/agents/*`, on
//! `sys/policies/acl/hive-*`, and on `auth/cert/certs/hive-*` (`delete` too), plus the
//! `identity/` and `sys/auth/cert` stanzas [`crate::agent_entity`] and [`crate::agent_cutoff`] use).
//!
//! **Two credentials, deliberately unrelated.** The certificate reaches the
//! store; the queue secret identifies the agent to the swarm queue. Both sit
//! under `swarm/agents/<agent>`, so neither costs a grant — but the second is
//! 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
//! [`read_back_as_agent`].
//!
//! The authority is the store's own agent CA, generated inside its agent PKI
//! mount; its key never leaves the store. It signs no host leaf, and no host
//! role pins it, so an agent's certificate satisfies only that agent's role.
//! [`crate::agent_renewal`] re-issues a live agent's leaf once it is past half
//! its validity (the role's `ttl`) or missing, by queueing the same node as creation.
use crate::store::AccessStore;
use anyhow::{Context, Result, bail};
use swarm_secret_client::{
SecretStore,
client::{DEFAULT_CERT_MOUNT, Settings},
mtls, policy, queue,
};
/// The PKI mount agent leaves are issued from, as `swarm-controller.nix` sets
/// it from `deploy.bao.agentPkiMountPath`.
pub const ENV_AGENT_PKI_MOUNT: &str = "SWARM_CONTROLLER_AGENT_PKI_MOUNT";
/// The role on [`ENV_AGENT_PKI_MOUNT`] agent leaves are issued through, as
/// `swarm-controller.nix` sets it from `deploy.bao.agentPkiRoleName`.
pub const ENV_AGENT_PKI_ROLE: &str = "SWARM_CONTROLLER_AGENT_PKI_ROLE";
/// Where agent leaves are issued: `(mount, role)`, read from `get`.
///
/// # Errors
/// Naming the first of the two variables that is unset or empty.
fn agent_pki(get: impl Fn(&str) -> Option<String>) -> Result<(String, String)> {
let required = |var: &str| -> Result<String> {
get(var)
.filter(|v| !v.is_empty())
.with_context(|| format!("{var} is unset or empty, so no agent leaf can be issued"))
};
Ok((
required(ENV_AGENT_PKI_MOUNT)?,
required(ENV_AGENT_PKI_ROLE)?,
))
}
/// What the cert-auth role for `agent` is written from.
struct RoleInputs<'a> {
/// Role 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<String>,
}
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: crate::agent_ssh::attached(ssh),
})
}
/// The inputs for `agent`'s role, with the signing policy attached when the role
/// it replaces has it. The role is where the agent's SSH switch lives, so a
/// rewrite for a new certificate keeps what it has (`crate::agent_ssh`).
async fn inputs_keeping_ssh<'a>(
store: &impl AccessStore,
agent: &str,
credential: &'a mtls::Credential,
) -> Result<RoleInputs<'a>> {
let name = policy::agent_object_name(agent).at(MintStep::Role)?;
let ssh = crate::agent_ssh::role_has_ssh(store, &name)
.await
.with_context(|| format!("reading the policies of the cert-auth role {name}"))
.at(MintStep::Role)?;
role_inputs(agent, credential, ssh).at(MintStep::Role)
}
/// Write the shared policy, name the agent's entity and enable it, then write
/// the cert-auth role that attaches the policy.
///
/// Enabling clears the flag [`crate::agent_cutoff::cut_off`] set when an agent
/// of this name was destroyed, so a new agent of that name can log in.
///
/// The entity comes before the role and is a precondition for it. The role
/// attaches the shared policy alone, so an agent whose entity is not named after
/// it reads nothing under it. This also runs for a live agent's renewal, which
/// rewrites the role its current leaf logs in under: when the entity step
/// fails, or finds the alias on another entity, the role is left as it was and
/// the renewal fails while the agent keeps working.
async fn publish_access(
store: &impl AccessStore,
agent: &str,
inputs: &RoleInputs<'_>,
) -> 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
)
})
.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"))
.at(MintStep::Entity)?;
crate::agent_cutoff::restore(store, agent)
.await
.with_context(|| format!("enabling {agent}'s identity entity; the role is not changed"))
.at(MintStep::Entity)?;
store
.write_cert_role(
DEFAULT_CERT_MOUNT,
&inputs.name,
inputs.ca,
&inputs.name,
&inputs
.attached
.iter()
.map(String::as_str)
.collect::<Vec<_>>(),
)
.await
.with_context(|| format!("writing the cert-auth role {}", inputs.name))
.at(MintStep::Role)
}
/// 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
/// nothing else — there is no work factor and no rate limit behind it, so the
/// only defence is that guessing is not worth attempting. Encoded it is 43
/// characters.
const QUEUE_SECRET_BYTES: usize = 32;
/// Generate a queue secret: [`QUEUE_SECRET_BYTES`] from the kernel's CSPRNG,
/// base64url without padding.
///
/// The alphabet matters and is the reason for `URL_SAFE_NO_PAD` rather than
/// the standard engine: the secret is destined to be carried in a token the
/// verifying end splits on `.`, so it must not be able to contain one. This
/// alphabet is `[A-Za-z0-9_-]`, and `=` padding is dropped as well so the
/// value survives anything that treats it as a word.
///
/// # Errors
/// When the kernel will not supply randomness. Bubbled rather than panicked
/// on: the caller is a job node that reports a named failure, and a secret
/// from a degraded source is worse than no secret.
pub(crate) fn generate_queue_secret() -> Result<String> {
let mut bytes = [0u8; QUEUE_SECRET_BYTES];
getrandom::fill(&mut bytes).context("drawing a queue secret from the kernel's CSPRNG")?;
Ok(base64::Engine::encode(
&base64::engine::general_purpose::URL_SAFE_NO_PAD,
bytes,
))
}
/// The part of [`mint_and_verify`] a failure came from.
///
/// Attached to the error as its outermost context, so the
/// message leads with it and [`failed_step`] reads it back as a value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MintStep {
/// Reading the environment and the agent's name, before any store call.
Setup,
/// Logging in to the store as the controller.
StoreLogin,
/// Issuing the leaf from the agent PKI role.
IssueLeaf,
/// Publishing the leaf at [`mtls::identity_path`].
WriteBaoMtls,
/// Reading or publishing the queue secret at [`queue::agent_queue_path`].
Queue,
/// Writing the shared ACL document.
Policy,
/// Naming the agent's identity entity.
Entity,
/// Deriving or writing the cert-auth role.
Role,
/// The login as the agent and the reads after it.
ReadBack,
}
impl MintStep {
/// The step as it reads in a log line.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
MintStep::Setup => "setup",
MintStep::StoreLogin => "store login",
MintStep::IssueLeaf => "issue leaf",
MintStep::WriteBaoMtls => "write bao-mtls",
MintStep::Queue => "queue",
MintStep::Policy => "policy",
MintStep::Entity => "entity",
MintStep::Role => "role",
MintStep::ReadBack => "read back",
}
}
}
impl std::fmt::Display for MintStep {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
/// The [`MintStep`] `error` was attached to, if [`mint_and_verify`] raised it.
#[must_use]
pub fn failed_step(error: &anyhow::Error) -> Option<MintStep> {
error.downcast_ref::<MintStep>().copied()
}
/// Names the [`MintStep`] a failing result belongs to.
trait AtStep<T> {
fn at(self, step: MintStep) -> Result<T>;
}
impl<T, E: Into<anyhow::Error>> AtStep<T> for Result<T, E> {
fn at(self, step: MintStep) -> Result<T> {
self.map_err(|e| e.into().context(step))
}
}
/// Give `agent` an identity at the store, and prove it works.
///
/// Six 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;
/// 2. publish it at [`mtls::identity_path`], where the agent's hive collects
/// it under the hive's own certificate;
/// 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, and enable it ([`crate::agent_cutoff::restore`]);
/// 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.
///
/// Policy and entity before role: see [`publish_access`].
///
/// ⚠️ **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
/// queue secret alone: this is re-run against running agents, which hold that
/// secret in a live connection. Revoking one means deleting the path;
/// replacing one by age is [`crate::agent_renewal`]'s.
///
/// # Errors
/// Anything that stops one of those six steps, or the read-back login, with the
/// [`MintStep`] ([`failed_step`]) leading the message. 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.
pub async fn mint_and_verify(agent: &str) -> Result<()> {
let (mount, pki_role) = agent_pki(|k| std::env::var(k).ok()).at(MintStep::Setup)?;
let name = policy::agent_object_name(agent).at(MintStep::Setup)?;
let path = mtls::identity_path(agent).at(MintStep::Setup)?;
let queue_path = queue::agent_queue_path(agent).at(MintStep::Setup)?;
let store = crate::store::connect()
.await
.context("logging in to the swarm secret store")
.at(MintStep::StoreLogin)?;
let credential = store
.issue_client_certificate(&mount, &pki_role, &name)
.await
.with_context(|| format!("issuing {name}'s certificate from {mount}/issue/{pki_role}"))
.at(MintStep::IssueLeaf)?;
store
.write(&path, &credential)
.await
.with_context(|| format!("publishing the agent identity at {path}"))
.at(MintStep::WriteBaoMtls)?;
// Read before write, and `read_optional` rather than `read`, so that only
// a genuine 404 leads to a new secret — see that method for why every
// other failure has to stay a failure here.
let existing: Option<queue::AgentCredential> = store
.read_optional(&queue_path)
.await
.with_context(|| format!("checking whether {queue_path} already holds a credential"))
.at(MintStep::Queue)?;
// The secret and its mint time survive a re-run. An object naming a
// different agent is corrected by rewriting the name around the *same*
// `value`, which no live connection notices.
let wanted = match &existing {
Some(existing) => queue::AgentCredential {
agent: agent.to_owned(),
..existing.clone()
},
None => crate::agent_renewal::fresh(agent, crate::agent_renewal::unix_now())
.at(MintStep::Queue)?,
};
if existing.as_ref() == Some(&wanted) {
tracing::info!(
agent,
%queue_path,
"agent queue credential already published; left as it is"
);
} else {
store
.write(&queue_path, &wanted)
.await
.with_context(|| format!("publishing the agent queue credential at {queue_path}"))
.at(MintStep::Queue)?;
tracing::info!(
agent,
%queue_path,
// Never "rotated": the secret is the same one, only the name
// around it moved.
corrected = existing.is_some(),
"agent queue credential published"
);
}
let queue_credential = wanted;
let inputs = inputs_keeping_ssh(&store, agent, &credential).await?;
publish_access(&store, agent, &inputs).await?;
tracing::info!(agent, %path, role = %name, "agent store identity published");
read_back_as_agent(&credential, &name, &path, &queue_path, &queue_credential)
.await
.at(MintStep::ReadBack)?;
tracing::info!(
agent,
role = %name,
"agent store identity verified: the issued leaf logged in and read both its own paths"
);
Ok(())
}
/// Revoke `agent`'s queue credential: delete the path
/// [`mint_and_verify`]'s step 3 published, and everything ever written at it.
///
/// The undo of that one step and of no other. The leaf, the ACL document and
/// the cert-auth role that make up the rest of an agent's identity stay where
/// they are — they are what a *hive* uses to collect an agent's secrets, they
/// are minted afresh on every run of the mint, and tearing them down is not
/// what the queue credential outliving its holder is about.
///
/// **Deletes every version, not the newest one.** The mint rewrites this path
/// whenever the principal it names has to be corrected, so a soft delete would
/// leave the identical secret sitting in version history, readable at
/// `?version=N` by anything that can read the path at all — a value still
/// recoverable has not been revoked. See
/// [`SecretStore::delete_all_versions`][swarm_secret_client::SecretStore::delete_all_versions].
///
/// **Idempotent**: revoking an agent that never had a credential, or one
/// already revoked, succeeds and says so. A teardown that runs twice is
/// ordinary, and a second run that failed would be a worse fault than the one
/// this exists to fix.
///
/// # Errors
/// When the store cannot be reached or refuses the delete. The caller decides
/// what that costs — `set_agent_state` logs it and lets the destroy proceed,
/// since a credential that is still live is a smaller harm than an agent that
/// cannot be torn down.
pub async fn revoke_queue_credential(agent: &str) -> Result<()> {
let queue_path = queue::agent_queue_path(agent)?;
let store = crate::store::connect()
.await
.context("logging in to the swarm secret store")?;
store
.delete_all_versions(&queue_path)
.await
.with_context(|| format!("revoking the agent queue credential at {queue_path}"))?;
// At `info` and unconditional: an unlogged revocation is indistinguishable
// from a leak, and this line is the only record an operator has that the
// credential stopped being usable. It cannot say whether one was there —
// the controller's grant on these paths is write-only by design, so it
// deletes blind.
tracing::info!(
agent,
%queue_path,
"agent queue credential revoked: every version of the path deleted"
);
Ok(())
}
/// The consumer of everything [`mint_and_verify`] wrote: log in **as the
/// agent**, with the leaf just issued, and read back both paths just
/// published.
///
/// The address and the store's CA come from this process's own `BAO_*`
/// environment; the *identity* deliberately does not — see
/// [`SecretStore::connect_with_identity`]. The freshly issued private key
/// never touches a filesystem.
///
/// 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
/// 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.
///
/// # Errors
/// When the store refuses the login (the role, the authority or the common
/// name disagree), when either read is denied (the policy does not cover the
/// path, or the role attached the wrong policy), or when what comes back is
/// not what went in.
async fn read_back_as_agent(
credential: &mtls::Credential,
role: &str,
path: &str,
queue_path: &str,
queue_credential: &queue::AgentCredential,
) -> Result<()> {
let settings = Settings::from_env()
.context("reading this daemon's own store settings for the read-back")?;
// The concatenated blob `connect_with_identity` takes, built in memory.
let mut identity = credential.cert.clone().into_bytes();
identity.push(b'\n');
identity.extend_from_slice(credential.key.as_bytes());
let as_agent =
SecretStore::connect_with_identity(&settings, &identity, role, DEFAULT_CERT_MOUNT)
.await
.with_context(|| {
format!("logging in to the store as {role} with the leaf just issued")
})?;
let read_back: mtls::Credential = as_agent
.read(path)
.await
.with_context(|| format!("reading {path} back under {role}'s own token"))?;
// Compared, not merely decoded: a successful read of an object written by
// some earlier run would otherwise pass this check while this run's leaf
// was the one nobody could use. Nothing about either value is printed.
if read_back != *credential {
bail!("the store returned a different object at {path} than the one just published");
}
let read_back: queue::AgentCredential = as_agent
.read(queue_path)
.await
.with_context(|| format!("reading {queue_path} back under {role}'s own token"))?;
// The agent is compared as well as the secret: the verifying end refuses
// an object naming a different agent than the path, so a mismatch here is
// the same class of fault as an unreadable path.
if read_back != *queue_credential {
bail!("the store returned a different object at {queue_path} than the one just published");
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{
ENV_AGENT_PKI_MOUNT, ENV_AGENT_PKI_ROLE, MintStep, QUEUE_SECRET_BYTES, agent_pki,
failed_step, generate_queue_secret, inputs_keeping_ssh, publish_access, 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
/// a composite the verifying end splits on `.`, so a secret that could
/// contain one would make that split ambiguous. Also the entropy claim —
/// a generator that silently returned a short or constant value would pass
/// every other test in this file.
#[test]
fn a_queue_secret_is_high_entropy_and_carries_no_separator() {
let a = generate_queue_secret().expect("the kernel supplies randomness");
let b = generate_queue_secret().expect("twice");
assert_ne!(a, b, "two draws must not agree");
// base64 without padding: one character per six bits, rounded up.
assert_eq!(a.len(), (QUEUE_SECRET_BYTES * 8).div_ceil(6));
assert!(
a.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'),
"{a}"
);
assert!(!a.contains('.'), "{a}");
assert!(!a.contains('='), "{a}");
}
/// Revocation undoes step 3 of the mint and nothing else, and both ends
/// spell the path through one function — the property that keeps a
/// revocation from missing the object it is meant to remove.
///
/// The second half is what stops this being a tautology: the agent's
/// other published object, the mTLS leaf, is at a different path and is
/// deliberately left alone. Revoking both would take away the identity a
/// re-created agent is re-minted under, for a credential problem that is
/// only about the queue.
#[test]
fn revocation_names_the_path_the_mint_published_and_not_the_leaf_beside_it() {
use swarm_secret_client::{mtls, queue};
assert_eq!(
queue::agent_queue_path("atlas").expect("a plain name is legal"),
"swarm/agents/atlas/queue"
);
assert_ne!(
queue::agent_queue_path("atlas").expect("legal"),
mtls::identity_path("atlas").expect("legal"),
);
}
/// A name the store must never be asked to delete under. `revoke_queue_credential`
/// builds its path with the same validating function the mint does, so a
/// traversal is refused before a request is made rather than addressing
/// some other principal's secret.
#[test]
fn a_traversal_never_becomes_a_revocation() {
swarm_secret_client::queue::agent_queue_path("../pr1ma")
.expect_err("a traversal is not a legal agent name");
}
fn both(k: &str) -> Option<String> {
match k {
ENV_AGENT_PKI_MOUNT => Some("pki-agents".to_owned()),
ENV_AGENT_PKI_ROLE => Some("swarm-agent".to_owned()),
_ => None,
}
}
#[test]
fn the_issue_path_comes_from_the_two_variables_the_module_sets() {
assert_eq!(
agent_pki(both).expect("both set"),
("pki-agents".to_owned(), "swarm-agent".to_owned())
);
}
#[test]
fn a_missing_or_empty_pki_variable_is_named() {
for var in [ENV_AGENT_PKI_MOUNT, ENV_AGENT_PKI_ROLE] {
let unset = agent_pki(|k| if k == var { None } else { both(k) })
.expect_err("one variable is unset");
assert!(format!("{unset:#}").contains(var), "{unset:#}");
let empty = agent_pki(|k| {
if k == var {
Some(String::new())
} else {
both(k)
}
})
.expect_err("one variable is empty");
assert!(format!("{empty:#}").contains(var), "{empty:#}");
}
}
/// 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.
#[test]
fn the_role_pins_the_leafs_own_issuer_under_the_agents_name() {
let credential = mtls::Credential {
cert: "LEAF".to_owned(),
key: "KEY".to_owned(),
ca: "AGENT-CA".to_owned(),
};
let inputs = role_inputs("atlas", &credential, false).expect("legal");
assert_eq!(inputs.name, "hive-agent-atlas");
assert_eq!(
inputs.name,
policy::agent_object_name("atlas").expect("legal"),
"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"
);
// 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, false).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, false).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:?}"
);
}
/// 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 again = inputs_keeping_ssh(&on, "atlas", &credential)
.await
.expect("reads");
assert_eq!(again.attached, ["hive-agent", "hive-agent-ssh"]);
// The control: a role that never had it does not gain it.
let fresh = inputs_keeping_ssh(&FakeAccess::default(), "atlas", &credential)
.await
.expect("reads");
assert_eq!(fresh.attached, ["hive-agent"]);
let off = FakeAccess::default();
let inputs = role_inputs("atlas", &credential, false).expect("legal");
publish_access(&off, "atlas", &inputs)
.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]
async fn re_creating_a_cut_off_agent_enables_its_entity_and_writes_its_role() {
let store = FakeAccess::default().with_entity("atlas");
crate::agent_cutoff::cut_off(&store, "atlas")
.await
.expect("accepted");
assert!(store.is_disabled("atlas"), "the control: it was cut off");
assert_eq!(store.role("hive-agent-atlas"), None);
let credential = credential();
let inputs = role_inputs("atlas", &credential, false).expect("legal");
publish_access(&store, "atlas", &inputs)
.await
.expect("a store that accepts everything");
assert!(!store.is_disabled("atlas"), "{:?}", store.calls());
assert!(store.role("hive-agent-atlas").is_some());
let calls = store.calls();
let at = |call: &str| calls.iter().position(|c| c == call).expect(call);
assert!(
at("set_entity_disabled atlas false")
< at("write_cert_role hive-agent-atlas hive-agent"),
"the entity is enabled before the role is written: {calls:?}"
);
}
#[tokio::test]
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, false).expect("legal");
let err = publish_access(&store, "atlas", &inputs)
.await
.expect_err("the enable is refused");
assert_eq!(failed_step(&err), Some(MintStep::Entity), "{err:#}");
assert!(store.roles_written().is_empty(), "{:?}", store.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, false).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, false).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())
);
}
/// Each refusal names the step it came from, in the message and as a value
/// the job runner logs. Two different steps, so a tag that named every
/// failure alike would fail one of them.
#[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, false).expect("legal");
let err = publish_access(&FakeAccess::refusing_policies(), "atlas", &inputs)
.await
.expect_err("the policy write is refused");
assert_eq!(failed_step(&err), Some(MintStep::Policy), "{err:#}");
assert!(format!("{err:#}").starts_with("policy: "), "{err:#}");
let err = publish_access(&FakeAccess::without_accessor(), "atlas", &inputs)
.await
.expect_err("the entity step fails");
assert_eq!(failed_step(&err), Some(MintStep::Entity), "{err:#}");
assert!(format!("{err:#}").starts_with("entity: "), "{err:#}");
}
#[test]
fn an_error_from_outside_the_mint_has_no_step() {
assert_eq!(failed_step(&anyhow::anyhow!("elsewhere")), None);
}
}