diff --git a/.claude/tasks/plans/2026-11-09-db-trial-stack.md b/.claude/tasks/plans/2026-11-09-db-trial-stack.md index a0d7f1db..3ddb2bee 100644 --- a/.claude/tasks/plans/2026-11-09-db-trial-stack.md +++ b/.claude/tasks/plans/2026-11-09-db-trial-stack.md @@ -3571,6 +3571,38 @@ bash scripts/db-trial-check.sh # environment-free checks of db:trial and the db image, default logging, the async Vite config and its port guard at the first real `app` and the next `db:sandbox trial`). STOP; no `create`. +#### Closure code-review fixes (2026-11-11) + +1. `resource_exists`: absent only on the daemon's whole sentence for that name (`absent_answer`); the stub's `fail/daemon` fails every docker call with the real dial error; `plan` skips the resource listing with the daemon down. +2. `container_running` tri-state (an inspect error dies); `backup_now` refuses when a container still runs after the stop; `status_containers` reads through `container_exists`. +3. `restore`: each fresh volume is checked empty by the helper before the extraction (`volume_is_empty`). +4. `rotate-secrets` final message: old access tokens refused, refresh tokens and sessions stay valid. +5. `DB_INIT` and the rotation SQL: `\set VERBOSITY terse`, `log_statement`, `log_min_duration_statement` and `pgaudit.log` off before the first value-carrying statement. +6. `backup` and `migrate` call `refuse_while_app_runs`. +7. `restore` and `rotate-secrets` check `trial_host` before the confirmation and the backup. +8. `start` and `stop` lock before reading conf and secrets. +9. `grant owner|admin` for an address with no account refuses with the account hint (`no_account` marker from the factor-count SQL). +10. `SUPABASE.md` §5.2: rotate-secrets wording, no avatar worker, two post-create checks, backup and migrate rows. + +Harness cases added: daemon lost mid-command (create on container, network and volume inspects, destroy, restore), plan with the daemon down lists nothing, inspect failing during the stop, container still running after the stop, non-empty volume at extraction, helper emptiness check order, backup and migrate refused while the app runs, unusable app host for restore and rotate-secrets, start and stop lock first, grant with no account (owner, admin), final rotate message, DB_INIT and rotation SQL lines and order. + +#### Closure re-review fixes (2026-11-11) + +Measured on Docker 29.9.3: a failing `inspect -f` (absent resource or lost daemon) exits 1, writes one newline to stdout and the sentence to stderr (container `No such container: `, volume `get : no such volume`, network `network not found`, image `No such image: `; a lost daemon: `failed to connect to the docker API at ; ...`). + +- C1: `resource_exists` and `container_running` judge stderr alone against `absent_answer` (the merged `2>&1` compare refused every absent resource); `container_running` checks existence first, then reads the state apart. Other docker calls merging `2>&1` audited, none compared with a docker sentence. +- C2: `first_absent_image` goes through `image_exists` (strict); a lost daemon refuses ("cannot tell whether its images are present") instead of "docker pull it"; `check_backup` refuses when that helper dies. +- C3: stub: every failing inspect, four kinds, writes `"\n"` to stdout and the sentence to stderr; image honours the daemon-lost hooks; new hook `daemon-lost-after-create`; header comment updated. +- I1: SUPABASE.md: signing out right after `rotate-secrets` does not end a session (auth-js 2.117.3 ignores the 401/403 of `/logout`); the app's scope is `global`; sign in again, then sign out; revocation by the script is an owner decision not taken. +- M1: refusing-while-app-runs lists now include `backup` and `migrate` (SUPABASE.md, TRIAL_STACK.md); the `cmd_app` shortcut comment rewritten. +- M2: post-create checks: `pg_authid` as `supabase_admin`, secrets scanned through `grep -cFf <(sed ...)`, the server log location read with `show logging_collector; show log_directory; show log_destination;`, what to do above 0, unproven items added. +- M3: `set pg_stat_statements.track_utility = off;` in `DB_INIT` and `set local ...` in the rotation SQL. +- M4: `backup_failed` takes the stack's fate as an optional third argument; "still runs after the stop" no longer says it stays stopped. +- M5: `inspect_refused` shows Docker's answer (quoted, 200 characters), "unexpected value" wording, hint `docker info`. +- M6: create's EXIT trap prints a hint when its listing fails; destroy's way-out line says when a listing failed. +- M7: `cmd_start` checks `trial_host` before starting or recreating a container. +- H: `lines_before_first` checks order; the rotation check asserts every silencing line; the tautological `! grep "already exists"` replaced by one that can fail; cases: stream split of the stub, plan, create, backup and status on stub-absent resources, create with the daemon lost after the first resource, restore with the daemon lost, start with no usable host. + ## Review record (orchestrator) Each reviewer was a fresh subagent; each fix round went to a fresh executor; the orchestrator re-ran the checks diff --git a/docs/development/SUPABASE.md b/docs/development/SUPABASE.md index 87bd21ff..1601ba6f 100644 --- a/docs/development/SUPABASE.md +++ b/docs/development/SUPABASE.md @@ -858,11 +858,11 @@ before any Docker call. Their typed confirmations: `destroy` asks `faunosteo_tri | `create` | after the Rust pipeline check of the equine seed (`pnpm r5:import-equine --check`): secrets, network, volumes, services, the branch's migrations, then `equine_draft.sql` only (never `seed.sql`); checks that the historical sheet is the only official one and that its draft equals `document.json` | | `start` / `stop` | the same containers, data kept; `start` recreates a missing service container from `stack.conf` and `secrets.env` | | `status` | containers, keys' expiry, migrations, equine sheet, accounts with roles, rights and verified factors, backups | -| `migrate` | a cold backup, then the branch's new migrations; refuses an applied file edited since, or a new file older than the last applied; a migrate that stops part-way names its backup | -| `backup` | stops the stack a few seconds, refuses when free space is below 110 % of the volumes, archives each volume read-only through a `--rm` helper with no network, then lists it back and compares its entry count with the volume's (an archive cut short is refused), copies the state files, writes checksums last, restarts; a failed backup keeps no folder (a folder without `SHA256SUMS` is listed as incomplete) | +| `migrate` | a cold backup, then the branch's new migrations; refuses while the app runs (it stops the stack), an applied file edited since, or a new file older than the last applied; a migrate that stops part-way names its backup | +| `backup` | refuses while the app runs; stops the stack a few seconds (and refuses when a container still runs after the stop), refuses when free space is below 110 % of the volumes, archives each volume read-only through a `--rm` helper with no network, then lists it back and compares its entry count with the volume's (an archive cut short is refused), copies the state files, writes checksums last, restarts; a failed backup keeps no folder (a folder without `SHA256SUMS` is listed as incomplete) | | `restore ` | checksums, archives and the recorded database image checked first, typed confirmation `restore `, a backup of the current volumes, then the backup's volumes and secrets and the containers recreated; also rebuilds a destroyed stack | | `rotate-keys` | a backup, then new anon and service keys signed with the same secret (one more year), Storage recreated and the gateway restarted; works on a stopped stack too (Storage is then recreated by the next `start`); the previous keys stay valid until their own expiry. Expired keys never block `start` or `rotate-keys`; `app`, `assets` and `account` refuse them and name `rotate-keys`; `status` reports them | -| `rotate-secrets` | a backup, then a new JWT secret, keys and role passwords set in one transaction as `supabase_admin` (values through psql variables, statement and error-statement logging off, a digest marker proving the commit); the new values wait in `secrets.next.env` (600) until the commit is known; every container recreated; sessions signed with the old secret end, account passwords and TOTP factors stay | +| `rotate-secrets` | a backup, then a new JWT secret, keys and role passwords set in one transaction as `supabase_admin` (values through psql variables, statement and error-statement logging off, a digest marker proving the commit); the new values wait in `secrets.next.env` (600) until the commit is known; every container recreated; access tokens signed with the old secret are refused, while refresh tokens and sessions stay valid (a browser refreshes and gets a token signed with the new secret; the script revokes no session, which is an owner decision not taken; to end sessions after a rotation, sign in again, then sign out: see the paragraph below the table), account passwords and TOTP factors stay | | `account ` | a confirmed account through GoTrue's admin API on 58999, password typed twice at a hidden prompt (12 characters min.); the address is lowercased | | `grant ` | `owner` / `admin`: the operator path of the V1-2 contract §5.7.1 (verified TOTP factors shown, two for owner, one for admin; typed confirmation `grant `; `private.assign_platform_role(null, …)` and an audit row); a platform role already active is refused. Catalogue rights mirror `public.grant_catalog_right` in operator SQL (the RPC needs an owner session at aal2): a confirmed active account; a verified TOTP factor for every right but `catalog_reader`; `admin` held first for `sheet_editor` and `sheet_publisher` (the refusal names `pnpm db:trial grant admin`); one active row per right; typed confirmation; the insert and an audit row `catalog_grant.operator_bootstrap` in one transaction; no platform role is granted implicitly. `granted_by` is the holder's own id (the column is NOT NULL and the operator has no profile), so the rights screen shows a self-grant; the audit row names the operator. A catalogue right already active exits 0 with nothing written | | `assets` | the equine drawings with `pnpm assets:upload` through the gateway (no overwrite, rerun = nothing to do), then every row confirmed with its object | @@ -880,7 +880,14 @@ First use, after the owner's go: `plan`, `create`, `assets`, then the two accoun grant asking `admin` first, as `grant_catalog_right` does); O1 is unchanged. A production practitioner holds neither `admin` nor a catalogue editing right by default. -Rotation and recovery: `rotate-keys` before the keys expire (`status` shows the date); `rotate-secrets` to revoke. +Rotation and recovery: `rotate-keys` before the keys expire (`status` shows the date); `rotate-secrets` refuses the access tokens signed with the old secret (refresh tokens and sessions survive it). +Ending a session after a rotation: signing out in the app right after `rotate-secrets` does not end it. The app calls +`auth.signOut()` with auth-js's default scope `global`, which revokes every session of the account, but GoTrue refuses +the `/logout` call of a token signed with the old secret, and auth-js 2.117.3 ignores that 401 or 403 and only drops the +local session: the server session and its refresh token stay valid (a token refreshed with the new secret lasts up to +`jwt_exp`, 3600 s). To end the sessions, sign in again (the token is then signed with the new secret), then sign out: +that revokes all sessions of the account, other browsers included. The script revokes nothing itself (an owner +decision not taken). If `rotate-secrets` loses psql, the marker the transaction wrote tells whether it committed and only the matching file stays `secrets.env` (password probes prove nothing here: loopback connections are `trust` in this image); if a rotation stops after the commit, `pnpm db:trial start` recreates the containers; if it stops before the commit is @@ -910,9 +917,9 @@ API, Storage's env) must be signed with this stack's JWT secret for the issuer ` role. The identity answer is a static name, not authentication: the exact port, the container checks and the key signatures carry the rest. -Limits: one host; no verification worker on this stack, so a draft submitted for verification stays waiting; the +Limits: one host; no verification worker on this stack, so a draft submitted for verification stays waiting, and no avatar worker either, so a profile photo stays pending; the historical sheet allows one working copy at a time; Mailpit is reachable on the host only; backups sit on the same -disk (copy them elsewhere by hand). `app` takes no lock: `stop`, `destroy`, `restore`, `rotate-keys` or +disk (copy them elsewhere by hand). `app` takes no lock: `stop`, `backup`, `migrate`, `destroy`, `restore`, `rotate-keys` or `rotate-secrets` run in another terminal in the second before Vite listens on 5194 goes ahead. After `rotate-keys` the app keeps the previous anon key until its expiry; after `rotate-secrets` its requests are refused (401) until `app` is restarted; after `stop` or `destroy` it proxies to whatever later listens on 127.0.0.1:58801. Launch no @@ -921,10 +928,36 @@ other `db:trial` command while `app` is starting. Proven only at the first `create` (the stub harness cannot prove them): the database init under a volume, the `supabase_admin` password used by `rotate-secrets`, the pids caps, `tar` and `du` in the database image (checked by `create` before anything is made), the image's default logging settings, the backup helper's `tar` restoring -ownership in the database image, `set local pgaudit.log` accepted by the image, loopback `trust` in its `pg_hba.conf` +ownership in the database image, `set local pgaudit.log` and `set local pg_stat_statements.track_utility` accepted by +the image, the init's session-level `set` lines (`log_statement`, `log_min_duration_statement`, +`log_min_error_statement`, `pgaudit.log`, `pg_stat_statements.track_utility`) accepted by it, whether the image's own +init runs an `ALTER USER supabase_admin ... PASSWORD` before the schema file (where the init's settings cannot help), loopback `trust` in its `pg_hba.conf` (the rotation's commit marker relies on it), psql's echo of an error statement (a failing rotation statement must not print a secret), and the grant and factor SQL, which the harness checks as text and which first -runs at the first real `grant`. +runs at the first real `grant`. Checks to make right after that first `create`, printing no value, only counts. +`` is `$FAUNOSTEO_TRIAL_DIR/secrets.env` (default `~/.cache/faunosteo-trial/secrets.env`), `` is +`faunosteo_trial_db`: + +```bash +# roles holding a password: exactly the five the script sets (postgres is not a superuser in this image) +docker exec psql -X -h 127.0.0.1 -U supabase_admin -d postgres -At \ + -c 'select rolname from pg_authid where rolpassword is not null order by 1' +# the server log: where it goes is read at that moment, not assumed +docker exec psql -X -h 127.0.0.1 -U supabase_admin -d postgres -At \ + -c 'show logging_collector; show log_directory; show log_destination;' +# secrets in the container log and in the in-container files that output names (counts only, the secret +# never on argv: grep reads the pattern from a process substitution) +for key in TR_PW TR_JWT_SECRET; do + docker logs 2>&1 | grep -cFf <(sed -n "s/^$key=//p" ) + docker exec sh -c 'cat "$1"/*' _ | grep -cFf <(sed -n "s/^$key=//p" ) +done +``` + +Every count must be 0 and the role list must be the five roles (`supabase_admin`, `postgres`, `authenticator`, +`supabase_auth_admin`, `supabase_storage_admin`). A count above 0: stop, tell the owner, do not use the stack for +anything else. `rotate-secrets` is the command that renews both scanned secrets (`rotate-keys` renews only the two +API keys), with the owner's go; scan again afterwards. The old values stay in the log until the volume is recreated +and in the pre-rotate backup of `backups/`, so scan for them too. ## 6. Generated types diff --git a/docs/development/TRIAL_STACK.md b/docs/development/TRIAL_STACK.md index 2eb125a5..e8361ba9 100644 --- a/docs/development/TRIAL_STACK.md +++ b/docs/development/TRIAL_STACK.md @@ -170,7 +170,7 @@ recreates a missing container; backups check free space, list each archive back backup of what it would remove and names the manual removal commands; the stub harness is run by hand, not by `check:all` (it reads the host's real ports). R13 (owner 2026-11-10): the shared trial dev server serves exactly one of two listed stacks (`apps/app/vite/trial-targets.ts`), and `db-trial.sh` checks each operation's target and -every key it hands over (`SUPABASE.md` §5.2). Known limit: `app` takes no lock, so a `stop`, `destroy`, `restore` +every key it hands over (`SUPABASE.md` §5.2). Known limit: `app` takes no lock, so a `stop`, `backup`, `migrate`, `destroy`, `restore` or rotation run in another terminal just before Vite listens goes ahead (`SUPABASE.md` §5.2). The tests of §5 that create a stack under a test prefix were not run (owner brief: environment-free checks only); the stub harness replaces them until the first `create`. diff --git a/scripts/db-trial-check.sh b/scripts/db-trial-check.sh index 8912e6a8..8e6ac903 100755 --- a/scripts/db-trial-check.sh +++ b/scripts/db-trial-check.sh @@ -76,16 +76,31 @@ sed 's/kill -TERM/kill -INT/' "$WORK/term-trial.sh" > "$WORK/int-trial.sh" cat > "$WORK/bin/docker" <<'STUB' #!/usr/bin/env bash # docker stand-in: one log line per call (newlines shown as \n), one file per pretend resource. -# Files under $STUB_STATE/fail/ make one answer fail: daemon, networks-create, networks-create-after +# Files under $STUB_STATE/fail/ make one answer fail: daemon (every call but `context`, with the real dial error), +# daemon-lost (docker info answers, every inspect gets that dial error; daemon-lost-volume / -network / -container +# for one kind), inspect-after-stop (container inspects fail once a docker stop ran), stop-noop (docker stop leaves +# the container running), networks-create, networks-create-after # (the network is made, then the call fails), inspect-error (every inspect fails with an answer other # than "no such" or "not found"), helper, archive, extract (the restore's tar -x), image-nondb (every # image but the database's is absent), . Other hooks: term-in-archive (the script gets a # SIGTERM while it archives), append-on-rm (the file named inside gets a byte when a container is # removed), term-on (an ERE: the first call whose argv line, or tag: for an exec, matches it # sends the script a SIGTERM), answers/measure-kib (the volumes' measured size), answers/.seq (one line per call). -# An absent resource answers in Docker's own wording per kind. +# answers/volume-ls (what `ls -A /volume` prints: a non-empty volume). +# Every failing `inspect -f` (an absent resource of any of the four kinds, a lost daemon) writes a +# bare newline to stdout and the sentence to stderr, rc 1, as Docker 29.9.3 does (measured): the +# script must judge stderr alone. Docker's own wording per kind: +# "Error response from daemon: No such container: ", "... get : no such volume", +# "... network not found", "... No such image: ". +# daemon-lost-after-create: once a network or volume was created, every later call but `context` +# and `info` gets the dial error (the daemon is lost part-way through a create). set -uo pipefail st="$STUB_STATE" +dial_error() { + echo + echo "failed to connect to the docker API at unix:///stub/docker.sock; check if the path is correct and if the daemon is running: dial unix /stub/docker.sock: connect: no such file or directory" >&2 + exit 1 +} { printf 'docker'; for a in "$@"; do printf ' %s' "${a//$'\n'/\\n}"; done; printf '\n'; } >> "$STUB_LOG" save_stdin() { local f; f="$st/stdin/$(date +%s%N)"; cat > "$f"; printf '%s' "$f"; } term_if() { # text: once, when it matches the term-on pattern @@ -95,8 +110,12 @@ term_if() { # text: once, when it matches the term-on pattern term_if "docker $*" sub="${1:-}" [ $# -eq 0 ] || shift +if [ -f "$st/fail/daemon" ] && [ "$sub" != context ]; then dial_error; fi +absent_error() { echo; echo "$1" >&2; exit 1; } # the failing inspect's two streams +if [ -f "$st/lost-now" ] && [ "$sub" != context ] && [ "$sub" != info ]; then dial_error; fi +lost() { [ -f "$st/fail/daemon-lost" ] || [ -f "$st/fail/daemon-lost-$1" ]; } # kind: its inspects hit the dial error case "$sub" in - info) [ ! -f "$st/fail/daemon" ] || exit 1; echo /stub/docker-root ;; + info) echo /stub/docker-root ;; context) [ "${1:-}" = inspect ] || exit 97 [ ! -f "$st/fail/context" ] || exit 1 @@ -104,22 +123,26 @@ case "$sub" in ps) exit 0 ;; image) [ "${1:-}" = inspect ] || exit 97 - [ ! -f "$st/fail/image" ] || exit 1 - if [ -f "$st/fail/image-nondb" ]; then case "${*: -1}" in *supabase/postgres*) ;; *) exit 1 ;; esac; fi ;; + img="${*: -1}" + if lost image; then dial_error; fi + [ ! -f "$st/fail/image" ] || absent_error "Error response from daemon: No such image: $img" + if [ -f "$st/fail/image-nondb" ]; then + case "$img" in *supabase/postgres*) ;; *) absent_error "Error response from daemon: No such image: $img" ;; esac + fi ;; network | volume) kind="${sub}s" op="${1:-}" shift case "$op" in inspect) if [ "${1:-}" = -f ]; then shift 2; fi - if [ -f "$st/fail/inspect-error" ]; then echo "Error response from daemon: permission denied" >&2; exit 1; fi - # Docker's real wordings (checked on a real host): they differ by kind. + if lost "$sub"; then dial_error; fi + if [ -f "$st/fail/inspect-error" ]; then absent_error "Error response from daemon: permission denied"; fi + # Docker's real wordings: they differ by kind. if [ ! -f "$st/$kind/$1" ]; then case "$sub" in - volume) echo "Error response from daemon: get $1: no such volume" >&2 ;; - *) echo "Error response from daemon: network $1 not found" >&2 ;; + volume) absent_error "Error response from daemon: get $1: no such volume" ;; + *) absent_error "Error response from daemon: network $1 not found" ;; esac - exit 1 fi ;; create) [ ! -f "$st/fail/$kind-create" ] || exit 1 @@ -127,6 +150,7 @@ case "$sub" in # Like Docker: a volume create on an existing name succeeds and prints the name. if [ -f "$st/$kind/$name" ]; then if [ "$sub" = volume ]; then echo "$name"; exit 0; fi; exit 1; fi : > "$st/$kind/$name" + if [ -f "$st/fail/daemon-lost-after-create" ]; then : > "$st/lost-now"; fi [ ! -f "$st/fail/$kind-create-after" ] || exit 1 echo "$name" ;; rm) for n in "$@"; do [ -f "$st/$kind/$n" ] || exit 1; rm -f "$st/$kind/$n"; done ;; @@ -137,8 +161,10 @@ case "$sub" in shift fmt='' if [ "${1:-}" = -f ]; then fmt="$2"; shift 2; fi - if [ -f "$st/fail/inspect-error" ]; then echo "Error response from daemon: permission denied" >&2; exit 1; fi - [ -f "$st/containers/$1" ] || { echo "Error response from daemon: No such container: $1" >&2; exit 1; } + if lost container; then dial_error; fi + if [ -f "$st/fail/inspect-error" ]; then absent_error "Error response from daemon: permission denied"; fi + if [ -f "$st/fail/inspect-after-stop" ] && [ -f "$st/stopped" ]; then absent_error "Error response from daemon: permission denied"; fi + [ -f "$st/containers/$1" ] || absent_error "Error response from daemon: No such container: $1" case "$fmt" in '{{.State.Running}}') if [ "$(cat "$st/containers/$1")" = running ]; then echo true; else echo false; fi ;; '{{.Name}}') echo "/$1" ;; @@ -160,6 +186,7 @@ case "$sub" in if [ "$name" = faunosteo_trial_helper ] && [ -f "$st/fail/helper" ]; then exit 1; fi if [ "$input" -eq 1 ]; then [ ! -f "$st/fail/extract" ] || exit 1; save_stdin >/dev/null; fi # Measure: 100 KiB and 2 entries ("." and "./content"), what the archive below lists. + if printf '%s\n' "$@" | grep -qx -- 'ls -A /volume'; then cat "$st/answers/volume-ls" 2>/dev/null || true; fi if printf '%s\n' "$@" | grep -q -- 'du -sk /volume'; then printf '%s\n2\n' "$(cat "$st/answers/measure-kib" 2>/dev/null || echo 100)" fi @@ -189,7 +216,11 @@ case "$sub" in for n in "$@"; do [ -f "$st/containers/$n" ] || exit 1; echo running > "$st/containers/$n"; done ;; stop) while [ "${1:-}" = -t ]; do shift 2; done - for n in "$@"; do [ -f "$st/containers/$n" ] || exit 1; echo exited > "$st/containers/$n"; done ;; + : > "$st/stopped" + for n in "$@"; do + [ -f "$st/containers/$n" ] || exit 1 + [ -f "$st/fail/stop-noop" ] || echo exited > "$st/containers/$n" + done ;; rm) if [ "${1:-}" = -f ]; then shift; fi if [ -f "$st/append-on-rm" ]; then printf x >> "$(cat "$st/append-on-rm")"; fi @@ -530,6 +561,18 @@ expect_run() { # name port inner memory pids [volume] ALLOWED_NAMES='faunosteo_trial_(db|mail|auth|storage|rest|gateway|helper|net)|supabase_(admin|auth_admin|storage_admin|migrations)|faunosteo_stack|faunosteo_trial' only_trial_names() { ! docker_calls | grep -oE '(faunosteo|supabase)_[A-Za-z0-9_]+' | grep -vxE "$ALLOWED_NAMES" | grep -q .; } no_volume_removal() { ! log_has 'docker volume rm'; } +# Each whole line of the text comes before its first line matching the ERE, in the order given. +lines_before_first() { # text pattern line... + local text="$1" pattern="$2" first n line previous=0 + first="$(grep -nE -- "$pattern" <<<"$text" | head -n 1 | cut -d: -f1)" + [ -n "$first" ] || return 1 + shift 2 + for line in "$@"; do + n="$(grep -nxF -- "$line" <<<"$text" | head -n 1 | cut -d: -f1)" + [ -n "$n" ] && [ "$n" -lt "$first" ] && [ "$n" -gt "$previous" ] || return 1 + previous="$n" + done +} line_of() { grep -nF -- "$1" "$CASE/log" | head -n 1 | cut -d: -f1; } before() { local a b; a="$(line_of "$1")"; b="$(line_of "$2")"; [ -n "$a" ] && [ -n "$b" ] && [ "$a" -lt "$b" ]; } hold_port() { # port → LISTENER pid, or returns 1 when the port cannot be held @@ -626,6 +669,8 @@ new_case daemon-down; : > "$CASE/stub/fail/daemon"; trial create check "Docker daemon down: create refuses, no state written" bash -c '[ "$1" -ne 0 ] && grep -qF "Docker daemon unreachable" "$2" && [ ! -e "$3" ]' _ "$RC" "$CASE/out" "$STATE_DIR" trial plan check "Docker daemon down: plan still answers and names it" bash -c '[ "$1" -eq 0 ] && grep -qF "Docker daemon is unreachable" "$2" && grep -qF "create would refuse" "$2"' _ "$RC" "$CASE/out" +mkdir -p "$CASE/stub/volumes"; : > "$CASE/stub/volumes/faunosteo_trial_db"; trial plan +check "Docker daemon down: plan lists no existing resource (one exists in the stub) and says so" bash -c '[ "$1" -eq 0 ] && grep -qF "existing containers, network and volumes are not listed" "$2" && ! grep -qF "already exists" "$2"' _ "$RC" "$CASE/out" new_case inspect-error; : > "$CASE/stub/fail/inspect-error"; trial create check "an inspect error other than 'No such' is never read as absent: create refuses before any change" bash -c '[ "$1" -ne 0 ] && grep -qF "docker container inspect faunosteo_trial_db failed" "$2" && [ -z "$3" ] && [ ! -e "$4" ]' _ "$RC" "$CASE/out" "$(changes)" "$STATE_DIR" new_case helper-fails; : > "$CASE/stub/fail/helper"; trial create @@ -669,6 +714,7 @@ check "create: GoTrue mail links on the trial host's app origin" bash -c 'grep - check "create: gateway refuses GoTrue's admin API" log_has 'location /auth/v1/admin/ { return 403; }' check "create: gateway names itself on /__faunosteo_stack" log_has 'location = /__faunosteo_stack { default_type text/plain; return 200 "faunosteo_trial\n"; }' check "create: the database init turns statement logging on errors off first" log_has 'set log_min_error_statement = panic;' +check "create: the database init silences statement logging and verbose errors, in order, before the first ALTER" lines_before_first "$(run_line faunosteo_trial_db | sed 's/\\n/\n/g')" '^ALTER ' '\set VERBOSITY terse' "set log_statement = 'none';" 'set log_min_duration_statement = -1;' 'set log_min_error_statement = panic;' "set pgaudit.log = 'none';" 'set pg_stat_statements.track_utility = off;' check "create: migrations through the CLI on the trial URL only" log_has 'supabase --workdir . migration up --db-url postgresql://postgres@127.0.0.1:58432/postgres?sslmode=disable PGPASSWORD=set' check "create: equine_draft.sql loaded" stdin_has "-- Official equine anatomical sheet" check "create: seed.sql never loaded" stdin_lacks faunosteo.local @@ -807,6 +853,7 @@ rm -f "$CASE/stub/commit-then-fail" "$CASE/stub/answers/rotation-marker" cp -p "$STATE_DIR/secrets.env" "$CASE/secrets.before-rotation" fresh_log; trial rotate-secrets check "rotate-secrets: succeeds" [ "$RC" -eq 0 ] +check "rotate-secrets: the final message does not claim sessions end" bash -c 'grep -qF "access tokens signed with the old secret are refused; refresh tokens and sessions stay valid" "$1" && grep -qF "account passwords and TOTP factors unchanged" "$1" && ! grep -qF "sign in again" "$1"' _ "$CASE/out" check "rotate-secrets: every value renewed" bash -c 'for k in TR_PW TR_JWT_SECRET TR_ANON_KEY TR_SERVICE_KEY; do [ "$(grep "^$k=" "$1")" != "$(grep "^$k=" "$2")" ] || exit 1; done' _ "$STATE_DIR/secrets.env" "$CASE/secrets.before-rotation" check "rotate-secrets: a restorable, protected backup holding the previous secrets (no secrets.prev needed)" bash -c 'd=$(ls -d "$1"/*-pre-rotate* | tail -n 1); [ -n "$d" ] && [ "$(stat -c %a "$d")" = 700 ] && for f in db.tar storage.tar secrets.env stack.conf migrations.sha256 SHA256SUMS; do [ "$(stat -c %a "$d/$f")" = 600 ] || exit 1; done && cd "$d" && sha256sum --quiet -c SHA256SUMS && cmp -s secrets.env "$2"' _ "$STATE_DIR/backups" "$CASE/secrets.before-rotation" check "rotate-secrets: values reach psql by environment names only" log_has 'docker exec -i -e PGPASSWORD -e PGAPPNAME -e NEW_PW -e NEW_JWT -e ROT_DIGEST faunosteo_trial_db psql -X -q -h 127.0.0.1 -U supabase_admin' @@ -851,6 +898,7 @@ check "restore: succeeds" [ "$RC" -eq 0 ] check "restore: says the backup brings back older secrets" out_has "holds other secrets than the current ones" check "restore: a backup of the current state first" before 'faunosteo_trial_db:/volume:ro --entrypoint tar' 'docker volume rm faunosteo_trial_db' check "restore: volumes replaced then filled by the helper" before 'docker volume create --label faunosteo.stack=trial faunosteo_trial_db' 'docker run --rm -i --name faunosteo_trial_helper --network none -v faunosteo_trial_db:/volume --entrypoint tar' +check "restore: each fresh volume is checked empty by the helper before the extraction" before 'ls -A /volume' 'docker run --rm -i --name faunosteo_trial_helper' check "restore: the backup's secrets are back" cmp -s "$STATE_DIR/secrets.env" "$BK/secrets.env" check "restore: six containers recreated" bash -c 'for n in db mail auth storage rest gateway; do grep -q "^docker run -d --name faunosteo_trial_$n " "$1" || exit 1; done' _ "$CASE/log" check "restore: only trial names" only_trial_names @@ -997,7 +1045,7 @@ new_case b2-lock; trial create rm -f "$CASE/stub/volumes/faunosteo_trial_db" flock "$STATE_DIR/.lock" sleep 6 & LOCK_HOLDER=$! sleep 0.5 -for action in backup migrate rotate-keys rotate-secrets; do +for action in backup migrate rotate-keys rotate-secrets start stop; do fresh_log; trial "$action" check "$action: the lock is taken before the stack is checked" bash -c '[ "$1" -ne 0 ] && grep -qF "another pnpm db:trial command is running" "$2"' _ "$RC" "$CASE/out" done @@ -1040,6 +1088,7 @@ rm -f "$CASE/stub/fail/rotate"; : > "$CASE/stub/commit-then-fail"; printf '1\n0\ check "a rotation session active once then gone, marker equal: the rotation goes on" bash -c '[ "$1" -eq 0 ] && grep -qF "took the new secrets although psql failed" "$2" && [ "$3" -eq 2 ]' _ "$RC" "$CASE/out" "$(polls)" rm -f "$CASE/stub/commit-then-fail" "$CASE/stub/answers/rotation-marker"; fresh_log; trial rotate-secrets check "rotate-secrets: the SQL is one explicit transaction (begin before every alter, commit last)" sql_is_one_explicit_transaction "$(rotation_sql)" +check "rotate-secrets: the rotation SQL silences every logging channel, in order, before any alter" lines_before_first "$(cat "$(rotation_sql)")" '^alter ' '\set VERBOSITY terse' 'begin;' "set local log_statement = 'none';" 'set local log_min_duration_statement = -1;' 'set local log_min_error_statement = panic;' "set local pgaudit.log = 'none';" 'set local pg_stat_statements.track_utility = off;' check "rotate-secrets: the rotation session is named (PGAPPNAME passed by name)" bash -c '[ "$1" -eq 0 ] && grep -qF -- "-e PGAPPNAME" "$2"' _ "$RC" "$CASE/log" check "rotate-secrets: no --single-transaction on the rotation session" no_single_transaction_rotation @@ -1408,6 +1457,123 @@ check "app: the renewed anon key passes" [ "$RC" -eq 0 ] fresh_log; trial stop; fresh_log; trial app check "app: refused while the stack is stopped" refused "is not running" +echo "-- closure code-review fixes (2026-11-11)" +# 1: a daemon lost between the dispatch check and an inspect is never read as "absent". +lost_refused() { # inspect kind, resource name + [ "$RC" -ne 0 ] && [ -z "$(changes)" ] && out_has "docker $1 inspect $2 failed (exit 1)" +} +new_case lost-create; : > "$CASE/stub/fail/daemon-lost"; trial create +check "daemon lost mid-command: create refuses, nothing created, no state written" bash -c '[ "$1" -ne 0 ] && [ ! -e "$2" ]' _ "$RC" "$STATE_DIR" +check "daemon lost mid-command: create names the container inspect" lost_refused container faunosteo_trial_db +new_case lost-create-network; : > "$CASE/stub/fail/daemon-lost-network"; trial create +check "daemon lost on a network inspect: create refuses, nothing created" lost_refused network faunosteo_trial_net +new_case lost-create-volume; : > "$CASE/stub/fail/daemon-lost-volume"; trial create +check "daemon lost on a volume inspect: create refuses, nothing created" lost_refused volume faunosteo_trial_db +b2_stack lost-stack +COUNT="$(backup_count)"; : > "$CASE/stub/fail/daemon-lost" +fresh_log; trial_tty faunosteo_trial destroy +check "daemon lost mid-command: destroy refuses, nothing removed, state files and backups kept" bash -c '[ -f "$1/secrets.env" ] && [ "$2" -eq "$3" ]' _ "$STATE_DIR" "$COUNT" "$(backup_count)" +check "daemon lost mid-command: destroy names the inspect that failed" lost_refused container faunosteo_trial_db +fresh_log; trial_tty "restore $RN" restore "$RN" +check "daemon lost mid-command: restore refuses, nothing removed, no backup taken" bash -c '[ -f "$1/secrets.env" ] && [ "$2" -eq "$3" ]' _ "$STATE_DIR" "$COUNT" "$(backup_count)" +check "daemon lost mid-command: restore names the inspect that failed (the recorded image, read first)" lost_refused image public.ecr.aws/supabase/postgres:17.11.0.004 +rm -f "$CASE/stub/fail/daemon-lost" + +# 2: container_running is tri-state; backup proves the stack stopped. +new_case stop-inspect; trial create +: > "$CASE/stub/fail/inspect-after-stop"; fresh_log; trial backup +check "backup: an inspect failing during the stop is refused, never read as stopped, no complete folder" bash -c '[ "$1" -ne 0 ] && grep -qF "failed (exit 1)" "$2" && ! ls "$3"/backups/*/SHA256SUMS >/dev/null 2>&1' _ "$RC" "$CASE/out" "$STATE_DIR" +new_case stop-noop; trial create +: > "$CASE/stub/fail/stop-noop"; COUNT="$(backup_count)"; fresh_log; trial backup +check "backup: a container still running after the stop is refused, nothing archived, no folder kept" bash -c '[ "$1" -ne 0 ] && grep -qF "still runs after the stop" "$2" && ! grep -qF "stays stopped" "$2" && ! grep -qF -- "--entrypoint tar" "$3" && [ "$4" -eq "$5" ]' _ "$RC" "$CASE/out" "$CASE/log" "$COUNT" "$(backup_count)" + +# 3: docker volume create succeeds on an existing name. +b2_stack nonempty-volume +echo lost+found > "$CASE/stub/answers/volume-ls" +fresh_log; trial_tty "restore $RN" restore "$RN" +check "restore: a volume not empty once created is refused, nothing extracted, the pre-restore backup named" bash -c '[ "$1" -ne 0 ] && grep -qF "is not empty after its creation" "$2" && grep -qE "pnpm db:trial restore [0-9]{8}T[0-9]{6}Z-pre-restore" "$2" && ! grep -qF "docker run --rm -i" "$3"' _ "$RC" "$CASE/out" "$CASE/log" +check "restore: the emptiness is read by the helper, read-only, no network" log_has 'docker run --rm --name faunosteo_trial_helper --network none -v faunosteo_trial_db:/volume:ro --entrypoint sh public.ecr.aws/supabase/postgres:17.11.0.004 -c ls -A /volume' + +# 6: backup and migrate stop the stack, so they refuse while the app runs. +new_case app-stop; trial create +printf -- '-- harness\n' > "$WORK/repo/supabase/migrations/29991231000003_harness_app.sql" +if hold_port 5194; then + fresh_log; trial backup + check "backup while port 5194 is in use (the app): refused, nothing stopped" refused "5194 is in use" + fresh_log; trial migrate + check "migrate while port 5194 is in use (the app): refused, nothing stopped, no backup" bash -c 'grep -qF "5194 is in use" "$1" && ! grep -qF faunosteo_trial_helper "$2"' _ "$CASE/out" "$CASE/log" + check "migrate while port 5194 is in use: no stop, no migration" refused "5194 is in use" + release_port +else fail "could not hold 127.0.0.1:5194 for the backup and migrate cases"; fi +rm -f "$WORK/repo/supabase/migrations/29991231000003_harness_app.sql" + +# 7: the app host is checked before the first change. +refused_keeping_backups() { refused "$1" && [ "$COUNT" -eq "$(backup_count)" ]; } +b2_stack host-precheck +COUNT="$(backup_count)"; EXTRA=(FAUNOSTEO_TRIAL_HOST=8.8.8.8) +fresh_log; trial_tty "restore $RN" restore "$RN" +check "restore with no usable app host: refused before the confirmation, no backup, nothing changed" bash -c '! grep -qF "to go on" "$1"' _ "$CASE/out" +check "restore with no usable app host: refused on the host" refused_keeping_backups "must be loopback or a private address" +fresh_log; trial rotate-secrets +check "rotate-secrets with no usable app host: refused before any backup, nothing changed" refused_keeping_backups "must be loopback or a private address" +EXTRA=() + +# 9: an address with no account is told as such on the role path too. +new_case no-account-grant; trial create +echo no_account > "$CASE/stub/answers/grant-factor-count" +for role in owner admin; do + fresh_stdin; fresh_log; trial_tty "grant $role" grant ghost@trial.test "$role" + check "grant $role for an address with no account: refused with the account hint, nothing granted" bash -c '[ "$1" -ne 0 ] && grep -qF "no account ghost@trial.test (pnpm db:trial account ghost@trial.test first): nothing was granted" "$2" && ! grep -qF "verified TOTP factor(s)" "$2"' _ "$RC" "$CASE/out" + check "grant $role for an address with no account: no role SQL ran" no_role_assigned +done +check "grant: the factor count answers no_account from SQL, the email through -v" bash -c 'grep -qF "no_account" "$1" && grep -qF -- "-v email=ghost@trial.test" "$2"' _ "$(stdin_of grant-factor-count)" "$CASE/log" + +echo "-- closure re-review fixes (2026-11-11)" +# C1/C3: a failing inspect writes a bare newline to stdout and the sentence to stderr (Docker 29.9.3). +stub_inspect() { # kind name → stdout bytes, then stderr, of the stub's failing inspect + STUB_STATE="$CASE/stub" STUB_LOG="$CASE/log" "$WORK/bin/docker" "$1" inspect -f '{{.Id}}' "$2" 2>"$CASE/err" | od -An -c | tr -d ' ' +} +stub_streams_split() { + local kind + for kind in container volume network image; do + [ "$(stub_inspect "$kind" x1)" = '\n' ] || return 1 + [ -s "$CASE/err" ] && [ "$(wc -l < "$CASE/err")" -eq 1 ] || return 1 + done +} +new_case streams; trial plan; : > "$CASE/stub/fail/image" +check "the stub's absent answers: a bare newline on stdout, one sentence on stderr, for all four kinds" stub_streams_split +: > "$CASE/stub/fail/daemon-lost" +check "the stub's daemon-lost answers split the streams the same way, image included" bash -c '[ "$1" = "\n" ] && [ "$2" = "\n" ]' _ "$(stub_inspect image x1)" "$(stub_inspect container x1)" +new_case absent-plan; trial plan +check "plan: stub-absent resources read as absent, nothing already exists" bash -c '[ "$1" -eq 0 ] && grep -qF "create would proceed" "$2" && ! grep -qF "already exists" "$2"' _ "$RC" "$CASE/out" +new_case absent-flow; trial create +check "create: the preflight reads every stub-absent resource as absent and goes on" [ "$RC" -eq 0 ] +fresh_log; trial backup +check "backup: the helper check reads the absent helper container as absent and goes on" bash -c '[ "$1" -eq 0 ] && ls "$2"/backups/*/SHA256SUMS >/dev/null 2>&1' _ "$RC" "$STATE_DIR" +fresh_log; trial status +check "status: reads absent resources as absent, no refusal" bash -c '[ "$1" -eq 0 ] && ! grep -qF "refused" "$2"' _ "$RC" "$CASE/out" +new_case absent-status; trial status +check "status without a stack: stub-absent resources, none listed, no refusal" bash -c '[ "$1" -eq 0 ] && ! grep -qF "present anyway" "$2" && ! grep -qF "refused" "$2"' _ "$RC" "$CASE/out" + +# C2: a lost daemon is not "image not present locally". +b2_stack lost-restore +COUNT="$(backup_count)"; : > "$CASE/stub/fail/daemon-lost" +fresh_log; trial_tty "restore $RN" restore "$RN" +check "restore with the daemon lost: refused on the image inspect, never 'docker pull', nothing removed" bash -c '[ "$1" -ne 0 ] && grep -qF "cannot tell whether its images are present" "$2" && grep -qF "docker image inspect" "$2" && ! grep -qF "docker pull" "$2" && ! grep -qF "not present locally" "$2" && [ "$3" -eq "$4" ]' _ "$RC" "$CASE/out" "$COUNT" "$(backup_count)" +check "restore with the daemon lost: no change at all" [ -z "$(changes)" ] + +# M6: the create trap ends with its hint when its listing fails. +new_case lost-after-create; : > "$CASE/stub/fail/daemon-lost-after-create"; trial create +check "create with the daemon lost after the first resource: the trap runs, nothing removed, state files kept, the hint printed" bash -c '[ "$1" -ne 0 ] && grep -qF "nothing was removed" "$2" && grep -qF "the listing failed" "$2" && grep -qF "inspect the faunosteo_trial resources by hand" "$2" && grep -qF "pnpm db:trial status shows the state" "$2" && [ -f "$3/secrets.env" ] && [ -f "$3/stack.conf" ] && ! grep -qE "^docker (rm|volume rm|network rm) " "$4"' _ "$RC" "$CASE/out" "$STATE_DIR" "$CASE/log" + +# M7: start checks the app host before it starts or recreates anything. +new_case start-host; trial create; trial stop +rm -f "$CASE/stub/containers/faunosteo_trial_rest" "$CASE/stub/ports/faunosteo_trial_rest" +EXTRA=(FAUNOSTEO_TRIAL_HOST=8.8.8.8); fresh_log; trial start +check "start with no usable app host: refused before any container is started or recreated" refused "must be loopback or a private address" +EXTRA=() + + echo "-- db:sandbox state guard" new_case sandbox-guard; mkdir -p "$CASE/trialstate"; : > "$CASE/trialstate/.faunosteo-trial-state" EXTRA=(FAUNOSTEO_SANDBOX_DIR="$CASE/trialstate"); sandbox down diff --git a/scripts/db-trial.sh b/scripts/db-trial.sh index 5d9c21a4..524adfba 100755 --- a/scripts/db-trial.sh +++ b/scripts/db-trial.sh @@ -274,19 +274,44 @@ require_local_daemon() { # Checked at dispatch for every command but plan: past it, "absent" means absent, not "daemon down". daemon_ok() { docker info --format '{{.ServerVersion}}' >/dev/null 2>&1; } # Always -f with a named field: a bare inspect prints the containers' env-file values. -# "No such " is the only answer read as absent; any other inspect error refuses. +# Absent is only the daemon's whole sentence for that very name, read on stderr alone (a failing +# inspect prints a bare newline on stdout; a dead daemon's "no such file or directory" contains +# "no such" too); any other inspect error refuses. +absent_answer() { # kind name: the daemon's wording, which differs by kind + case "$1" in + container) printf 'Error response from daemon: No such container: %s' "$2" ;; + volume) printf 'Error response from daemon: get %s: no such volume' "$2" ;; + network) printf 'Error response from daemon: network %s not found' "$2" ;; + image) printf 'Error response from daemon: No such image: %s' "$2" ;; + esac +} +inspect_refused() { # kind name what answer: Docker's answer shown quoted, cut at 200 characters + local shown="${4:0:200}" + die "docker $1 inspect $2 $3 (answer ${shown@Q}), not the daemon's own 'absent' sentence: refused; check that the Docker daemon answers (docker info)" +} resource_exists() { # kind name: 0 exists, 1 absent, else die - local out status=0 - out="$(docker "$1" inspect -f '{{.Name}}' "$2" 2>&1)" || status=$? + local err status=0 field='{{.Name}}' + if [ "$1" = image ]; then field='{{.Id}}'; fi + err="$(docker "$1" inspect -f "$field" "$2" 2>&1 >/dev/null)" || status=$? [ "$status" -ne 0 ] || return 0 - # Docker's wordings differ by kind: "No such container: x", "get x: no such volume", "network x not found". - case "${out,,}" in *"no such"* | *"not found"*) return 1 ;; esac - die "docker $1 inspect $2 failed (exit $status) with an answer other than 'no such' / 'not found': refused, nothing was changed" + [ "$err" != "$(absent_answer "$1" "$2")" ] || return 1 + inspect_refused "$1" "$2" "failed (exit $status)" "$err" } container_exists() { resource_exists container "$1"; } -container_running() { [ "$(docker container inspect -f '{{.State.Running}}' "$1" 2>/dev/null)" = true ]; } +container_running() { # 0 running, 1 stopped or absent, else die: an error is never "not running" + local out status=0 + container_exists "$1" || return 1 + out="$(docker container inspect -f '{{.State.Running}}' "$1" 2>/dev/null)" || status=$? + [ "$status" -eq 0 ] || inspect_refused container "$1" "failed (exit $status) after it was found" "no answer kept" + case "$out" in + true) return 0 ;; + false) return 1 ;; + esac + inspect_refused container "$1" "returned an unexpected value" "$out" +} volume_exists() { resource_exists volume "$1"; } network_exists() { resource_exists network "$1"; } +image_exists() { resource_exists image "$1"; } existing_resources() { # the literal names of this stack that exist now, one per line local name for name in "${CONTAINERS[@]}"; do if container_exists "$name"; then echo "container $name"; fi; done @@ -394,10 +419,16 @@ sb_psql() { # The role passwords the CLI sets at its first database start (/etc/postgresql.schema.sql, run # once on an empty data folder), for the roles this stack uses, supabase_admin included so that # rotate-secrets can renew every password through it. A failing ALTER would log its statement, -# password included, at the default log_min_error_statement: the session turns that off first. +# password included, at the default log_min_error_statement: the session turns statement logging +# and verbose errors off first (psql sends the literal password, the server would log it). # shellcheck disable=SC2016 DB_INIT='cat <<'"'"'EOF'"'"' > /etc/postgresql.schema.sql && exec docker-entrypoint.sh postgres -D /etc/postgresql -c shared_buffers=128MB +\set VERBOSITY terse +set log_statement = '"'"'none'"'"'; +set log_min_duration_statement = -1; set log_min_error_statement = panic; +set pgaudit.log = '"'"'none'"'"'; +set pg_stat_statements.track_utility = off; \set pgpass `echo "$PGPASSWORD"` \set jwt_secret `echo "$JWT_SECRET"` ALTER DATABASE postgres SET "app.settings.jwt_secret" TO :'"'"'jwt_secret'"'"'; @@ -592,7 +623,8 @@ require_running() { done } # Before the commands that change keys or data under a running app (rotate-keys, rotate-secrets, -# restore, destroy) and before stop (the app would proxy to whatever then answers 58801). A local +# restore, destroy) and before those that stop the stack (stop, backup, migrate: the app would +# proxy to whatever then answers 58801). A local # listener check: no probe of a LAN address. refuse_while_app_runs() { if port_listening "$PORT_APP"; then @@ -763,6 +795,9 @@ plan_container_line() { # name printf ' %-24s %-46s 127.0.0.1:%s -> %-5s --memory %-5s --pids-limit %-4s volume %s\n' \ "$1" "$(image_of "$1")" "$host" "$inner" "$memory" "$pids" "$(mount_of "$1")" } +# Without a daemon every inspect would refuse: plan lists no existing resource then. +PLAN_DAEMON=yes +plan_exists() { [ "$PLAN_DAEMON" = yes ] && resource_exists "$1" "$2"; } plan_containers() { # refusals-array # shellcheck disable=SC2178 # a nameref to the caller's array local -n refs=$1 @@ -770,7 +805,7 @@ plan_containers() { # refusals-array echo "Containers (network $NET, restart policy no, --cpus 1):" for name in "${CONTAINERS[@]}"; do plan_container_line "$name" - if container_exists "$name"; then refs+=("container $name already exists"); fi + if plan_exists container "$name"; then refs+=("container $name already exists"); fi done echo " $HELPER (create's image check, backup, restore, destroy only): --rm, --network none, image $IMAGE_DB" } @@ -779,12 +814,12 @@ plan_network_volumes() { # refusals-array local -n refs=$1 local root vol echo "Network: $NET" - if network_exists "$NET"; then refs+=("network $NET already exists"); fi + if plan_exists network "$NET"; then refs+=("network $NET already exists"); fi root="$(docker info --format '{{.DockerRootDir}}' 2>/dev/null || echo '')" echo "Volumes:" for vol in "$VOL_DB" "$VOL_STORAGE"; do printf ' %-24s %s/volumes/%s/_data\n' "$vol" "$root" "$vol" - if volume_exists "$vol"; then refs+=("volume $vol already exists"); fi + if plan_exists volume "$vol"; then refs+=("volume $vol already exists"); fi done } plan_images() { @@ -849,7 +884,11 @@ cmd_plan() { say "db:trial plan: read-only, nothing is created, started or changed" echo "Docker endpoint: $(daemon_endpoint) (a local unix socket, checked at dispatch)" if daemon_ok; then echo "Docker daemon: reachable" - else refusals+=("the Docker daemon is unreachable (every Docker answer below is unknown)"); fi + else + PLAN_DAEMON=no + echo "Docker daemon: unreachable, so the existing containers, network and volumes are not listed" + refusals+=("the Docker daemon is unreachable (every Docker answer below is unknown)") + fi plan_containers refusals plan_network_volumes refusals plan_images @@ -885,7 +924,9 @@ create_stopped() { # cleared on success: a run of the trap is always a failure return 0 fi echo "db-trial: create stopped before the end; nothing was removed. Present now:" >&2 - existing_resources | sed 's/^/ /' >&2 + if ! existing_resources | sed 's/^/ /' >&2; then + echo "db-trial: the listing failed (Docker did not answer): inspect the faunosteo_trial resources by hand (docker ps -a, docker network ls, docker volume ls)" >&2 + fi echo "pnpm db:trial status shows the state; pnpm db:trial destroy (after the owner's go) backs the data up and clears it, or names the manual removal when it cannot back up." >&2 } FIRST_RESOURCE=no @@ -952,16 +993,20 @@ cmd_create() { cmd_start() { [ $# -eq 0 ] || die "start takes no argument" - require_created + require_state take_lock + require_created memory_ok + # run_auth needs it, before any container is started or recreated. + trial_host >/dev/null start_containers say "trial stack running; the app: pnpm db:trial app" } cmd_stop() { [ $# -eq 0 ] || die "stop takes no argument" - require_created + require_state take_lock + require_created # A running app would keep proxying session tokens to whatever answers 58801 next. refuse_while_app_runs stop_containers @@ -1010,10 +1055,12 @@ status_database() { inventory_counts | sed 's/^/ /' } status_containers() { - local name + local name state say "containers" for name in "${CONTAINERS[@]}"; do - printf ' %-24s %s\n' "$name" "$(docker container inspect -f '{{.State.Status}} {{.Config.Image}}' "$name" 2>/dev/null || echo absent)" + state=absent + if container_exists "$name"; then state="$(docker container inspect -f '{{.State.Status}} {{.Config.Image}}' "$name")"; fi + printf ' %-24s %s\n' "$name" "$state" done } status_keys_and_host() { @@ -1072,6 +1119,13 @@ new_backup_dir() { # label → a fresh folder (700) ( umask 077; mkdir -p "$BACKUPS"; mkdir "$dir" ) || die "cannot create $dir" printf '%s' "$dir" } +# `docker volume create` succeeds on an existing name: a restore extracts into an empty volume only. +volume_is_empty() { # volume + local out + out="$(docker run --rm --name "$HELPER" --network none -v "$1:/volume:ro" --entrypoint sh "$IMAGE_DB" \ + -c 'ls -A /volume')" || die "$1 cannot be read by the helper: nothing was extracted" + [ -z "$out" ] +} # Size in KiB and entry count of a volume, by the helper (read-only, no network); the entries are # counted from the root tar archives, so the archive must list as many. volume_measure() { # volume → " " @@ -1090,12 +1144,12 @@ backup_volume() { # dir volume entries # A caller that cannot go on without its backup (destroy) names a function that prints the way # out; backup_failed appends it, so the one failure message stays one message. BACKUP_WAY_OUT='' -backup_failed() { # dir why: this run's files go by name, then the (now empty) folder +backup_failed() { # dir why [what became of the stack]: this run's files go by name, then the (now empty) folder local f trap - EXIT for f in "${BACKUP_FILES[@]}"; do rm -f -- "$1/$f"; done rmdir -- "$1" 2>/dev/null || true - die "backup failed ($2): nothing kept, the stack stays stopped (pnpm db:trial start)${BACKUP_WAY_OUT:+ + die "backup failed ($2): nothing kept; ${3:-the stack stays stopped (pnpm db:trial start)}${BACKUP_WAY_OUT:+ $($BACKUP_WAY_OUT '')}" } # An exit that is not backup_failed's (Ctrl+C, a failing command): the folder stays, unfinished. @@ -1123,6 +1177,10 @@ backup_now() { # label restart(yes|no) → LAST_BACKUP BACKUP_RUNNING="$dir" trap backup_interrupted EXIT stop_containers + # A container that kept running would write under the archive. + for name in "${CONTAINERS[@]}"; do + if container_running "$name"; then backup_failed "$dir" "$name still runs after the stop" "the stack is not fully stopped (pnpm db:trial status)"; fi + done for vol in "${vols[@]}"; do read -r kib entries <<<"$(volume_measure "$vol")" if ! [[ "$kib" =~ ^[0-9]+$ && "$entries" =~ ^[0-9]+$ ]]; then backup_failed "$dir" "measure of $vol"; fi @@ -1152,6 +1210,7 @@ cmd_backup() { require_state take_lock require_created + refuse_while_app_runs backup_now manual yes } @@ -1182,7 +1241,7 @@ verify_backup_integrity() { # dir [outcome] first_absent_image() { # the recorded IMAGE_* (parse_conf ran), the first one not here; nothing when all are local image for image in "$IMAGE_DB" "$IMAGE_AUTH" "$IMAGE_REST" "$IMAGE_STORAGE" "$IMAGE_MAIL" "$IMAGE_GATEWAY"; do - if ! docker image inspect -f '{{.Id}}' "$image" >/dev/null 2>&1; then printf '%s' "$image"; return 0; fi + if ! image_exists "$image"; then printf '%s' "$image"; return 0; fi done } # Restorable: the folder is intact, both archives, the secrets and stack.conf are there and @@ -1199,7 +1258,8 @@ check_backup() { # dir [outcome] ( parse_secrets "$dir/secrets.env" && key_check "$TR_ANON_KEY" anon any "$TR_JWT_SECRET" \ && key_check "$TR_SERVICE_KEY" service_role any "$TR_JWT_SECRET" ) >/dev/null 2>&1 \ || refuse_backup "$dir" "holds keys that are not signed with its own secret" "$outcome" - image="$(parse_conf "$dir/stack.conf"; first_absent_image)" + image="$(parse_conf "$dir/stack.conf"; first_absent_image)" \ + || refuse_backup "$dir" "cannot tell whether its images are present (the daemon's answer is above)" "$outcome" [ -z "$image" ] || refuse_backup "$dir" "records an image that is not present locally ($image: docker pull it first)" "$outcome" } @@ -1242,6 +1302,8 @@ cmd_restore() { [ -d "$dir" ] || die "no backup $name in $BACKUPS" check_backup "$dir" "nothing was changed" refuse_while_app_runs + # run_auth needs it, long after the volumes were replaced. + trial_host >/dev/null # Without a trial volume backup_now's own helper check does not run: refused here, before the confirmation. if container_exists "$HELPER"; then die "a $HELPER container is left over (docker rm $HELPER): refused, nothing was changed" @@ -1271,6 +1333,7 @@ cmd_restore() { for vol in "$VOL_DB" "$VOL_STORAGE"; do if volume_exists "$vol"; then docker volume rm "$vol" >/dev/null; fi docker volume create --label faunosteo.stack=trial "$vol" >/dev/null + volume_is_empty "$vol" || die "$vol is not empty after its creation: nothing was extracted" docker run --rm -i --name "$HELPER" --network none -v "$vol:/volume" --entrypoint tar "$IMAGE_DB" \ --numeric-owner -xpf - -C /volume < "$dir/${vol#faunosteo_trial_}.tar" done @@ -1290,7 +1353,8 @@ cmd_restore() { # The script never runs these itself. destroy_way_out_line() { # kind command local names - names="$(existing_resources | sed -n "s/^$1 //p" | tr '\n' ' ')" + names="$(existing_resources | sed -n "s/^$1 //p" | tr '\n' ' ')" \ + || { echo " ($1 listing failed, Docker did not answer: inspect the faunosteo_trial $1s by hand)"; return 0; } if [ -n "$names" ]; then echo " $2 ${names% }"; fi } destroy_way_out() { # kept backup folder, or empty @@ -1364,6 +1428,7 @@ cmd_migrate() { [ -n "$pending" ] || { say "no new migration"; return 0; } echo "new migrations:" printf '%s\n' "$pending" | sed 's/^/ /' + refuse_while_app_runs backup_now pre-migrate yes check_backup "$LAST_BACKUP" "no migration was applied; the backup is kept and the stack runs" trap migrate_stopped EXIT @@ -1448,11 +1513,13 @@ rotate_in_database() { # new password, new JWT secret, digest of the new secrets docker exec -i -e PGPASSWORD -e PGAPPNAME -e NEW_PW -e NEW_JWT -e ROT_DIGEST "$DB" \ psql -X -q -h 127.0.0.1 -U supabase_admin -d postgres -v ON_ERROR_STOP=1 >/dev/null <<'SQL' -- db-trial:rotate +\set VERBOSITY terse begin; set local log_statement = 'none'; set local log_min_duration_statement = -1; set local log_min_error_statement = panic; set local pgaudit.log = 'none'; +set local pg_stat_statements.track_utility = off; \set newpw `printf '%s' "$NEW_PW"` \set newjwt `printf '%s' "$NEW_JWT"` \set digest `printf '%s' "$ROT_DIGEST"` @@ -1523,6 +1590,8 @@ cmd_rotate_secrets() { # database holds, so it is never overwritten. [ ! -e "$next" ] || die "$next exists: a previous rotate-secrets left it, and it may hold the only copy of the secrets the database took. Never overwritten: pnpm db:trial restore (pnpm db:trial status lists the backups) is the way back; nothing was changed" refuse_while_app_runs + # run_auth needs it, long after the secrets changed. + trial_host >/dev/null # Keys that are not this stack's: refused before the backup stops anything. require_trial_keys backup_now pre-rotate yes @@ -1545,7 +1614,7 @@ cmd_rotate_secrets() { remove_containers start_containers trap - EXIT - say "secrets renewed: sessions signed with the old secret are refused (sign in again); account passwords and TOTP factors unchanged; backup before it: $(basename "$LAST_BACKUP")" + say "secrets renewed: access tokens signed with the old secret are refused; refresh tokens and sessions stay valid (a browser refreshes and gets a token signed with the new secret; this script writes no session); account passwords and TOTP factors unchanged; backup before it: $(basename "$LAST_BACKUP")" } # ---- accounts, grants, drawings, app @@ -1605,12 +1674,12 @@ cmd_account() { # the operator path (null actor, JWT-less session) and an audit row records the reason and the # operator. Owner needs two verified TOTP factors, admin one. The database counts them, here and # again inside the grant transaction; a factor name is sanitised in SQL before it reaches the terminal. -factor_count() { # email: verified TOTP factors of the account +factor_count() { # email: verified TOTP factors of the account, or no_account sb_psql -t -A -v email="$1" <<'SQL' -- db-trial:grant-factor-count -select count(*) -from auth.mfa_factors f join auth.users u on u.id = f.user_id -where u.email = :'email' and f.factor_type = 'totp' and f.status = 'verified'; +select case when not exists (select 1 from auth.users where email = :'email') then 'no_account' + else (select count(*) from auth.mfa_factors f join auth.users u on u.id = f.user_id + where u.email = :'email' and f.factor_type = 'totp' and f.status = 'verified')::text end; SQL } factor_list() { # email: name | creation time, one line per verified TOTP factor @@ -1627,6 +1696,7 @@ grant_platform_role() { # email role local need=1 factors count [ "$2" != owner ] || need=2 count="$(factor_count "$1")" || die "the verified TOTP factors of $1 cannot be counted: nothing was granted" + [ "$count" != no_account ] || die "no account $1 (pnpm db:trial account $1 first): nothing was granted" [[ "$count" =~ ^[0-9]+$ ]] || die "the verified TOTP factors of $1 cannot be counted: nothing was granted" [ "$count" -ge "$need" ] \ || die "$1 holds $count verified TOTP factor(s), $2 needs $need: the account enrols them in Mon compte > Sécurité first (nothing was granted)" @@ -1764,12 +1834,14 @@ app_stop() { say "trial app stopped; the stack runs until pnpm db:trial stop" } # shortcut: app takes no lock and checks the gateway identity only at its start. refuse_while_app_runs -# (stop, destroy, restore, rotate-keys, rotate-secrets) sees the app only once Vite listens on 5194, +# (stop, backup, migrate, destroy, restore, rotate-keys, rotate-secrets) sees the app only once Vite listens on 5194, # so one of them run in another terminal in the second before Vite binds goes ahead: after rotate-keys # the app keeps the previous anon key until its expiry; after rotate-secrets its requests are refused # (401) until app is restarted; after stop or destroy it proxies to whatever later listens on -# 127.0.0.1:58801. Holding the lock all session would block backup and migrate with a misleading -# message. Upgrade: a dedicated app lock, shared by app and exclusive for those commands. +# 127.0.0.1:58801. Holding the lock all session would lock out every other command, backup, migrate +# and the restarts included, with "another pnpm db:trial command is running", which says nothing of +# the app (the commands above already refuse, naming it, once Vite listens). +# Upgrade: a dedicated app lock, shared by app and exclusive for those commands. cmd_app() { [ $# -eq 0 ] || die "app takes no argument" local host