Repository navigation
create supabase_admin_monitor role for monitoring services #2642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
76dff61
5df50d6
5722d94
6703218
6ac7f02
0a7aa2b
4f4c09a
d99361d
c867c0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| -- 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 $$; | ||
|
|
||
| -- 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". | ||
|
Comment on lines
+19
to
+46
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind rewording the comments to be more succinct? I find them really hard to parse
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the "Deliberately NOT granted" part is needed |
||
| 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; | ||
|
Comment on lines
+47
to
+48
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we need to update postgres_exporter queries to use pg_file_settings/pg_show_all_file_settings? How do we roll that out safely? It seems to me both the introduction of supabase_monitor_admin and the postgres_exporter query changes need to happen atomically |
||
|
|
||
| -- migrate:down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| -- 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 | -1 | t | ||
| (1 row) | ||
|
|
||
| -- 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 | ||
| (7 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 | ||
| select count(*) from pg_shdepend | ||
| where refclassid = 'pg_authid'::regclass | ||
| and refobjid = 'supabase_monitor_admin'::regrole | ||
| 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 | ||
| ------------------------ | ||
| 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) | ||
| 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; | ||
| -- 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. | ||
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not needed, only
default_transaction_read_onlyis