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
1 changed files with 737 additions and 107 deletions
|
|
@ -34,7 +34,9 @@ use axum::extract::State;
|
|||
use axum::http::StatusCode;
|
||||
use serde::de::{DeserializeOwned, IgnoredAny};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swarm_secret_client::{Error, SecretStore, backend_env, forge, github, gitlab, matrix};
|
||||
use swarm_secret_client::{
|
||||
Error, SecretStore, backend_env, backend_secrets, forge, github, gitlab, matrix,
|
||||
};
|
||||
use utoipa::{IntoParams, ToSchema};
|
||||
|
||||
use crate::routes::agents::swarm_hive;
|
||||
|
|
@ -755,6 +757,48 @@ pub async fn delete_backend_env(
|
|||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
/// Delete an agent's backend secrets from the store, every version of them.
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/api/hives/{hive}/agents/{agent}/backend-secrets",
|
||||
params(
|
||||
("hive" = String, Path, description = "hive the agent runs on"),
|
||||
("agent" = String, Path, description = "agent the secrets belong to"),
|
||||
),
|
||||
responses(
|
||||
(status = 204, description = "deleted"),
|
||||
(status = 400, description = "the agent is not an identifier, or the hive is not in this swarm (problem+json)", body = String),
|
||||
(status = 404, description = "no backend secrets are stored (problem+json)", body = String),
|
||||
(status = 500, description = "the store could not be read or written (problem+json)", body = String),
|
||||
),
|
||||
tag = "agents"
|
||||
)]
|
||||
pub async fn delete_backend_secrets(
|
||||
State(state): State<AppState>,
|
||||
axum::extract::Path((hive, agent)): axum::extract::Path<(String, String)>,
|
||||
) -> Result<StatusCode, problem_details::ProblemDetails> {
|
||||
let hive = swarm_hive(&state, &hive).map_err(|(s, d)| error_problem(s, &d))?;
|
||||
let agent = hive_types::Ident::parse(&agent)
|
||||
.map_err(|reason| error_problem(StatusCode::BAD_REQUEST, reason))?
|
||||
.into_string();
|
||||
let path = backend_secrets::secrets_path(&agent)
|
||||
.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.to_string()))?;
|
||||
|
||||
let store = state.accounts.open().await.map_err(|e| {
|
||||
tracing::warn!(error = %e, "connecting to the swarm secret store failed");
|
||||
error_problem(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string())
|
||||
})?;
|
||||
remove_plain::<IgnoredAny>(&store, &path)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::warn!(%hive, %agent, error = ?e, "deleting the backend secrets failed");
|
||||
e.problem()
|
||||
})?;
|
||||
|
||||
tracing::info!(%hive, %agent, "backend secrets deleted");
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
|
|
@ -1285,6 +1329,33 @@ pub(crate) mod tests {
|
|||
assert_eq!(deleted(&store).len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn deleting_backend_secrets_removes_that_agents_leaf_only() {
|
||||
let store = Arc::new(
|
||||
FakeStore::default()
|
||||
.with(
|
||||
"swarm/agents/atlas/backend-secrets",
|
||||
json!({"env": "K=1\n"}),
|
||||
)
|
||||
.with("swarm/agents/atlas/backend-env", json!({"env": "A=1\n"})),
|
||||
);
|
||||
let del = |agent: &'static str| {
|
||||
super::delete_backend_secrets(
|
||||
axum::extract::State(state(&store)),
|
||||
axum::extract::Path(("pr1ma".to_owned(), agent.to_owned())),
|
||||
)
|
||||
};
|
||||
assert_eq!(
|
||||
del("atlas").await.expect("stored"),
|
||||
axum::http::StatusCode::NO_CONTENT
|
||||
);
|
||||
assert_eq!(deleted(&store), ["swarm/agents/atlas/backend-secrets"]);
|
||||
let problem = del("red").await.expect_err("none");
|
||||
assert_eq!(problem.status, Some(axum::http::StatusCode::NOT_FOUND));
|
||||
let problem = del("../x").await.expect_err("refused");
|
||||
assert_eq!(problem.status, Some(axum::http::StatusCode::BAD_REQUEST));
|
||||
}
|
||||
|
||||
const CATGIRL: &str = "swarm/agents/atlas/matrix/catgirl";
|
||||
const CATGIRL_PASSPHRASE: &str = "swarm/agents/atlas/matrix-recovery/catgirl";
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue