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
|
|
@ -118,14 +118,25 @@ impl LoginState {
|
|||
}
|
||||
}
|
||||
|
||||
/// Whether this agent is configured to authenticate to its backend with an
|
||||
/// API key rather than a Claude OAuth session — set by
|
||||
/// `services.hyperhive.agent.useApiKey` via the `HIVE_USE_API_KEY` env var
|
||||
/// (`nix/agent-modules/agent-service.nix`). Read fresh on every call rather
|
||||
/// than cached: it's an env var, not a value worth a `OnceLock` for.
|
||||
/// Whether this agent authenticates to its backend with an API key rather than
|
||||
/// a Claude OAuth session: `services.hyperhive.agent.useApiKey` sets
|
||||
/// `HIVE_USE_API_KEY=1` (`nix/agent-modules/agent-service.nix`), and an agent
|
||||
/// whose environment holds a key — one an operator stored through the swarm UI
|
||||
/// reaches it as `ANTHROPIC_API_KEY` (`nix/agent-modules/backend-env.nix`) —
|
||||
/// counts too, so it tries the key instead of waiting for a login. Read fresh
|
||||
/// on every call rather than cached: it's an env var, not a value worth a
|
||||
/// `OnceLock` for.
|
||||
#[must_use]
|
||||
pub fn using_api_key() -> bool {
|
||||
std::env::var("HIVE_USE_API_KEY").is_ok_and(|v| v == "1")
|
||||
api_key_mode(|name| std::env::var(name).ok())
|
||||
}
|
||||
|
||||
/// [`using_api_key`] over `env`, so a test needs no process environment.
|
||||
fn api_key_mode(env: impl Fn(&str) -> Option<String>) -> bool {
|
||||
env("HIVE_USE_API_KEY").is_some_and(|v| v == "1")
|
||||
|| ["ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN"]
|
||||
.into_iter()
|
||||
.any(|name| env(name).is_some_and(|v| !v.trim().is_empty()))
|
||||
}
|
||||
|
||||
/// Baseline for [`has_fresh_credentials`] when the caller has no specific
|
||||
|
|
@ -306,7 +317,29 @@ mod tests {
|
|||
use std::fs;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use super::{NO_PRIOR_FAILURE, has_fresh_credentials, has_session};
|
||||
use super::{NO_PRIOR_FAILURE, api_key_mode, has_fresh_credentials, has_session};
|
||||
|
||||
fn mode(vars: &[(&str, &str)]) -> bool {
|
||||
api_key_mode(|name| {
|
||||
vars.iter()
|
||||
.find(|(n, _)| *n == name)
|
||||
.map(|(_, v)| (*v).to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_key_in_the_environment_or_the_flag_makes_an_api_key_agent() {
|
||||
assert!(!mode(&[]));
|
||||
assert!(mode(&[("HIVE_USE_API_KEY", "1")]));
|
||||
assert!(mode(&[("ANTHROPIC_API_KEY", "sk-ant-x")]));
|
||||
assert!(mode(&[("ANTHROPIC_AUTH_TOKEN", "t")]));
|
||||
// The control: neither an off flag nor an empty key counts.
|
||||
assert!(!mode(&[("HIVE_USE_API_KEY", "0")]));
|
||||
assert!(!mode(&[
|
||||
("ANTHROPIC_API_KEY", " "),
|
||||
("ANTHROPIC_AUTH_TOKEN", "")
|
||||
]));
|
||||
}
|
||||
|
||||
/// Test-only shorthand: most cases don't exercise the unreadable-mtime
|
||||
/// fallback, so they don't care about its baseline.
|
||||
|
|
|
|||
Loading…
Reference in a new issue