-- lab7: separate least-privilege DB roles for the services. Passwords are NOT here: scripts/migrate.py sets them -- from operator bootstrap files and the services read them from OpenBao. The owner role `messenger` is only used -- by migrations and the operator. DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_api') THEN CREATE ROLE app_api LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT CONNECTION LIMIT 40; END IF; IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_worker') THEN CREATE ROLE app_worker LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT CONNECTION LIMIT 20; END IF; END $$; REVOKE ALL ON DATABASE messenger FROM PUBLIC; GRANT CONNECT ON DATABASE messenger TO app_api, app_worker; REVOKE ALL ON SCHEMA public FROM PUBLIC; GRANT USAGE ON SCHEMA public TO app_api, app_worker; -- API: business tables of every endpoint, the outbox relay, the reconciler and scripts/jobs.py. GRANT SELECT, INSERT, UPDATE, DELETE ON users, chats, chat_members, messages, message_attachments, sessions, idempotency_keys, attachments, attachment_variants, attachment_jobs, outbox, job_audit TO app_api; -- Worker: only the processing pipeline. No users/sessions/messages/chats, no DELETE anywhere. GRANT SELECT, UPDATE ON attachments, attachment_jobs TO app_worker; GRANT SELECT, INSERT ON attachment_variants, job_audit TO app_worker; GRANT INSERT ON outbox TO app_worker; -- retry scheduling writes a new outbox row; publishing stays in the API GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO app_api, app_worker;