# First-run provisioning for the lab Postgres: two databases (odoo, metabase) # and three roles. Runs once, from postgres:16's /docker-entrypoint-initdb.d, # only while PGDATA is empty. # # Why an init script instead of POSTGRES_DB/POSTGRES_USER: the stock image only # knows one database and one role, and this namespace needs # - `odoo` : owner of db `odoo`, CREATEDB (Odoo creates its own database # on first `-i base` and needs the privilege to do it) # - `metabase` : owner of db `metabase` (Metabase's own application DB) # - `metabase_ro` : the read-only BI identity used in ยง4 of the card, with # default_transaction_read_only + a statement timeout. Its # SELECT grants over Odoo's tables are applied AFTER Odoo has # installed them (the odoo Deployment's initContainer), because # at this point the `odoo` database has no tables at all. # # Passwords arrive as env vars from Secret `customer1/pg-users` (out of band); # nothing here is a credential literal. apiVersion: v1 kind: ConfigMap metadata: name: pg-init namespace: customer1 data: 01-init-dbs.sh: | #!/bin/bash set -euo pipefail : "${ODOO_DB_PASSWORD:?}" : "${METABASE_DB_PASSWORD:?}" : "${METABASE_RO_PASSWORD:?}" # Roles + databases. psql autocommits each statement, which CREATE DATABASE # requires (it cannot run inside a transaction block). psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \ -v odoo_pw="$ODOO_DB_PASSWORD" \ -v metabase_pw="$METABASE_DB_PASSWORD" \ -v ro_pw="$METABASE_RO_PASSWORD" <<'EOSQL' CREATE ROLE odoo LOGIN CREATEDB PASSWORD :'odoo_pw'; CREATE ROLE metabase LOGIN PASSWORD :'metabase_pw'; CREATE ROLE metabase_ro LOGIN PASSWORD :'ro_pw' CONNECTION LIMIT 10; -- Read-only BI identity: these are role-level defaults, so they hold for -- every session Metabase opens, including ones it recycles. ALTER ROLE metabase_ro SET default_transaction_read_only = on; ALTER ROLE metabase_ro SET statement_timeout = '60s'; ALTER ROLE metabase_ro SET idle_in_transaction_session_timeout = '60s'; CREATE DATABASE odoo OWNER odoo; CREATE DATABASE metabase OWNER metabase; -- No cross-tenant CONNECT: the BI role reaches `odoo` only, the Metabase -- application role never reaches `odoo`. (PUBLIC carries CONNECT on every -- new database, which is the usual way a "read-only" role ends up able to -- reach databases it was never granted.) REVOKE CONNECT ON DATABASE odoo FROM PUBLIC; GRANT CONNECT ON DATABASE odoo TO odoo, metabase_ro; REVOKE CONNECT ON DATABASE metabase FROM PUBLIC; GRANT CONNECT ON DATABASE metabase TO metabase; EOSQL # Schema-level setup in the (still empty) odoo database. Table grants and # the credential-table revokes are (re)applied by the odoo initContainer. psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname odoo <<'EOSQL' -- Odoo creates the `odoo` role's tables later; PUBLIC must not keep CREATE -- on the public schema (PG16 keeps it when the database was made by a -- non-superuser), or the "read-only" BI role can create tables after -- lifting its own default_transaction_read_only. REVOKE CREATE ON SCHEMA public FROM PUBLIC; GRANT USAGE ON SCHEMA public TO metabase_ro; ALTER DEFAULT PRIVILEGES FOR ROLE odoo IN SCHEMA public GRANT SELECT ON TABLES TO metabase_ro; EOSQL