hive-c0re: deliver an agent's credential from the store to its state dir

mara on #4015: "not merging code without callers", and on the same PR
"see issue, we decided what the first thing should be". #3726 decided it:
the controller writes a token to the store and tells the hive; the hive
reads it back and writes /agents/<agent>/state/matrix-token-<account> at
0600, where matrix.nix's existing systemd.paths glob re-fires the daemon.
So this is the hive half of that, and the library's first caller.

The notice names a credential and never carries one, and deploy_subject's
own doc is why: the auth-callout responder scopes publish and leaves sub
unrestricted, so a hive that wanted another's messages could subscribe to
them. A secret in that payload would be readable swarm-wide. The value is
read from the store under the reading hive's own certificate, where the
store's policy is what actually scopes it.

Two boundaries guard the two addresses, and they are not the same check.
`path::matrix_account` guards the address in the store. `Ident` guards the
address on disk -- `agent_state_dir` takes one, so an unvalidated name off
the queue cannot reach a directory. I had written the first and assumed it
covered both; the compiler refused the `&str` and was right. `token_path`
now takes the newtype so a call site cannot forget.

The write is atomic because the path-watcher fires on the file appearing:
written in place it would be visible while partial, and the daemon would
read a truncated credential exactly once, which is the hardest possible
failure to reproduce. The temp name is dot-prefixed so it cannot match the
`matrix-token*` glob on its way past.

The publish grant is here because without it the failure is invisible.
policy.rs already says why for its siblings: a refused publish reaches the
client as a timeout, so the symptom is a hive that never receives a
credential with nothing in either log naming a permission. Two tests: the
controller may publish, a hive may not -- its own subject included. A
forged notice leaks nothing, but it would make a hive fetch and overwrite
a token file for a name the forger chose.

Refs #3726
This commit is contained in:
atlas 2026-09-02 23:28:31 +02:00 committed by mara
commit 7a03ce096a
7 changed files with 282 additions and 1 deletions

View file

@ -198,7 +198,24 @@ async fn drain_swarm_events(
return;
}
};
tracing::info!(%subject, %deploy_subject, "swarm events: listening");
// Also this hive's own, and for a second reason on top of the deploy
// subject's: the payload names an agent in *this* hive's state dir, so a
// notice for another hive is not merely noise, it is unactionable here.
let credential_subject = swarm_queue_client::credential_subject(&hive);
let mut credential_sub = match client.subscribe(credential_subject.clone()).await {
Ok(sub) => sub,
Err(e) => {
tracing::warn!(
subject = %credential_subject, error = %e,
"swarm events: subscribe failed; this hive will not hear credential notices"
);
return;
}
};
tracing::info!(
%subject, %deploy_subject, %credential_subject,
"swarm events: listening"
);
loop {
tokio::select! {
@ -223,6 +240,13 @@ async fn drain_swarm_events(
};
handle_deploy_request(&coord, &msg.payload).await;
}
msg = credential_sub.next() => {
let Some(msg) = msg else {
tracing::warn!(subject = %credential_subject, "swarm events: credential subscription closed");
return;
};
handle_credential_notice(&hive, &msg.payload).await;
}
_ = shutdown.changed() => {
tracing::info!("swarm events: shutdown signal received");
return;
@ -236,6 +260,39 @@ async fn drain_swarm_events(
///
/// A payload that will not decode is worth a `warn`: the controller and this
/// end share one type, so a decode failure means they disagree about it.
/// Deliver the credential a [`swarm_queue_client::CredentialNotice`] names.
///
/// `hive` doubles as the cert-auth role this hive logs into the store as:
/// `glue-bao-tls.nix` mints the client certificate with the hive name as its
/// CN, and a bao cert role matches on CN — so the two share a name by
/// construction rather than by convention.
///
/// ⚠️ Nothing here can log the secret, and that is structural rather than
/// careful: the notice carries only names, and `deliver` writes the value
/// without returning it.
async fn handle_credential_notice(hive: &str, payload: &[u8]) {
let notice: swarm_queue_client::CredentialNotice = match serde_json::from_slice(payload) {
Ok(notice) => notice,
Err(e) => {
tracing::warn!(error = %e, "swarm events: undecodable credential notice");
return;
}
};
if let Err(e) = crate::workers::credential::deliver(&notice, hive).await {
// Warn rather than retry: the controller republishes, and a hive that
// spun here would hold the queue task off its other two subjects.
tracing::warn!(
agent = %notice.agent, account = %notice.account, error = ?e,
"swarm events: credential delivery failed"
);
return;
}
tracing::info!(
agent = %notice.agent, account = %notice.account,
"swarm events: credential delivered"
);
}
async fn handle_deploy_request(
coord: &std::sync::Arc<crate::coordinator::Coordinator>,
payload: &[u8],

View file

@ -0,0 +1,144 @@
//! Delivering an agent's external-account credential from the swarm's secret
//! store into that agent's own state dir.
//!
//! The controller publishes a [`CredentialNotice`] naming an agent and an
//! account; this reads the value out of the store and writes it where the
//! agent's matrix daemon already watches for it. Nothing here activates
//! anything: `nix/agent-modules/matrix.nix` has a `systemd.paths` unit
//! globbing `/agents/*/state/matrix-token*` that re-fires the daemon when a
//! token appears, so arrival is the whole trigger.
//!
//! 🔑 The notice carries no secret — see [`swarm_queue_client::credential_subject`]
//! for why that is a requirement rather than a preference. The value is read
//! from the store under this hive's own identity.
use std::os::unix::fs::PermissionsExt as _;
use std::path::PathBuf;
use anyhow::{Context, Result};
use hive_types::Ident;
use swarm_queue_client::CredentialNotice;
use swarm_secret_client::{SecretStore, path};
use crate::paths::agent_state_dir;
/// The basename every matrix token must start with.
///
/// `nix/agent-modules/matrix.nix` asserts the same prefix on every configured
/// `tokenFile`, because its path-watcher globs for it. A name written here
/// that did not match would land a file the daemon never notices — no error,
/// just a credential that silently never arrives.
const TOKEN_PREFIX: &str = "matrix-token";
/// Where `agent`'s credential for `account` is written.
///
/// Takes an [`Ident`] rather than a `&str` because the name arrives off the
/// queue: `agent_state_dir` addresses a directory, and an unvalidated name
/// there is a path-traversal argument. The compiler refusing the `&str` is the
/// check — nothing here has to remember to perform one.
#[must_use]
pub fn token_path(agent: &Ident, account: &str) -> PathBuf {
agent_state_dir(agent).join(format!("{TOKEN_PREFIX}-{account}"))
}
/// Read the credential `notice` names and write it into the agent's state dir.
///
/// `cert_role` is the role on the store's `cert` auth mount whose policy scopes
/// what this hive may read.
///
/// # Errors
/// The store refusing, being unreachable, or holding nothing at that path; a
/// name that is not a single path segment; or the write failing.
pub async fn deliver(notice: &CredentialNotice, cert_role: &str) -> Result<()> {
// Parsed before anything is read, so a malformed name costs a decode and
// not a round trip to the store.
let agent = Ident::parse(&notice.agent)
.map_err(|e| anyhow::anyhow!("agent name {:?} off the queue: {e}", notice.agent))?;
let secret_path = path::matrix_account(&notice.agent, &notice.account)
.context("building the credential's path in the store")?;
let store = SecretStore::from_env(cert_role)
.await
.context("connecting to the swarm secret store")?;
let value = store
.read(&secret_path)
.await
.with_context(|| format!("reading {secret_path} from the store"))?;
write_token(&token_path(&agent, &notice.account), &value)
}
/// Write `value` to `dest` at `0600`, atomically.
///
/// Atomic because the daemon's path-watcher fires on the file *appearing*: a
/// token written in place would be visible while still partial, and the daemon
/// would read a truncated credential exactly once, at the moment it is hardest
/// to reproduce. The temp name is dot-prefixed so it cannot match the
/// `matrix-token*` glob on its way past.
fn write_token(dest: &PathBuf, value: &str) -> Result<()> {
let dir = dest
.parent()
.context("a token path always has a parent state dir")?;
let name = dest
.file_name()
.context("a token path always has a file name")?;
let mut tmp = dir.join(".");
tmp.as_mut_os_string().push(name);
tmp.as_mut_os_string().push(".partial");
std::fs::write(&tmp, value).with_context(|| format!("writing {}", tmp.display()))?;
// Before the rename, so the file is never briefly readable by others under
// its final name.
std::fs::set_permissions(&tmp, std::fs::Permissions::from_mode(0o600))
.with_context(|| format!("restricting {}", tmp.display()))?;
std::fs::rename(&tmp, dest).with_context(|| format!("publishing {}", dest.display()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_written_name_matches_the_glob_the_daemon_watches() {
// The other end of this agreement is an assertion in
// `nix/agent-modules/matrix.nix` and a `systemd.paths` glob — neither
// reachable from a Rust test, so the prefix is pinned here.
let p = token_path(&Ident::parse("dmatrix").expect("a legal agent name"), "ccc");
let name = p.file_name().unwrap().to_str().unwrap();
assert!(name.starts_with("matrix-token"), "got {name}");
assert_eq!(name, "matrix-token-ccc");
}
#[test]
fn the_partial_file_cannot_match_that_glob() {
// A temp name starting with `matrix-token` would be picked up
// mid-write; the dot prefix is what stops it.
let p = token_path(&Ident::parse("dmatrix").expect("a legal agent name"), "ccc");
let name = p.file_name().unwrap().to_str().unwrap();
let tmp = format!(".{name}.partial");
assert!(!tmp.starts_with("matrix-token"), "got {tmp}");
}
#[test]
fn a_name_that_could_address_another_agent_is_refused() {
// `path::matrix_account` owns this rule; asserted here because this is
// the module that feeds it names off the wire.
assert!(path::matrix_account("../argus", "ccc").is_err());
assert!(path::matrix_account("dmatrix", "../../etc/x").is_err());
assert!(path::matrix_account("dmatrix", "ccc").is_ok());
}
#[test]
fn an_agent_name_off_the_queue_must_pass_the_ident_parser_too() {
// Two independent refusals, not one restated: `path::matrix_account`
// guards the address in the *store*, `Ident` guards the address on
// *disk*. `token_path` cannot even be called without the second,
// which is why it takes an `Ident` rather than validating internally.
for bad in ["../argus", "dmatrix/../argus", "Dmatrix", "d matrix", ""] {
assert!(Ident::parse(bad).is_err(), "{bad:?} must be refused");
}
// The control: without it, a parser that rejected everything would
// satisfy the loop above.
assert!(Ident::parse("dmatrix").is_ok());
}
}

View file

@ -8,6 +8,7 @@
pub mod agent_sockets;
pub mod auto_update;
pub mod crash_watch;
pub mod credential;
pub mod knowledge;
pub mod mcp_sockets;
pub mod scheduled_prompts_worker;