fix(infra): grant audit roles to current_user, not hardcoded portal
CI / commits (pull_request) Successful in 1m10s
CI / check (pull_request) Successful in 1m15s
CI / scan (pull_request) Successful in 1m20s
CI / a11y (pull_request) Successful in 1m22s
CI / perf (pull_request) Successful in 2m59s

The bootstrap SQL ended with:

    GRANT audit_owner, audit_writer, audit_reader, audit_archiver TO portal;

which assumed POSTGRES_USER is `portal`. The compose file (and the
.env.example) document POSTGRES_USER as overridable — anyone who
changed it to something else (e.g. `apf_portal`) hit:

    ERROR: role "portal" does not exist
    psql: .../01-init.sql:48: ERROR: role "portal" does not exist

`current_user` resolves at script execution to whoever is running
the init SQL — that is, the superuser Postgres just created from
POSTGRES_USER, regardless of its name. Use it instead of hard-coding
`portal`.

Recovery for anyone hit by the original bug:

    cd infra/local
    docker compose -f dev.compose.yml down -v   # wipes the
                                                # half-initialised
                                                # postgres-data volume
    docker compose -f dev.compose.yml up -d     # bootstrap re-runs
                                                # cleanly
This commit is contained in:
Julien Gautier
2026-05-08 20:55:55 +02:00
parent d5c5c45175
commit 44deeab35d
+14 -5
View File
@@ -41,8 +41,17 @@ ALTER DEFAULT PRIVILEGES FOR ROLE audit_owner IN SCHEMA audit
GRANT DELETE ON TABLES TO audit_archiver; GRANT DELETE ON TABLES TO audit_archiver;
-- ---------------------------------------------------------------- Dev convenience -- ---------------------------------------------------------------- Dev convenience
-- The default `portal` superuser bypasses these grants anyway, but -- The dev superuser (created by Postgres from POSTGRES_USER, default
-- granting the audit roles explicitly lets us test role-based access -- `portal` but overridable in infra/local/.env) bypasses these grants
-- with `SET ROLE audit_writer;` etc. from a psql session against the -- anyway, but granting the audit roles explicitly lets us test
-- dev DB. Production never grants all four to one user. -- role-based access with `SET ROLE audit_writer;` etc. from a psql
GRANT audit_owner, audit_writer, audit_reader, audit_archiver TO portal; -- session against the dev DB.
--
-- `current_user` resolves at script execution time to whoever is
-- running the init SQL — i.e. the superuser created from POSTGRES_USER,
-- whatever its name happens to be. Hard-coding `portal` here was
-- wrong: it broke any setup where the contributor changed
-- POSTGRES_USER in their .env.
--
-- Production never grants all four roles to one user.
GRANT audit_owner, audit_writer, audit_reader, audit_archiver TO current_user;