Files
faunosteo/scripts/db-sandbox.sh
T

540 lines
28 KiB
Bash
Executable File

#!/usr/bin/env bash
# Throwaway Supabase stack for testing migrations without touching the DEV
# stack (docs/development/SUPABASE.md §5.1). Same images as the CLI stack,
# its own Docker network, ports published on 127.0.0.1 only and distinct from
# DEV, no Docker volume (the images declare none and nothing is mounted), so
# `down` removes only the containers and network named below.
#
# pnpm db:sandbox up fresh all migrations, then the seeds (= db reset)
# pnpm db:sandbox up upgrade <version> [<then>]
# migrations before <version>, seeds, two GoTrue
# accounts, then `migration up` (= DEV path); with
# <then>, the migrations before <then> first, then the rest
# pnpm db:sandbox test-db the pgTAP files (`supabase test db`)
# pnpm db:sandbox types [--check] generated types (write, or diff with the committed file)
# pnpm db:sandbox test packages/supabase/tests/sandbox (HTTP, concurrency)
# pnpm db:sandbox e2e-shell [args] = pnpm e2e:shell: the /app shell in Chromium (apps/app/e2e); args go to Playwright (a spec file, --grep)
# pnpm db:sandbox verify-e2e [args] the sheet verification worker against the sandbox: an
# unpublished gateway, the worker container, then
# tests/sandbox/sheet-verification.sandbox.ts (args go to vitest)
# pnpm db:sandbox avatar-e2e [args] (= pnpm e2e:avatar) the avatar worker (default mode: avatars + sweep) against the
# sandbox: a gateway on 127.0.0.1:$PORT_GATEWAY, the worker
# container, then tests/e2e/avatar-flow.e2e.ts (args go to vitest)
# pnpm db:sandbox sheet-assets-e2e (= pnpm e2e:sheet-assets) the sheet-asset upload tool against the sandbox:
# a gateway on 127.0.0.1:$PORT_GATEWAY, then scripts/sheet-assets-e2e.sh
# pnpm db:sandbox trial the owner's trial (docs/development/V1-2-6_OWNER_TRIAL.md): a gateway on
# 127.0.0.1:$PORT_TRIAL_GATEWAY and the app on http://$FAUNOSTEO_TRIAL_HOST:$PORT_TRIAL_APP
# (default: this machine's LAN address; 127.0.0.1 = host-only),
# foreground, Ctrl+C stops both
# pnpm db:sandbox trial-account <email> a confirmed account, password at a hidden prompt (12 characters min.)
# pnpm db:sandbox trial-grant <email> <catalog_reader|sheet_editor|sheet_reviewer|sheet_publisher>
# pnpm db:sandbox sql psql as postgres on stdin
# pnpm db:sandbox down
#
# Secrets are generated at `up` (random password, JWT secret, keys) into a
# 600 file under $FAUNOSTEO_SANDBOX_DIR (default ~/.cache/faunosteo-db-sandbox),
# outside the repository, and never printed. Every Supabase CLI call names the
# sandbox with --db-url: none may fall back to the local DEV database.
set -euo pipefail
cd "$(dirname "$0")/.."
. scripts/dev-env.sh
PG_IMAGE=public.ecr.aws/supabase/postgres:17.11.0.004
AUTH_IMAGE=public.ecr.aws/supabase/gotrue:v2.197.0
REST_IMAGE=public.ecr.aws/supabase/postgrest:v16.4
STORAGE_IMAGE=public.ecr.aws/supabase/storage-api:v1.79.36
MAIL_IMAGE=public.ecr.aws/supabase/mailpit:v1.31.3
NET=faunosteo_dbsandbox_net
DB=faunosteo_dbsandbox_db
AUTH=faunosteo_dbsandbox_auth
REST=faunosteo_dbsandbox_rest
STORAGE=faunosteo_dbsandbox_storage
MAIL=faunosteo_dbsandbox_mail
GATEWAY=faunosteo_dbsandbox_gateway
TRIAL_GATEWAY=faunosteo_dbsandbox_trial_gateway
WORKER=faunosteo_dbsandbox_worker_verify
AVATAR_WORKER=faunosteo_dbsandbox_worker_avatar
GATEWAY_IMAGE=nginx:1.31-alpine
WORKER_IMAGE=faunosteo_worker:dev
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025 PORT_GATEWAY=57800
PORT_TRIAL_GATEWAY=57801 PORT_TRIAL_APP=5193
STATE="${FAUNOSTEO_SANDBOX_DIR:-$HOME/.cache/faunosteo-db-sandbox}"
SECRETS="$STATE/secrets.sh"
CLI=node_modules/.bin/supabase
die() { echo "db-sandbox: $*" >&2; exit 1; }
# Both sides resolved: a repository reached through a symlink is still the repository.
REPO="$(realpath "$PWD")"
case "$(realpath -m "$STATE")" in
"$REPO" | "$REPO"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;;
esac
# The db:trial state folder (scripts/db-trial.sh) is never the sandbox's: `down` would delete
# files there while the trial volumes keep the matching passwords.
TRIAL_STATE="$(realpath -m "${FAUNOSTEO_TRIAL_DIR:-$HOME/.cache/faunosteo-trial}")"
if [ "$(realpath -m "$STATE")" = "$TRIAL_STATE" ] || [ -e "$STATE/.faunosteo-trial-state" ]; then
die "FAUNOSTEO_SANDBOX_DIR is the db:trial state folder: refused, nothing was run"
fi
# Every value must be present: an empty password would turn --db-url into an
# argument the CLI might read as absent and fall back to the DEV database.
load_secrets() {
[ -f "$SECRETS" ] || die "not running (pnpm db:sandbox up fresh)"
. "$SECRETS"
[ -n "${SB_PW:-}" ] && [ -n "${SB_JWT_SECRET:-}" ] && [ -n "${SB_ANON_KEY:-}" ] && [ -n "${SB_SERVICE_KEY:-}" ] \
&& [ -n "${SB_SEED_PW:-}" ] \
|| die "secrets file incomplete (pnpm db:sandbox down, then up)"
export PGPASSWORD="$SB_PW"
}
# No password in the URL: the CLI reads PGPASSWORD, so no secret is on a command line.
db_url() { printf 'postgresql://postgres@127.0.0.1:%s/postgres?sslmode=disable' "$PORT_DB"; }
sb_psql() {
docker exec -i -e PGPASSWORD "$DB" \
psql -X -q -h 127.0.0.1 -U postgres -d postgres -v ON_ERROR_STOP=1 "$@"
}
wait_for() {
local what="$1"; shift
for _ in $(seq 1 120); do "$@" >/dev/null 2>&1 && return 0; sleep 1; done
die "timeout waiting for $what"
}
new_secrets() {
mkdir -p "$STATE"; chmod 700 "$STATE"
( umask 077; node -e '
const c = require("crypto");
const secret = c.randomBytes(32).toString("hex");
const b64 = (o) => Buffer.from(JSON.stringify(o)).toString("base64url");
const jwt = (p) => { const h = b64({ alg: "HS256", typ: "JWT" }), b = b64(p);
return h + "." + b + "." + c.createHmac("sha256", secret).update(h + "." + b).digest("base64url"); };
const exp = Math.floor(Date.now() / 1000) + 30 * 86400;
console.log("SB_PW=" + c.randomBytes(24).toString("hex"));
console.log("SB_SEED_PW=" + c.randomBytes(24).toString("hex"));
console.log("SB_JWT_SECRET=" + secret);
console.log("SB_ANON_KEY=" + jwt({ role: "anon", iss: "db-sandbox", exp }));
console.log("SB_SERVICE_KEY=" + jwt({ role: "service_role", iss: "db-sandbox", exp }));
' > "$SECRETS.new" && mv "$SECRETS.new" "$SECRETS" )
load_secrets
# The values reach the containers through 600 env files, never through argv.
( umask 077
printf 'POSTGRES_PASSWORD=%s\nPGPASSWORD=%s\nJWT_SECRET=%s\n' "$SB_PW" "$SB_PW" "$SB_JWT_SECRET" > "$STATE/db.env"
printf 'GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres\nGOTRUE_JWT_SECRET=%s\n' \
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/auth.env"
printf 'ANON_KEY=%s\nSERVICE_KEY=%s\nAUTH_JWT_SECRET=%s\nDATABASE_URL=postgresql://supabase_storage_admin:%s@%s:5432/postgres\n' \
"$SB_ANON_KEY" "$SB_SERVICE_KEY" "$SB_JWT_SECRET" "$SB_PW" "$DB" > "$STATE/storage.env"
printf 'PGRST_DB_URI=postgresql://authenticator:%s@%s:5432/postgres\nPGRST_JWT_SECRET=%s\n' \
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/rest.env"
printf 'Authorization: Bearer %s\n' "$SB_SERVICE_KEY" > "$STATE/service-header" )
}
# The role passwords the CLI sets at db start (its /etc/postgresql.schema.sql),
# reduced to what the sandbox services use.
DB_INIT='cat <<'"'"'EOF'"'"' > /etc/postgresql.schema.sql && exec docker-entrypoint.sh postgres -D /etc/postgresql
\set pgpass `echo "$PGPASSWORD"`
\set jwt_secret `echo "$JWT_SECRET"`
ALTER DATABASE postgres SET "app.settings.jwt_secret" TO :'"'"'jwt_secret'"'"';
ALTER DATABASE postgres SET "app.settings.jwt_exp" TO '"'"'3600'"'"';
ALTER USER postgres WITH PASSWORD :'"'"'pgpass'"'"';
ALTER USER authenticator WITH PASSWORD :'"'"'pgpass'"'"';
ALTER USER supabase_auth_admin WITH PASSWORD :'"'"'pgpass'"'"';
ALTER USER supabase_storage_admin WITH PASSWORD :'"'"'pgpass'"'"';
EOF'
start_services() {
docker network create "$NET" >/dev/null
docker run -d --name "$DB" --network "$NET" -p 127.0.0.1:$PORT_DB:5432 \
--env-file "$STATE/db.env" -e POSTGRES_HOST=/var/run/postgresql -e JWT_EXP=3600 \
-e POSTGRES_INITDB_ARGS="--allow-group-access --locale-provider=icu --encoding=UTF-8 --icu-locale=en_US.UTF-8" \
--entrypoint sh "$PG_IMAGE" -c "$DB_INIT" >/dev/null
wait_for "database roles" docker exec -e PGPASSWORD "$DB" \
psql -h 127.0.0.1 -U supabase_auth_admin -d postgres -c 'select 1'
wait_for "database" docker exec -e PGPASSWORD "$DB" \
psql -h 127.0.0.1 -U postgres -d postgres -c 'select 1'
docker run -d --name "$MAIL" --network "$NET" -p 127.0.0.1:$PORT_MAIL:8025 "$MAIL_IMAGE" >/dev/null
docker run -d --name "$AUTH" --network "$NET" -p 127.0.0.1:$PORT_AUTH:9999 \
-e GOTRUE_API_HOST=0.0.0.0 -e GOTRUE_API_PORT=9999 -e API_EXTERNAL_URL=http://127.0.0.1:$PORT_AUTH \
-e GOTRUE_DB_DRIVER=postgres --env-file "$STATE/auth.env" \
-e GOTRUE_DB_MIGRATIONS_PATH=/usr/local/etc/auth/migrations \
-e GOTRUE_SITE_URL=http://127.0.0.1:5173 -e GOTRUE_DISABLE_SIGNUP=false \
-e GOTRUE_JWT_EXP=3600 \
-e GOTRUE_JWT_AUD=authenticated -e GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated \
-e GOTRUE_JWT_ADMIN_ROLES=service_role \
-e GOTRUE_EXTERNAL_EMAIL_ENABLED=true -e GOTRUE_EXTERNAL_PHONE_ENABLED=false \
-e GOTRUE_EXTERNAL_ANONYMOUS_USERS_ENABLED=false -e GOTRUE_MAILER_AUTOCONFIRM=false \
-e GOTRUE_MAILER_URLPATHS_CONFIRMATION=http://127.0.0.1:$PORT_AUTH/verify \
-e GOTRUE_SMTP_HOST="$MAIL" -e GOTRUE_SMTP_PORT=1025 \
-e GOTRUE_SMTP_ADMIN_EMAIL=admin@sandbox.local -e GOTRUE_SMTP_SENDER_NAME=sandbox \
-e GOTRUE_SMTP_MAX_FREQUENCY=1s -e GOTRUE_RATE_LIMIT_EMAIL_SENT=10000 \
-e GOTRUE_RATE_LIMIT_VERIFY=10000 -e GOTRUE_RATE_LIMIT_TOKEN_REFRESH=10000 \
-e GOTRUE_PASSWORD_MIN_LENGTH=12 "$AUTH_IMAGE" >/dev/null
wait_for "auth" curl -fsS http://127.0.0.1:$PORT_AUTH/health
docker run -d --name "$STORAGE" --network "$NET" -p 127.0.0.1:$PORT_STORAGE:5000 \
--env-file "$STATE/storage.env" \
-e FILE_SIZE_LIMIT=52428800 -e STORAGE_BACKEND=file -e FILE_STORAGE_BACKEND_PATH=/mnt \
-e TENANT_ID=stub -e STORAGE_S3_REGION=local -e GLOBAL_S3_BUCKET=stub \
-e ENABLE_IMAGE_TRANSFORMATION=false -e IMAGE_TRANSFORMATION_ENABLED=false \
-e S3_PROTOCOL_ENABLED=false -e DB_MIGRATIONS_FREEZE_AT= "$STORAGE_IMAGE" >/dev/null
wait_for "storage" curl -fsS http://127.0.0.1:$PORT_STORAGE/status
}
start_rest() {
docker run -d --name "$REST" --network "$NET" -p 127.0.0.1:$PORT_REST:3000 \
--env-file "$STATE/rest.env" \
-e PGRST_DB_SCHEMAS=public -e PGRST_DB_EXTRA_SEARCH_PATH=public,extensions \
-e PGRST_DB_ANON_ROLE=anon -e PGRST_DB_MAX_ROWS=1000 \
"$REST_IMAGE" >/dev/null
wait_for "postgrest" curl -fsS -o /dev/null http://127.0.0.1:$PORT_REST/
}
# The CLI's own migration runner, always against the sandbox URL.
migrate_up() { # workdir
"$CLI" --workdir "$1" migration up --db-url "$(db_url)"
}
seed() {
local paths
paths="$(sed -n 's/^sql_paths = \[\(.*\)\]/\1/p' supabase/config.toml | tr -d '" ' | tr ',' ' ')"
for f in $paths; do
echo " seed ${f#./}"; sb_psql --single-transaction < "supabase/${f#./}" >/dev/null
done
}
# Nobody signs in to these two accounts: each password is random, used once and
# kept nowhere; the body reaches curl on stdin (printf is a builtin), never argv.
throwaway_password() { od -An -N18 -tx1 /dev/urandom | tr -d ' \n'; }
gotrue_accounts() {
printf '{"email":"pre-confirmed@sandbox.local","password":"%s","email_confirm":true}' "$(throwaway_password)" \
| curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
-H @"$STATE/service-header" -H 'Content-Type: application/json' --data-binary @- >/dev/null
printf '{"email":"pre-unconfirmed@sandbox.local","password":"%s"}' "$(throwaway_password)" \
| curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
--data-binary @- >/dev/null
}
# The seed gives its accounts the DEV password (supabase/seed.sql); the sandbox
# replaces it at once by this session's secret, through GoTrue, body on stdin.
seed_passwords() {
local id
for id in $(sb_psql -t -A -c "select id from auth.users where email like '%@faunosteo.local'"); do
[[ "$id" =~ ^[0-9a-f-]{36}$ ]] || die "unexpected seed account id"
printf '{"password":"%s"}' "$SB_SEED_PW" \
| curl -fsS -X PUT "http://127.0.0.1:$PORT_AUTH/admin/users/$id" \
-H @"$STATE/service-header" -H 'Content-Type: application/json' --data-binary @- >/dev/null
done
}
# A CLI workdir holding only the migrations before <version>; kept in the
# state folder and refilled at each run (the CLI writes .temp there).
upgrade_workdir() { # version
local base="$STATE/upgrade-base"
mkdir -p "$base/supabase/migrations"
rm -f "$base"/supabase/migrations/*.sql
cp supabase/config.toml "$base/supabase/"
for f in supabase/migrations/*.sql; do
if [ "$(basename "$f" | cut -d_ -f1)" \< "$1" ]; then cp "$f" "$base/supabase/migrations/"; fi
done
printf '%s' "$base"
}
cmd_up() {
local mode="${1:-}"
# Arguments checked before anything starts: a refusal leaves no container or secret.
case "$mode" in
fresh) ;;
upgrade) [[ "${2:-}" =~ ^[0-9]{14}$ ]] \
|| die "up upgrade needs the first new migration version, e.g. 20261025100000"
[ -z "${3:-}" ] || { [[ "$3" =~ ^[0-9]{14}$ ]] && [ "$2" \< "$3" ]; } \
|| die "up upgrade <version> <then>: <then> is a later migration version" ;;
*) die "up fresh | up upgrade <version> [<then>]" ;;
esac
docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)"
new_secrets
start_services
case "$mode" in
fresh)
migrate_up . ; seed; seed_passwords ;;
upgrade)
migrate_up "$(upgrade_workdir "$2")"
seed; seed_passwords; gotrue_accounts
echo "== before: $(sb_psql -t -A -c 'select count(*) from public.profiles') profiles"
if [ -n "${3:-}" ]; then
migrate_up "$(upgrade_workdir "$3")"
echo "== between: $(sb_psql -t -A -c 'select count(*) from public.workspaces') workspaces"
fi
migrate_up . ;;
esac
start_rest
echo "== sandbox up ($mode): db 127.0.0.1:$PORT_DB, auth :$PORT_AUTH, rest :$PORT_REST, mail :$PORT_MAIL"
}
# pnpm db:sandbox migrate: forward only, on the running sandbox; the branch's
# pending migrations and nothing else (no reset, no seed, no container recreated).
cmd_migrate() {
[ $# -eq 0 ] || die "migrate takes no argument"
docker ps --format '{{.Names}}' | grep -qx "$DB" || die "sandbox not up (pnpm db:sandbox up fresh)"
load_secrets
local last='select max(version) from supabase_migrations.schema_migrations'
echo "== before: $(sb_psql -t -A -c "$last")"
migrate_up .
echo "== after: $(sb_psql -t -A -c "$last")"
}
cmd_down() {
docker rm -f "$AVATAR_WORKER" "$WORKER" "$TRIAL_GATEWAY" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
docker network rm "$NET" >/dev/null 2>&1 || true
rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header"
echo "== sandbox removed"
}
# A test runner gets the endpoints and keys through its environment, never argv.
with_sandbox_env() (
export SANDBOX_DB_CONTAINER="$DB" SANDBOX_PG_PASSWORD="$SB_PW" SANDBOX_SEED_PASSWORD="$SB_SEED_PW"
export SANDBOX_AUTH_URL="http://127.0.0.1:$PORT_AUTH" SANDBOX_REST_URL="http://127.0.0.1:$PORT_REST"
export SANDBOX_STORAGE_URL="http://127.0.0.1:$PORT_STORAGE" SANDBOX_MAIL_URL="http://127.0.0.1:$PORT_MAIL"
export SANDBOX_ANON_KEY="$SB_ANON_KEY" SANDBOX_SERVICE_KEY="$SB_SERVICE_KEY"
"$@"
)
# The Kong routes of the CLI stack (/auth/v1/, /rest/v1/, /storage/v1/) on the
# sandbox network; host port only for avatar-e2e, on 127.0.0.1 like the services.
GATEWAY_CONF='server {
listen 8000;
client_max_body_size 8m;
proxy_http_version 1.1;
location /auth/v1/ { proxy_pass http://'"$AUTH"':9999/; }
location /rest/v1/ { proxy_pass http://'"$REST"':3000/; }
location /storage/v1/ { proxy_pass http://'"$STORAGE"':5000/; }
location / { return 404; }
}'
start_gateway() { # [docker run options]
docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true "$@" \
--entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \
"$GATEWAY_CONF" >/dev/null
wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/
}
# The service key reaches the container through a 600 file removed once it started;
# memory as the compose service of the same mode (docker-compose.yml).
start_worker() { # name worker-id memory extra-env-lines args...
local name="$1" id="$2" memory="$3" extra="$4"; shift 4
worker_env="$(umask 077; mktemp "$STATE/worker.XXXXXX")"
printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=%s\nWORKER_POLL_SECONDS=1\n%s' \
"$GATEWAY" "$SB_SERVICE_KEY" "$id" "$extra" > "$worker_env"
docker run -d --name "$name" --network "$NET" --env-file "$worker_env" \
--read-only --cap-drop ALL --security-opt no-new-privileges:true \
--memory "$memory" --cpus 1 --pids-limit 64 --user nonroot \
"$WORKER_IMAGE" "$@" >/dev/null
rm -f "$worker_env"; worker_env=
for _ in $(seq 1 30); do
docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' && break
[ "$(docker inspect -f '{{.State.Running}}' "$name")" = true ] || die "worker exited before worker_start"
sleep 1
done
docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start"
echo "== worker up ($WORKER_IMAGE, $*, via $GATEWAY)"
}
# The worker's JSON lines hold no secret by design (crates/worker log allowlist);
# they are printed only after checking that neither key appears in them.
print_worker_log() { # name
local lines
lines="$(docker logs "$1" 2>&1)" || return 0
case "$lines" in
*"$SB_SERVICE_KEY"* | *"$SB_ANON_KEY"*) echo "== worker log withheld: it contains a key" ;;
*) echo "== worker log"; printf '%s\n' "$lines" ;;
esac
}
worker_cleanup() { # name
[ -n "${worker_env:-}" ] && rm -f "$worker_env"
print_worker_log "$1"
docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true
}
worker_preflight() { # name
load_secrets
docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)"
docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true
}
cmd_verify_e2e() {
worker_preflight "$WORKER"
trap 'worker_cleanup "$WORKER"' EXIT
start_gateway
start_worker "$WORKER" sandbox-verify 1280m '' run --jobs sheet-verify
SANDBOX_VERIFY_WORKER="$WORKER" with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase \
test:sandbox tests/sandbox/sheet-verification.sandbox.ts "$@"
}
# The avatar suite drives supabase-js through the gateway's loopback port; its
# psql calls name the sandbox database container. The suite accepts no other
# target, and a port already taken stops the run before anything starts.
cmd_avatar_e2e() {
worker_preflight "$AVATAR_WORKER"
if (exec 3<>"/dev/tcp/127.0.0.1/$PORT_GATEWAY") 2>/dev/null; then
die "port 127.0.0.1:$PORT_GATEWAY is already in use: free it, nothing was started"
fi
trap 'worker_cleanup "$AVATAR_WORKER"' EXIT
start_gateway -p 127.0.0.1:$PORT_GATEWAY:8000
[ "$(docker port "$GATEWAY" 8000/tcp)" = "127.0.0.1:$PORT_GATEWAY" ] \
|| die "the gateway is not published on 127.0.0.1:$PORT_GATEWAY only"
start_worker "$AVATAR_WORKER" sandbox-avatar 512m \
$'WORKER_SWEEP_SECONDS=5\nWORKER_QUARANTINE_GRACE_SECONDS=0\n' run
SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_ANON_KEY="$SB_ANON_KEY" \
SUPABASE_SERVICE_ROLE_KEY="$SB_SERVICE_KEY" AVATAR_E2E_DB_CONTAINER="$DB" \
node_modules/.bin/pnpm --filter @faunosteo/supabase test:e2e "$@"
}
# The upload tool talks to the gateway's loopback port with the typed keys in
# its environment (never argv); the runner refuses any other target. Nothing is
# started when the port is taken; the gateway is removed at exit.
cmd_sheet_assets_e2e() {
load_secrets
docker rm -f "$GATEWAY" >/dev/null 2>&1 || true
if (exec 3<>"/dev/tcp/127.0.0.1/$PORT_GATEWAY") 2>/dev/null; then
die "port 127.0.0.1:$PORT_GATEWAY is already in use: free it, nothing was started"
fi
trap 'docker rm -f "$GATEWAY" >/dev/null 2>&1 || true' EXIT
start_gateway -p 127.0.0.1:$PORT_GATEWAY:8000
[ "$(docker port "$GATEWAY" 8000/tcp)" = "127.0.0.1:$PORT_GATEWAY" ] \
|| die "the gateway is not published on 127.0.0.1:$PORT_GATEWAY only"
SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_ANON_KEY="$SB_ANON_KEY" \
SUPABASE_SERVICE_ROLE_KEY="$SB_SERVICE_KEY" bash scripts/sheet-assets-e2e.sh
}
# ---- Owner trial (docs/development/V1-2-6_OWNER_TRIAL.md) -------------------
port_taken() { (exec 3<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null; }
# The one place the trial host is decided: FAUNOSTEO_TRIAL_HOST, default the DEV host (the LAN
# address); 127.0.0.1 = host-only. Loopback or a private IPv4 of an interface of this machine,
# nothing else (no 0.0.0.0, no name, no public address).
trial_host() {
local host="${FAUNOSTEO_TRIAL_HOST:-$FAUNOSTEO_DEV_HOST}" a b c d
[[ "$host" =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$ ]] \
|| die "FAUNOSTEO_TRIAL_HOST must be an IPv4 address (not a name, not 0.0.0.0): $host"
a=$((10#${BASH_REMATCH[1]})) b=$((10#${BASH_REMATCH[2]}))
c=$((10#${BASH_REMATCH[3]})) d=$((10#${BASH_REMATCH[4]}))
[ "$a" -le 255 ] && [ "$b" -le 255 ] && [ "$c" -le 255 ] && [ "$d" -le 255 ] \
|| die "FAUNOSTEO_TRIAL_HOST is not an IPv4 address: $host"
[ "$host" = "$a.$b.$c.$d" ] || die "FAUNOSTEO_TRIAL_HOST: write the address without leading zeros"
if [ "$a" -eq 127 ]; then host=127.0.0.1
elif [ "$a" -eq 10 ] || { [ "$a" -eq 172 ] && [ "$b" -ge 16 ] && [ "$b" -le 31 ]; } \
|| { [ "$a" -eq 192 ] && [ "$b" -eq 168 ]; }; then :
else die "FAUNOSTEO_TRIAL_HOST must be loopback or a private address (10/8, 172.16/12, 192.168/16): $host"
fi
ip -4 -o addr | grep -qF "inet $host/" || die "FAUNOSTEO_TRIAL_HOST $host is not an address of this machine"
TRIAL_HOST="$host"
}
trial_email() { # email: one fixed shape, nothing else reaches curl or psql
[[ "${1:-}" =~ ^[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9.-]{1,120}\.[A-Za-z]{2,24}$ ]] \
|| die "an e-mail address is expected"
}
# Its own gateway container and loopback port, so the other commands' gateway is untouched.
start_trial_gateway() {
docker run -d --name "$TRIAL_GATEWAY" --network "$NET" --security-opt no-new-privileges:true \
-p 127.0.0.1:$PORT_TRIAL_GATEWAY:8000 \
--entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \
"$GATEWAY_CONF" >/dev/null
wait_for "trial gateway" docker exec "$TRIAL_GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/
[ "$(docker port "$TRIAL_GATEWAY" 8000/tcp)" = "127.0.0.1:$PORT_TRIAL_GATEWAY" ] \
|| die "the trial gateway is not published on 127.0.0.1:$PORT_TRIAL_GATEWAY only"
}
trial_stop() {
[ -z "${trial_pid:-}" ] || kill "$trial_pid" 2>/dev/null || true
docker rm -f "$TRIAL_GATEWAY" >/dev/null 2>&1 || true
}
# The app origin is the Vite server itself (/auth/v1, /rest/v1, /storage/v1 are proxied to the
# trial gateway, apps/app/vite/trial.config.ts): same origin, the app needs no CORS. The anon key reaches Vite
# through its environment, never argv.
cmd_trial() {
load_secrets
trial_host
[ -z "$(docker ps -a -q -f "name=^${TRIAL_GATEWAY}\$")" ] \
|| die "$TRIAL_GATEWAY exists (stop the running trial, or pnpm db:sandbox down)"
for port in $PORT_TRIAL_GATEWAY $PORT_TRIAL_APP; do
! port_taken "$port" || die "port 127.0.0.1:$port is already in use: nothing was started"
done
trap trial_stop EXIT
trap 'exit 130' INT TERM
start_trial_gateway
echo "== trial: http://$TRIAL_HOST:$PORT_TRIAL_APP (Ctrl+C stops the app and its gateway)"
( cd apps/app && FAUNOSTEO_TRIAL_GATEWAY="http://127.0.0.1:$PORT_TRIAL_GATEWAY" \
FAUNOSTEO_TRIAL_HOST="$TRIAL_HOST" \
VITE_SUPABASE_URL="http://$TRIAL_HOST:$PORT_TRIAL_APP" VITE_SUPABASE_ANON_KEY="$SB_ANON_KEY" \
exec env -u PGPASSWORD node_modules/.bin/vite --config vite/trial.config.ts --host "$TRIAL_HOST" \
--port $PORT_TRIAL_APP --strictPort ) &
trial_pid=$!
wait "$trial_pid"
}
# The password is typed twice at a hidden prompt, kept in this process only and sent to GoTrue
# on stdin through node (JSON escaping); nothing secret is printed.
cmd_trial_account() {
trial_email "${1:-}"
load_secrets
local pw confirm
read -rsp "Password (12 characters min.): " pw; echo
read -rsp "Again: " confirm; echo
[ "$pw" = "$confirm" ] || die "the two passwords differ"
[ "${#pw}" -ge 12 ] || die "12 characters min."
TRIAL_EMAIL="$1" TRIAL_PW="$pw" node -e '
process.stdout.write(JSON.stringify({ email: process.env.TRIAL_EMAIL,
password: process.env.TRIAL_PW, email_confirm: true }));' \
| curl -fsS -o /dev/null -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
-H @"$STATE/service-header" -H 'Content-Type: application/json' --data-binary @- \
|| die "account not created (already existing, or password refused)"
echo "== account created and confirmed: $1"
}
# The sheet_editor / sheet_publisher rights need an active admin role (O1); same SQL as
# apps/app/e2e/support/admin.ts. Email and kind are psql variables, never SQL text.
cmd_trial_grant() {
local email="${1:-}" kind="${2:-}"
trial_email "$email"
[[ "$kind" =~ ^(catalog_reader|sheet_editor|sheet_reviewer|sheet_publisher)$ ]] \
|| die "kind: catalog_reader | sheet_editor | sheet_reviewer | sheet_publisher"
load_secrets
sb_psql -v email="$email" -v kind="$kind" --single-transaction >/dev/null <<'SQL'
-- no row: \gset fails and the run stops (no account with this e-mail)
select id::text as uid from auth.users where email = :'email' \gset
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by)
select :'uid'::uuid, :'kind'::public.catalog_grant_kind, 'owner trial',
case when :'kind' = 'catalog_reader' then null else 'owner trial' end, :'uid'::uuid
where not exists (select 1 from public.anatomical_catalog_grants
where user_id = :'uid'::uuid and grant_kind = :'kind'::public.catalog_grant_kind and revoked_at is null);
select private.assign_platform_role(null, :'uid'::uuid, 'admin')
where :'kind' in ('sheet_editor', 'sheet_publisher')
and not exists (select 1 from public.profile_roles
where user_id = :'uid'::uuid and role = 'admin' and revoked_at is null);
SQL
echo "== $kind granted to $email"
}
case "${1:-}" in
up) shift; cmd_up "$@" ;;
down) cmd_down ;;
test-db) load_secrets; "$CLI" test db --db-url "$(db_url)" ;;
types)
load_secrets
out="$("$CLI" gen types typescript --db-url "$(db_url)" --schema public | node_modules/.bin/prettier --parser typescript)"
if [ "${2:-}" = --check ]; then
diff <(printf '%s\n' "$out") packages/supabase/src/database.types.ts && echo "== types match"
else
printf '%s\n' "$out" > packages/supabase/src/database.types.ts; echo "== types written"
fi ;;
test) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox "$@" ;;
e2e-shell) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell "$@" ;;
verify-e2e) shift; cmd_verify_e2e "$@" ;;
avatar-e2e) shift; cmd_avatar_e2e "$@" ;;
sheet-assets-e2e) cmd_sheet_assets_e2e ;;
trial) cmd_trial ;;
trial-account) shift; cmd_trial_account "$@" ;;
trial-grant) shift; cmd_trial_grant "$@" ;;
migrate) shift; cmd_migrate "$@" ;;
sql) load_secrets; sb_psql ;;
*) sed -n '2,36p' "$0"; exit 1 ;;
esac