Files
fleet/scripts/ci/bus-factor-audit.sh
CatoandClaude Opus 5 1bcf33fd9a feat(fleet): find a value copied into code that already has one home (#100)
* feat(fleet): find a value copied into code that already has one home

Renaming aoz-housing took four PRs across three repos and still missed things,
because the name had been copied where nothing connects it: a package name, a
GitHub URL builder, a seed map, a site override key, a JSON key in another repo
entirely. None of it broke loudly. A retired host answers 308, a renamed repo
redirects, a stale slug matches until the day it does not — so every copy looked
healthy right up to the moment it was wrong.

The first sweep found 495 across 16 repos, including a retired product name
serving on loki's public footer and a seed script that would have written two
retired hosts into the database.

A literal is a finding only when it duplicates a source of truth, or is wrong:
retired values (from registers/retired.json), a public IP in a connection (the
box address lives in loki _box-env.sh and in vars.HETZNER_IP), one person's home
directory (DEV_ROOT), a consumer mail address in source, and an arbitrary hex
class (globals.css tokens).

THIS AUDIT CONTAINS NO EXAMPLE OF WHAT IT HUNTS. One that hardcoded the box IP
and the operator's address in order to find them would be the bug it reports,
and in a public repo would publish the address too. The retired strings come
from the register — which is why `maonakamoto` is now a kind:"account" row in
retired.json rather than a literal inside a detector.

Not findings, deliberately: comments, including lines INSIDE block comments,
because the history of an incident is correct under the name it had then;
markdown, which org-drift already polices; registers, *.allow, *.baseline and
lockfiles, which ARE the source; test fixtures, in their own tier and outside
the ratchet; and hardcoded-values.allow, where every exception carries a reason
a reader can judge. An allow entry matching nothing is itself reported: an
allowlist that cannot shrink is a ratchet that cannot fall.

It caught its own bugs three times, which is the part worth trusting. The first
generic rules read CIDR tables as box addresses and import paths like
@/components/home/sections as home directories (ip 66 -> 26, home 84 -> 43).
Tightening the home rule to demand a trailing slash then LOST `default_cwd
"/home/g"`, and the stale-allowlist check surfaced it. And arbitrary-hex
reported 0 across 44 repos — believable only because a raw grep agreed.

25 assertions pin both directions of every rule against fixtures.

Two fixes in fleet itself:

bus-factor-audit.sh kept its own copy of the box IP; it now sources _box-env.sh
when the loki checkout is there and otherwise requires the value, naming where
it lives.

deploy-freshness-audit.mjs NEVER SAW orangecat. It matched deploy workflows on
/deploy/i and orangecat's is cd.yml, so the largest repo in the org has been
outside that audit since the day I wrote it — not reported clean, never looked
at. Found today, while orangecat's main had not reached the box since 483cb9e0.
Coverage 21 -> 22, and the audit now judges it.

Baseline set from the fleet AFTER this sweep's repo cleanups (348 runtime, down
from 495), so it is a floor rather than a comfortable number.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(bus-factor): require the box address where it is USED, not where it is set

Requiring HETZNER_IP at assignment aborted the audit's own offline test suite —
9 of 10 cases failed on a script that had not yet decided to ssh anywhere. The
address is only needed under --box, so that is where the check belongs. Still no
literal default: BOX, else BOX_UBUNTU from _box-env.sh, else HETZNER_IP, else a
clear refusal naming all three. The usage text no longer promises a default that
does not exist.

test-bus-factor-audit.sh: 10 passed, 0 failed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 01:42:33 -07:00

198 lines
9.5 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Fleet audit: could someone who is not Cato operate each live app tomorrow?
#
# "Ready for takeoff in whatever form" means the fleet keeps running when its
# one operator is not there. loki's check-deploy-ready.sh already proves each
# app has CI and a Deploy that reaches the box, and the watchdog proves each
# URL answers. What nothing checked is the part that needs a HUMAN who knows
# things:
#
# env-example Every live app reads secrets from a box .env that only the
# box has. If the repo does not name those keys in a committed
# .env.example, an operator cannot rotate a leaked key or
# rebuild the box without reverse-engineering the code. The
# names are not secrets; the absence of the names is the
# bus factor. Read from each repo's REMOTE default branch,
# ratcheted: the count of live apps without one may fall,
# never rise.
#
# --box Two facts only the box knows, checked over ssh (local runs
# and dispatch only — no fleet workflow holds a box key):
# rollback /opt/<app>/app is a release symlink, so
# rollback.sh can flip it. An app deployed by
# hand into a plain directory has no way back.
# backup the app's database has a dump in
# /opt/backups/pg younger than BACKUP_MAX_HOURS
# (36). A backup timer that stopped is silent;
# the dump's mtime is not.
# off-box /opt/backups/restic.env exists, i.e. dumps
# leave the disk they protect. One fact for the
# whole box, reported, not ratcheted — it is a
# decision (a Storage Box), not a drift.
#
# Not checked, and said so: whether a restore has ever been EXERCISED. That
# is a drill, not an audit; the audit only tells you the drill is possible.
#
# The app list is loki's scripts/hetzner/apps.conf on its default branch —
# the one register provisioning reads — never a local checkout.
#
# Usage:
# bus-factor-audit.sh report
# bus-factor-audit.sh --check exit 1 if the env-example count rose above the baseline
# bus-factor-audit.sh --box add the ssh checks (needs BOX or HETZNER_IP)
# bus-factor-audit.sh --emit-baseline write the current count to the baseline file
#
# Env: ORG (bitbaum), APPS_CONF_REPO (bitbaum/loki), APPS_CONF_PATH,
# BOX or HETZNER_IP (no default), BACKUP_MAX_HOURS (36), BASELINE (path).
set -uo pipefail
ORG="${ORG:-bitbaum}"
APPS_CONF_REPO="${APPS_CONF_REPO:-bitbaum/loki}"
APPS_CONF_PATH="${APPS_CONF_PATH:-scripts/hetzner/apps.conf}"
# The box address has ONE home: loki scripts/hetzner/_box-env.sh. In Actions the
# value arrives as the org variable HETZNER_IP; locally, source that file if the
# checkout is there. Either way this file does not keep a copy of the number.
_box_env="${DEV_ROOT:-$HOME/dev}/loki/scripts/hetzner/_box-env.sh"
# shellcheck source=/dev/null
[ -f "$_box_env" ] && . "$_box_env"
BOX="${BOX:-${BOX_UBUNTU:-${HETZNER_IP:+ubuntu@${HETZNER_IP}}}}"
BACKUP_MAX_HOURS="${BACKUP_MAX_HOURS:-36}"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
BASELINE="${BASELINE:-$HERE/bus-factor.baseline}"
CHECK=0 DO_BOX=0 EMIT=0
for a in "$@"; do
case "$a" in
--check) CHECK=1 ;;
--box) DO_BOX=1 ;;
--emit-baseline) EMIT=1 ;;
-h|--help) sed -n '2,50p' "$0"; exit 0 ;;
*) echo "unknown argument: $a" >&2; exit 2 ;;
esac
done
# ── the register ───────────────────────────────────────────────────────────
#
# Rows: name|port|domains|repo_path|app_dir|db|owner|kind|status|plan|price|since
# Only live rows: a prospect has no operator problem yet.
live_rows() {
gh api -H "Accept: application/vnd.github.raw" "repos/${APPS_CONF_REPO}/contents/${APPS_CONF_PATH}" \
| command grep -v '^\s*#' | command grep -v '^\s*$' \
| awk -F'|' '$9 == "live" { print $1 "|" $4 "|" $5 "|" $6 }'
}
# Candidate names, in the order the fleet actually uses them.
ENV_EXAMPLES=".env.example .env.local.example .env.selfhost.example .env.sample"
# Does the repo's default branch name its env keys at app_dir? Prints the
# file found, or nothing.
env_example_of() { # <repo> <app_dir>
local repo="$1" dir="$2" f path
for f in $ENV_EXAMPLES; do
path="${dir:+$dir/}$f"
if gh api "repos/${ORG}/${repo}/contents/${path}" --jq .path >/dev/null 2>&1; then
echo "$path"; return 0
fi
done
return 1
}
rows="$(live_rows)"
n_live="$(printf '%s\n' "$rows" | sed '/^$/d' | wc -l)"
if [ "$n_live" -eq 0 ]; then
echo "no live rows read from ${APPS_CONF_REPO}:${APPS_CONF_PATH} — a token or path problem, not an empty fleet" >&2
exit 2
fi
echo "bus-factor audit — ${n_live} live apps in ${APPS_CONF_REPO}:${APPS_CONF_PATH}"
echo
printf ' %-22s %-12s %s\n' "app" "env-example" "where"
missing=0
missing_names=""
while IFS='|' read -r name repo_path app_dir db; do
[ -n "$name" ] || continue
repo="$(basename "$repo_path")"
dir="$app_dir"; [ "$dir" = "." ] && dir=""
if found="$(env_example_of "$repo" "$dir")"; then
printf ' %-22s %-12s %s\n' "$name" "yes" "${ORG}/${repo}:${found}"
else
printf ' %-22s %-12s %s\n' "$name" "MISSING" "${ORG}/${repo}${dir:+/$dir} names none of: ${ENV_EXAMPLES}"
missing=$((missing + 1)); missing_names="${missing_names} ${name}"
fi
done <<<"$rows"
echo
echo "live apps whose secrets are not named in the repo: ${missing}${missing_names:+ (${missing_names# })}"
# ── the box half ───────────────────────────────────────────────────────────
box_failures=0
if [ "$DO_BOX" -eq 1 ]; then
# Only --box needs an address, so only --box requires one. There is no
# literal default: the box address lives once, in loki
# scripts/hetzner/_box-env.sh (sourced above when that checkout is present)
# and as the org Actions variable HETZNER_IP.
if [ -z "$BOX" ]; then
echo "bus-factor --box: no box address. Set BOX or HETZNER_IP, or run where" >&2
echo " \${DEV_ROOT:-\$HOME/dev}/loki/scripts/hetzner/_box-env.sh exists." >&2
exit 1
fi
echo
echo "box (${BOX})"
# One ssh round-trip: the release symlink per app, the newest dump per db,
# and whether dumps leave the disk.
remote_script='
for name in '"$(printf '%s\n' "$rows" | cut -d'|' -f1 | tr '\n' ' ')"'; do
if [ -L "/opt/$name/app" ]; then echo "rollback $name yes $(readlink /opt/$name/app | xargs basename)";
elif [ -d "/opt/$name/app" ]; then echo "rollback $name NO plain-directory";
else echo "rollback $name NO absent"; fi
done
for db in '"$(printf '%s\n' "$rows" | cut -d'|' -f4 | command grep -v '^-$' | command grep -v '^supabase:' | sort -u | tr '\n' ' ')"'; do
newest=$(ls -1t /opt/backups/pg/*"$db"* 2>/dev/null | head -1)
if [ -n "$newest" ]; then
age_h=$(( ( $(date +%s) - $(stat -c %Y "$newest") ) / 3600 ))
echo "backup $db ${age_h}h $(basename "$newest")"
else echo "backup $db none -"; fi
done
[ -e /opt/backups/restic.env ] && echo "offbox yes" || echo "offbox NO"
systemctl is-active pg-backup.timer 2>/dev/null | sed "s/^/timer /"
'
if ! out="$(ssh -o BatchMode=yes -o ConnectTimeout=10 "$BOX" "$remote_script" 2>&1)"; then
echo " ssh to ${BOX} failed: ${out}" >&2
box_failures=$((box_failures + 1))
else
printf '%s\n' "$out" | while read -r kind subject value rest; do
case "$kind" in
rollback) [ "$value" = "yes" ] && printf ' rollback %-22s yes (%s)\n' "$subject" "$rest" \
|| printf ' rollback %-22s NO — %s\n' "$subject" "$rest" ;;
backup) if [ "$value" = "none" ]; then printf ' backup %-22s NONE\n' "$subject";
elif [ "${value%h}" -gt "$BACKUP_MAX_HOURS" ]; then printf ' backup %-22s STALE %s (%s)\n' "$subject" "$value" "$rest";
else printf ' backup %-22s %s (%s)\n' "$subject" "$value" "$rest"; fi ;;
offbox) [ "$subject" = "yes" ] && echo " off-box dumps leave the disk (restic.env present)" \
|| echo " off-box NO — dumps live on the disk they protect (drop /opt/backups/restic.env: install-backups.sh phase 2)" ;;
timer) echo " timer pg-backup.timer ${subject}" ;;
esac
done
box_failures=$(printf '%s\n' "$out" | awk -v max="$BACKUP_MAX_HOURS" '
$1=="rollback" && $3!="yes" {f++}
$1=="backup" && ($3=="none" || substr($3,1,length($3)-1)+0 > max) {f++}
END {print f+0}')
fi
fi
# ── ratchet ────────────────────────────────────────────────────────────────
if [ "$EMIT" -eq 1 ]; then
echo "$missing" > "$BASELINE"
echo "baseline written: $missing -> $BASELINE"
fi
if [ "$CHECK" -eq 1 ]; then
base="$(cat "$BASELINE" 2>/dev/null || echo 0)"
if [ "$missing" -gt "$base" ]; then
echo
echo "RATCHET: ${missing} live apps without a committed env example, baseline ${base} — a new one shipped without naming its keys" >&2
exit 1
fi
echo "ratchet: ${missing} <= baseline ${base}"
fi
[ "$box_failures" -eq 0 ] || { echo "box: ${box_failures} app(s) cannot be rolled back or restored" >&2; exit 1; }