fix(supabase): db:trial closure review fixes

Strict absent detection (stderr sentence per kind, measured on Docker 29.9.3), tri-state container_running, empty-volume check before restore, quieter DB init and rotation logging, backup/migrate refuse while the app runs, lock first in start/stop, truthful rotate-secrets and post-create checks. Stub reproduces Docker's stdout/stderr split. Harness 364/0.
This commit is contained in:
bchanot committed 2026-11-11 14:16:32 +01:00
1 parent a78da9f4e2
commit d3bdbbe0ba
5 files changed
+357 -54

No files matched your search

+103 -31
View File
@@ -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 <kind>" 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 '<Docker data root>')"
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 → "<KiB> <entries>"
@@ -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 <pre-rotate backup> (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