feat(supabase): owner trial sandbox commands and the V1-2.6 trial guide

This commit is contained in:
bchanot committed 2026-11-04 14:11:23 +01:00
1 parent aa5c10d73c
commit ac5fbfbc44
5 files changed
+297 -2

No files matched your search

+101 -2
View File
@@ -22,6 +22,11 @@
# 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://127.0.0.1:$PORT_TRIAL_APP,
# 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
#
@@ -45,11 +50,13 @@ 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
@@ -252,7 +259,7 @@ cmd_up() {
}
cmd_down() {
docker rm -f "$AVATAR_WORKER" "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
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"
@@ -375,6 +382,95 @@ cmd_sheet_assets_e2e() {
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; }
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, no CORS. The anon key reaches Vite
# through its environment, never argv.
cmd_trial() {
load_secrets
[ -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://127.0.0.1:$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" \
VITE_SUPABASE_URL="http://127.0.0.1:$PORT_TRIAL_APP" VITE_SUPABASE_ANON_KEY="$SB_ANON_KEY" \
exec node_modules/.bin/vite --config vite/trial.config.ts --host 127.0.0.1 \
--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 ;;
@@ -392,6 +488,9 @@ case "${1:-}" in
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 "$@" ;;
sql) load_secrets; sb_psql ;;
*) sed -n '2,31p' "$0"; exit 1 ;;
*) sed -n '2,36p' "$0"; exit 1 ;;
esac