From 76dff617aec83c175c96478ac2b13a966eac6294 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 12 Nov 2026 16:14:41 +0900 Subject: [PATCH 1/6] create supabase_admin_monitor role for monitoring services --- .../files/postgresql_config/supautils.conf.j2 | 4 +- .../00000000000000-initial-schema.sql | 13 +++ migrations/tests/database/privs.sql | 7 ++ nix/tests/expected/roles.out | 10 +- nix/tests/expected/z_15_roles.out | 4 +- .../expected/z_15_supabase_monitor_admin.out | 100 ++++++++++++++++ nix/tests/expected/z_17_roles.out | 8 +- .../expected/z_17_supabase_monitor_admin.out | 110 ++++++++++++++++++ nix/tests/expected/z_multigres-17_roles.out | 8 +- .../z_multigres-orioledb-17_roles.out | 10 +- nix/tests/sql/z_15_supabase_monitor_admin.sql | 61 ++++++++++ nix/tests/sql/z_17_supabase_monitor_admin.sql | 65 +++++++++++ testinfra/test_ami_nix.py | 39 +++++++ 13 files changed, 424 insertions(+), 15 deletions(-) create mode 100644 nix/tests/expected/z_15_supabase_monitor_admin.out create mode 100644 nix/tests/expected/z_17_supabase_monitor_admin.out create mode 100644 nix/tests/sql/z_15_supabase_monitor_admin.sql create mode 100644 nix/tests/sql/z_17_supabase_monitor_admin.sql diff --git a/ansible/files/postgresql_config/supautils.conf.j2 b/ansible/files/postgresql_config/supautils.conf.j2 index 1bea3f64d6..b5bf45d6c6 100644 --- a/ansible/files/postgresql_config/supautils.conf.j2 +++ b/ansible/files/postgresql_config/supautils.conf.j2 @@ -13,6 +13,6 @@ supautils.restrict_extension_versions = 'warn' supautils.privileged_extensions_superuser = 'supabase_admin' supautils.privileged_role = 'supabase_privileged_role' supautils.privileged_role_allowed_configs = 'auto_explain.*, deadlock_timeout, log_duration, log_lock_waits, log_min_duration_statement, log_min_error_statement, log_min_messages, log_parameter_max_length, log_replication_commands, log_statement, log_temp_files, pg_net.batch_size, pg_net.ttl, pg_stat_statements.*, pgaudit.log, pgaudit.log_catalog, pgaudit.log_client, pgaudit.log_level, pgaudit.log_relation, pgaudit.log_rows, pgaudit.log_statement, pgaudit.log_statement_once, pgaudit.role, pgrst.*, plan_filter.*, safeupdate.enabled, session_replication_role, track_functions, track_io_timing, wal_compression' -supautils.reserved_memberships = 'pg_read_server_files, pg_write_server_files, pg_execute_server_program, supabase_admin, supabase_auth_admin, supabase_storage_admin, supabase_read_only_user, supabase_realtime_admin, supabase_replication_admin, supabase_etl_admin, dashboard_user, pgbouncer, authenticator, pg_write_all_data' -supautils.reserved_roles = 'supabase_admin, supabase_auth_admin, supabase_storage_admin, supabase_read_only_user, supabase_realtime_admin, supabase_replication_admin, supabase_etl_admin, dashboard_user, pgbouncer, service_role*, authenticator*, authenticated*, anon*, supabase_privileged_role' +supautils.reserved_memberships = 'pg_read_server_files, pg_write_server_files, pg_execute_server_program, supabase_admin, supabase_auth_admin, supabase_storage_admin, supabase_read_only_user, supabase_realtime_admin, supabase_replication_admin, supabase_etl_admin, supabase_monitor_admin, dashboard_user, pgbouncer, authenticator, pg_write_all_data' +supautils.reserved_roles = 'supabase_admin, supabase_auth_admin, supabase_storage_admin, supabase_read_only_user, supabase_realtime_admin, supabase_replication_admin, supabase_etl_admin, supabase_monitor_admin, dashboard_user, pgbouncer, service_role*, authenticator*, authenticated*, anon*, supabase_privileged_role' supautils.hint_roles = 'anon, authenticated, service_role' diff --git a/migrations/db/init-scripts/00000000000000-initial-schema.sql b/migrations/db/init-scripts/00000000000000-initial-schema.sql index b32db6b939..25d22ee610 100644 --- a/migrations/db/init-scripts/00000000000000-initial-schema.sql +++ b/migrations/db/init-scripts/00000000000000-initial-schema.sql @@ -19,6 +19,19 @@ grant create on database postgres to supabase_etl_admin; create role supabase_read_only_user with login bypassrls; grant pg_read_all_data to supabase_read_only_user; +-- Supabase monitoring user: no password and can only connect via localhost, +-- reads every table +-- bypassing RLS, read-only, owns nothing. +-- so the name can never be pre-empted: a plain CREATE fails loudly if it exists. +create role supabase_monitor_admin with login bypassrls connection limit 2; +grant pg_read_all_data, pg_monitor to supabase_monitor_admin; +alter role supabase_monitor_admin set default_transaction_read_only = on; +alter role supabase_monitor_admin set transaction_read_only = on; +alter role supabase_monitor_admin set search_path = ''; +alter role supabase_monitor_admin set statement_timeout = '3s'; +alter role supabase_monitor_admin set lock_timeout = '3s'; +alter role supabase_monitor_admin set idle_in_transaction_session_timeout = '3s'; + -- Extension namespacing create schema if not exists extensions; create extension if not exists "uuid-ossp" with schema extensions; diff --git a/migrations/tests/database/privs.sql b/migrations/tests/database/privs.sql index ea4f1318ab..e7d44ce5ee 100644 --- a/migrations/tests/database/privs.sql +++ b/migrations/tests/database/privs.sql @@ -27,3 +27,10 @@ SELECT schema_privs_are('extensions', 'service_role', array['USAGE']); -- Role memberships SELECT is_member_of('pg_read_all_data', 'postgres'); SELECT is_member_of('pg_signal_backend', 'postgres'); + +-- read-only monitoring role. +SELECT isnt_superuser('supabase_monitor_admin'); +SELECT is_member_of('pg_read_all_data', 'supabase_monitor_admin'); +SELECT is_member_of('pg_monitor', 'supabase_monitor_admin'); +SELECT database_privs_are(current_database(), 'supabase_monitor_admin', ARRAY['CONNECT', 'TEMPORARY']); +SELECT schema_privs_are('public', 'supabase_monitor_admin', array['USAGE']); diff --git a/nix/tests/expected/roles.out b/nix/tests/expected/roles.out index 78a60dc24d..4bd75cbc4d 100644 --- a/nix/tests/expected/roles.out +++ b/nix/tests/expected/roles.out @@ -48,11 +48,12 @@ order by rolname; supabase_auth_admin | t | t | f | f | f | f | -1 | f | supabase_etl_admin | f | t | f | t | f | t | -1 | t | supabase_functions_admin | t | t | f | f | f | f | -1 | f | + supabase_monitor_admin | f | t | f | t | f | f | 2 | t | supabase_privileged_role | f | f | f | t | f | f | -1 | f | supabase_read_only_user | f | t | f | t | f | f | -1 | t | supabase_replication_admin | f | t | f | t | f | t | -1 | f | supabase_storage_admin | t | t | f | f | f | f | -1 | f | -(31 rows) +(32 rows) select rolname, @@ -60,8 +61,8 @@ select from pg_roles r where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections') order by rolname; - rolname | rolconfig -----------------------------+------------------------------------------------------------------------------------------ + rolname | rolconfig +----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------ anon | {statement_timeout=3s} authenticated | {statement_timeout=8s} authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s} @@ -89,11 +90,12 @@ order by rolname; supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none} supabase_etl_admin | supabase_functions_admin | + supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=\"\"",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} supabase_privileged_role | supabase_read_only_user | {default_transaction_read_only=on} supabase_replication_admin | supabase_storage_admin | {search_path=storage,log_statement=none} -(31 rows) +(32 rows) -- Check all privileges of the roles on the schemas select schema_name, privilege_type, grantee, default_for diff --git a/nix/tests/expected/z_15_roles.out b/nix/tests/expected/z_15_roles.out index c7aaabf361..216b50a787 100644 --- a/nix/tests/expected/z_15_roles.out +++ b/nix/tests/expected/z_15_roles.out @@ -33,10 +33,12 @@ order by supabase_etl_admin | pg_monitor | f supabase_etl_admin | pg_read_all_data | f supabase_etl_admin | supabase_privileged_role | f + supabase_monitor_admin | pg_monitor | f + supabase_monitor_admin | pg_read_all_data | f supabase_read_only_user | pg_monitor | f supabase_read_only_user | pg_read_all_data | f supabase_storage_admin | authenticator | f -(23 rows) +(25 rows) -- Check all privileges of non-superuser roles on functions select diff --git a/nix/tests/expected/z_15_supabase_monitor_admin.out b/nix/tests/expected/z_15_supabase_monitor_admin.out new file mode 100644 index 0000000000..4224791350 --- /dev/null +++ b/nix/tests/expected/z_15_supabase_monitor_admin.out @@ -0,0 +1,100 @@ +-- supabase_monitor_admin: passwordless, read-only +-- monitoring role (quick_explainer.md, +-- migrations/db/init-scripts/00000000000000-initial-schema.sql). +-- attributes: login, no password, bypassrls, nothing else +select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, + rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password +from pg_authid where rolname = 'supabase_monitor_admin'; + rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolbypassrls | rolconnlimit | no_password +----------+------------+---------------+-------------+-------------+----------------+--------------+--------------+------------- + f | t | f | f | t | f | t | 2 | t +(1 row) + +-- per-role settings (read-only by default, empty search_path, timeouts) +select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; + setting +---------------------------------------- + default_transaction_read_only=on + idle_in_transaction_session_timeout=3s + lock_timeout=3s + search_path="" + statement_timeout=3s + transaction_read_only=on +(6 rows) + +-- memberships: exactly pg_monitor and pg_read_all_data +select roleid::regrole::text as role, admin_option +from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1; + role | admin_option +------------------+-------------- + pg_monitor | f + pg_read_all_data | f +(2 rows) + +-- nobody is a member of it +select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; + count +------- + 0 +(1 row) + +-- owns nothing and holds no object-level ACL entries anywhere +select count(*) from pg_shdepend +where refclassid = 'pg_authid'::regclass + and refobjid = 'supabase_monitor_admin'::regrole + and deptype in ('o', 'a'); + count +------- + 0 +(1 row) + +-- no CREATE on the database +select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); + has_database_privilege +------------------------ + f +(1 row) + +-- reads every table regardless of RLS, but cannot write. NOTE: this uses +-- SET SESSION AUTHORIZATION, which a future supautils impersonation guard +-- would block; switch to a probe role with identical grants when that lands +-- (see quick_explainer.md). +create table public.monitor_probe (id int); +insert into public.monitor_probe values (1), (2); +alter table public.monitor_probe enable row level security; +alter table public.monitor_probe force row level security; +alter table public.monitor_probe owner to postgres; +set session authorization supabase_monitor_admin; +select count(*) from public.monitor_probe; + count +------- + 2 +(1 row) + +insert into public.monitor_probe values (3); +ERROR: permission denied for table monitor_probe +reset session authorization; +drop table public.monitor_probe; +-- reserved role (supautils.conf.j2): the non-superuser postgres role cannot +-- grant it, modify it, grant to it, or drop it. Same pattern as +-- supautils_reserved_roles.sql. +begin; +set role postgres; +savepoint s; +grant supabase_monitor_admin to postgres; +ERROR: "supabase_monitor_admin" role memberships are reserved, only superusers can grant them +rollback to savepoint s; +alter role supabase_monitor_admin password 'x'; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +alter role supabase_monitor_admin set search_path = public; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +grant authenticated to supabase_monitor_admin; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +drop role supabase_monitor_admin; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +reset role; +rollback; diff --git a/nix/tests/expected/z_17_roles.out b/nix/tests/expected/z_17_roles.out index b966f8f9d5..f6c418dc69 100644 --- a/nix/tests/expected/z_17_roles.out +++ b/nix/tests/expected/z_17_roles.out @@ -70,10 +70,12 @@ order by supabase_etl_admin | pg_monitor | f supabase_etl_admin | pg_read_all_data | f supabase_etl_admin | supabase_privileged_role | f + supabase_monitor_admin | pg_monitor | f + supabase_monitor_admin | pg_read_all_data | f supabase_read_only_user | pg_monitor | f supabase_read_only_user | pg_read_all_data | f supabase_storage_admin | authenticator | f -(25 rows) +(27 rows) -- Check version-specific privileges of the roles on the schemas select schema_name, privilege_type, grantee, default_for @@ -166,10 +168,12 @@ order by supabase_etl_admin | pg_monitor | f supabase_etl_admin | pg_read_all_data | f supabase_etl_admin | supabase_privileged_role | f + supabase_monitor_admin | pg_monitor | f + supabase_monitor_admin | pg_read_all_data | f supabase_read_only_user | pg_monitor | f supabase_read_only_user | pg_read_all_data | f supabase_storage_admin | authenticator | f -(24 rows) +(26 rows) -- Check all privileges of non-superuser roles on functions select diff --git a/nix/tests/expected/z_17_supabase_monitor_admin.out b/nix/tests/expected/z_17_supabase_monitor_admin.out new file mode 100644 index 0000000000..9e7212e17d --- /dev/null +++ b/nix/tests/expected/z_17_supabase_monitor_admin.out @@ -0,0 +1,110 @@ +-- supabase_monitor_admin: passwordless, read-only +-- monitoring role (quick_explainer.md, +-- migrations/db/init-scripts/00000000000000-initial-schema.sql) +-- attributes: login, no password, bypassrls, nothing else +-- only accessible via localhost +select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, + rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password +from pg_authid where rolname = 'supabase_monitor_admin'; + rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolbypassrls | rolconnlimit | no_password +----------+------------+---------------+-------------+-------------+----------------+--------------+--------------+------------- + f | t | f | f | t | f | t | 2 | t +(1 row) + +-- per-role settings (read-only by default, empty search_path, timeouts) +select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; + setting +---------------------------------------- + default_transaction_read_only=on + idle_in_transaction_session_timeout=3s + lock_timeout=3s + search_path="" + statement_timeout=3s + transaction_read_only=on +(6 rows) + +-- memberships: exactly pg_monitor and pg_read_all_data (inherited; the role is +-- a plain INHERIT role). SET ROLE to them only drops privileges, checked below. +select roleid::regrole::text as role, admin_option, inherit_option, set_option +from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1; + role | admin_option | inherit_option | set_option +------------------+--------------+----------------+------------ + pg_monitor | f | t | t + pg_read_all_data | f | t | t +(2 rows) + +-- nobody is a member of it +select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; + count +------- + 0 +(1 row) + +-- owns nothing and holds no object-level ACL entries anywhere +-- keep in mind, the postgres role can assign it access to objects at a later point, but that's fine. +select count(*) from pg_shdepend +where refclassid = 'pg_authid'::regclass + and refobjid = 'supabase_monitor_admin'::regrole + and deptype in ('o', 'a'); + count +------- + 0 +(1 row) + +-- no CREATE on the database +select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); + has_database_privilege +------------------------ + f +(1 row) + +-- reads every table regardless of RLS, but cannot write; SET ROLE to a +-- predefined role only loses privileges (BYPASSRLS stays with supabase_monitor_admin, +-- so the RLS-protected rows disappear). NOTE: this uses SET SESSION +-- AUTHORIZATION, which supautils impersonation may block +create table public.monitor_probe (id int); +insert into public.monitor_probe values (1), (2); +alter table public.monitor_probe enable row level security; +alter table public.monitor_probe force row level security; +alter table public.monitor_probe owner to postgres; +set session authorization supabase_monitor_admin; +select count(*) from public.monitor_probe; + count +------- + 2 +(1 row) + +insert into public.monitor_probe values (3); +ERROR: permission denied for table monitor_probe +set role pg_read_all_data; +select count(*) from public.monitor_probe; + count +------- + 0 +(1 row) + +reset session authorization; +drop table public.monitor_probe; +-- reserved role (supautils.conf.j2): the non-superuser postgres role cannot +-- grant it, modify it, grant to it, or drop it. Same pattern as +-- supautils_reserved_roles.sql. +begin; +set role postgres; +savepoint s; +grant supabase_monitor_admin to postgres; +ERROR: "supabase_monitor_admin" role memberships are reserved, only superusers can grant them +rollback to savepoint s; +alter role supabase_monitor_admin password 'x'; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +alter role supabase_monitor_admin set search_path = public; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +grant authenticated to supabase_monitor_admin; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +drop role supabase_monitor_admin; +ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it +rollback to savepoint s; +reset role; +rollback; diff --git a/nix/tests/expected/z_multigres-17_roles.out b/nix/tests/expected/z_multigres-17_roles.out index 7e88404eac..1319908966 100644 --- a/nix/tests/expected/z_multigres-17_roles.out +++ b/nix/tests/expected/z_multigres-17_roles.out @@ -67,10 +67,12 @@ order by supabase_etl_admin | pg_monitor | f supabase_etl_admin | pg_read_all_data | f supabase_etl_admin | supabase_privileged_role | f + supabase_monitor_admin | pg_monitor | f + supabase_monitor_admin | pg_read_all_data | f supabase_read_only_user | pg_monitor | f supabase_read_only_user | pg_read_all_data | f supabase_storage_admin | authenticator | f -(22 rows) +(24 rows) -- Check version-specific privileges of the roles on the schemas select schema_name, privilege_type, grantee, default_for @@ -158,10 +160,12 @@ order by supabase_etl_admin | pg_monitor | f supabase_etl_admin | pg_read_all_data | f supabase_etl_admin | supabase_privileged_role | f + supabase_monitor_admin | pg_monitor | f + supabase_monitor_admin | pg_read_all_data | f supabase_read_only_user | pg_monitor | f supabase_read_only_user | pg_read_all_data | f supabase_storage_admin | authenticator | f -(21 rows) +(23 rows) -- Check all privileges of non-superuser roles on functions select diff --git a/nix/tests/expected/z_multigres-orioledb-17_roles.out b/nix/tests/expected/z_multigres-orioledb-17_roles.out index 9e530eee17..1593ae606c 100644 --- a/nix/tests/expected/z_multigres-orioledb-17_roles.out +++ b/nix/tests/expected/z_multigres-orioledb-17_roles.out @@ -45,11 +45,12 @@ order by rolname; supabase_auth_admin | t | t | f | f | f | f | -1 | f | supabase_etl_admin | f | t | f | t | f | t | -1 | t | supabase_functions_admin | t | t | f | f | f | f | -1 | f | + supabase_monitor_admin | f | t | f | t | f | f | 2 | t | supabase_privileged_role | f | f | f | t | f | f | -1 | f | supabase_read_only_user | f | t | f | t | f | f | -1 | t | supabase_replication_admin | f | t | f | t | f | t | -1 | f | supabase_storage_admin | t | t | f | f | f | f | -1 | f | -(28 rows) +(29 rows) select rolname, @@ -57,8 +58,8 @@ select from pg_roles r where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections') order by rolname; - rolname | rolconfig -----------------------------+------------------------------------------------------------------------------------------ + rolname | rolconfig +----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------ anon | {statement_timeout=3s} authenticated | {statement_timeout=8s} authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s} @@ -83,11 +84,12 @@ order by rolname; supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none} supabase_etl_admin | supabase_functions_admin | + supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=\"\"",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} supabase_privileged_role | supabase_read_only_user | {default_transaction_read_only=on} supabase_replication_admin | supabase_storage_admin | {search_path=storage,log_statement=none} -(28 rows) +(29 rows) -- Check all privileges of the roles on the schemas select schema_name, privilege_type, grantee, default_for diff --git a/nix/tests/sql/z_15_supabase_monitor_admin.sql b/nix/tests/sql/z_15_supabase_monitor_admin.sql new file mode 100644 index 0000000000..473432c0f0 --- /dev/null +++ b/nix/tests/sql/z_15_supabase_monitor_admin.sql @@ -0,0 +1,61 @@ +-- supabase_monitor_admin: passwordless, read-only +-- monitoring role ( +-- migrations/db/init-scripts/00000000000000-initial-schema.sql). + +-- attributes: login, no password, bypassrls, nothing else +select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, + rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password +from pg_authid where rolname = 'supabase_monitor_admin'; + +-- per-role settings (read-only by default, empty search_path, timeouts) +select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; + +-- memberships: exactly pg_monitor and pg_read_all_data +select roleid::regrole::text as role, admin_option +from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1; + +-- nobody is a member of it +select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; + +-- owns nothing and holds no object-level ACL entries anywhere +select count(*) from pg_shdepend +where refclassid = 'pg_authid'::regclass + and refobjid = 'supabase_monitor_admin'::regrole + and deptype in ('o', 'a'); + +-- no CREATE on the database +select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); + +-- reads every table regardless of RLS, but cannot write. NOTE: this uses +-- SET SESSION AUTHORIZATION, which a future supautils impersonation guard +-- would block; switch to a probe role with identical grants when that lands +-- (see quick_explainer.md). +create table public.monitor_probe (id int); +insert into public.monitor_probe values (1), (2); +alter table public.monitor_probe enable row level security; +alter table public.monitor_probe force row level security; +alter table public.monitor_probe owner to postgres; +set session authorization supabase_monitor_admin; +select count(*) from public.monitor_probe; +insert into public.monitor_probe values (3); +reset session authorization; +drop table public.monitor_probe; + +-- reserved role (supautils.conf.j2): the non-superuser postgres role cannot +-- grant it, modify it, grant to it, or drop it. Same pattern as +-- supautils_reserved_roles.sql. +begin; +set role postgres; +savepoint s; +grant supabase_monitor_admin to postgres; +rollback to savepoint s; +alter role supabase_monitor_admin password 'x'; +rollback to savepoint s; +alter role supabase_monitor_admin set search_path = public; +rollback to savepoint s; +grant authenticated to supabase_monitor_admin; +rollback to savepoint s; +drop role supabase_monitor_admin; +rollback to savepoint s; +reset role; +rollback; diff --git a/nix/tests/sql/z_17_supabase_monitor_admin.sql b/nix/tests/sql/z_17_supabase_monitor_admin.sql new file mode 100644 index 0000000000..506cfdfef9 --- /dev/null +++ b/nix/tests/sql/z_17_supabase_monitor_admin.sql @@ -0,0 +1,65 @@ +-- supabase_monitor_admin: passwordless, read-only, for monitoring from localhost +-- migrations/db/init-scripts/00000000000000-initial-schema.sql) + +-- attributes: login, no password, bypassrls, nothing else +-- only accessible via localhost +select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, + rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password +from pg_authid where rolname = 'supabase_monitor_admin'; + +-- per-role settings (read-only by default, empty search_path, timeouts) +select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; + +-- memberships: exactly pg_monitor and pg_read_all_data (inherited; the role is +-- a plain INHERIT role). SET ROLE to them only drops privileges, checked below. +select roleid::regrole::text as role, admin_option, inherit_option, set_option +from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1; + +-- nobody is a member of it +select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; + +-- owns nothing and holds no object-level ACL entries anywhere +-- keep in mind, the postgres role can assign it access to objects at a later point, but that's fine. +select count(*) from pg_shdepend +where refclassid = 'pg_authid'::regclass + and refobjid = 'supabase_monitor_admin'::regrole + and deptype in ('o', 'a'); + +-- no CREATE on the database +select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); + +-- reads every table regardless of RLS, but cannot write; SET ROLE to a +-- predefined role only loses privileges (BYPASSRLS stays with supabase_monitor_admin, +-- so the RLS-protected rows disappear). NOTE: this uses SET SESSION +-- AUTHORIZATION, which supautils impersonation may block +create table public.monitor_probe (id int); +insert into public.monitor_probe values (1), (2); +alter table public.monitor_probe enable row level security; +alter table public.monitor_probe force row level security; +alter table public.monitor_probe owner to postgres; +set session authorization supabase_monitor_admin; +select count(*) from public.monitor_probe; +insert into public.monitor_probe values (3); +set role pg_read_all_data; +select count(*) from public.monitor_probe; +reset session authorization; +drop table public.monitor_probe; + +-- reserved role (supautils.conf.j2): the non-superuser postgres role cannot +-- grant it, modify it, grant to it, or drop it. Same pattern as +-- supautils_reserved_roles.sql. +begin; +set role postgres; +savepoint s; +grant supabase_monitor_admin to postgres; +rollback to savepoint s; +alter role supabase_monitor_admin password 'x'; +rollback to savepoint s; +alter role supabase_monitor_admin set search_path = public; +rollback to savepoint s; +grant authenticated to supabase_monitor_admin; +rollback to savepoint s; +drop role supabase_monitor_admin; +rollback to savepoint s; +reset role; +rollback; diff --git a/testinfra/test_ami_nix.py b/testinfra/test_ami_nix.py index b5acf94c58..879e4c4c99 100644 --- a/testinfra/test_ami_nix.py +++ b/testinfra/test_ami_nix.py @@ -1157,6 +1157,45 @@ def test_postgrest_read_only_session_attrs(host): print("Warning: Failed to restore PostgreSQL configuration") +def test_supabase_monitor_admin_role_is_passwordless_and_reachable_on_loopback(host): + """supabase_monitor_admin (migrations/db/init-scripts/00000000000000-initial-schema.sql) + has no password and relies on the same pg_hba trust rules. + + Unlike the other service roles, this role must never be given a password in + nix/tools/postgresql_schema.sql or the fixture at the top of this file. + It should only be accessible from localhost. + """ + ssh = host["ssh"] + psql_admin = "sudo -u postgres psql -U supabase_admin -h localhost -d postgres -At -c " + + result = run_ssh_command( + ssh, + psql_admin + + "\"select rolcanlogin, rolpassword is null, rolbypassrls, rolsuper, " + "rolcreaterole, rolcreatedb, rolreplication " + "from pg_authid where rolname = 'supabase_monitor_admin'\"", + ) + assert result["succeeded"], result["stderr"] + assert result["stdout"].strip() == "t|t|t|f|f|f|f", ( + f"unexpected supabase_monitor_admin attributes: {result['stdout']!r}" + ) + + # Passwordless loopback login works, and the per-role read-only default + # applies to the resulting session. + result = run_ssh_command( + ssh, + "sudo -u postgres psql -U supabase_monitor_admin -h 127.0.0.1 -d postgres -At " + "-c 'select current_user' -c 'show default_transaction_read_only' 2>&1", + ) + assert result["succeeded"], ( + "supabase_monitor_admin could not log in over loopback without a password: " + f"{result['stdout']!r} {result['stderr']!r}" + ) + assert result["stdout"].split() == ["supabase_monitor_admin", "on"], ( + f"unexpected session state for supabase_monitor_admin: {result['stdout']!r}" + ) + + def test_custom_overrides_take_precedence_over_generated_optimizations(host): """Verify CLI-managed PostgreSQL config overrides generated optimizations. From 5df50d6ab4f649d4090cd0a06df2a73ec3706826 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 12 Nov 2026 19:18:42 +0900 Subject: [PATCH 2/6] fix: sync monitor role test output and apply formatter --- nix/tests/expected/z_15_supabase_monitor_admin.out | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix/tests/expected/z_15_supabase_monitor_admin.out b/nix/tests/expected/z_15_supabase_monitor_admin.out index 4224791350..b6fbfdb785 100644 --- a/nix/tests/expected/z_15_supabase_monitor_admin.out +++ b/nix/tests/expected/z_15_supabase_monitor_admin.out @@ -1,5 +1,5 @@ -- supabase_monitor_admin: passwordless, read-only --- monitoring role (quick_explainer.md, +-- monitoring role ( -- migrations/db/init-scripts/00000000000000-initial-schema.sql). -- attributes: login, no password, bypassrls, nothing else select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, From 5722d944dee1e94dba6f420b74ed7efae9b0ca00 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 12 Nov 2026 19:44:42 +0900 Subject: [PATCH 3/6] fix: sync PG17 monitor role test output and apply formatter --- nix/tests/expected/z_17_supabase_monitor_admin.out | 3 +-- testinfra/test_ami_nix.py | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/nix/tests/expected/z_17_supabase_monitor_admin.out b/nix/tests/expected/z_17_supabase_monitor_admin.out index 9e7212e17d..c9c124091d 100644 --- a/nix/tests/expected/z_17_supabase_monitor_admin.out +++ b/nix/tests/expected/z_17_supabase_monitor_admin.out @@ -1,5 +1,4 @@ --- supabase_monitor_admin: passwordless, read-only --- monitoring role (quick_explainer.md, +-- supabase_monitor_admin: passwordless, read-only, for monitoring from localhost -- migrations/db/init-scripts/00000000000000-initial-schema.sql) -- attributes: login, no password, bypassrls, nothing else -- only accessible via localhost diff --git a/testinfra/test_ami_nix.py b/testinfra/test_ami_nix.py index 879e4c4c99..ea67dd355e 100644 --- a/testinfra/test_ami_nix.py +++ b/testinfra/test_ami_nix.py @@ -1162,16 +1162,18 @@ def test_supabase_monitor_admin_role_is_passwordless_and_reachable_on_loopback(h has no password and relies on the same pg_hba trust rules. Unlike the other service roles, this role must never be given a password in - nix/tools/postgresql_schema.sql or the fixture at the top of this file. + nix/tools/postgresql_schema.sql or the fixture at the top of this file. It should only be accessible from localhost. """ ssh = host["ssh"] - psql_admin = "sudo -u postgres psql -U supabase_admin -h localhost -d postgres -At -c " + psql_admin = ( + "sudo -u postgres psql -U supabase_admin -h localhost -d postgres -At -c " + ) result = run_ssh_command( ssh, psql_admin - + "\"select rolcanlogin, rolpassword is null, rolbypassrls, rolsuper, " + + '"select rolcanlogin, rolpassword is null, rolbypassrls, rolsuper, ' "rolcreaterole, rolcreatedb, rolreplication " "from pg_authid where rolname = 'supabase_monitor_admin'\"", ) From 67032180da3da352d45b8e431ef9c5b6c06c19e0 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 13 Nov 2026 13:53:25 +0900 Subject: [PATCH 4/6] feat: create supabase_monitor_admin in a migration, single pg_regress test, no connection limit --- .../00000000000000-initial-schema.sql | 13 --- .../20261113000000_supabase_monitor_admin.sql | 19 +++ nix/tests/expected/roles.out | 8 +- ...r_admin.out => supabase_monitor_admin.out} | 27 +++-- .../expected/z_17_supabase_monitor_admin.out | 109 ------------------ .../z_multigres-orioledb-17_roles.out | 8 +- ...r_admin.sql => supabase_monitor_admin.sql} | 18 +-- nix/tests/sql/z_17_supabase_monitor_admin.sql | 65 ----------- testinfra/test_ami_nix.py | 2 +- 9 files changed, 53 insertions(+), 216 deletions(-) create mode 100644 migrations/db/migrations/20261113000000_supabase_monitor_admin.sql rename nix/tests/expected/{z_15_supabase_monitor_admin.out => supabase_monitor_admin.out} (83%) delete mode 100644 nix/tests/expected/z_17_supabase_monitor_admin.out rename nix/tests/sql/{z_15_supabase_monitor_admin.sql => supabase_monitor_admin.sql} (77%) delete mode 100644 nix/tests/sql/z_17_supabase_monitor_admin.sql diff --git a/migrations/db/init-scripts/00000000000000-initial-schema.sql b/migrations/db/init-scripts/00000000000000-initial-schema.sql index 25d22ee610..b32db6b939 100644 --- a/migrations/db/init-scripts/00000000000000-initial-schema.sql +++ b/migrations/db/init-scripts/00000000000000-initial-schema.sql @@ -19,19 +19,6 @@ grant create on database postgres to supabase_etl_admin; create role supabase_read_only_user with login bypassrls; grant pg_read_all_data to supabase_read_only_user; --- Supabase monitoring user: no password and can only connect via localhost, --- reads every table --- bypassing RLS, read-only, owns nothing. --- so the name can never be pre-empted: a plain CREATE fails loudly if it exists. -create role supabase_monitor_admin with login bypassrls connection limit 2; -grant pg_read_all_data, pg_monitor to supabase_monitor_admin; -alter role supabase_monitor_admin set default_transaction_read_only = on; -alter role supabase_monitor_admin set transaction_read_only = on; -alter role supabase_monitor_admin set search_path = ''; -alter role supabase_monitor_admin set statement_timeout = '3s'; -alter role supabase_monitor_admin set lock_timeout = '3s'; -alter role supabase_monitor_admin set idle_in_transaction_session_timeout = '3s'; - -- Extension namespacing create schema if not exists extensions; create extension if not exists "uuid-ossp" with schema extensions; diff --git a/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql b/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql new file mode 100644 index 0000000000..cd0857c375 --- /dev/null +++ b/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql @@ -0,0 +1,19 @@ +-- migrate:up + +-- Supabase monitoring user: no password and can only connect via localhost, +-- reads every table bypassing RLS, read-only, owns nothing. +do $$ +begin + if not exists (select from pg_roles where rolname = 'supabase_monitor_admin') then + create role supabase_monitor_admin with login bypassrls; + grant pg_read_all_data, pg_monitor to supabase_monitor_admin; + alter role supabase_monitor_admin set default_transaction_read_only = on; + alter role supabase_monitor_admin set transaction_read_only = on; + alter role supabase_monitor_admin set search_path = pg_catalog, pg_temp; + alter role supabase_monitor_admin set statement_timeout = '3s'; + alter role supabase_monitor_admin set lock_timeout = '3s'; + alter role supabase_monitor_admin set idle_in_transaction_session_timeout = '3s'; + end if; +end $$; + +-- migrate:down diff --git a/nix/tests/expected/roles.out b/nix/tests/expected/roles.out index 4bd75cbc4d..330a0a0e95 100644 --- a/nix/tests/expected/roles.out +++ b/nix/tests/expected/roles.out @@ -48,7 +48,7 @@ order by rolname; supabase_auth_admin | t | t | f | f | f | f | -1 | f | supabase_etl_admin | f | t | f | t | f | t | -1 | t | supabase_functions_admin | t | t | f | f | f | f | -1 | f | - supabase_monitor_admin | f | t | f | t | f | f | 2 | t | + supabase_monitor_admin | f | t | f | t | f | f | -1 | t | supabase_privileged_role | f | f | f | t | f | f | -1 | f | supabase_read_only_user | f | t | f | t | f | f | -1 | t | supabase_replication_admin | f | t | f | t | f | t | -1 | f | @@ -61,8 +61,8 @@ select from pg_roles r where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections') order by rolname; - rolname | rolconfig -----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------ + rolname | rolconfig +----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- anon | {statement_timeout=3s} authenticated | {statement_timeout=8s} authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s} @@ -90,7 +90,7 @@ order by rolname; supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none} supabase_etl_admin | supabase_functions_admin | - supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=\"\"",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} + supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=pg_catalog, pg_temp",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} supabase_privileged_role | supabase_read_only_user | {default_transaction_read_only=on} supabase_replication_admin | diff --git a/nix/tests/expected/z_15_supabase_monitor_admin.out b/nix/tests/expected/supabase_monitor_admin.out similarity index 83% rename from nix/tests/expected/z_15_supabase_monitor_admin.out rename to nix/tests/expected/supabase_monitor_admin.out index b6fbfdb785..de349a7d8a 100644 --- a/nix/tests/expected/z_15_supabase_monitor_admin.out +++ b/nix/tests/expected/supabase_monitor_admin.out @@ -1,23 +1,22 @@ --- supabase_monitor_admin: passwordless, read-only --- monitoring role ( --- migrations/db/init-scripts/00000000000000-initial-schema.sql). --- attributes: login, no password, bypassrls, nothing else +-- supabase_monitor_admin: passwordless, read-only monitoring role +-- (migrations/db/migrations/20261113000000_supabase_monitor_admin.sql) +-- attributes: login, no password, bypassrls, no connection limit, nothing else select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password from pg_authid where rolname = 'supabase_monitor_admin'; rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolbypassrls | rolconnlimit | no_password ----------+------------+---------------+-------------+-------------+----------------+--------------+--------------+------------- - f | t | f | f | t | f | t | 2 | t + f | t | f | f | t | f | t | -1 | t (1 row) --- per-role settings (read-only by default, empty search_path, timeouts) +-- per-role settings (read-only by default, search_path pg_catalog + pg_temp, timeouts) select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; setting ---------------------------------------- default_transaction_read_only=on idle_in_transaction_session_timeout=3s lock_timeout=3s - search_path="" + search_path=pg_catalog, pg_temp statement_timeout=3s transaction_read_only=on (6 rows) @@ -55,10 +54,9 @@ select has_database_privilege('supabase_monitor_admin', current_database(), 'CRE f (1 row) --- reads every table regardless of RLS, but cannot write. NOTE: this uses --- SET SESSION AUTHORIZATION, which a future supautils impersonation guard --- would block; switch to a probe role with identical grants when that lands --- (see quick_explainer.md). +-- reads every table regardless of RLS, but cannot write; SET ROLE to a +-- predefined role only loses privileges (BYPASSRLS stays with +-- supabase_monitor_admin) create table public.monitor_probe (id int); insert into public.monitor_probe values (1), (2); alter table public.monitor_probe enable row level security; @@ -73,6 +71,13 @@ select count(*) from public.monitor_probe; insert into public.monitor_probe values (3); ERROR: permission denied for table monitor_probe +set role pg_read_all_data; +select count(*) from public.monitor_probe; + count +------- + 0 +(1 row) + reset session authorization; drop table public.monitor_probe; -- reserved role (supautils.conf.j2): the non-superuser postgres role cannot diff --git a/nix/tests/expected/z_17_supabase_monitor_admin.out b/nix/tests/expected/z_17_supabase_monitor_admin.out deleted file mode 100644 index c9c124091d..0000000000 --- a/nix/tests/expected/z_17_supabase_monitor_admin.out +++ /dev/null @@ -1,109 +0,0 @@ --- supabase_monitor_admin: passwordless, read-only, for monitoring from localhost --- migrations/db/init-scripts/00000000000000-initial-schema.sql) --- attributes: login, no password, bypassrls, nothing else --- only accessible via localhost -select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, - rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password -from pg_authid where rolname = 'supabase_monitor_admin'; - rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolbypassrls | rolconnlimit | no_password -----------+------------+---------------+-------------+-------------+----------------+--------------+--------------+------------- - f | t | f | f | t | f | t | 2 | t -(1 row) - --- per-role settings (read-only by default, empty search_path, timeouts) -select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; - setting ----------------------------------------- - default_transaction_read_only=on - idle_in_transaction_session_timeout=3s - lock_timeout=3s - search_path="" - statement_timeout=3s - transaction_read_only=on -(6 rows) - --- memberships: exactly pg_monitor and pg_read_all_data (inherited; the role is --- a plain INHERIT role). SET ROLE to them only drops privileges, checked below. -select roleid::regrole::text as role, admin_option, inherit_option, set_option -from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1; - role | admin_option | inherit_option | set_option -------------------+--------------+----------------+------------ - pg_monitor | f | t | t - pg_read_all_data | f | t | t -(2 rows) - --- nobody is a member of it -select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; - count -------- - 0 -(1 row) - --- owns nothing and holds no object-level ACL entries anywhere --- keep in mind, the postgres role can assign it access to objects at a later point, but that's fine. -select count(*) from pg_shdepend -where refclassid = 'pg_authid'::regclass - and refobjid = 'supabase_monitor_admin'::regrole - and deptype in ('o', 'a'); - count -------- - 0 -(1 row) - --- no CREATE on the database -select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); - has_database_privilege ------------------------- - f -(1 row) - --- reads every table regardless of RLS, but cannot write; SET ROLE to a --- predefined role only loses privileges (BYPASSRLS stays with supabase_monitor_admin, --- so the RLS-protected rows disappear). NOTE: this uses SET SESSION --- AUTHORIZATION, which supautils impersonation may block -create table public.monitor_probe (id int); -insert into public.monitor_probe values (1), (2); -alter table public.monitor_probe enable row level security; -alter table public.monitor_probe force row level security; -alter table public.monitor_probe owner to postgres; -set session authorization supabase_monitor_admin; -select count(*) from public.monitor_probe; - count -------- - 2 -(1 row) - -insert into public.monitor_probe values (3); -ERROR: permission denied for table monitor_probe -set role pg_read_all_data; -select count(*) from public.monitor_probe; - count -------- - 0 -(1 row) - -reset session authorization; -drop table public.monitor_probe; --- reserved role (supautils.conf.j2): the non-superuser postgres role cannot --- grant it, modify it, grant to it, or drop it. Same pattern as --- supautils_reserved_roles.sql. -begin; -set role postgres; -savepoint s; -grant supabase_monitor_admin to postgres; -ERROR: "supabase_monitor_admin" role memberships are reserved, only superusers can grant them -rollback to savepoint s; -alter role supabase_monitor_admin password 'x'; -ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it -rollback to savepoint s; -alter role supabase_monitor_admin set search_path = public; -ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it -rollback to savepoint s; -grant authenticated to supabase_monitor_admin; -ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it -rollback to savepoint s; -drop role supabase_monitor_admin; -ERROR: "supabase_monitor_admin" is a reserved role, only superusers can modify it -rollback to savepoint s; -reset role; -rollback; diff --git a/nix/tests/expected/z_multigres-orioledb-17_roles.out b/nix/tests/expected/z_multigres-orioledb-17_roles.out index 1593ae606c..87352a9688 100644 --- a/nix/tests/expected/z_multigres-orioledb-17_roles.out +++ b/nix/tests/expected/z_multigres-orioledb-17_roles.out @@ -45,7 +45,7 @@ order by rolname; supabase_auth_admin | t | t | f | f | f | f | -1 | f | supabase_etl_admin | f | t | f | t | f | t | -1 | t | supabase_functions_admin | t | t | f | f | f | f | -1 | f | - supabase_monitor_admin | f | t | f | t | f | f | 2 | t | + supabase_monitor_admin | f | t | f | t | f | f | -1 | t | supabase_privileged_role | f | f | f | t | f | f | -1 | f | supabase_read_only_user | f | t | f | t | f | f | -1 | t | supabase_replication_admin | f | t | f | t | f | t | -1 | f | @@ -58,8 +58,8 @@ select from pg_roles r where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections') order by rolname; - rolname | rolconfig -----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------ + rolname | rolconfig +----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- anon | {statement_timeout=3s} authenticated | {statement_timeout=8s} authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s} @@ -84,7 +84,7 @@ order by rolname; supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none} supabase_etl_admin | supabase_functions_admin | - supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=\"\"",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} + supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=pg_catalog, pg_temp",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} supabase_privileged_role | supabase_read_only_user | {default_transaction_read_only=on} supabase_replication_admin | diff --git a/nix/tests/sql/z_15_supabase_monitor_admin.sql b/nix/tests/sql/supabase_monitor_admin.sql similarity index 77% rename from nix/tests/sql/z_15_supabase_monitor_admin.sql rename to nix/tests/sql/supabase_monitor_admin.sql index 473432c0f0..743dc42ef1 100644 --- a/nix/tests/sql/z_15_supabase_monitor_admin.sql +++ b/nix/tests/sql/supabase_monitor_admin.sql @@ -1,13 +1,12 @@ --- supabase_monitor_admin: passwordless, read-only --- monitoring role ( --- migrations/db/init-scripts/00000000000000-initial-schema.sql). +-- supabase_monitor_admin: passwordless, read-only monitoring role +-- (migrations/db/migrations/20261113000000_supabase_monitor_admin.sql) --- attributes: login, no password, bypassrls, nothing else +-- attributes: login, no password, bypassrls, no connection limit, nothing else select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password from pg_authid where rolname = 'supabase_monitor_admin'; --- per-role settings (read-only by default, empty search_path, timeouts) +-- per-role settings (read-only by default, search_path pg_catalog + pg_temp, timeouts) select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; -- memberships: exactly pg_monitor and pg_read_all_data @@ -26,10 +25,9 @@ where refclassid = 'pg_authid'::regclass -- no CREATE on the database select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); --- reads every table regardless of RLS, but cannot write. NOTE: this uses --- SET SESSION AUTHORIZATION, which a future supautils impersonation guard --- would block; switch to a probe role with identical grants when that lands --- (see quick_explainer.md). +-- reads every table regardless of RLS, but cannot write; SET ROLE to a +-- predefined role only loses privileges (BYPASSRLS stays with +-- supabase_monitor_admin, so the RLS-protected rows disappear) create table public.monitor_probe (id int); insert into public.monitor_probe values (1), (2); alter table public.monitor_probe enable row level security; @@ -38,6 +36,8 @@ alter table public.monitor_probe owner to postgres; set session authorization supabase_monitor_admin; select count(*) from public.monitor_probe; insert into public.monitor_probe values (3); +set role pg_read_all_data; +select count(*) from public.monitor_probe; reset session authorization; drop table public.monitor_probe; diff --git a/nix/tests/sql/z_17_supabase_monitor_admin.sql b/nix/tests/sql/z_17_supabase_monitor_admin.sql deleted file mode 100644 index 506cfdfef9..0000000000 --- a/nix/tests/sql/z_17_supabase_monitor_admin.sql +++ /dev/null @@ -1,65 +0,0 @@ --- supabase_monitor_admin: passwordless, read-only, for monitoring from localhost --- migrations/db/init-scripts/00000000000000-initial-schema.sql) - --- attributes: login, no password, bypassrls, nothing else --- only accessible via localhost -select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, - rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password -from pg_authid where rolname = 'supabase_monitor_admin'; - --- per-role settings (read-only by default, empty search_path, timeouts) -select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; - --- memberships: exactly pg_monitor and pg_read_all_data (inherited; the role is --- a plain INHERIT role). SET ROLE to them only drops privileges, checked below. -select roleid::regrole::text as role, admin_option, inherit_option, set_option -from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1; - --- nobody is a member of it -select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; - --- owns nothing and holds no object-level ACL entries anywhere --- keep in mind, the postgres role can assign it access to objects at a later point, but that's fine. -select count(*) from pg_shdepend -where refclassid = 'pg_authid'::regclass - and refobjid = 'supabase_monitor_admin'::regrole - and deptype in ('o', 'a'); - --- no CREATE on the database -select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); - --- reads every table regardless of RLS, but cannot write; SET ROLE to a --- predefined role only loses privileges (BYPASSRLS stays with supabase_monitor_admin, --- so the RLS-protected rows disappear). NOTE: this uses SET SESSION --- AUTHORIZATION, which supautils impersonation may block -create table public.monitor_probe (id int); -insert into public.monitor_probe values (1), (2); -alter table public.monitor_probe enable row level security; -alter table public.monitor_probe force row level security; -alter table public.monitor_probe owner to postgres; -set session authorization supabase_monitor_admin; -select count(*) from public.monitor_probe; -insert into public.monitor_probe values (3); -set role pg_read_all_data; -select count(*) from public.monitor_probe; -reset session authorization; -drop table public.monitor_probe; - --- reserved role (supautils.conf.j2): the non-superuser postgres role cannot --- grant it, modify it, grant to it, or drop it. Same pattern as --- supautils_reserved_roles.sql. -begin; -set role postgres; -savepoint s; -grant supabase_monitor_admin to postgres; -rollback to savepoint s; -alter role supabase_monitor_admin password 'x'; -rollback to savepoint s; -alter role supabase_monitor_admin set search_path = public; -rollback to savepoint s; -grant authenticated to supabase_monitor_admin; -rollback to savepoint s; -drop role supabase_monitor_admin; -rollback to savepoint s; -reset role; -rollback; diff --git a/testinfra/test_ami_nix.py b/testinfra/test_ami_nix.py index ea67dd355e..82e336d6dd 100644 --- a/testinfra/test_ami_nix.py +++ b/testinfra/test_ami_nix.py @@ -1158,7 +1158,7 @@ def test_postgrest_read_only_session_attrs(host): def test_supabase_monitor_admin_role_is_passwordless_and_reachable_on_loopback(host): - """supabase_monitor_admin (migrations/db/init-scripts/00000000000000-initial-schema.sql) + """supabase_monitor_admin (migrations/db/migrations/20261113000000_supabase_monitor_admin.sql) has no password and relies on the same pg_hba trust rules. Unlike the other service roles, this role must never be given a password in From 6ac7f020ca387e0c26a74b3699306415343adf8f Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 13 Nov 2026 14:09:22 +0900 Subject: [PATCH 5/6] fix: sync supabase_monitor_admin test comment with expected output --- nix/tests/sql/supabase_monitor_admin.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix/tests/sql/supabase_monitor_admin.sql b/nix/tests/sql/supabase_monitor_admin.sql index 743dc42ef1..0bd08b1a43 100644 --- a/nix/tests/sql/supabase_monitor_admin.sql +++ b/nix/tests/sql/supabase_monitor_admin.sql @@ -27,7 +27,7 @@ select has_database_privilege('supabase_monitor_admin', current_database(), 'CRE -- reads every table regardless of RLS, but cannot write; SET ROLE to a -- predefined role only loses privileges (BYPASSRLS stays with --- supabase_monitor_admin, so the RLS-protected rows disappear) +-- supabase_monitor_admin) create table public.monitor_probe (id int); insert into public.monitor_probe values (1), (2); alter table public.monitor_probe enable row level security; From d99361dcd46ff5382952cc76e17952e802c9b561 Mon Sep 17 00:00:00 2001 From: Brian Date: Wed, 18 Nov 2026 20:23:53 +0900 Subject: [PATCH 6/6] updated permissions so the new role can check PG Configs for read-only s tates. Also granted the role the ability to disable pg_stat_statements.track --- ansible/files/postgres_exporter.service.j2 | 2 +- .../20261113000000_supabase_monitor_admin.sql | 31 ++++++ nix/tests/expected/roles.out | 6 +- nix/tests/expected/supabase_monitor_admin.out | 101 +++++++++++++++++- nix/tests/expected/z_15_roles.out | 41 +++---- nix/tests/expected/z_17_roles.out | 49 ++++----- nix/tests/expected/z_multigres-17_roles.out | 49 ++++----- .../z_multigres-orioledb-17_roles.out | 6 +- nix/tests/sql/supabase_monitor_admin.sql | 58 +++++++++- testinfra/test_ami_nix.py | 35 +++++- 10 files changed, 292 insertions(+), 86 deletions(-) diff --git a/ansible/files/postgres_exporter.service.j2 b/ansible/files/postgres_exporter.service.j2 index 4e55f9689d..18d23e0f00 100644 --- a/ansible/files/postgres_exporter.service.j2 +++ b/ansible/files/postgres_exporter.service.j2 @@ -11,7 +11,7 @@ User=postgres Group=postgres Restart=always RestartSec=3 -Environment="DATA_SOURCE_NAME=host=localhost dbname=postgres sslmode=disable user=supabase_admin pg_stat_statements.track=none application_name=postgres_exporter" +Environment="DATA_SOURCE_NAME=host=localhost dbname=postgres sslmode=disable user=supabase_monitor_admin pg_stat_statements.track=none application_name=postgres_exporter" [Install] WantedBy=multi-user.target diff --git a/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql b/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql index cd0857c375..9629a9e368 100644 --- a/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql +++ b/migrations/db/migrations/20261113000000_supabase_monitor_admin.sql @@ -16,4 +16,35 @@ begin end if; end $$; +-- The monitor's own queries must not show up in the customer's +-- pg_stat_statements. Off by default at login, and the role may also pass +-- `-c pg_stat_statements.track=none` itself (like postgres_exporter does), +-- which otherwise fails with "permission denied to set parameter" because +-- the parameter is superuser-only. Same for temp_file_limit, which the +-- fleet's postgres_exporter passes outside prod; the default is unlimited, +-- so being able to set it gives the role nothing. Outside the +-- `if not exists` block so that a role created by an earlier revision of +-- this migration also gets them. +alter role supabase_monitor_admin set pg_stat_statements.track = 'none'; +grant set on parameter pg_stat_statements.track to supabase_monitor_admin; +grant set on parameter temp_file_limit to supabase_monitor_admin; + +-- The per-role default_transaction_read_only = on above hides the +-- system-wide value, so `show default_transaction_read_only` cannot tell the +-- monitor whether the project has been put into read-only mode +-- (manage_readonly_mode.sh: ALTER SYSTEM SET default_transaction_read_only). +-- pg_file_settings shows the configuration files' values, including +-- postgresql.auto.conf. initdb revokes the view and its function from +-- PUBLIC; the view calls the function as the caller, so both grants are +-- needed (SELECT also comes with pg_read_all_data, but is granted explicitly +-- so this does not depend on that membership). Nothing secret: pg_monitor +-- already reads every live setting. +-- +-- Deliberately NOT granted: pg_hba_file_rules() and pg_ident_file_mappings(). +-- They would show who may connect from where and how; pg_read_all_data +-- gives SELECT on the views, but without EXECUTE on the functions reading +-- them still fails with "permission denied". +grant select on pg_catalog.pg_file_settings to supabase_monitor_admin; +grant execute on function pg_catalog.pg_show_all_file_settings() to supabase_monitor_admin; + -- migrate:down diff --git a/nix/tests/expected/roles.out b/nix/tests/expected/roles.out index 330a0a0e95..21b8829375 100644 --- a/nix/tests/expected/roles.out +++ b/nix/tests/expected/roles.out @@ -61,8 +61,8 @@ select from pg_roles r where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections') order by rolname; - rolname | rolconfig -----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + rolname | rolconfig +----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- anon | {statement_timeout=3s} authenticated | {statement_timeout=8s} authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s} @@ -90,7 +90,7 @@ order by rolname; supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none} supabase_etl_admin | supabase_functions_admin | - supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=pg_catalog, pg_temp",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} + supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=pg_catalog, pg_temp",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s,pg_stat_statements.track=none} supabase_privileged_role | supabase_read_only_user | {default_transaction_read_only=on} supabase_replication_admin | diff --git a/nix/tests/expected/supabase_monitor_admin.out b/nix/tests/expected/supabase_monitor_admin.out index de349a7d8a..8bbfbee157 100644 --- a/nix/tests/expected/supabase_monitor_admin.out +++ b/nix/tests/expected/supabase_monitor_admin.out @@ -9,17 +9,19 @@ from pg_authid where rolname = 'supabase_monitor_admin'; f | t | f | f | t | f | t | -1 | t (1 row) --- per-role settings (read-only by default, search_path pg_catalog + pg_temp, timeouts) +-- per-role settings (read-only by default, search_path pg_catalog + pg_temp, +-- timeouts, its own queries not tracked by pg_stat_statements) select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; setting ---------------------------------------- default_transaction_read_only=on idle_in_transaction_session_timeout=3s lock_timeout=3s + pg_stat_statements.track=none search_path=pg_catalog, pg_temp statement_timeout=3s transaction_read_only=on -(6 rows) +(7 rows) -- memberships: exactly pg_monitor and pg_read_all_data select roleid::regrole::text as role, admin_option @@ -37,16 +39,61 @@ select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::re 0 (1 row) --- owns nothing and holds no object-level ACL entries anywhere +-- owns nothing select count(*) from pg_shdepend where refclassid = 'pg_authid'::regclass and refobjid = 'supabase_monitor_admin'::regrole - and deptype in ('o', 'a'); + and deptype = 'o'; count ------- 0 (1 row) +-- object-level privileges: exactly SET on pg_stat_statements.track and +-- temp_file_limit (not ALTER SYSTEM), and SELECT/EXECUTE on pg_file_settings +-- (to see read-only mode behind its own per-role +-- default_transaction_read_only = on). Not the pg_hba.conf / pg_ident.conf +-- readers: SELECT on those views comes with pg_read_all_data, but EXECUTE on +-- their functions must stay revoked. +select pg_describe_object(classid, objid, objsubid) as object +from pg_shdepend +where refclassid = 'pg_authid'::regclass + and refobjid = 'supabase_monitor_admin'::regrole + and deptype = 'a' +order by 1; + object +-------------------------------------- + function pg_show_all_file_settings() + parameter pg_stat_statements.track + parameter temp_file_limit + view pg_file_settings +(4 rows) + +select p as parameter, + has_parameter_privilege('supabase_monitor_admin', p, 'SET') as can_set, + has_parameter_privilege('supabase_monitor_admin', p, 'ALTER SYSTEM') as can_alter_system +from (values ('pg_stat_statements.track'), ('temp_file_limit')) t(p) +order by 1; + parameter | can_set | can_alter_system +--------------------------+---------+------------------ + pg_stat_statements.track | t | f + temp_file_limit | t | f +(2 rows) + +select v as object, + has_table_privilege('supabase_monitor_admin', 'pg_catalog.' || v, 'SELECT') as can_select, + has_function_privilege('supabase_monitor_admin', 'pg_catalog.' || f, 'EXECUTE') as can_execute +from (values ('pg_file_settings', 'pg_show_all_file_settings()'), + ('pg_hba_file_rules', 'pg_hba_file_rules()'), + ('pg_ident_file_mappings', 'pg_ident_file_mappings()')) t(v, f) +order by 1; + object | can_select | can_execute +------------------------+------------+------------- + pg_file_settings | t | t + pg_hba_file_rules | t | f + pg_ident_file_mappings | t | f +(3 rows) + -- no CREATE on the database select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); has_database_privilege @@ -80,6 +127,52 @@ select count(*) from public.monitor_probe; reset session authorization; drop table public.monitor_probe; +-- it can switch off pg_stat_statements tracking and cap temp files for its +-- own session (both are otherwise superuser-only: "permission denied to set +-- parameter") +set session authorization supabase_monitor_admin; +set pg_stat_statements.track = none; +set temp_file_limit = '15MB'; +show pg_stat_statements.track; + pg_stat_statements.track +-------------------------- + none +(1 row) + +show temp_file_limit; + temp_file_limit +----------------- + 15MB +(1 row) + +reset session authorization; +reset pg_stat_statements.track; +reset temp_file_limit; +-- it sees read-only mode as set by manage_readonly_mode.sh through +-- pg_file_settings, even though its own sessions default to read-only +alter system set default_transaction_read_only = on; +set session authorization supabase_monitor_admin; +select setting, applied from pg_file_settings where name = 'default_transaction_read_only'; + setting | applied +---------+--------- + on | t +(1 row) + +reset session authorization; +alter system reset default_transaction_read_only; +set session authorization supabase_monitor_admin; +select count(*) from pg_file_settings where name = 'default_transaction_read_only'; + count +------- + 0 +(1 row) + +-- but it cannot read pg_hba.conf / pg_ident.conf +select count(*) from pg_hba_file_rules; +ERROR: permission denied for function pg_hba_file_rules +select count(*) from pg_ident_file_mappings; +ERROR: permission denied for function pg_ident_file_mappings +reset session authorization; -- reserved role (supautils.conf.j2): the non-superuser postgres role cannot -- grant it, modify it, grant to it, or drop it. Same pattern as -- supautils_reserved_roles.sql. diff --git a/nix/tests/expected/z_15_roles.out b/nix/tests/expected/z_15_roles.out index 216b50a787..379c53ee29 100644 --- a/nix/tests/expected/z_15_roles.out +++ b/nix/tests/expected/z_15_roles.out @@ -51,24 +51,25 @@ cross join lateral pg_catalog.aclexplode(p.proacl) as acl where p.pronamespace::regnamespace::text = 'pg_catalog' and acl.grantee::regrole::text != 'supabase_admin' order by object_name, grantee, privilege_type; - schema | object_name | grantee | privilege_type -------------+--------------------------------+-------------------+---------------- - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | pg_get_backend_memory_contexts | pg_read_all_stats | EXECUTE - pg_catalog | pg_get_shmem_allocations | pg_read_all_stats | EXECUTE - pg_catalog | pg_ls_archive_statusdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logicalmapdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logicalsnapdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_replslotdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_waldir | pg_monitor | EXECUTE - pg_catalog | pg_reload_conf | postgres | EXECUTE - pg_catalog | verify_heapam | postgres | EXECUTE -(17 rows) + schema | object_name | grantee | privilege_type +------------+--------------------------------+------------------------+---------------- + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | pg_get_backend_memory_contexts | pg_read_all_stats | EXECUTE + pg_catalog | pg_get_shmem_allocations | pg_read_all_stats | EXECUTE + pg_catalog | pg_ls_archive_statusdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logicalmapdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logicalsnapdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_replslotdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_waldir | pg_monitor | EXECUTE + pg_catalog | pg_reload_conf | postgres | EXECUTE + pg_catalog | pg_show_all_file_settings | supabase_monitor_admin | EXECUTE + pg_catalog | verify_heapam | postgres | EXECUTE +(18 rows) diff --git a/nix/tests/expected/z_17_roles.out b/nix/tests/expected/z_17_roles.out index f6c418dc69..4cb6759677 100644 --- a/nix/tests/expected/z_17_roles.out +++ b/nix/tests/expected/z_17_roles.out @@ -186,28 +186,29 @@ cross join lateral pg_catalog.aclexplode(p.proacl) as acl where p.pronamespace::regnamespace::text = 'pg_catalog' and acl.grantee::regrole::text != 'supabase_admin' order by object_name, grantee, privilege_type; - schema | object_name | grantee | privilege_type -------------+--------------------------------+-------------------+---------------- - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | pg_current_logfile | pg_monitor | EXECUTE - pg_catalog | pg_current_logfile | pg_monitor | EXECUTE - pg_catalog | pg_get_backend_memory_contexts | pg_read_all_stats | EXECUTE - pg_catalog | pg_get_shmem_allocations | pg_read_all_stats | EXECUTE - pg_catalog | pg_ls_archive_statusdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logicalmapdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logicalsnapdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_replslotdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_waldir | pg_monitor | EXECUTE - pg_catalog | pg_reload_conf | postgres | EXECUTE - pg_catalog | verify_heapam | postgres | EXECUTE -(21 rows) + schema | object_name | grantee | privilege_type +------------+--------------------------------+------------------------+---------------- + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | pg_current_logfile | pg_monitor | EXECUTE + pg_catalog | pg_current_logfile | pg_monitor | EXECUTE + pg_catalog | pg_get_backend_memory_contexts | pg_read_all_stats | EXECUTE + pg_catalog | pg_get_shmem_allocations | pg_read_all_stats | EXECUTE + pg_catalog | pg_ls_archive_statusdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logicalmapdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logicalsnapdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_replslotdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_waldir | pg_monitor | EXECUTE + pg_catalog | pg_reload_conf | postgres | EXECUTE + pg_catalog | pg_show_all_file_settings | supabase_monitor_admin | EXECUTE + pg_catalog | verify_heapam | postgres | EXECUTE +(22 rows) diff --git a/nix/tests/expected/z_multigres-17_roles.out b/nix/tests/expected/z_multigres-17_roles.out index 1319908966..087af23884 100644 --- a/nix/tests/expected/z_multigres-17_roles.out +++ b/nix/tests/expected/z_multigres-17_roles.out @@ -178,28 +178,29 @@ cross join lateral pg_catalog.aclexplode(p.proacl) as acl where p.pronamespace::regnamespace::text = 'pg_catalog' and acl.grantee::regrole::text != 'supabase_admin' order by object_name, grantee, privilege_type; - schema | object_name | grantee | privilege_type -------------+--------------------------------+-------------------+---------------- - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | bt_index_parent_check | postgres | EXECUTE - pg_catalog | pg_current_logfile | pg_monitor | EXECUTE - pg_catalog | pg_current_logfile | pg_monitor | EXECUTE - pg_catalog | pg_get_backend_memory_contexts | pg_read_all_stats | EXECUTE - pg_catalog | pg_get_shmem_allocations | pg_read_all_stats | EXECUTE - pg_catalog | pg_ls_archive_statusdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logicalmapdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_logicalsnapdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_replslotdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE - pg_catalog | pg_ls_waldir | pg_monitor | EXECUTE - pg_catalog | pg_reload_conf | postgres | EXECUTE - pg_catalog | verify_heapam | postgres | EXECUTE -(21 rows) + schema | object_name | grantee | privilege_type +------------+--------------------------------+------------------------+---------------- + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | bt_index_parent_check | postgres | EXECUTE + pg_catalog | pg_current_logfile | pg_monitor | EXECUTE + pg_catalog | pg_current_logfile | pg_monitor | EXECUTE + pg_catalog | pg_get_backend_memory_contexts | pg_read_all_stats | EXECUTE + pg_catalog | pg_get_shmem_allocations | pg_read_all_stats | EXECUTE + pg_catalog | pg_ls_archive_statusdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logicalmapdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_logicalsnapdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_replslotdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_tmpdir | pg_monitor | EXECUTE + pg_catalog | pg_ls_waldir | pg_monitor | EXECUTE + pg_catalog | pg_reload_conf | postgres | EXECUTE + pg_catalog | pg_show_all_file_settings | supabase_monitor_admin | EXECUTE + pg_catalog | verify_heapam | postgres | EXECUTE +(22 rows) diff --git a/nix/tests/expected/z_multigres-orioledb-17_roles.out b/nix/tests/expected/z_multigres-orioledb-17_roles.out index 87352a9688..100c4a5f5d 100644 --- a/nix/tests/expected/z_multigres-orioledb-17_roles.out +++ b/nix/tests/expected/z_multigres-orioledb-17_roles.out @@ -58,8 +58,8 @@ select from pg_roles r where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections') order by rolname; - rolname | rolconfig -----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + rolname | rolconfig +----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- anon | {statement_timeout=3s} authenticated | {statement_timeout=8s} authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s} @@ -84,7 +84,7 @@ order by rolname; supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none} supabase_etl_admin | supabase_functions_admin | - supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=pg_catalog, pg_temp",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s} + supabase_monitor_admin | {default_transaction_read_only=on,transaction_read_only=on,"search_path=pg_catalog, pg_temp",statement_timeout=3s,lock_timeout=3s,idle_in_transaction_session_timeout=3s,pg_stat_statements.track=none} supabase_privileged_role | supabase_read_only_user | {default_transaction_read_only=on} supabase_replication_admin | diff --git a/nix/tests/sql/supabase_monitor_admin.sql b/nix/tests/sql/supabase_monitor_admin.sql index 0bd08b1a43..15450b6a21 100644 --- a/nix/tests/sql/supabase_monitor_admin.sql +++ b/nix/tests/sql/supabase_monitor_admin.sql @@ -6,7 +6,8 @@ select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password from pg_authid where rolname = 'supabase_monitor_admin'; --- per-role settings (read-only by default, search_path pg_catalog + pg_temp, timeouts) +-- per-role settings (read-only by default, search_path pg_catalog + pg_temp, +-- timeouts, its own queries not tracked by pg_stat_statements) select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1; -- memberships: exactly pg_monitor and pg_read_all_data @@ -16,11 +17,36 @@ from pg_auth_members where member = 'supabase_monitor_admin'::regrole order by 1 -- nobody is a member of it select count(*) from pg_auth_members where roleid = 'supabase_monitor_admin'::regrole; --- owns nothing and holds no object-level ACL entries anywhere +-- owns nothing select count(*) from pg_shdepend where refclassid = 'pg_authid'::regclass and refobjid = 'supabase_monitor_admin'::regrole - and deptype in ('o', 'a'); + and deptype = 'o'; + +-- object-level privileges: exactly SET on pg_stat_statements.track and +-- temp_file_limit (not ALTER SYSTEM), and SELECT/EXECUTE on pg_file_settings +-- (to see read-only mode behind its own per-role +-- default_transaction_read_only = on). Not the pg_hba.conf / pg_ident.conf +-- readers: SELECT on those views comes with pg_read_all_data, but EXECUTE on +-- their functions must stay revoked. +select pg_describe_object(classid, objid, objsubid) as object +from pg_shdepend +where refclassid = 'pg_authid'::regclass + and refobjid = 'supabase_monitor_admin'::regrole + and deptype = 'a' +order by 1; +select p as parameter, + has_parameter_privilege('supabase_monitor_admin', p, 'SET') as can_set, + has_parameter_privilege('supabase_monitor_admin', p, 'ALTER SYSTEM') as can_alter_system +from (values ('pg_stat_statements.track'), ('temp_file_limit')) t(p) +order by 1; +select v as object, + has_table_privilege('supabase_monitor_admin', 'pg_catalog.' || v, 'SELECT') as can_select, + has_function_privilege('supabase_monitor_admin', 'pg_catalog.' || f, 'EXECUTE') as can_execute +from (values ('pg_file_settings', 'pg_show_all_file_settings()'), + ('pg_hba_file_rules', 'pg_hba_file_rules()'), + ('pg_ident_file_mappings', 'pg_ident_file_mappings()')) t(v, f) +order by 1; -- no CREATE on the database select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE'); @@ -41,6 +67,32 @@ select count(*) from public.monitor_probe; reset session authorization; drop table public.monitor_probe; +-- it can switch off pg_stat_statements tracking and cap temp files for its +-- own session (both are otherwise superuser-only: "permission denied to set +-- parameter") +set session authorization supabase_monitor_admin; +set pg_stat_statements.track = none; +set temp_file_limit = '15MB'; +show pg_stat_statements.track; +show temp_file_limit; +reset session authorization; +reset pg_stat_statements.track; +reset temp_file_limit; + +-- it sees read-only mode as set by manage_readonly_mode.sh through +-- pg_file_settings, even though its own sessions default to read-only +alter system set default_transaction_read_only = on; +set session authorization supabase_monitor_admin; +select setting, applied from pg_file_settings where name = 'default_transaction_read_only'; +reset session authorization; +alter system reset default_transaction_read_only; +set session authorization supabase_monitor_admin; +select count(*) from pg_file_settings where name = 'default_transaction_read_only'; +-- but it cannot read pg_hba.conf / pg_ident.conf +select count(*) from pg_hba_file_rules; +select count(*) from pg_ident_file_mappings; +reset session authorization; + -- reserved role (supautils.conf.j2): the non-superuser postgres role cannot -- grant it, modify it, grant to it, or drop it. Same pattern as -- supautils_reserved_roles.sql. diff --git a/testinfra/test_ami_nix.py b/testinfra/test_ami_nix.py index 82e336d6dd..c0a210eda4 100644 --- a/testinfra/test_ami_nix.py +++ b/testinfra/test_ami_nix.py @@ -1182,21 +1182,48 @@ def test_supabase_monitor_admin_role_is_passwordless_and_reachable_on_loopback(h f"unexpected supabase_monitor_admin attributes: {result['stdout']!r}" ) - # Passwordless loopback login works, and the per-role read-only default - # applies to the resulting session. + # Passwordless loopback login works, and the per-role defaults apply to + # the resulting session: read-only, and its own queries are not tracked + # by pg_stat_statements. result = run_ssh_command( ssh, "sudo -u postgres psql -U supabase_monitor_admin -h 127.0.0.1 -d postgres -At " - "-c 'select current_user' -c 'show default_transaction_read_only' 2>&1", + "-c 'select current_user' -c 'show default_transaction_read_only' " + "-c 'show pg_stat_statements.track' 2>&1", ) assert result["succeeded"], ( "supabase_monitor_admin could not log in over loopback without a password: " f"{result['stdout']!r} {result['stderr']!r}" ) - assert result["stdout"].split() == ["supabase_monitor_admin", "on"], ( + assert result["stdout"].split() == ["supabase_monitor_admin", "on", "none"], ( f"unexpected session state for supabase_monitor_admin: {result['stdout']!r}" ) + # The monitor may also pass the fleet postgres_exporter's startup options + # itself (both parameters are superuser-only without the role's SET + # grants), and it reads the system-wide default_transaction_read_only from + # pg_file_settings (read-only mode, see manage_readonly_mode.sh), which its + # own per-role default hides from SHOW. This image is not in read-only + # mode. pg_hba.conf / pg_ident.conf stay unreadable. + result = run_ssh_command( + ssh, + "sudo -u postgres env PGOPTIONS='-c pg_stat_statements.track=none -c temp_file_limit=15MB' " + "psql -U supabase_monitor_admin -h 127.0.0.1 -d postgres -At " + "-c 'show pg_stat_statements.track' " + "-c 'show temp_file_limit' " + '-c "select coalesce((select setting from pg_file_settings ' + "where name = 'default_transaction_read_only' and applied), 'off')\" " + "-c \"select has_function_privilege('pg_hba_file_rules()', 'EXECUTE') " + "or has_function_privilege('pg_ident_file_mappings()', 'EXECUTE')\" 2>&1", + ) + assert result["succeeded"], ( + "supabase_monitor_admin could not log in with the exporter's options or " + f"read pg_file_settings: {result['stdout']!r} {result['stderr']!r}" + ) + assert result["stdout"].split() == ["none", "15MB", "off", "f"], ( + f"unexpected monitoring output for supabase_monitor_admin: {result['stdout']!r}" + ) + def test_custom_overrides_take_precedence_over_generated_optimizations(host): """Verify CLI-managed PostgreSQL config overrides generated optimizations.