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
|
|
@ -26,10 +26,13 @@ case "$1 $2" in
|
|||
}
|
||||
;;&
|
||||
"kv get")
|
||||
[ "$3" = -field=env ] || {
|
||||
echo "fake bao: get without -field=env: $*" >&2
|
||||
exit 95
|
||||
}
|
||||
case "$3" in
|
||||
-field=env | -field=key) ;;
|
||||
*)
|
||||
echo "fake bao: get without -field=env or -field=key: $*" >&2
|
||||
exit 95
|
||||
;;
|
||||
esac
|
||||
leaf=$FAKE_BAO_KV/$(printf '%s' "$4" | tr / _)
|
||||
[ -z "${FAKE_BAO_DENY_READ-}" ] || {
|
||||
printf 'Error reading %s: Error making API request.\n\nURL: GET https://bao.t.local:8200/v1/%s\nCode: 403. Errors:\n\n* 1 error occurred:\n\t* permission denied\n' "$4" "$4" >&2
|
||||
|
|
@ -46,7 +49,7 @@ case "$1 $2" in
|
|||
exit 2
|
||||
fi
|
||||
if [ "$(cat "$leaf.field" 2>/dev/null)" = other ]; then
|
||||
echo 'Field "env" not present in secret' >&2
|
||||
echo "Field \"${3#-field=}\" not present in secret" >&2
|
||||
exit 1
|
||||
fi
|
||||
cat "$leaf"
|
||||
|
|
|
|||
|
|
@ -13,8 +13,10 @@ fail() {
|
|||
}
|
||||
|
||||
leaf=secret/swarm/agents/a1/backend-env
|
||||
secrets_leaf=secret/swarm/agents/a1/backend-secrets
|
||||
login_call="login -method=cert -token-only"
|
||||
get_call="kv get -field=env $leaf"
|
||||
secrets_call="kv get -field=env $secrets_leaf"
|
||||
|
||||
# setup <description>
|
||||
setup() {
|
||||
|
|
@ -43,6 +45,7 @@ setup() {
|
|||
deny_write=
|
||||
race=
|
||||
stored=$kv/$(printf '%s' "$leaf" | tr / _)
|
||||
stored_secrets=$kv/$(printf '%s' "$secrets_leaf" | tr / _)
|
||||
}
|
||||
|
||||
go() {
|
||||
|
|
@ -124,6 +127,11 @@ expect_leaf() {
|
|||
expect_same "$stored" "$1"
|
||||
}
|
||||
|
||||
# A secrets leaf holding <text> before the run.
|
||||
put_secrets() {
|
||||
printf '%s' "$1" >"$stored_secrets"
|
||||
}
|
||||
|
||||
file_text=$'ANTHROPIC_API_KEY=k-file\nANTHROPIC_BASE_URL=http://x\n'
|
||||
|
||||
setup "no store identity delivered"
|
||||
|
|
@ -139,7 +147,7 @@ setup "a store holding nothing is seeded from the file, bytes unchanged, and the
|
|||
put_earlier "$file_text"
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call" "kv put -cas=0 $leaf env=@$earlier" "$get_call"
|
||||
expect_calls "$login_call" "$get_call" "kv put -cas=0 $leaf env=@$earlier" "$get_call" "$secrets_call"
|
||||
expect_leaf "$file_text"
|
||||
expect_same "$rt/env" "$file_text"
|
||||
expect_earlier_untouched "$file_text"
|
||||
|
|
@ -160,7 +168,7 @@ put_earlier "$file_text"
|
|||
put_leaf $'ANTHROPIC_API_KEY=k-bao\n'
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call"
|
||||
expect_calls "$login_call" "$get_call" "$secrets_call"
|
||||
expect_leaf $'ANTHROPIC_API_KEY=k-bao\n'
|
||||
expect_same "$rt/env" $'ANTHROPIC_API_KEY=k-bao\n'
|
||||
expect_earlier_untouched "$file_text"
|
||||
|
|
@ -189,8 +197,8 @@ expect_no_file "$rt/env.new"
|
|||
setup "no file and nothing in the store writes nothing"
|
||||
go
|
||||
expect_rc 0
|
||||
expect_out "no file to create it from"
|
||||
expect_calls "$login_call" "$get_call"
|
||||
expect_out "no file to create them from"
|
||||
expect_calls "$login_call" "$get_call" "$secrets_call"
|
||||
expect_no_file "$rt/env"
|
||||
expect_no_file "$stored"
|
||||
|
||||
|
|
@ -198,16 +206,17 @@ setup "an empty file is never stored"
|
|||
put_earlier ""
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call"
|
||||
expect_calls "$login_call" "$get_call" "$secrets_call"
|
||||
expect_no_file "$rt/env"
|
||||
expect_no_file "$stored"
|
||||
|
||||
setup "nothing in the store leaves an env fetched earlier alone"
|
||||
# An operator who takes the key away must not see the agent keep loading it.
|
||||
setup "nothing in the store removes an env fetched earlier"
|
||||
printf kept >"$rt/env"
|
||||
chmod 0600 "$rt/env"
|
||||
go
|
||||
expect_rc 0
|
||||
expect_same "$rt/env" kept
|
||||
expect_no_file "$rt/env"
|
||||
|
||||
setup "a store that refuses the login with a file in place changes nothing and fails the unit"
|
||||
put_earlier "$file_text"
|
||||
|
|
@ -226,7 +235,7 @@ put_earlier "$file_text"
|
|||
race=$'ANTHROPIC_API_KEY=k-winner\n'
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call" "kv put -cas=0 $leaf env=@$earlier" "$get_call"
|
||||
expect_calls "$login_call" "$get_call" "kv put -cas=0 $leaf env=@$earlier" "$get_call" "$secrets_call"
|
||||
expect_err "could not create $leaf"
|
||||
expect_leaf $'ANTHROPIC_API_KEY=k-winner\n'
|
||||
expect_same "$rt/env" $'ANTHROPIC_API_KEY=k-winner\n'
|
||||
|
|
@ -237,9 +246,9 @@ put_earlier "$file_text"
|
|||
deny_write=1
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call" "kv put -cas=0 $leaf env=@$earlier" "$get_call"
|
||||
expect_calls "$login_call" "$get_call" "kv put -cas=0 $leaf env=@$earlier" "$get_call" "$secrets_call"
|
||||
expect_err "permission denied"
|
||||
expect_err "holds no usable env"
|
||||
expect_out "hold nothing"
|
||||
expect_no_file "$rt/env"
|
||||
expect_no_file "$stored"
|
||||
expect_earlier_untouched "$file_text"
|
||||
|
|
@ -249,8 +258,8 @@ put_earlier "$file_text"
|
|||
put_leaf ""
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call"
|
||||
expect_err "holds no usable env"
|
||||
expect_calls "$login_call" "$get_call" "$secrets_call"
|
||||
expect_err "holds no usable value"
|
||||
expect_leaf ""
|
||||
expect_no_file "$rt/env"
|
||||
|
||||
|
|
@ -260,7 +269,7 @@ put_leaf "x"
|
|||
printf other >"$stored.field"
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call"
|
||||
expect_calls "$login_call" "$get_call" "$secrets_call"
|
||||
expect_leaf "x"
|
||||
expect_no_file "$rt/env"
|
||||
|
||||
|
|
@ -275,6 +284,49 @@ expect_calls "$login_call" "$get_call"
|
|||
expect_no_file "$rt/env"
|
||||
expect_earlier_untouched "$file_text"
|
||||
|
||||
setup "secrets alone are written as they are"
|
||||
put_secrets $'ANTHROPIC_API_KEY=sk-ant-key\n'
|
||||
go
|
||||
expect_rc 0
|
||||
expect_calls "$login_call" "$get_call" "$secrets_call"
|
||||
expect_same "$rt/env" $'ANTHROPIC_API_KEY=sk-ant-key\n'
|
||||
if [ "$(stat -c %a "$rt/env")" != 600 ]; then fail "env is mode $(stat -c %a "$rt/env"), expected 600"; fi
|
||||
expect_no_file "$rt/env.new"
|
||||
expect_no_file "$rt/secrets.new"
|
||||
expect_no_file "$rt/text.new"
|
||||
|
||||
setup "the secrets follow the configuration, so they win over a variable it names"
|
||||
put_leaf $'ANTHROPIC_BASE_URL=http://x\nANTHROPIC_API_KEY=from-text\n'
|
||||
put_secrets $'ANTHROPIC_API_KEY=sk-ant-key\n'
|
||||
go
|
||||
expect_rc 0
|
||||
expect_same "$rt/env" $'ANTHROPIC_BASE_URL=http://x\nANTHROPIC_API_KEY=from-text\nANTHROPIC_API_KEY=sk-ant-key\n'
|
||||
|
||||
setup "a configuration with no final newline still gets the secrets on a line of their own"
|
||||
put_leaf "ANTHROPIC_BASE_URL=http://x"
|
||||
put_secrets $'ANTHROPIC_API_KEY=sk-ant-key\n'
|
||||
go
|
||||
expect_rc 0
|
||||
expect_same "$rt/env" $'ANTHROPIC_BASE_URL=http://x\nANTHROPIC_API_KEY=sk-ant-key\n'
|
||||
|
||||
setup "secrets an operator removed leave the configuration alone in the file"
|
||||
put_leaf $'ANTHROPIC_BASE_URL=http://x\n'
|
||||
printf 'ANTHROPIC_BASE_URL=http://x\nANTHROPIC_API_KEY=old\n' >"$rt/env"
|
||||
chmod 0600 "$rt/env"
|
||||
go
|
||||
expect_rc 0
|
||||
expect_same "$rt/env" $'ANTHROPIC_BASE_URL=http://x\n'
|
||||
|
||||
setup "a secrets leaf without the env field changes nothing"
|
||||
put_secrets "x"
|
||||
printf other >"$stored_secrets.field"
|
||||
printf kept >"$rt/env"
|
||||
chmod 0600 "$rt/env"
|
||||
go
|
||||
expect_rc 0
|
||||
expect_err "holds no usable value"
|
||||
expect_same "$rt/env" kept
|
||||
|
||||
# `UMask=0077` makes every file the script creates 0600; a stale leftover of a
|
||||
# stopped run must not block the next.
|
||||
setup "leftovers of a stopped run do not block the fetch"
|
||||
|
|
|
|||
Loading…
Reference in a new issue