Skip to content

Commit 6703218

Browse files
feat: create supabase_monitor_admin in a migration, single pg_regress test, no connection limit
1 parent 5722d94 commit 6703218

9 files changed

Lines changed: 53 additions & 216 deletions

‎migrations/db/init-scripts/00000000000000-initial-schema.sql‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ grant create on database postgres to supabase_etl_admin;
1919
create role supabase_read_only_user with login bypassrls;
2020
grant pg_read_all_data to supabase_read_only_user;
2121

22-
-- Supabase monitoring user: no password and can only connect via localhost,
23-
-- reads every table
24-
-- bypassing RLS, read-only, owns nothing.
25-
-- so the name can never be pre-empted: a plain CREATE fails loudly if it exists.
26-
create role supabase_monitor_admin with login bypassrls connection limit 2;
27-
grant pg_read_all_data, pg_monitor to supabase_monitor_admin;
28-
alter role supabase_monitor_admin set default_transaction_read_only = on;
29-
alter role supabase_monitor_admin set transaction_read_only = on;
30-
alter role supabase_monitor_admin set search_path = '';
31-
alter role supabase_monitor_admin set statement_timeout = '3s';
32-
alter role supabase_monitor_admin set lock_timeout = '3s';
33-
alter role supabase_monitor_admin set idle_in_transaction_session_timeout = '3s';
34-
3522
-- Extension namespacing
3623
create schema if not exists extensions;
3724
create extension if not exists "uuid-ossp" with schema extensions;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- migrate:up
2+
3+
-- Supabase monitoring user: no password and can only connect via localhost,
4+
-- reads every table bypassing RLS, read-only, owns nothing.
5+
do $$
6+
begin
7+
if not exists (select from pg_roles where rolname = 'supabase_monitor_admin') then
8+
create role supabase_monitor_admin with login bypassrls;
9+
grant pg_read_all_data, pg_monitor to supabase_monitor_admin;
10+
alter role supabase_monitor_admin set default_transaction_read_only = on;
11+
alter role supabase_monitor_admin set transaction_read_only = on;
12+
alter role supabase_monitor_admin set search_path = pg_catalog, pg_temp;
13+
alter role supabase_monitor_admin set statement_timeout = '3s';
14+
alter role supabase_monitor_admin set lock_timeout = '3s';
15+
alter role supabase_monitor_admin set idle_in_transaction_session_timeout = '3s';
16+
end if;
17+
end $$;
18+
19+
-- migrate:down

‎nix/tests/expected/roles.out‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ order by rolname;
4848
supabase_auth_admin | t | t | f | f | f | f | -1 | f |
4949
supabase_etl_admin | f | t | f | t | f | t | -1 | t |
5050
supabase_functions_admin | t | t | f | f | f | f | -1 | f |
51-
supabase_monitor_admin | f | t | f | t | f | f | 2 | t |
51+
supabase_monitor_admin | f | t | f | t | f | f | -1 | t |
5252
supabase_privileged_role | f | f | f | t | f | f | -1 | f |
5353
supabase_read_only_user | f | t | f | t | f | f | -1 | t |
5454
supabase_replication_admin | f | t | f | t | f | t | -1 | f |
@@ -61,8 +61,8 @@ select
6161
from pg_roles r
6262
where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections')
6363
order by rolname;
64-
rolname | rolconfig
65-
----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
64+
rolname | rolconfig
65+
----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6666
anon | {statement_timeout=3s}
6767
authenticated | {statement_timeout=8s}
6868
authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s}
@@ -90,7 +90,7 @@ order by rolname;
9090
supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none}
9191
supabase_etl_admin |
9292
supabase_functions_admin |
93-
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}
93+
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}
9494
supabase_privileged_role |
9595
supabase_read_only_user | {default_transaction_read_only=on}
9696
supabase_replication_admin |

nix/tests/expected/z_15_supabase_monitor_admin.out renamed to nix/tests/expected/supabase_monitor_admin.out

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
-- supabase_monitor_admin: passwordless, read-only
2-
-- monitoring role (
3-
-- migrations/db/init-scripts/00000000000000-initial-schema.sql).
4-
-- attributes: login, no password, bypassrls, nothing else
1+
-- supabase_monitor_admin: passwordless, read-only monitoring role
2+
-- (migrations/db/migrations/20261113000000_supabase_monitor_admin.sql)
3+
-- attributes: login, no password, bypassrls, no connection limit, nothing else
54
select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin,
65
rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password
76
from pg_authid where rolname = 'supabase_monitor_admin';
87
rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolbypassrls | rolconnlimit | no_password
98
----------+------------+---------------+-------------+-------------+----------------+--------------+--------------+-------------
10-
f | t | f | f | t | f | t | 2 | t
9+
f | t | f | f | t | f | t | -1 | t
1110
(1 row)
1211

13-
-- per-role settings (read-only by default, empty search_path, timeouts)
12+
-- per-role settings (read-only by default, search_path pg_catalog + pg_temp, timeouts)
1413
select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1;
1514
setting
1615
----------------------------------------
1716
default_transaction_read_only=on
1817
idle_in_transaction_session_timeout=3s
1918
lock_timeout=3s
20-
search_path=""
19+
search_path=pg_catalog, pg_temp
2120
statement_timeout=3s
2221
transaction_read_only=on
2322
(6 rows)
@@ -55,10 +54,9 @@ select has_database_privilege('supabase_monitor_admin', current_database(), 'CRE
5554
f
5655
(1 row)
5756

58-
-- reads every table regardless of RLS, but cannot write. NOTE: this uses
59-
-- SET SESSION AUTHORIZATION, which a future supautils impersonation guard
60-
-- would block; switch to a probe role with identical grants when that lands
61-
-- (see quick_explainer.md).
57+
-- reads every table regardless of RLS, but cannot write; SET ROLE to a
58+
-- predefined role only loses privileges (BYPASSRLS stays with
59+
-- supabase_monitor_admin)
6260
create table public.monitor_probe (id int);
6361
insert into public.monitor_probe values (1), (2);
6462
alter table public.monitor_probe enable row level security;
@@ -73,6 +71,13 @@ select count(*) from public.monitor_probe;
7371

7472
insert into public.monitor_probe values (3);
7573
ERROR: permission denied for table monitor_probe
74+
set role pg_read_all_data;
75+
select count(*) from public.monitor_probe;
76+
count
77+
-------
78+
0
79+
(1 row)
80+
7681
reset session authorization;
7782
drop table public.monitor_probe;
7883
-- reserved role (supautils.conf.j2): the non-superuser postgres role cannot

‎nix/tests/expected/z_17_supabase_monitor_admin.out‎

Lines changed: 0 additions & 109 deletions
This file was deleted.

‎nix/tests/expected/z_multigres-orioledb-17_roles.out‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ order by rolname;
4545
supabase_auth_admin | t | t | f | f | f | f | -1 | f |
4646
supabase_etl_admin | f | t | f | t | f | t | -1 | t |
4747
supabase_functions_admin | t | t | f | f | f | f | -1 | f |
48-
supabase_monitor_admin | f | t | f | t | f | f | 2 | t |
48+
supabase_monitor_admin | f | t | f | t | f | f | -1 | t |
4949
supabase_privileged_role | f | f | f | t | f | f | -1 | f |
5050
supabase_read_only_user | f | t | f | t | f | f | -1 | t |
5151
supabase_replication_admin | f | t | f | t | f | t | -1 | f |
@@ -58,8 +58,8 @@ select
5858
from pg_roles r
5959
where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections')
6060
order by rolname;
61-
rolname | rolconfig
62-
----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
61+
rolname | rolconfig
62+
----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6363
anon | {statement_timeout=3s}
6464
authenticated | {statement_timeout=8s}
6565
authenticator | {"session_preload_libraries=supautils, safeupdate",statement_timeout=8s,lock_timeout=8s}
@@ -84,7 +84,7 @@ order by rolname;
8484
supabase_auth_admin | {search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none}
8585
supabase_etl_admin |
8686
supabase_functions_admin |
87-
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}
87+
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}
8888
supabase_privileged_role |
8989
supabase_read_only_user | {default_transaction_read_only=on}
9090
supabase_replication_admin |

nix/tests/sql/z_15_supabase_monitor_admin.sql renamed to nix/tests/sql/supabase_monitor_admin.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
-- supabase_monitor_admin: passwordless, read-only
2-
-- monitoring role (
3-
-- migrations/db/init-scripts/00000000000000-initial-schema.sql).
1+
-- supabase_monitor_admin: passwordless, read-only monitoring role
2+
-- (migrations/db/migrations/20261113000000_supabase_monitor_admin.sql)
43

5-
-- attributes: login, no password, bypassrls, nothing else
4+
-- attributes: login, no password, bypassrls, no connection limit, nothing else
65
select rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin,
76
rolreplication, rolbypassrls, rolconnlimit, rolpassword is null as no_password
87
from pg_authid where rolname = 'supabase_monitor_admin';
98

10-
-- per-role settings (read-only by default, empty search_path, timeouts)
9+
-- per-role settings (read-only by default, search_path pg_catalog + pg_temp, timeouts)
1110
select unnest(rolconfig) as setting from pg_roles where rolname = 'supabase_monitor_admin' order by 1;
1211

1312
-- memberships: exactly pg_monitor and pg_read_all_data
@@ -26,10 +25,9 @@ where refclassid = 'pg_authid'::regclass
2625
-- no CREATE on the database
2726
select has_database_privilege('supabase_monitor_admin', current_database(), 'CREATE');
2827

29-
-- reads every table regardless of RLS, but cannot write. NOTE: this uses
30-
-- SET SESSION AUTHORIZATION, which a future supautils impersonation guard
31-
-- would block; switch to a probe role with identical grants when that lands
32-
-- (see quick_explainer.md).
28+
-- reads every table regardless of RLS, but cannot write; SET ROLE to a
29+
-- predefined role only loses privileges (BYPASSRLS stays with
30+
-- supabase_monitor_admin, so the RLS-protected rows disappear)
3331
create table public.monitor_probe (id int);
3432
insert into public.monitor_probe values (1), (2);
3533
alter table public.monitor_probe enable row level security;
@@ -38,6 +36,8 @@ alter table public.monitor_probe owner to postgres;
3836
set session authorization supabase_monitor_admin;
3937
select count(*) from public.monitor_probe;
4038
insert into public.monitor_probe values (3);
39+
set role pg_read_all_data;
40+
select count(*) from public.monitor_probe;
4141
reset session authorization;
4242
drop table public.monitor_probe;
4343

0 commit comments

Comments
 (0)