Files
adminandClaude Opus 5 30152eda52 One account: read the platform's identity, stop keeping your own
This module no longer has users of its own. It reads the family's shared
identity store -- app_user, session and module_moderator in a neutral
schema -- so the session created when someone signs in at the hub is the
session this app resolves. One origin, one cookie, no handoff: no signed
assertion, no audience binding, nothing to replay.

What that costs this app, deliberately:

- Its login page is gone. Signing in happens at the hub, and the sign-in
  card points there.
- It can read identity and it cannot write it. The grants say so, not a
  convention: no INSERT on app_user or session, nothing at all on the
  password hash, and no way to make anyone a moderator -- of itself or
  of anything else. A module that could insert a session row could mint
  a credential for anyone.
- App tokens are listed and revoked here, but minted at the hub.
- "Administrator" is platform-wide; "moderator" is this module alone.
  An admin check now passes for either.

The cutover revision merges this schema's account into the platform's,
matched on lower(email), repoints every reference to it, and drops the
local tables. The DROP is deliberately not CASCADE: a reference the
repoint list does not know about must refuse the drop loudly rather than
disappear quietly. That refusal is the completeness proof.

Requires dredd-core v0.10.0. A standalone deployment -- its own database,
its own identity, no platform -- is unchanged and still supported.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-26 12:54:02 +02:00

359 lines
19 KiB
PL/PgSQL

-- =================================================================================================
-- Managed Postgres provisioning for one app of the family, in the shared database:
-- schema galena, migrator galena_migrator, app galena_app, extensions: pgcrypto, migrator statement_timeout 30min
-- search_path galena, dredd, extensions; shared identity schema dredd, owned by atrium_migrator (created by its owner, USAGE only here)
--
-- Rendered by dredd-core. Do not edit: re-render, and let CI check it is current (exit 1 = stale):
-- python -m dredd_core.managed provision --sql-only \
-- --schema galena --app-role galena_app --migrator-role galena_migrator --extension pgcrypto --identity-schema dredd --identity-owner atrium_migrator \
-- --output db/managed/provision.sql (CI: --check-file db/managed/provision.sql)
--
-- Preferred path, from a shell (passwords come from environment variables and are never printed):
-- python -m dredd_core.managed provision --admin-url ADMIN_DATABASE_URL \
-- --schema galena --app-role galena_app --migrator-role galena_migrator --extension pgcrypto --identity-schema dredd --identity-owner atrium_migrator \
-- --migrator-password-env GALENA_MIGRATOR_PASSWORD --app-password-env GALENA_APP_PASSWORD
--
-- SQL editor path (no shell): paste this whole file into the provider's SQL editor, replace both
-- change_me passwords in step 2 there (never in the committed file; at least 16 printable ASCII
-- characters, different per role), and run it once as the platform admin (postgres). Re-running is
-- safe: an existing role keeps its password unless reset_passwords is set to true (do that after a
-- restore, which may drop custom role passwords). It never touches schema public and never drops
-- anything.
-- =================================================================================================
BEGIN;
-- 1. preflight: refuse a connection, schema, role or extension this must not touch
DO $dredd_provision_preflight$
DECLARE
target_schema text := 'galena';
migrator_role text := 'galena_migrator';
app_role text := 'galena_app';
admin_roles text[] := ARRAY['postgres', 'postgres_managed']::text[];
extension_names text[] := ARRAY['pgcrypto']::text[];
identity_schema text := 'dredd';
identity_owner text := 'atrium_migrator';
owns_identity boolean := false;
seen text;
r text;
e text;
BEGIN
IF current_user::text IN (migrator_role, app_role) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: connected as ' || quote_ident(current_user::text)
|| ', one of the roles being provisioned',
HINT = 'Run it as the platform admin: postgres on a managed provider (postgres_managed in the local emulation).';
END IF;
IF (SELECT rolsuper FROM pg_roles WHERE rolname = current_user) THEN
RAISE WARNING USING
MESSAGE = 'provision: connected as the superuser ' || quote_ident(current_user::text),
HINT = 'Fine on a local cluster or in CI; on a managed provider run it as postgres, which is not a superuser there.';
ELSIF NOT (current_user::text = ANY (admin_roles)) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: connected as ' || quote_ident(current_user::text)
|| ', which is neither the platform admin (' || array_to_string(admin_roles, ', ')
|| ') nor a superuser',
HINT = 'Run it as postgres on a managed provider (postgres_managed in the local emulation).';
ELSIF NOT (SELECT rolcreaterole FROM pg_roles WHERE rolname = current_user)
AND EXISTS (SELECT 1 FROM unnest(ARRAY[migrator_role, app_role]) AS n
WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = n)) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: ' || quote_ident(current_user::text) || ' has no CREATEROLE, and a role is missing',
HINT = 'Run it as the platform admin, which can create roles.';
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = 'extensions') THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: schema extensions does not exist',
HINT = 'A managed provider has it. On a plain PostgreSQL test cluster run '
|| 'python -m dredd_core.managed emulate (TEST-ONLY) first.';
END IF;
SELECT pg_get_userbyid(nspowner)::text INTO seen FROM pg_namespace WHERE nspname = target_schema;
IF seen IS NOT NULL AND seen <> migrator_role THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: schema ' || quote_ident(target_schema) || ' already exists and is owned by '
|| quote_ident(seen) || ', not ' || quote_ident(migrator_role),
HINT = 'Another app, or an earlier setup, uses that schema; provisioning never takes a schema over. '
|| 'Check the schema and role names.';
END IF;
IF identity_schema <> '' THEN
SELECT pg_get_userbyid(nspowner)::text INTO seen FROM pg_namespace WHERE nspname = identity_schema;
IF seen IS NOT NULL AND seen <> identity_owner THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: the shared identity schema ' || quote_ident(identity_schema)
|| ' already exists and is owned by ' || quote_ident(seen) || ', not '
|| quote_ident(identity_owner),
HINT = 'Another deployment, or an earlier setup, uses that name; provisioning never takes a '
|| 'schema over. Check --identity-schema and --identity-owner.';
END IF;
IF seen IS NULL AND NOT owns_identity THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: the shared identity schema ' || quote_ident(identity_schema)
|| ' does not exist, and this app does not own it',
HINT = 'Provision the app whose migrator is ' || quote_ident(identity_owner) || ' first: its '
|| 'run creates the schema, and this one only asks for USAGE on it. (Ordering the other '
|| 'way round is what a compose depends_on is for.)';
END IF;
END IF;
FOREACH r IN ARRAY ARRAY[migrator_role, app_role] LOOP
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = r
AND (rolsuper OR rolcreaterole OR rolcreatedb OR rolreplication OR rolbypassrls
OR NOT rolcanlogin)) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: role ' || quote_ident(r) || ' already exists and is not a plain LOGIN role',
HINT = 'An app role is LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS: '
|| 'fix it with ALTER ROLE, or check the role names.';
END IF;
END LOOP;
FOREACH e IN ARRAY extension_names LOOP
SELECT n.nspname::text INTO seen
FROM pg_extension x JOIN pg_namespace n ON n.oid = x.extnamespace WHERE x.extname = e;
IF seen IS NOT NULL AND seen <> 'extensions' THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: extension ' || quote_ident(e) || ' is installed in schema ' || quote_ident(seen)
|| ', not extensions',
HINT = 'The app roles'' search_path is their schema and extensions, so its functions and operator '
|| 'classes would not resolve. Move it (ALTER EXTENSION ... SET SCHEMA extensions) once '
|| 'nothing depends on where it is.';
END IF;
END LOOP;
END
$dredd_provision_preflight$;
-- 2. roles: create galena_migrator and galena_app (plain LOGIN roles) where missing
DO $dredd_provision_roles$
DECLARE
migrator_role text := 'galena_migrator';
app_role text := 'galena_app';
migrator_password text := 'change_me'; -- replace change_me here (in the SQL editor, never in the committed file)
app_password text := 'change_me'; -- replace change_me here (in the SQL editor, never in the committed file)
reset_passwords boolean := false; -- true: also set the password of a role that already exists
placeholders text[] := ARRAY['change_me', 'changeme', 'change-me', 'replace_me', 'replaceme', 'placeholder', 'password', 'your_password', 'yourpassword']::text[];
names text[];
passwords text[];
pw text;
i int;
BEGIN
names := ARRAY[migrator_role, app_role];
passwords := ARRAY[migrator_password, app_password];
FOR i IN 1..2 LOOP
CONTINUE WHEN EXISTS (SELECT 1 FROM pg_roles WHERE rolname = names[i]) AND NOT reset_passwords;
pw := passwords[i];
IF pw IS NULL
OR (NOT starts_with(pw, 'SCRAM-SHA-256$')
AND (pw !~ '^[!-~]([ -~]*[!-~])?$'
OR length(pw) < 16
OR (SELECT count(DISTINCT c) FROM regexp_split_to_table(pw, '') AS c) < 6
OR EXISTS (SELECT 1 FROM unnest(placeholders) AS w WHERE strpos(lower(pw), w) > 0)
OR lower(pw) IN (migrator_role, app_role))) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: the password for ' || quote_ident(names[i]) || ' is empty or a placeholder',
HINT = 'Use at least 16 printable ASCII characters, not change_me, and a different one per role.';
END IF;
IF passwords[3 - i] IS NOT NULL AND pw = passwords[3 - i] THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: the passwords for ' || quote_ident(migrator_role) || ' and '
|| quote_ident(app_role) || ' are identical',
HINT = 'Each role needs its own password.';
END IF;
END LOOP;
FOR i IN 1..2 LOOP
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = names[i]) THEN
EXECUTE 'CREATE ROLE ' || quote_ident(names[i])
|| ' LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS INHERIT PASSWORD '
|| quote_literal(passwords[i]);
ELSIF reset_passwords THEN
EXECUTE 'ALTER ROLE ' || quote_ident(names[i]) || ' PASSWORD ' || quote_literal(passwords[i]);
END IF;
END LOOP;
END
$dredd_provision_roles$;
-- 3. temporary membership: PostgreSQL 16+ needs SET on galena_migrator to hand it a schema
GRANT galena_migrator TO CURRENT_USER;
-- 4. schema galena, owned by galena_migrator
CREATE SCHEMA IF NOT EXISTS galena AUTHORIZATION galena_migrator;
-- 5. schema galena: nothing for PUBLIC
REVOKE ALL ON SCHEMA galena FROM PUBLIC;
-- 6. schema galena: USAGE for galena_app
GRANT USAGE ON SCHEMA galena TO galena_app;
-- 7. schema extensions: USAGE for galena_migrator and galena_app
GRANT USAGE ON SCHEMA extensions TO galena_migrator, galena_app;
-- 8. schema extensions: check that the USAGE grant took
DO $dredd_provision_extensions_usage$
DECLARE
migrator_role text := 'galena_migrator';
app_role text := 'galena_app';
BEGIN
IF NOT (has_schema_privilege(migrator_role, 'extensions', 'USAGE')
AND has_schema_privilege(app_role, 'extensions', 'USAGE')) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: could not grant USAGE on schema extensions to ' || quote_ident(migrator_role)
|| ' and ' || quote_ident(app_role),
HINT = 'The connected role holds no grant option on schema extensions (PostgreSQL then only warns '
|| '"no privileges were granted"). The apps need it for pg_trgm operator classes and extension '
|| 'functions: have the platform owner GRANT USAGE ON SCHEMA extensions TO both roles.';
END IF;
END
$dredd_provision_extensions_usage$;
-- 9. temporary membership in the identity owner atrium_migrator: needed to grant on dredd
GRANT atrium_migrator TO CURRENT_USER;
-- 10. schema dredd: USAGE for galena_migrator and galena_app
GRANT USAGE ON SCHEMA dredd TO galena_migrator, galena_app;
-- 11. schema dredd: check that the USAGE grant took
DO $dredd_provision_identity_usage$
DECLARE
identity_schema text := 'dredd';
migrator_role text := 'galena_migrator';
app_role text := 'galena_app';
identity_owner text := 'atrium_migrator';
BEGIN
IF NOT (has_schema_privilege(migrator_role, identity_schema, 'USAGE')
AND has_schema_privilege(app_role, identity_schema, 'USAGE')) THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: could not grant USAGE on the shared identity schema '
|| quote_ident(identity_schema) || ' to ' || quote_ident(migrator_role) || ' and '
|| quote_ident(app_role),
HINT = 'The connected role holds no grant option there (PostgreSQL then only warns "no '
|| 'privileges were granted"), because the schema belongs to ' || quote_ident(identity_owner)
|| '. The temporary membership in that role is what carries the grant option: check '
|| '--identity-owner names the role that really owns the schema.';
END IF;
END
$dredd_provision_identity_usage$;
-- 12. search_path of galena_migrator
ALTER ROLE galena_migrator SET search_path = galena, dredd, extensions;
-- 13. search_path of galena_app
ALTER ROLE galena_app SET search_path = galena, dredd, extensions;
-- 14. statement_timeout of galena_migrator
ALTER ROLE galena_migrator SET statement_timeout = '30min';
-- 15. extension pgcrypto in schema extensions
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA extensions;
-- 16. end the temporary membership: no standing SET ROLE galena_migrator for the admin
REVOKE galena_migrator FROM CURRENT_USER;
-- 17. end the temporary membership in atrium_migrator
REVOKE atrium_migrator FROM CURRENT_USER;
-- 18. verify the end state
DO $dredd_provision_verify$
DECLARE
target_schema text := 'galena';
migrator_role text := 'galena_migrator';
app_role text := 'galena_app';
extension_names text[] := ARRAY['pgcrypto']::text[];
timeout_setting text := 'statement_timeout=30min';
path_setting text := 'search_path=galena, dredd, extensions';
identity_schema text := 'dredd';
identity_owner text := 'atrium_migrator';
admin_roles text[] := ARRAY['postgres', 'postgres_managed']::text[];
admin_is_super boolean := (SELECT rolsuper FROM pg_roles WHERE rolname = current_user);
problems text[] := '{}'::text[];
held boolean;
seen text;
r text;
e text;
BEGIN
FOREACH r IN ARRAY ARRAY[migrator_role, app_role] LOOP
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = r AND rolcanlogin
AND NOT (rolsuper OR rolcreaterole OR rolcreatedb OR rolreplication OR rolbypassrls)) THEN
problems := problems || ('role ' || quote_ident(r) || ' is missing or not a plain LOGIN role');
CONTINUE;
END IF;
IF NOT has_schema_privilege(r, 'extensions', 'USAGE') THEN
problems := problems || (quote_ident(r) || ' has no USAGE on schema extensions');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_db_role_setting s
WHERE s.setrole = (SELECT oid FROM pg_roles WHERE rolname = r) AND s.setdatabase = 0
AND path_setting = ANY (s.setconfig)) THEN
problems := problems || (quote_ident(r) || ' does not have ' || path_setting);
END IF;
IF NOT admin_is_super THEN
IF current_setting('server_version_num')::int >= 160000 THEN
held := pg_has_role(r::name, 'SET') OR pg_has_role(r::name, 'USAGE');
ELSE
held := pg_has_role(r::name, 'MEMBER');
END IF;
IF held THEN
problems := problems || (quote_ident(current_user::text) || ' can still SET ROLE or use the privileges of '
|| quote_ident(r) || ' (a membership granted by another role?)');
END IF;
END IF;
END LOOP;
IF (SELECT pg_get_userbyid(nspowner)::text FROM pg_namespace WHERE nspname = target_schema)
IS DISTINCT FROM migrator_role THEN
problems := problems || ('schema ' || quote_ident(target_schema) || ' is missing or not owned by '
|| quote_ident(migrator_role));
ELSE
IF has_schema_privilege('public', target_schema, 'USAGE') OR has_schema_privilege('public', target_schema, 'CREATE') THEN
problems := problems || ('PUBLIC still holds a privilege on schema ' || quote_ident(target_schema));
END IF;
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = app_role) THEN
IF NOT has_schema_privilege(app_role, target_schema, 'USAGE') THEN
problems := problems || (quote_ident(app_role) || ' has no USAGE on schema ' || quote_ident(target_schema));
END IF;
IF has_schema_privilege(app_role, target_schema, 'CREATE') THEN
problems := problems || (quote_ident(app_role) || ' can CREATE in schema ' || quote_ident(target_schema));
END IF;
END IF;
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_db_role_setting s
WHERE s.setrole = (SELECT oid FROM pg_roles WHERE rolname = migrator_role) AND s.setdatabase = 0
AND timeout_setting = ANY (s.setconfig)) THEN
problems := problems || (quote_ident(migrator_role) || ' does not have ' || timeout_setting);
END IF;
IF identity_schema <> '' THEN
SELECT pg_get_userbyid(nspowner)::text INTO seen FROM pg_namespace WHERE nspname = identity_schema;
IF seen IS DISTINCT FROM identity_owner THEN
problems := problems || ('the shared identity schema ' || quote_ident(identity_schema)
|| ' is missing or not owned by ' || quote_ident(identity_owner));
ELSE
IF (SELECT rolsuper FROM pg_roles WHERE rolname = identity_owner)
OR identity_owner = ANY (admin_roles) THEN
problems := problems || ('the identity owner ' || quote_ident(identity_owner)
|| ' is a superuser or the platform admin role');
END IF;
IF has_schema_privilege('public', identity_schema, 'USAGE')
OR has_schema_privilege('public', identity_schema, 'CREATE') THEN
problems := problems || ('PUBLIC still holds a privilege on schema ' || quote_ident(identity_schema));
END IF;
FOREACH r IN ARRAY ARRAY[migrator_role, app_role] LOOP
CONTINUE WHEN NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = r);
IF NOT has_schema_privilege(r, identity_schema, 'USAGE') THEN
problems := problems || (quote_ident(r) || ' has no USAGE on schema ' || quote_ident(identity_schema));
END IF;
-- Only the owner creates in the shared schema: its tables are the owning app's migration,
-- and a second app creating one there is how two apps end up with two account tables.
IF r <> identity_owner AND has_schema_privilege(r, identity_schema, 'CREATE') THEN
problems := problems || (quote_ident(r) || ' can CREATE in schema ' || quote_ident(identity_schema));
END IF;
END LOOP;
END IF;
END IF;
FOREACH e IN ARRAY extension_names LOOP
IF NOT EXISTS (SELECT 1 FROM pg_extension x JOIN pg_namespace n ON n.oid = x.extnamespace
WHERE x.extname = e AND n.nspname = 'extensions') THEN
problems := problems || ('extension ' || quote_ident(e) || ' is not installed in schema extensions');
END IF;
END LOOP;
IF cardinality(problems) > 0 THEN
RAISE EXCEPTION USING
MESSAGE = 'provision: the end state does not hold: ' || array_to_string(problems, '; '),
HINT = 'Nothing was committed. Fix the cause and run the provisioning again.';
END IF;
END
$dredd_provision_verify$;
COMMIT;