feat(supabase): db:trial accounts, grants, equine drawings, app
account: confirmed account through GoTrue's admin API, hidden password typed twice. grant: owner/admin by the operator path (factors counted in SQL and again in the transaction, names sanitised); catalogue rights mirror grant_catalog_right (admin first for sheet_editor), audit row, no implicit admin. Every grant typed at a terminal. assets: R5.5 tool through the gateway. app: Vite stopped and waited on exit. 323 stub-only harness cases.
This commit is contained in:
1 parent
b85614aa09
commit
3b687a218e
3 files changed
+649
-7
No files matched your search
+258
-2
@@ -1548,6 +1548,248 @@ cmd_rotate_secrets() {
|
||||
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")"
|
||||
}
|
||||
|
||||
# ---- accounts, grants, drawings, app
|
||||
|
||||
trial_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"
|
||||
}
|
||||
|
||||
# Typed twice at a hidden prompt into the caller's variable; it lives in this process only. The
|
||||
# locals carry a prefix so that no caller's variable name can collide with them (nameref).
|
||||
# IFS is emptied: leading and trailing blanks are part of the password and count in the minimum.
|
||||
read_new_password() { # variable name
|
||||
local -n _rnp_typed=$1
|
||||
local _rnp_again
|
||||
IFS= read -rsp "Password (12 characters min.): " _rnp_typed || die "no password read: no account created"
|
||||
echo >&2
|
||||
IFS= read -rsp "Again: " _rnp_again || die "no password read: no account created"
|
||||
echo >&2
|
||||
[ "$_rnp_typed" = "$_rnp_again" ] || die "the two passwords differ: no account created"
|
||||
[ "${#_rnp_typed}" -ge 12 ] || die "12 characters min.: no account created"
|
||||
}
|
||||
|
||||
# GoTrue's admin API on its loopback port; the body (JSON built by node from the environment) goes
|
||||
# through stdin, the service key header through a process substitution (builtin printf, from the
|
||||
# very key client_key checked): never argv, never a file that could differ from it.
|
||||
create_account() { # email password
|
||||
TRIAL_EMAIL="$1" TRIAL_PW="$2" node -e '
|
||||
process.stdout.write(JSON.stringify({ email: process.env.TRIAL_EMAIL,
|
||||
password: process.env.TRIAL_PW, email_confirm: true }));' \
|
||||
| curl -q --noproxy '*' -fsS -o /dev/null -X POST "http://127.0.0.1:$PORT_AUTH/admin/users" \
|
||||
-H @<(printf 'Authorization: Bearer %s\n' "$TR_SERVICE_KEY") -H 'Content-Type: application/json' --data-binary @- \
|
||||
|| die "account not created (already existing, or password refused): nothing was changed"
|
||||
}
|
||||
# Terminal-only commands (a typed answer is read) refuse before any Docker call.
|
||||
require_terminal() { # action
|
||||
[ -t 0 ] || die "$1 needs a terminal (a typed answer is read, stdin is not one): nothing was changed"
|
||||
}
|
||||
cmd_account() {
|
||||
local password email
|
||||
[ $# -eq 1 ] || die "account <email>"
|
||||
trial_email "$1"
|
||||
email="${1,,}" # GoTrue stores addresses lowercased
|
||||
require_terminal account
|
||||
require_state
|
||||
take_lock
|
||||
require_running
|
||||
published_exactly "$AUTH"
|
||||
client_key "$TR_SERVICE_KEY" service_role
|
||||
read_new_password password
|
||||
create_account "$email" "$password"
|
||||
say "account $email created and confirmed"
|
||||
echo "Next: pnpm db:trial app, sign in, enrol TOTP in Mon compte > Sécurité (two factors for the owner account), then pnpm db:trial grant"
|
||||
}
|
||||
|
||||
# Platform roles follow V1-2_ANATOMICAL_SHEETS.md §5.7.1: the account enrols its factors first, the
|
||||
# operator reads them (name, creation time) and confirms, then the role is assigned last through
|
||||
# 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
|
||||
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';
|
||||
SQL
|
||||
}
|
||||
factor_list() { # email: name | creation time, one line per verified TOTP factor
|
||||
sb_psql -t -A -F ' | ' -v email="$1" <<'SQL'
|
||||
-- db-trial:grant-factors
|
||||
select left(regexp_replace(coalesce(nullif(f.friendly_name, ''), '(no name)'), '[^[:alnum:] ._-]', '?', 'g'), 40),
|
||||
to_char(f.created_at at time zone 'UTC', 'YYYY-MM-DD HH24:MI "UTC"')
|
||||
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'
|
||||
order by f.created_at;
|
||||
SQL
|
||||
}
|
||||
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" =~ ^[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)"
|
||||
factors="$(factor_list "$1")" || die "the verified TOTP factors of $1 cannot be read: nothing was granted"
|
||||
echo "Verified TOTP factors of $1 (check they are the ones its holder enrolled):"
|
||||
printf '%s\n' "$factors" | sed 's/^/ /'
|
||||
confirm "grant $2"
|
||||
sb_psql -v email="$1" -v role="$2" -v need="$need" -v operator="$(id -un)" >/dev/null <<'SQL' \
|
||||
|| die "$2 not granted to $1 (fewer factors than needed now, already active, or account inactive): nothing was granted"
|
||||
-- db-trial:grant-role
|
||||
begin;
|
||||
select id::text as uid from auth.users where email = :'email' \gset
|
||||
-- The factors are counted again here: when one went away since the check, no row comes back,
|
||||
-- \gset fails, ON_ERROR_STOP ends the session and the transaction rolls back unfinished.
|
||||
select private.assign_platform_role(null, :'uid'::uuid, :'role'::public.platform_role)::text as role_id
|
||||
where (select count(*) from auth.mfa_factors f
|
||||
where f.user_id = :'uid'::uuid and f.factor_type = 'totp' and f.status = 'verified') >= :need \gset
|
||||
select private.log_event(null, 'platform_role.operator_bootstrap', 'profile_role', :'role_id'::uuid,
|
||||
:'uid'::uuid, null, jsonb_build_object('role', :'role', 'operator', :'operator',
|
||||
'reason', 'db:trial operator bootstrap (V1-2 contract 5.7.1)'));
|
||||
commit;
|
||||
SQL
|
||||
say "$2 granted to $1: the account signs in again and passes the step-up (aal2)"
|
||||
}
|
||||
|
||||
# Catalogue rights mirror public.grant_catalog_right (it needs an owner JWT at aal2, so the operator
|
||||
# path cannot call it): confirmed active account; a verified TOTP factor for every kind but
|
||||
# catalog_reader; admin for sheet_editor and sheet_publisher; no active row of that kind. Nothing
|
||||
# grants a platform role here: the practitioner is made admin first, on purpose.
|
||||
catalog_grant_status() { # email kind: ok | already | no_account | inactive | no_totp | not_admin
|
||||
sb_psql -t -A -v email="$1" -v kind="$2" <<'SQL'
|
||||
-- db-trial:grant-catalog
|
||||
select case
|
||||
when u.id is null then 'no_account'
|
||||
when not private.is_confirmed_active_account(u.id) then 'inactive'
|
||||
when :'kind' <> 'catalog_reader' and not private.has_verified_totp(u.id) then 'no_totp'
|
||||
when :'kind' in ('sheet_editor', 'sheet_publisher') and not private.is_platform_admin(u.id) then 'not_admin'
|
||||
when exists (select 1 from public.anatomical_catalog_grants g
|
||||
where g.user_id = u.id and g.grant_kind = :'kind'::public.catalog_grant_kind
|
||||
and g.revoked_at is null and (g.expires_at is null or g.expires_at > now())) then 'already'
|
||||
else 'ok' end
|
||||
from (select 1) one left join auth.users u on u.email = :'email';
|
||||
SQL
|
||||
}
|
||||
grant_catalog_right() { # email kind
|
||||
local status
|
||||
status="$(catalog_grant_status "$1" "$2")" || die "the state of $1 cannot be read: nothing was granted"
|
||||
case "$status" in
|
||||
ok) ;;
|
||||
already) say "$2 already active for $1: nothing changed"; return 0 ;;
|
||||
no_account) die "no account $1 (pnpm db:trial account $1 first): nothing was granted" ;;
|
||||
inactive) die "the account $1 is inactive or its e-mail is not confirmed: nothing was granted" ;;
|
||||
no_totp) die "$1 holds no verified TOTP factor: the account enrols one in Mon compte > Sécurité first (nothing was granted)" ;;
|
||||
not_admin) die "$2 needs $1 to hold admin first: pnpm db:trial grant $1 admin, then this again (nothing was granted)" ;;
|
||||
*) die "unexpected answer from the database: nothing is known about $2 for $1" ;;
|
||||
esac
|
||||
confirm "grant $2"
|
||||
# granted_by is the account's own id: the column is NOT NULL and the operator has no profile.
|
||||
sb_psql -v email="$1" -v kind="$2" -v operator="$(id -un)" >/dev/null <<'SQL' \
|
||||
|| die "$2 not granted to $1 (the account changed since the check?): nothing was granted"
|
||||
-- db-trial:grant-catalog-write
|
||||
begin;
|
||||
select id::text as uid from auth.users where email = :'email' \gset
|
||||
select 1 as locked from public.profiles where id = :'uid'::uuid for no key update \gset
|
||||
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by)
|
||||
select :'uid'::uuid, :'kind'::public.catalog_grant_kind, 'db:trial operator bootstrap',
|
||||
case when :'kind' = 'catalog_reader' then null else 'db:trial operator bootstrap' end, :'uid'::uuid
|
||||
where private.is_confirmed_active_account(:'uid'::uuid)
|
||||
and (:'kind' = 'catalog_reader' or private.has_verified_totp(:'uid'::uuid))
|
||||
and (:'kind' not in ('sheet_editor', 'sheet_publisher') or private.is_platform_admin(:'uid'::uuid))
|
||||
and not exists (select 1 from public.anatomical_catalog_grants g
|
||||
where g.user_id = :'uid'::uuid and g.grant_kind = :'kind'::public.catalog_grant_kind
|
||||
and g.revoked_at is null and (g.expires_at is null or g.expires_at > now()))
|
||||
returning id::text as grant_id \gset
|
||||
select private.log_event(null, 'catalog_grant.operator_bootstrap', 'catalog_grant', :'grant_id'::uuid,
|
||||
:'uid'::uuid, null, jsonb_build_object('kind', :'kind', 'operator', :'operator',
|
||||
'reason', 'db:trial operator bootstrap'));
|
||||
commit;
|
||||
SQL
|
||||
say "$2 granted to $1"
|
||||
}
|
||||
cmd_grant() {
|
||||
local email
|
||||
[ $# -eq 2 ] || die "grant <email> <owner|admin|catalog_reader|sheet_editor|sheet_reviewer|sheet_publisher>"
|
||||
trial_email "$1"
|
||||
email="${1,,}" # GoTrue stores addresses lowercased
|
||||
[[ "$2" =~ ^(owner|admin|catalog_reader|sheet_editor|sheet_reviewer|sheet_publisher)$ ]] \
|
||||
|| die "right: owner | admin | catalog_reader | sheet_editor | sheet_reviewer | sheet_publisher"
|
||||
require_terminal grant
|
||||
require_state
|
||||
take_lock
|
||||
require_running
|
||||
case "$2" in
|
||||
owner | admin) grant_platform_role "$email" "$2" ;;
|
||||
*) grant_catalog_right "$email" "$2" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# The existing R5.5 tool (no overwrite, verify after upload, rerun = nothing to do) through the
|
||||
# trial gateway; the service key in its environment only; the LAN address and pnpm's INIT_CWD
|
||||
# dropped so the tool accepts loopback only and resolves the seed folder from the repository.
|
||||
upload_assets() { # [--check]
|
||||
( unset FAUNOSTEO_DEV_HOST INIT_CWD
|
||||
SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_SERVICE_ROLE_KEY="$TR_SERVICE_KEY" \
|
||||
bash scripts/upload-sheet-assets.sh "$@" "$SEED" )
|
||||
}
|
||||
cmd_assets() {
|
||||
[ $# -eq 0 ] || die "assets takes no argument"
|
||||
local out expected
|
||||
require_state
|
||||
take_lock
|
||||
require_running
|
||||
gateway_identity
|
||||
client_key "$TR_SERVICE_KEY" service_role
|
||||
upload_assets || die "the upload tool failed: some drawings may be uploaded already, pnpm db:trial assets again does nothing for those"
|
||||
out="$(upload_assets --check)" || die "the upload tool's check failed: pnpm db:trial assets again"
|
||||
printf '%s\n' "$out"
|
||||
expected="$(expected_assets | grep -c .)"
|
||||
[ "$(grep -c 'nothing to do' <<<"$out")" -eq "$expected" ] || die "the tool does not report every drawing verified"
|
||||
[ "$(uploaded_count)" -eq "$expected" ] || die "not every drawing row is confirmed with its object in the bucket"
|
||||
say "equine drawings: $expected/$expected uploaded, verified and confirmed"
|
||||
}
|
||||
|
||||
# No proxy variable reaches Vite (Node 24 honours NODE_USE_ENV_PROXY): its identity fetch and its
|
||||
# proxy to the gateway go straight to 127.0.0.1.
|
||||
# The app origin is the Vite server itself (/auth/v1, /rest/v1, /storage/v1 proxied to the trial
|
||||
# gateway by apps/app/vite/trial.config.ts): same origin, no CORS. The anon key reaches Vite
|
||||
# through its environment, never argv.
|
||||
# The traps find Vite through the job table, so no signal window between the launch and the
|
||||
# assignment of its pid leaves it running; the stop waits for it, so the port is free on return.
|
||||
app_stop() {
|
||||
local pid
|
||||
for pid in $(jobs -p); do kill "$pid" 2>/dev/null || true; done
|
||||
wait 2>/dev/null || true
|
||||
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,
|
||||
# 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.
|
||||
cmd_app() {
|
||||
[ $# -eq 0 ] || die "app takes no argument"
|
||||
local host
|
||||
require_running
|
||||
gateway_identity
|
||||
client_key "$TR_ANON_KEY" anon
|
||||
host="$(trial_host)"
|
||||
if port_listening "$PORT_APP"; then die "port $PORT_APP is in use: nothing was started"; fi
|
||||
trap app_stop EXIT
|
||||
trap 'exit 130' INT TERM
|
||||
say "trial app: http://$host:$PORT_APP (Ctrl+C stops the app; the stack runs until pnpm db:trial stop)"
|
||||
( cd apps/app && FAUNOSTEO_TRIAL_GATEWAY="http://127.0.0.1:$PORT_GATEWAY" FAUNOSTEO_TRIAL_HOST="$host" \
|
||||
VITE_SUPABASE_URL="http://$host:$PORT_APP" VITE_SUPABASE_ANON_KEY="$TR_ANON_KEY" \
|
||||
NODE_OPTIONS=--max-old-space-size=768 \
|
||||
exec env -u http_proxy -u HTTP_PROXY -u https_proxy -u HTTPS_PROXY -u all_proxy -u ALL_PROXY \
|
||||
-u no_proxy -u NO_PROXY -u NODE_USE_ENV_PROXY \
|
||||
node_modules/.bin/vite --config vite/trial.config.ts --host "$host" --port "$PORT_APP" --strictPort ) &
|
||||
wait $!
|
||||
}
|
||||
|
||||
# Every command's arguments are checked before any Docker call (a refusal here logs nothing).
|
||||
# The commands keep their own checks too, so each stays safe if called on its own.
|
||||
check_arguments() { # action args...
|
||||
@@ -1556,11 +1798,21 @@ check_arguments() { # action args...
|
||||
case "$action" in
|
||||
plan | create | start | stop | status)
|
||||
[ $# -eq 0 ] || die "$action takes no argument" ;;
|
||||
migrate | backup | rotate-keys | rotate-secrets | destroy)
|
||||
migrate | backup | rotate-keys | rotate-secrets)
|
||||
[ $# -eq 0 ] || die "$action takes no argument" ;;
|
||||
destroy) [ $# -eq 0 ] || die "$action takes no argument"; require_terminal destroy ;;
|
||||
app | assets) [ $# -eq 0 ] || die "$action takes no argument" ;;
|
||||
account) [ $# -eq 1 ] || die "account <email>"; trial_email "$1"; require_terminal account ;;
|
||||
grant)
|
||||
[ $# -eq 2 ] || die "grant <email> <owner|admin|catalog_reader|sheet_editor|sheet_reviewer|sheet_publisher>"
|
||||
trial_email "$1"
|
||||
[[ "$2" =~ ^(owner|admin|catalog_reader|sheet_editor|sheet_reviewer|sheet_publisher)$ ]] \
|
||||
|| die "right: owner | admin | catalog_reader | sheet_editor | sheet_reviewer | sheet_publisher"
|
||||
require_terminal grant ;;
|
||||
restore)
|
||||
[ $# -eq 1 ] && [[ "$1" =~ ^[0-9]{8}T[0-9]{6}Z-[a-z0-9-]+$ ]] \
|
||||
|| die "restore <backup>: the name of a folder in $BACKUPS (pnpm db:trial status lists them)" ;;
|
||||
|| die "restore <backup>: the name of a folder in $BACKUPS (pnpm db:trial status lists them)"
|
||||
require_terminal restore ;;
|
||||
*) usage; exit 1 ;;
|
||||
esac
|
||||
}
|
||||
@@ -1591,5 +1843,9 @@ case "$action" in
|
||||
rotate-keys) cmd_rotate_keys "$@" ;;
|
||||
rotate-secrets) cmd_rotate_secrets "$@" ;;
|
||||
destroy) cmd_destroy "$@" ;;
|
||||
app) cmd_app "$@" ;;
|
||||
account) cmd_account "$@" ;;
|
||||
grant) cmd_grant "$@" ;;
|
||||
assets) cmd_assets "$@" ;;
|
||||
*) usage; exit 1 ;;
|
||||
esac
|
||||
Reference in new issue
Block a user