Files
loki/scripts/hetzner/test-builder-auth-check.sh
CatoandClaude Opus 5.5 5f1e283117 fix(scripts): grep -q is never the reader of a pipe under pipefail (#874)
`echo "$x" | grep -q PAT` under `set -o pipefail` can report "not found"
for text that IS there: grep -q exits on its first match and closes the
pipe, the writer takes SIGPIPE (141), and pipefail makes the pipeline
status 141. An `if` then takes the else branch on a successful match.

#587 found this in one file and noted it "only fires when the payload
exceeds the ~4KB pipe buffer". That is not so, and the belief is why
82 copies in 22 scripts survived. Measured today on retire-site's real
1,158-byte plan output with has()'s exact pattern, 3,000 calls each:

  'DRY RUN'        line 3 of 20    echo|grep -q   2/3000 false misses
  'apps.conf row'  line 11 of 20   echo|grep -q   2/3000 false misses
  last phrase      line 20 of 20   echo|grep -q   0/3000
  'DRY RUN'                        here-string    0/3000

Rare per call, but verify makes hundreds of these calls, and the
retire-site suite went red on those two exact phrases twice this week,
passing every rerun. It is worse in a NEGATIVE check - hasnt() read a
false miss as a PASS. And several copies are not tests: deploy-hetzner.sh
exits 1 on a false miss, and the sshd drift watch would raise a false
security alert.

All 82 rewritten to `grep -q PAT <<<"$x"` (a here-string is not a
pipeline, so there is no writer to signal) by a script that only touched
the simple quoted-variable form and reported anything else. Checked: all
22 files pass `bash -n`, every file is a 1:1 line swap (82 lines), and
the 20 line-continuation sites keep the here-string before the trailing
backslash (the first draft of the rewrite got those wrong; caught in a
dry-run diff before anything was written).

#587's comment is corrected so it stops teaching the wrong threshold.

Gate: scripts/test/no-grep-q-in-a-pipe.ts ratchets it repo-wide,
including forms the rewrite skipped (e.g. "$(cmd)" | grep -q), and
self-tests that its detector still fires. Proven by restoring the
original deploy-hetzner.sh line.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 02:57:19 -07:00

69 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# builder-auth-check.sh must tell the three states apart from what Claude Code
# actually prints, and must never leak the token. The "disabled" fixture is
# the verbatim line from 2026-09-14.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$HERE/builder-auth-check.sh"
pass=0; fail=0
ok() { if [ "$1" = 0 ]; then pass=$((pass+1)); else fail=$((fail+1)); echo " ✗ $2"; fi }
# On a miss, SHOW what the command actually printed. A bare "expected output
# to mention 'DRY RUN'" is undiagnosable after the fact: this suite failed once
# inside a full `verify` on 2026-09-22 and passed 25/25 standalone, and there
# was no way to tell what the script had said instead. An assertion that hides
# its input turns an intermittent failure into a guess.
has() {
if grep -qi -- "$2" <<<"$1"; then ok 0 ""; else
ok 1 "expected output to mention '$2'"
printf ' got (first 400 chars): %s\n' "$(printf '%s' "$1" | head -c 400 | tr '\n' '|')"
fi
}
hasnt() { grep -qi -- "$2" <<<"$1" && ok 1 "output must NOT mention '$2'" || ok 0 ""; }
run() { PROBE_OUTPUT="$1" PROBE_EXIT="${2:-0}" bash "$SCRIPT" --report 2>&1; }
echo "→ the 2026-09-14 line is DISABLED, and the message says what to do"
out=$(run "Your organization has disabled Claude subscription access for Claude Code · Use an Anthropic API key instead, or ask your admin to enable access" 1)
has "$out" "^disabled:"
has "$out" "ANTHROPIC_API_KEY"
has "$out" "setup-token"
has "$out" "loki-box-runner"
echo "→ an expired or invalid token is DISABLED too"
out=$(run "OAuth token has expired. Please log in again." 1)
has "$out" "^disabled:"
echo "→ a real answer is OK"
out=$(run "ok" 0)
has "$out" "^ok:"
echo "→ a timeout or missing CLI is DOWN, not ok and not disabled"
out=$(run "" 124)
has "$out" "^down:"
hasnt "$out" "^ok:"
out=$(run "claude CLI missing at /nowhere" 3)
has "$out" "^down:"
echo "→ --report never touches alert state (no lib-alert needed)"
out=$(MON=/nonexistent run "ok" 0)
has "$out" "^ok:"
echo "→ the runner env is read for the token but the token never appears in output"
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
printf 'ANTHROPIC_API_KEY=sk-ant-api03-SECRETSECRET\nCLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-ALSOSECRET\n' > "$tmp/.env"
out=$(RUNNER_ENV="$tmp/.env" CLAUDE_BIN=/nonexistent/claude bash "$SCRIPT" --report 2>&1)
has "$out" "^down:"
has "$out" "claude CLI missing"
hasnt "$out" "SECRETSECRET"
hasnt "$out" "ALSOSECRET"
echo "→ no env credential and no sign-in is DOWN with the instruction, not DISABLED"
: > "$tmp/.env"
out=$(RUNNER_ENV="$tmp/.env" CLAUDE_BIN=/bin/true RUNNER_HOME="$tmp/nohome" RUNNER_USER=nobody bash "$SCRIPT" --report 2>&1)
has "$out" "^down:"
has "$out" "no env credential and no sign-in"
echo
echo "builder-auth-check: passed $pass, failed $fail"
[ "$fail" -eq 0 ]