swarm: an agent's backend secrets get a store leaf of their own, and its backend environment reads back
The credentials of an agent's backend (ANTHROPIC_API_KEY and the like) move to their own leaf, swarm/agents/<agent>/backend-secrets, written through new PUT, GET and DELETE routes on /api/hives/{hive}/agents/{agent}/backend-secrets. GET says only whether any are stored. The backend environment at backend-env now reads back as stored through GET on its route, so it holds configuration only.
The agent's hive-agent-backend-env unit now runs on every agent with a store, reads both leaves and writes the configuration and then the secrets into the one file the harness and the subagent daemon load, so a secret wins over a variable of the same name. It removes the file when the store holds neither. The harness counts an agent whose environment holds ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN as API-billed, so it tries the key instead of waiting for a Claude login; a real 401 still sets needs-login. The controller's policy may delete the new leaf.
Tests: the secret-client path, the controller routes, the script test of the unit with a stub bao, the module-eval cases and the harness's mode check. Docs: the swarm UI routes, the credentials reference and the API-key provider page.
This commit is contained in:
parent
c9204ad0e0
commit
33fdd5205e
2 changed files with 737 additions and 107 deletions
96
swarm-secret-client/src/backend_secrets.rs
Normal file
96
swarm-secret-client/src/backend_secrets.rs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
//! Where an agent's backend secrets live in the store, apart from the rest of
|
||||
//! its backend environment.
|
||||
//!
|
||||
//! [`crate::backend_env`] holds the configuration an operator may read back
|
||||
//! (the base URL, a model name). The credentials sit in this leaf, so the
|
||||
//! controller never has to hand one back: it writes the leaf and says only
|
||||
//! whether it holds anything. Both objects hold the text of a systemd
|
||||
//! `EnvironmentFile`. The agent's own `hive-agent-backend-env` unit
|
||||
//! (`nix/agent-modules/backend-env.nix`) reads both under the agent's
|
||||
//! certificate and writes one file, the configuration first and the secrets
|
||||
//! after it, so a variable named in both takes the secret's value.
|
||||
//!
|
||||
//! The agent's read stanza ([`crate::policy::render_shared_agent`]) covers the
|
||||
//! leaf like any other under its prefix. Only the controller writes it.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
Error,
|
||||
path::{Kind, principal_prefix},
|
||||
};
|
||||
|
||||
/// The last segment of the path.
|
||||
pub(crate) const LEAF: &str = "backend-secrets";
|
||||
|
||||
/// The path holding `agent`'s backend secrets.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`Error::PathSegment`] when `agent` contains anything but `[A-Za-z0-9_-]`,
|
||||
/// which is what keeps one agent's name from addressing another agent's secret.
|
||||
pub fn secrets_path(agent: &str) -> Result<String, Error> {
|
||||
let prefix = principal_prefix(Kind::Agent, agent)?;
|
||||
Ok(format!("{prefix}/{LEAF}"))
|
||||
}
|
||||
|
||||
/// What a [`secrets_path`] holds.
|
||||
///
|
||||
/// No `Debug` derive: `env` carries live credentials, and a derived `Debug` is
|
||||
/// one `{:?}` away from a log line.
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct BackendSecrets {
|
||||
/// The file text, `NAME=value` per line. `backend-env.nix` reads it with
|
||||
/// `bao kv get -field=env`, so this name is load-bearing for a reader this
|
||||
/// crate does not control.
|
||||
pub env: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for BackendSecrets {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("BackendSecrets")
|
||||
.field("env", &"<redacted>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_secrets_land_under_the_agent_prefix() {
|
||||
// Spelled out: `backend-env.nix` spells the same string.
|
||||
assert_eq!(
|
||||
secrets_path("atlas").expect("a plain name is legal"),
|
||||
"swarm/agents/atlas/backend-secrets"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_traversal_in_the_agent_name_is_refused() {
|
||||
for bad in ["../argus", "a/b", "atlas/../argus", "a.b", ""] {
|
||||
let e = secrets_path(bad).expect_err("a traversal is not legal");
|
||||
assert!(matches!(e, Error::PathSegment { kind: "agent", .. }), "{e}");
|
||||
}
|
||||
assert!(secrets_path("a-b_C9").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_stored_field_is_env() {
|
||||
let stored: BackendSecrets =
|
||||
serde_json::from_str(r#"{"env":"A=b\n"}"#).expect("the documented shape decodes");
|
||||
assert_eq!(stored.env, "A=b\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn debug_hides_the_credentials() {
|
||||
let shown = format!(
|
||||
"{:?}",
|
||||
BackendSecrets {
|
||||
env: "ANTHROPIC_API_KEY=sk-live".to_owned()
|
||||
}
|
||||
);
|
||||
assert!(!shown.contains("sk-live"), "{shown}");
|
||||
assert!(shown.contains("<redacted>"), "{shown}");
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
//! deployment's environment into a logged-in client ([`client`]), and, per kind
|
||||
//! of secret, the path it lives at together with the fields it holds
|
||||
//! ([`matrix`], [`queue`], [`mtls`], [`forge`], [`github`], [`grafana`],
|
||||
//! [`acp`], [`attic`], [`backend_env`], [`ci`], [`review_classifier`], [`builder`], [`ci_metrics`],
|
||||
//! [`acp`], [`attic`], [`backend_env`], [`backend_secrets`], [`ci`], [`review_classifier`], [`builder`], [`ci_metrics`],
|
||||
//! [`claude_login`], [`proxy`]).
|
||||
//! Each of those is a thing the controller and a hive must say identically, so
|
||||
//! it is said once here.
|
||||
|
|
@ -33,6 +33,7 @@ pub mod acp;
|
|||
pub mod archive;
|
||||
pub mod attic;
|
||||
pub mod backend_env;
|
||||
pub mod backend_secrets;
|
||||
pub mod builder;
|
||||
pub mod cert;
|
||||
#[cfg(feature = "otel")]
|
||||
|
|
|
|||
Loading…
Reference in a new issue