Skip to content

Commit 1772a43

Browse files
committed
test: roles privileges, memberships, attributes
regress the following aspects: * roles attributes * roles memberships * roles privileges on schemas * centralizes all role tests in roles.sql + Removes redundant checks in auth test + Removes redundant checks in storage test
1 parent 3cd7173 commit 1772a43

File tree

9 files changed

+664
-268
lines changed

9 files changed

+664
-268
lines changed

migrations/db/init-scripts/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
The effects of these migrations are tested on:
3+
4+
- [nix/tests/sql/auth.out](../../../nix/tests/expected/auth.out)
5+
- [nix/tests/sql/storage.out](../../../nix/tests/expected/storage.out)
6+
- [nix/tests/sql/roles.out](../../../nix/tests/expected/roles.out)
7+
- [nix/tests/sql/evtrigs.out](../../../nix/tests/expected/evtrigs.out)

nix/tests/expected/auth.out

-76
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,6 @@ where
1313
auth | supabase_admin
1414
(1 row)
1515

16-
-- attributes of the supabase_auth_admin
17-
select
18-
rolcreaterole ,
19-
rolcanlogin ,
20-
rolsuper ,
21-
rolinherit ,
22-
rolcreatedb ,
23-
rolreplication ,
24-
rolconnlimit ,
25-
rolbypassrls ,
26-
rolvaliduntil
27-
from pg_roles r
28-
where r.rolname = 'supabase_auth_admin';
29-
rolcreaterole | rolcanlogin | rolsuper | rolinherit | rolcreatedb | rolreplication | rolconnlimit | rolbypassrls | rolvaliduntil
30-
---------------+-------------+----------+------------+-------------+----------------+--------------+--------------+---------------
31-
t | t | f | f | f | f | -1 | f |
32-
(1 row)
33-
34-
select
35-
rolconfig
36-
from pg_roles r
37-
where r.rolname = 'supabase_auth_admin';
38-
rolconfig
39-
---------------------------------------------------------------------------------
40-
{search_path=auth,idle_in_transaction_session_timeout=60000,log_statement=none}
41-
(1 row)
42-
4316
-- auth schema tables with owners and rls policies
4417
select
4518
ns.nspname as schema_name,
@@ -129,52 +102,3 @@ order by
129102
auth | uid | supabase_auth_admin
130103
(3 rows)
131104

132-
-- roles which have USAGE on the auth schema
133-
select
134-
n.nspname as schema_name,
135-
r.rolname as role_name,
136-
a.privilege_type
137-
from
138-
pg_namespace n
139-
cross join lateral aclexplode(n.nspacl) as a
140-
join
141-
pg_roles r on a.grantee = r.oid
142-
where
143-
n.nspname = 'auth'
144-
and a.privilege_type = 'USAGE'
145-
order by
146-
r.rolname;
147-
schema_name | role_name | privilege_type
148-
-------------+---------------------+----------------
149-
auth | anon | USAGE
150-
auth | authenticated | USAGE
151-
auth | dashboard_user | USAGE
152-
auth | postgres | USAGE
153-
auth | service_role | USAGE
154-
auth | supabase_admin | USAGE
155-
auth | supabase_auth_admin | USAGE
156-
(7 rows)
157-
158-
-- roles which have CREATE on the auth schema
159-
select
160-
n.nspname as schema_name,
161-
r.rolname as role_name,
162-
a.privilege_type
163-
from
164-
pg_namespace n
165-
cross join lateral aclexplode(n.nspacl) as a
166-
join
167-
pg_roles r on a.grantee = r.oid
168-
where
169-
n.nspname = 'auth'
170-
and a.privilege_type = 'CREATE'
171-
order by
172-
r.rolname;
173-
schema_name | role_name | privilege_type
174-
-------------+---------------------+----------------
175-
auth | dashboard_user | CREATE
176-
auth | postgres | CREATE
177-
auth | supabase_admin | CREATE
178-
auth | supabase_auth_admin | CREATE
179-
(4 rows)
180-

nix/tests/expected/default_privs.out

-12
This file was deleted.

nix/tests/expected/roles.out

+580
Large diffs are not rendered by default.

nix/tests/expected/storage.out

-76
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,6 @@ where
1313
storage | supabase_admin
1414
(1 row)
1515

16-
-- attributes of the supabase_storage_admin
17-
select
18-
rolcreaterole ,
19-
rolcanlogin ,
20-
rolsuper ,
21-
rolinherit ,
22-
rolcreatedb ,
23-
rolreplication ,
24-
rolconnlimit ,
25-
rolbypassrls ,
26-
rolvaliduntil
27-
from pg_roles r
28-
where r.rolname = 'supabase_storage_admin';
29-
rolcreaterole | rolcanlogin | rolsuper | rolinherit | rolcreatedb | rolreplication | rolconnlimit | rolbypassrls | rolvaliduntil
30-
---------------+-------------+----------+------------+-------------+----------------+--------------+--------------+---------------
31-
t | t | f | f | f | f | -1 | f |
32-
(1 row)
33-
34-
select
35-
rolconfig
36-
from pg_roles r
37-
where r.rolname = 'supabase_storage_admin';
38-
rolconfig
39-
------------------------------------------
40-
{search_path=storage,log_statement=none}
41-
(1 row)
42-
4316
-- storage schema tables with owners and rls policies
4417
select
4518
ns.nspname as schema_name,
@@ -123,52 +96,3 @@ order by
12396
storage | search | supabase_storage_admin
12497
(4 rows)
12598

126-
-- roles which have USAGE on the storage schema
127-
select
128-
n.nspname as schema_name,
129-
r.rolname as role_name,
130-
a.privilege_type
131-
from
132-
pg_namespace n
133-
cross join lateral aclexplode(n.nspacl) as a
134-
join
135-
pg_roles r on a.grantee = r.oid
136-
where
137-
n.nspname = 'storage'
138-
and a.privilege_type = 'USAGE'
139-
order by
140-
r.rolname;
141-
schema_name | role_name | privilege_type
142-
-------------+------------------------+----------------
143-
storage | anon | USAGE
144-
storage | authenticated | USAGE
145-
storage | dashboard_user | USAGE
146-
storage | postgres | USAGE
147-
storage | service_role | USAGE
148-
storage | supabase_admin | USAGE
149-
storage | supabase_storage_admin | USAGE
150-
(7 rows)
151-
152-
-- roles which have CREATE on the storage schema
153-
select
154-
n.nspname as schema_name,
155-
r.rolname as role_name,
156-
a.privilege_type
157-
from
158-
pg_namespace n
159-
cross join lateral aclexplode(n.nspacl) as a
160-
join
161-
pg_roles r on a.grantee = r.oid
162-
where
163-
n.nspname = 'storage'
164-
and a.privilege_type = 'CREATE'
165-
order by
166-
r.rolname;
167-
schema_name | role_name | privilege_type
168-
-------------+------------------------+----------------
169-
storage | dashboard_user | CREATE
170-
storage | postgres | CREATE
171-
storage | supabase_admin | CREATE
172-
storage | supabase_storage_admin | CREATE
173-
(4 rows)
174-

nix/tests/sql/auth.sql

-51
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ join
99
where
1010
n.nspname = 'auth';
1111

12-
-- attributes of the supabase_auth_admin
13-
select
14-
rolcreaterole ,
15-
rolcanlogin ,
16-
rolsuper ,
17-
rolinherit ,
18-
rolcreatedb ,
19-
rolreplication ,
20-
rolconnlimit ,
21-
rolbypassrls ,
22-
rolvaliduntil
23-
from pg_roles r
24-
where r.rolname = 'supabase_auth_admin';
25-
26-
select
27-
rolconfig
28-
from pg_roles r
29-
where r.rolname = 'supabase_auth_admin';
30-
3112
-- auth schema tables with owners and rls policies
3213
select
3314
ns.nspname as schema_name,
@@ -87,35 +68,3 @@ where
8768
n.nspname = 'auth'
8869
order by
8970
p.proname;
90-
91-
-- roles which have USAGE on the auth schema
92-
select
93-
n.nspname as schema_name,
94-
r.rolname as role_name,
95-
a.privilege_type
96-
from
97-
pg_namespace n
98-
cross join lateral aclexplode(n.nspacl) as a
99-
join
100-
pg_roles r on a.grantee = r.oid
101-
where
102-
n.nspname = 'auth'
103-
and a.privilege_type = 'USAGE'
104-
order by
105-
r.rolname;
106-
107-
-- roles which have CREATE on the auth schema
108-
select
109-
n.nspname as schema_name,
110-
r.rolname as role_name,
111-
a.privilege_type
112-
from
113-
pg_namespace n
114-
cross join lateral aclexplode(n.nspacl) as a
115-
join
116-
pg_roles r on a.grantee = r.oid
117-
where
118-
n.nspname = 'auth'
119-
and a.privilege_type = 'CREATE'
120-
order by
121-
r.rolname;

nix/tests/sql/default_privs.sql

-2
This file was deleted.

nix/tests/sql/roles.sql

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
-- all roles and attributes
2+
select
3+
rolname,
4+
rolcreaterole ,
5+
rolcanlogin ,
6+
rolsuper ,
7+
rolinherit ,
8+
rolcreatedb ,
9+
rolreplication ,
10+
rolconnlimit ,
11+
rolbypassrls ,
12+
rolvaliduntil
13+
from pg_roles r
14+
-- TODO: this exclusion is to maintain compat with pg17, we should cover it
15+
where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections')
16+
order by rolname;
17+
18+
select
19+
rolname,
20+
rolconfig
21+
from pg_roles r
22+
-- TODO: this exclusion is to maintain compat with pg17, we should cover it
23+
where rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections')
24+
order by rolname;
25+
26+
-- all role memberships
27+
select
28+
r.rolname as member,
29+
g.rolname as "member_of (can become)",
30+
m.admin_option
31+
from
32+
pg_roles r
33+
left join
34+
pg_auth_members m on r.oid = m.member
35+
left join
36+
pg_roles g on m.roleid = g.oid
37+
-- TODO: this exclusion is to maintain compat with pg17, we should cover it
38+
where r.rolname not in ('pg_create_subscription', 'pg_maintain', 'pg_use_reserved_connections')
39+
order by
40+
r.rolname, g.rolname;
41+
42+
-- Check all privileges of the roles on the schemas
43+
select schema_name, privilege_type, grantee, default_for
44+
from (
45+
-- ALTER DEFAULT privileges on schemas
46+
select
47+
n.nspname as schema_name,
48+
a.privilege_type,
49+
r.rolname as grantee,
50+
d.defaclrole::regrole as default_for,
51+
case when n.nspname = 'public' then 0 else 1 end as schema_order
52+
from
53+
pg_default_acl d
54+
join
55+
pg_namespace n on d.defaclnamespace = n.oid
56+
cross join lateral aclexplode(d.defaclacl) as a
57+
join
58+
pg_roles r on a.grantee = r.oid
59+
where
60+
a.privilege_type != 'MAINTAIN' -- TODO: this is to maintain compat with pg17, we should cover it
61+
union all
62+
-- explicit grant usage and create on the schemas
63+
select
64+
n.nspname as schema_name,
65+
a.privilege_type,
66+
r.rolname as grantee,
67+
n.nspowner::regrole as default_for,
68+
case when n.nspname = 'public' then 0 else 1 end as schema_order
69+
from
70+
pg_namespace n
71+
cross join lateral aclexplode(n.nspacl) as a
72+
join
73+
pg_roles r on a.grantee = r.oid
74+
where
75+
a.privilege_type in ('CREATE', 'USAGE')
76+
) sub
77+
order by schema_order, schema_name, privilege_type, grantee, default_for;

nix/tests/sql/storage.sql

-51
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ join
99
where
1010
n.nspname = 'storage';
1111

12-
-- attributes of the supabase_storage_admin
13-
select
14-
rolcreaterole ,
15-
rolcanlogin ,
16-
rolsuper ,
17-
rolinherit ,
18-
rolcreatedb ,
19-
rolreplication ,
20-
rolconnlimit ,
21-
rolbypassrls ,
22-
rolvaliduntil
23-
from pg_roles r
24-
where r.rolname = 'supabase_storage_admin';
25-
26-
select
27-
rolconfig
28-
from pg_roles r
29-
where r.rolname = 'supabase_storage_admin';
30-
3112
-- storage schema tables with owners and rls policies
3213
select
3314
ns.nspname as schema_name,
@@ -87,35 +68,3 @@ where
8768
n.nspname = 'storage'
8869
order by
8970
p.proname;
90-
91-
-- roles which have USAGE on the storage schema
92-
select
93-
n.nspname as schema_name,
94-
r.rolname as role_name,
95-
a.privilege_type
96-
from
97-
pg_namespace n
98-
cross join lateral aclexplode(n.nspacl) as a
99-
join
100-
pg_roles r on a.grantee = r.oid
101-
where
102-
n.nspname = 'storage'
103-
and a.privilege_type = 'USAGE'
104-
order by
105-
r.rolname;
106-
107-
-- roles which have CREATE on the storage schema
108-
select
109-
n.nspname as schema_name,
110-
r.rolname as role_name,
111-
a.privilege_type
112-
from
113-
pg_namespace n
114-
cross join lateral aclexplode(n.nspacl) as a
115-
join
116-
pg_roles r on a.grantee = r.oid
117-
where
118-
n.nspname = 'storage'
119-
and a.privilege_type = 'CREATE'
120-
order by
121-
r.rolname;

0 commit comments

Comments
 (0)