Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/script-tests/nats-render-config.sh
atlas c10cc0e018 swarm-nats: take the callout issuer and user keys from the store
`swarm-bao-nats-callout-xkey` runs `ensure-callout-keys`, importing
/var/lib/swarm-nats-callout/{issuer,callout-user}.seed when present so the
keys keep their public halves, then fetches all three public halves from
the store. The verb removes each seed file once a read of the store returns
a seed; when the store's seed differs, it is kept and a warning names the
file. No tmpfiles rule touches those files, since one could run before the
import with the store sealed and leave the next run to mint a new key.

The server renders the issuer and user public halves from the fetched files,
checked for shape as the xkey's is, and does not start without them. The
responder reads both seeds from the store, so the copies into the container
and their LoadCredential entries are gone, and a tmpfiles rule removes the
copies a container still holds. `swarm-nats-callout-keys` is removed.

On the store's host the fetch unit is also ordered after
`swarm-bao-secret-publisher-policy`, so the publisher's denial of the seed
paths is written before the unit creates or keeps a seed there.

`deploy.nats.autoGenerateCallout`, `calloutUserPublicKey`,
`calloutIssuerPublicKey`, `calloutUserSeedFile` and `calloutIssuerSeedFile`
are removed with `mkRemovedOptionModule`, under `swarm.nats` too, each
message saying the keys live in the store.
2026-10-06 14:53:33 +02:00

92 lines
3 KiB
Shell

# Cases for ./nats-render-config.nix. Each case gets a fresh runtime directory,
# runs the rendered script against it, and asserts on the exit code and the
# config files left behind.
set -uo pipefail
failures=0
count=0
fail() {
echo "FAILED: $case: $*" >&2
failures=$((failures + 1))
}
# The shapes the script checks for: the key type's letter and 55 base32
# characters.
key=XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
issuer=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
user=UAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
setup() {
case=$1
dir=$(mktemp -d)
rt=$dir/rt
mkdir "$rt"
if ! sed "s|^d=.*|d=$rt|" "$RENDER" >"$dir/render"; then
echo "cannot read the rendered script $RENDER" >&2
exit 1
fi
chmod +x "$dir/render"
if ! grep -qxF "d=$rt" "$dir/render"; then fail "the rewrite did not reach the script's runtime directory"; fi
}
# All three public keys, as the fetch unit leaves them.
fetched() {
printf '%s\n' "$key" >"$rt/callout-xkey.pub"
printf '%s\n' "$issuer" >"$rt/issuer.pub"
printf '%s\n' "$user" >"$rt/callout-user.pub"
}
go() {
count=$((count + 1))
"$dir/render" >"$dir/out" 2>"$dir/err"
rc=$?
}
refused() {
go
if [ "$rc" -eq 0 ]; then fail "exited 0"; fi
if [ -e "$rt/callout.conf" ] || [ -e "$rt/nats.conf" ]; then fail "a config was written"; fi
}
setup "the fetched keys are rendered into auth_callout and the callout account, and the server's config includes them"
fetched
go
if [ "$rc" -ne 0 ]; then fail "exit $rc: $(cat "$dir/err")"; fi
if ! jq -e --arg k "$key" '.authorization.auth_callout.xkey == $k' "$rt/callout.conf" >/dev/null; then
fail "callout.conf does not carry the xkey as auth_callout.xkey"
fi
if ! jq -e --arg k "$issuer" '.authorization.auth_callout.issuer == $k' "$rt/callout.conf" >/dev/null; then
fail "callout.conf does not carry the issuer key as auth_callout.issuer"
fi
if ! jq -e --arg k "$user" '.authorization.auth_callout.auth_users == [$k] and .accounts.AUTH.users == [{nkey: $k}]' "$rt/callout.conf" >/dev/null; then
fail "callout.conf does not carry the user key as the one exempt user and the callout account's one member"
fi
if ! grep -qxF 'include "callout.conf"' "$rt/nats.conf"; then fail "nats.conf does not include callout.conf"; fi
setup "with no key file the start fails and writes no config"
refused
for file in callout-xkey.pub issuer.pub callout-user.pub; do
setup "with no $file the start fails and writes no config"
fetched
rm "$rt/$file"
refused
setup "a $file that is not its kind of public key fails the start and writes no config"
fetched
printf '%s' "not-a-key" >"$rt/$file"
refused
done
# Each key in the other's file: right shape, wrong letter.
setup "an issuer key where the user key belongs fails the start and writes no config"
fetched
printf '%s' "$issuer" >"$rt/callout-user.pub"
refused
if [ "$failures" -ne 0 ]; then
echo "script-test-nats-render-config: $failures of $count cases broke" >&2
exit 1
fi
echo "$count cases hold in script-test-nats-render-config"