Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/script-tests/agent-backend-env-fake-bao.sh
iris 33fdd5205e 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.
2026-11-20 20:36:33 +01:00

80 lines
2.7 KiB
Shell
Executable file

#!/usr/bin/env bash
# The stub `bao` for ./agent-backend-env.nix. Leaves are files under
# `$FAKE_BAO_KV`, one per path. Every call is logged, a `kv` call without the
# token `login` printed fails, and the answers are the ones openbao gives:
# `kv get` of a leaf nobody wrote exits 2 with "No value found at <path>", and
# a policy granting `create` and `read` only refuses a second `-cas=0` write
# with a 403.
#
# `FAKE_BAO_LOGIN_RC` makes the login fail, `FAKE_BAO_DENY_READ` and
# `FAKE_BAO_DENY_WRITE` answer 403 as a policy that grants neither does, and
# `FAKE_BAO_RACE` has another writer create the leaf with that text between
# this run's first read and its write.
echo "$*" >>"$FAKE_BAO_LOG"
case "$1 $2" in
"login -method=cert")
[ "${FAKE_BAO_LOGIN_RC-0}" = 0 ] || {
echo 'Error authenticating: Put "https://bao.t.local:8200/v1/auth/cert/login": dial tcp: lookup bao.t.local: no such host' >&2
exit 2
}
printf 's.fake'
;;
"kv get" | "kv put")
[ "${BAO_TOKEN-}" = s.fake ] || {
echo "fake bao: kv called without the login's token" >&2
exit 97
}
;;&
"kv get")
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
exit 2
}
if [ -n "${FAKE_BAO_RACE-}" ] && [ ! -e "$FAKE_BAO_KV/.raced" ]; then
: >"$FAKE_BAO_KV/.raced"
printf '%s' "$FAKE_BAO_RACE" >"$leaf"
echo "No value found at ${4/secret\//secret/data/}" >&2
exit 2
fi
if [ ! -e "$leaf" ]; then
echo "No value found at ${4/secret\//secret/data/}" >&2
exit 2
fi
if [ "$(cat "$leaf.field" 2>/dev/null)" = other ]; then
echo "Field \"${3#-field=}\" not present in secret" >&2
exit 1
fi
cat "$leaf"
;;
"kv put")
[ "$3" = -cas=0 ] || {
echo "fake bao: put without -cas=0: $*" >&2
exit 96
}
leaf=$FAKE_BAO_KV/$(printf '%s' "$4" | tr / _)
case "$5" in
env=@*) ;;
*)
echo "fake bao: put of something other than env=@file: $*" >&2
exit 94
;;
esac
if [ -n "${FAKE_BAO_DENY_WRITE-}" ] || [ -e "$leaf" ]; then
printf 'Error writing data to %s: Error making API request.\n\nURL: PUT https://bao.t.local:8200/v1/%s\nCode: 403. Errors:\n\n* 1 error occurred:\n\t* permission denied\n' "$4" "$4" >&2
exit 2
fi
cp "${5#env=@}" "$leaf"
;;
*)
echo "fake bao: unexpected call: $*" >&2
exit 98
;;
esac