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
|
|
@ -3,30 +3,31 @@
|
|||
# itself into the file the harness and the subagent daemon load as
|
||||
# `EnvironmentFile=`.
|
||||
#
|
||||
# The store holds the file's text at `swarm/agents/<agent>/backend-env`, field
|
||||
# `env` (`swarm_secret_client::backend_env`). Each run does one of three things:
|
||||
# The store holds the configuration at `swarm/agents/<agent>/backend-env` and the
|
||||
# credentials apart from it at `swarm/agents/<agent>/backend-secrets`, both as
|
||||
# the text of an env file in field `env` (`swarm_secret_client::backend_env`,
|
||||
# `::backend_secrets`). An operator can read the first back, never the second.
|
||||
# Each run does one of these things:
|
||||
#
|
||||
# - the store has a value: it is written to `/run/hive-agent-backend-env/env`;
|
||||
# - the store has none and `backendEnvironmentFile` is a non-empty file: the
|
||||
# file's exact bytes are created at the leaf with `-cas=0`, then read back
|
||||
# - the store has a configuration, secrets or both: they are written to
|
||||
# `/run/hive-agent-backend-env/env`, the configuration first, so a secret wins
|
||||
# over a variable of the same name. A harness whose environment then holds
|
||||
# `ANTHROPIC_API_KEY` bills the API and tries it instead of waiting for a
|
||||
# Claude login (`hive_agent::login::using_api_key`);
|
||||
# - the store has no configuration and `backendEnvironmentFile` is a non-empty
|
||||
# file: its exact bytes are created at the leaf with `-cas=0`, then read back
|
||||
# and written the same way. A run that loses the race to another writer, or
|
||||
# whose create the store's policy refuses, uses whatever the read-back finds;
|
||||
# - neither: nothing is written, which is an agent with no backend file.
|
||||
# - the store has neither: the file is removed, so a credential an operator took
|
||||
# away stops being loaded at the next start.
|
||||
#
|
||||
# It never rotates a value and never touches `backendEnvironmentFile`. That
|
||||
# file stays an `EnvironmentFile=` of its own, listed before the fetched one:
|
||||
# systemd lets a later file override an earlier one, so the store's value wins
|
||||
# when there is one, and the file carries the agent when the store is down.
|
||||
#
|
||||
# The leaf's create grant cannot constrain content, so while the leaf is empty
|
||||
# the agent's own process can also create it with any text, and the store's
|
||||
# value then wins over the file. That is no wider than the file the agent
|
||||
# already owns: the container activation's `heal_own_dir` (`./user.nix`)
|
||||
# re-owns the harness dir to the agent recursively at every boot, so the agent
|
||||
# already controls the legacy file.
|
||||
#
|
||||
# The unit runs as root because the file is root-owned `0600` where an operator
|
||||
# placed it. Root can still read the agent's `0400` store identity.
|
||||
# It never rotates a value and never touches `backendEnvironmentFile`, which stays
|
||||
# an `EnvironmentFile=` of its own, listed before the fetched one: a later file
|
||||
# overrides an earlier one, so the store wins and the file carries the agent when
|
||||
# the store is down. The create grant cannot constrain content, so the agent's own
|
||||
# process can create an empty leaf with any text; that is no wider than the file
|
||||
# it already owns (`heal_own_dir` in `./user.nix` re-owns the harness dir at boot).
|
||||
# The unit runs as root because an operator's file is root-owned `0600`.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
|
|
@ -48,13 +49,19 @@ let
|
|||
# `path::MOUNT`.
|
||||
leaf = "secret/swarm/agents/${agentName}/backend-env";
|
||||
|
||||
# The nix half of `swarm_secret_client::backend_secrets::secrets_path` plus `path::MOUNT`.
|
||||
secretsLeaf = "secret/swarm/agents/${agentName}/backend-secrets";
|
||||
|
||||
runtimeDir = unitName;
|
||||
envFile = "/run/${runtimeDir}/env";
|
||||
stagedFile = "/run/${runtimeDir}/env.new";
|
||||
stagedTextFile = "/run/${runtimeDir}/text.new";
|
||||
stagedSecretsFile = "/run/${runtimeDir}/secrets.new";
|
||||
errFile = "/run/${runtimeDir}/bao.err";
|
||||
|
||||
# An OAuth claude agent has no backend credentials to fetch.
|
||||
fetches = cfg.addr != null && (agentCfg.useApiKey || agentCfg.runtime == "acp");
|
||||
# Every agent with a store: an operator may store credentials for any of them,
|
||||
# and an agent with neither configuration nor secrets gets no file.
|
||||
fetches = cfg.addr != null;
|
||||
|
||||
earlier = agentCfg.backendEnvironmentFile;
|
||||
|
||||
|
|
@ -68,8 +75,8 @@ in
|
|||
description = ''
|
||||
The `EnvironmentFile=` entries that carry this agent's backend
|
||||
environment, in load order: `backendEnvironmentFile` when set, then the
|
||||
file `${unitName}.service` keeps when the agent has a store and uses an
|
||||
API key. Read-only: the harness and the subagent daemon take it as it is.
|
||||
file `${unitName}.service` keeps when the agent has a store. Read-only:
|
||||
the harness and the subagent daemon take it as it is.
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
@ -125,9 +132,11 @@ in
|
|||
|
||||
err=${lib.escapeShellArg errFile}
|
||||
staged=${lib.escapeShellArg stagedFile}
|
||||
staged_text=${lib.escapeShellArg stagedTextFile}
|
||||
staged_secrets=${lib.escapeShellArg stagedSecretsFile}
|
||||
live=${lib.escapeShellArg envFile}
|
||||
earlier=${lib.escapeShellArg (if earlier == null then "" else earlier)}
|
||||
trap 'rm -f "$err" "$staged"' EXIT
|
||||
trap 'rm -f "$err" "$staged" "$staged_text" "$staged_secrets"' EXIT
|
||||
|
||||
# Bounded, so a store that drops packets cannot hold the unit past its
|
||||
# start timeout.
|
||||
|
|
@ -140,51 +149,70 @@ in
|
|||
fi
|
||||
export BAO_TOKEN
|
||||
|
||||
# Sets READ to have, missing or unusable, and leaves the value in
|
||||
# $staged. `kv get` exits 2 with "No value found at <path>" for a leaf
|
||||
# nobody wrote, and 2 again for a refused read, so the message tells
|
||||
# them apart. The value goes from the store's answer into a file and
|
||||
# is never in a variable or an argument.
|
||||
# Sets READ to have, missing or unusable for the leaf at $1, and leaves
|
||||
# its field $2 in the file $3. `kv get` exits 2 with "No value found at
|
||||
# <path>" for a leaf nobody wrote, and 2 again for a refused read, so
|
||||
# the message tells them apart. The value goes from the store's answer
|
||||
# into a file and is never in a variable or an argument.
|
||||
read_leaf() {
|
||||
rm -f "$staged"
|
||||
if store kv get -field=env ${lib.escapeShellArg leaf} >"$staged" 2>"$err"; then
|
||||
if [ -s "$staged" ]; then READ=have; else READ=unusable; fi
|
||||
rm -f "$3"
|
||||
if store kv get -field="$2" "$1" >"$3" 2>"$err"; then
|
||||
if [ -s "$3" ]; then READ=have; else READ=unusable; fi
|
||||
elif grep -q '^No value found at ' "$err"; then
|
||||
READ=missing
|
||||
elif grep -q '^Field "env" not present' "$err"; then
|
||||
elif grep -q "^Field \"$2\" not present" "$err"; then
|
||||
READ=unusable
|
||||
else
|
||||
echo "could not read ${leaf}:" >&2
|
||||
echo "could not read $1:" >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
read_leaf
|
||||
if [ "$READ" = missing ]; then
|
||||
if [ -n "$earlier" ] && [ -s "$earlier" ]; then
|
||||
echo "${leaf} holds nothing; creating it from $earlier."
|
||||
if ! store kv put -cas=0 ${lib.escapeShellArg leaf} env=@"$earlier" >/dev/null 2>"$err"; then
|
||||
echo "could not create ${leaf}; reading it back in case another run did:" >&2
|
||||
cat "$err" >&2
|
||||
fi
|
||||
read_leaf
|
||||
else
|
||||
echo "${leaf} holds nothing and there is no file to create it from."
|
||||
exit 0
|
||||
read_leaf ${lib.escapeShellArg leaf} env "$staged_text"
|
||||
if [ "$READ" = missing ] && [ -n "$earlier" ] && [ -s "$earlier" ]; then
|
||||
echo "${leaf} holds nothing; creating it from $earlier."
|
||||
if ! store kv put -cas=0 ${lib.escapeShellArg leaf} env=@"$earlier" >/dev/null 2>"$err"; then
|
||||
echo "could not create ${leaf}; reading it back in case another run did:" >&2
|
||||
cat "$err" >&2
|
||||
fi
|
||||
read_leaf ${lib.escapeShellArg leaf} env "$staged_text"
|
||||
fi
|
||||
if [ "$READ" != have ]; then
|
||||
echo "${leaf} holds no usable env; the file in place, if any, is used." >&2
|
||||
TEXT=$READ
|
||||
read_leaf ${lib.escapeShellArg secretsLeaf} env "$staged_secrets"
|
||||
SECRETS=$READ
|
||||
|
||||
if [ "$TEXT" = unusable ] || [ "$SECRETS" = unusable ]; then
|
||||
echo "${leaf} or ${secretsLeaf} holds no usable value; the file in place, if any, is used." >&2
|
||||
exit 0
|
||||
fi
|
||||
if [ "$TEXT" != have ] && [ "$SECRETS" != have ]; then
|
||||
# Both were deleted, or never written: nothing the store holds may
|
||||
# outlive its removal, or an operator who takes the key away would
|
||||
# still see the agent bill the API.
|
||||
rm -f "$live"
|
||||
echo "${leaf} and ${secretsLeaf} hold nothing and there is no file to create them from."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# The configuration first and the secrets after it, so a secret wins when
|
||||
# both name a variable, as a later file does over an earlier one.
|
||||
{
|
||||
if [ "$TEXT" = have ]; then cat "$staged_text"; fi
|
||||
if [ "$SECRETS" = have ]; then
|
||||
if [ "$TEXT" = have ] && [ "$(tail -c1 "$staged_text" | od -An -tx1 | tr -d ' \n')" != 0a ]; then
|
||||
echo
|
||||
fi
|
||||
cat "$staged_secrets"
|
||||
fi
|
||||
} >"$staged"
|
||||
|
||||
if cmp -s "$staged" "$live"; then
|
||||
echo "this agent's backend environment at ${leaf} is unchanged."
|
||||
echo "this agent's backend environment at ${leaf} and ${secretsLeaf} is unchanged."
|
||||
exit 0
|
||||
fi
|
||||
mv -f "$staged" "$live"
|
||||
echo "fetched this agent's backend environment from ${leaf}."
|
||||
echo "fetched this agent's backend environment from ${leaf} and ${secretsLeaf}."
|
||||
'';
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue