Slug, schema, roles (workflow_app / workflow_migrator), session cookie, token prefix workflow_pat_, WORKFLOW_* env vars (WORKFLOW_RUN_INTEGRATION, WORKFLOW_FRONTEND_DIR, WORKFLOW_PORT), UI name, ROOT_PATH examples. - Standalone port 8141: the family's ports step by 20 (8021..8121), Scribe holds 8125, 8141 is unused by every sibling compose file. - dredd-core pinned at v0.24.0. - db/managed/provision.sql re-rendered with the plan's B1 command, which adds --extension pgcrypto; CI's --check-file and provision steps and the managed runbook pass the same flag so the live path matches the file. - CREDITS: one "skeleton from Parafa" entry; pyHanko-era rows removed. - README domain section is a stub until the console and API exist (7.5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
359 lines
19 KiB
PL/PgSQL
359 lines
19 KiB
PL/PgSQL
-- =================================================================================================
|
|
-- Managed Postgres provisioning for one app of the family, in the shared database:
|
|
-- schema workflow, migrator workflow_migrator, app workflow_app, extensions: pgcrypto, migrator statement_timeout 30min
|
|
-- search_path workflow, 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 workflow --app-role workflow_app --migrator-role workflow_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 workflow --app-role workflow_app --migrator-role workflow_migrator --extension pgcrypto --identity-schema dredd --identity-owner atrium_migrator \
|
|
-- --migrator-password-env WORKFLOW_MIGRATOR_PASSWORD --app-password-env WORKFLOW_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 := 'workflow';
|
|
migrator_role text := 'workflow_migrator';
|
|
app_role text := 'workflow_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 workflow_migrator and workflow_app (plain LOGIN roles) where missing
|
|
DO $dredd_provision_roles$
|
|
DECLARE
|
|
migrator_role text := 'workflow_migrator';
|
|
app_role text := 'workflow_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 workflow_migrator to hand it a schema
|
|
GRANT workflow_migrator TO CURRENT_USER;
|
|
|
|
-- 4. schema workflow, owned by workflow_migrator
|
|
CREATE SCHEMA IF NOT EXISTS workflow AUTHORIZATION workflow_migrator;
|
|
|
|
-- 5. schema workflow: nothing for PUBLIC
|
|
REVOKE ALL ON SCHEMA workflow FROM PUBLIC;
|
|
|
|
-- 6. schema workflow: USAGE for workflow_app
|
|
GRANT USAGE ON SCHEMA workflow TO workflow_app;
|
|
|
|
-- 7. schema extensions: USAGE for workflow_migrator and workflow_app
|
|
GRANT USAGE ON SCHEMA extensions TO workflow_migrator, workflow_app;
|
|
|
|
-- 8. schema extensions: check that the USAGE grant took
|
|
DO $dredd_provision_extensions_usage$
|
|
DECLARE
|
|
migrator_role text := 'workflow_migrator';
|
|
app_role text := 'workflow_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 workflow_migrator and workflow_app
|
|
GRANT USAGE ON SCHEMA dredd TO workflow_migrator, workflow_app;
|
|
|
|
-- 11. schema dredd: check that the USAGE grant took
|
|
DO $dredd_provision_identity_usage$
|
|
DECLARE
|
|
identity_schema text := 'dredd';
|
|
migrator_role text := 'workflow_migrator';
|
|
app_role text := 'workflow_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 workflow_migrator
|
|
ALTER ROLE workflow_migrator SET search_path = workflow, dredd, extensions;
|
|
|
|
-- 13. search_path of workflow_app
|
|
ALTER ROLE workflow_app SET search_path = workflow, dredd, extensions;
|
|
|
|
-- 14. statement_timeout of workflow_migrator
|
|
ALTER ROLE workflow_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 workflow_migrator for the admin
|
|
REVOKE workflow_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 := 'workflow';
|
|
migrator_role text := 'workflow_migrator';
|
|
app_role text := 'workflow_app';
|
|
extension_names text[] := ARRAY['pgcrypto']::text[];
|
|
timeout_setting text := 'statement_timeout=30min';
|
|
path_setting text := 'search_path=workflow, 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;
|