Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ansible/files/postgres_exporter.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions ansible/files/postgresql_config/supautils.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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'
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;

Copy link
Copy Markdown
Member

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_only is

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
7 changes: 7 additions & 0 deletions migrations/tests/database/privs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
10 changes: 6 additions & 4 deletions nix/tests/expected/roles.out
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ 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 | -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 |
supabase_storage_admin | t | t | f | f | f | f | -1 | f |
(31 rows)
(32 rows)

select
rolname,
rolconfig
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}
Expand Down Expand Up @@ -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=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 |
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
Expand Down
198 changes: 198 additions & 0 deletions nix/tests/expected/supabase_monitor_admin.out
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;
Loading
Loading