Files
loki/scripts/hetzner/test-first-admin.sh
CatoandClaude 1236487dd8 feat(site-cd): name the first admin when a site is created (#1779)
When Loki registers a new site for a person, their OrangeCat subject is
written to the site's runtime env as OWNER_ACTOR_IDS (fleet AGENTS.md,
"The back office", rule 2). Only in the run that creates the env, never
over a line already there, and only when the subject is a plain account
id. No linked OrangeCat account: skipped with one log line.


Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-30 22:23:20 +01:00

152 lines
7.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tests for first-admin-on-box.sh, its remote half run locally (no box, no
# network), and for the way register-site.sh calls it.
#
# What must hold, because the line decides who runs a product:
# - a new site's env gets OWNER_ACTOR_IDS once, and its other lines are kept;
# - a line already there is never replaced and never added to, whatever it
# holds, and a re-run changes nothing;
# - an env that does not exist is not created;
# - a subject that is not a plain account id is refused before ssh is called;
# - the subject is never printed;
# - register-site.sh calls it only in the run that created the env.
#
# `ssh` is stubbed on PATH: it drops its options and the host, then runs the
# rest (`bash -s -- <slug> <subject>`) with local bash, and records that it was
# called. FIRST_ADMIN_OPT points the remote half at a temp dir for /opt.
#
# Run: bash scripts/hetzner/test-first-admin.sh
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
PASSED=0
fail() { echo " ✗ $1" >&2; exit 1; }
ok() { PASSED=$((PASSED + 1)); echo " ✓ $1"; }
mkdir -p "$TMP/bin"
cat > "$TMP/bin/ssh" <<STUB
#!/usr/bin/env bash
echo called >> "$TMP/ssh-calls"
while [ \$# -gt 0 ] && [ "\$1" != bash ]; do shift; done
exec "\$@"
STUB
chmod +x "$TMP/bin/ssh"
OPT="$TMP/opt"
SUB="3f2b8c1e-9a4d-4e6f-8b1a-0c2d3e4f5a6b"
OTHER="9d8c7b6a-5f4e-4d3c-8b2a-1f0e9d8c7b6a"
run() { PATH="$TMP/bin:$PATH" FIRST_ADMIN_OPT="$OPT" DEPLOY_KEY_PATH="" bash "$HERE/first-admin-on-box.sh" "$@" 2>&1; }
count() { grep -cE "^OWNER_ACTOR_IDS=" "$1" || true; }
calls() { [ -f "$TMP/ssh-calls" ] && wc -l < "$TMP/ssh-calls" | tr -d ' ' || echo 0; }
echo
echo "a new site's env gets the line once"
E="$OPT/fresh/shared/.env"; mkdir -p "$(dirname "$E")"
printf 'NODE_ENV=production\nPORT=4050\n' > "$E"
OUT=$(run fresh "$SUB"); RC=$?
[ "$RC" = 0 ] || fail "must succeed (rc=$RC): $OUT"
[ "$(cat "$E")" = "$(printf 'NODE_ENV=production\nPORT=4050\nOWNER_ACTOR_IDS=%s' "$SUB")" ] || fail "env is not the old lines plus the new one: $(cat "$E")"
ok "the line is appended and the lines before it are kept"
grep -q "$SUB" <<<"$OUT" && fail "the subject was printed: $OUT"
ok "the subject is not printed"
echo
echo "a re-run changes nothing"
before=$(cksum < "$E")
OUT=$(run fresh "$SUB"); RC=$?
[ "$RC" = 0 ] || fail "a re-run must succeed (rc=$RC): $OUT"
[ "$(cksum < "$E")" = "$before" ] && [ "$(count "$E")" = 1 ] || fail "a re-run changed the env"
ok "same subject again: one line, the file untouched"
OUT=$(run fresh "$OTHER")
[ "$(cksum < "$E")" = "$before" ] || fail "another subject replaced or joined the first"
ok "another subject later: the first admin stays, nothing is added"
echo
echo "a line somebody already wrote is left alone, whatever it holds"
for line in 'OWNER_ACTOR_IDS=u_1, u_2' 'OWNER_ACTOR_IDS=' 'export OWNER_ACTOR_IDS=u_1'; do
E="$OPT/held/shared/.env"; mkdir -p "$(dirname "$E")"
printf 'PORT=4051\n%s\nSITE=x\n' "$line" > "$E"
before=$(cksum < "$E")
OUT=$(run held "$SUB"); RC=$?
[ "$RC" = 0 ] && [ "$(cksum < "$E")" = "$before" ] || fail "'$line' was not left alone (rc=$RC): $(cat "$E")"
done
ok "a list, an empty value and an exported line are all kept as they are"
echo
echo "a last line without its newline does not swallow the new one"
E="$OPT/nonl/shared/.env"; mkdir -p "$(dirname "$E")"
printf 'NODE_ENV=production\nPORT=4052' > "$E"
run nonl "$SUB" >/dev/null
[ "$(cat "$E")" = "$(printf 'NODE_ENV=production\nPORT=4052\nOWNER_ACTOR_IDS=%s' "$SUB")" ] || fail "lines ran together: $(cat "$E")"
ok "PORT keeps its value and the new line stands alone"
echo
echo "an env that is not there is not created"
OUT=$(run nosite "$SUB"); RC=$?
[ "$RC" != 0 ] || fail "must fail when the site has no env: $OUT"
[ ! -e "$OPT/nosite" ] || fail "something was created for a site with no env"
ok "no env: refused, nothing created"
echo
echo "a subject that is not a plain account id never reaches ssh"
E="$OPT/fresh2/shared/.env"; mkdir -p "$(dirname "$E")"
printf 'PORT=4053\n' > "$E"
n=$(calls)
for bad in '' 'abc12' "$(printf 'a%.0s' $(seq 65))" '-abc123' 'abc 123x' 'abc123;id' 'abc123$(id)' 'abc123`id`' "$(printf 'abc123\nPORT=1')" 'a@b.example' 'abc,def123' "abc123'x"; do
OUT=$(run fresh2 "$bad"); RC=$?
[ "$RC" = 2 ] || fail "'$bad' was not refused with 2 (rc=$RC): $OUT"
done
[ "$(calls)" = "$n" ] || fail "ssh was called for a refused subject"
[ "$(cat "$E")" = "PORT=4053" ] || fail "a refused subject changed the env"
ok "too short, too long, a leading dash, spaces, shell characters, a newline, an address and a list are refused"
OUT=$(run 'Bad Slug' "$SUB"); RC=$?
[ "$RC" = 2 ] && [ "$(calls)" = "$n" ] || fail "a bad slug was not refused before ssh"
ok "a slug that is not a slug is refused too"
OUT=$(run fresh2 'usr_Ab3-9Z'); RC=$?
[ "$RC" = 0 ] && grep -qx 'OWNER_ACTOR_IDS=usr_Ab3-9Z' "$E" || fail "an opaque id of the same alphabet must be accepted (rc=$RC)"
ok "an opaque id of the same alphabet is accepted"
echo
echo "register-site.sh names the first admin only in the run that created the env"
R="$HERE/register-site.sh"
mkdir -p "$TMP/fc/scripts/hetzner" "$TMP/dev" "$TMP/rel"
cp "$HERE"/*.sh "$HERE"/launch.sh.tmpl "$TMP/rel/" 2>/dev/null
printf 'old-app|4010|old.example.com|/nowhere/old|.|-|bitbaum|product|live|-|-|-\n' | tee "$TMP/fc/scripts/hetzner/apps.conf" > "$TMP/rel/apps.conf"
touch "$TMP/key"
dry() {
DEV_ROOT="$TMP/dev" LOKI_REPO_ROOT="$TMP/fc" DEPLOY_KEY_PATH="$TMP/key" SITES_BASE_DOMAIN=example.com \
bash "$TMP/rel/register-site.sh" "$@" --dry-run 2>&1
}
OUT=$(dry fresh-site --repo owner/fresh-site --first-admin "$SUB"); RC=$?
[ "$RC" = 0 ] || fail "dry-run with a first admin must succeed (rc=$RC): $OUT"
grep -q "would name the first admin (OWNER_ACTOR_IDS)" <<<"$OUT" || fail "dry-run must say the first admin would be named: $OUT"
grep -q "$SUB" <<<"$OUT" && fail "register-site printed the subject: $OUT"
ok "with --first-admin the dry run says so, without printing the subject"
OUT=$(dry fresh-site --repo owner/fresh-site); RC=$?
[ "$RC" = 0 ] || fail "dry-run without a first admin must succeed (rc=$RC): $OUT"
grep -qi "first admin" <<<"$OUT" && fail "no --first-admin must name nobody: $OUT"
ok "without --first-admin nothing about a first admin happens"
OUT=$(dry fresh-site --repo owner/fresh-site --first-admin 'abc123;id'); RC=$?
[ "$RC" = 2 ] || fail "a bad --first-admin must be refused with 2 (rc=$RC): $OUT"
grep -q "validating" <<<"$OUT" && fail "a bad --first-admin must be refused before anything else runs: $OUT"
grep -q 'abc123;id' <<<"$OUT" && fail "the refused value was printed: $OUT"
ok "a --first-admin that fails the pattern is refused first, and not repeated"
# The call sits behind the "created" answer of the one branch that writes a
# brand-new env, and nowhere else in the registrar.
[ "$(grep -c 'first-admin-on-box.sh" "\$SLUG" "\$FIRST_ADMIN"' "$R")" = 1 ] || fail "register-site.sh must call first-admin-on-box.sh exactly once"
grep -B2 'first-admin-on-box.sh" "\$SLUG" "\$FIRST_ADMIN"' "$R" | grep -q 'elif \[ "\$ENV_STATE" != created \]; then' \
|| fail "the call must sit behind the ENV_STATE != created guard"
[ "$(grep -c 'echo created' "$R")" = 1 ] && grep -q "PORT=\$PORT\\\\n' > /opt/\$SLUG/shared/.env); echo created; fi" "$R" \
|| fail "'created' must be printed only by the branch that writes a new env"
ok "the call is guarded by the env having been created in this run"
for other in new-site.sh provision-db-on-box.sh deploy.sh sync-infra.sh app-secrets.sh; do
grep -q 'OWNER_ACTOR_IDS\|first-admin' "$HERE/$other" && fail "$other must not name a first admin"
done
ok "no other provisioning or deploy script writes it"
echo
echo "first-admin: $PASSED passed"