Skip to content

Commit 4c36749

Browse files
authored
fix: user permissions for extension schemas (#560)
* chore: grant tiger schema to postgres on creation * chore: update postgis test for tiger schema * chore: add comment to unit test * chore: verify extensions schema privileges * fix: allow postgres to grant extension permissions to other roles * chore: add grants for tiger_data * chore: add comments to explain new grants * chore: add more privilege checks for public schema
1 parent 43b730d commit 4c36749

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- These schemas are created by extension to house all tiger related functions, owned by supabase_admin
2+
grant usage on schema tiger, tiger_data to postgres with grant option;
3+
-- Give postgres permission to all existing entities, also allows postgres to grant other roles
4+
grant all on all tables in schema tiger, tiger_data to postgres with grant option;
5+
grant all on all routines in schema tiger, tiger_data to postgres with grant option;
6+
grant all on all sequences in schema tiger, tiger_data to postgres with grant option;
7+
-- Update default privileges so that new entities are also accessible by postgres
8+
alter default privileges in schema tiger, tiger_data grant all on tables to postgres with grant option;
9+
alter default privileges in schema tiger, tiger_data grant all on routines to postgres with grant option;
10+
alter default privileges in schema tiger, tiger_data grant all on sequences to postgres with grant option;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- migrate:up
2+
grant all privileges on all tables in schema extensions to postgres with grant option;
3+
grant all privileges on all routines in schema extensions to postgres with grant option;
4+
grant all privileges on all sequences in schema extensions to postgres with grant option;
5+
alter default privileges in schema extensions grant all on tables to postgres with grant option;
6+
alter default privileges in schema extensions grant all on routines to postgres with grant option;
7+
alter default privileges in schema extensions grant all on sequences to postgres with grant option;
8+
9+
-- migrate:down
10+

migrations/tests/database/privs.sql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ SELECT function_privs_are('pgsodium', 'crypto_aead_det_decrypt', array['bytea',
77
SELECT function_privs_are('pgsodium', 'crypto_aead_det_encrypt', array['bytea', 'bytea', 'uuid', 'bytea'], 'service_role', array['EXECUTE']);
88
SELECT function_privs_are('pgsodium', 'crypto_aead_det_keygen', array[]::text[], 'service_role', array['EXECUTE']);
99

10+
-- Verify public schema privileges
11+
SELECT schema_privs_are('public', 'postgres', array['CREATE', 'USAGE']);
12+
SELECT schema_privs_are('public', 'anon', array['USAGE']);
13+
SELECT schema_privs_are('public', 'authenticated', array['USAGE']);
14+
SELECT schema_privs_are('public', 'service_role', array['USAGE']);
15+
1016
set role postgres;
1117
create table test_priv();
1218
SELECT table_owner_is('test_priv', 'postgres');
1319
SELECT table_privs_are('test_priv', 'supabase_admin', array['DELETE', 'INSERT', 'REFERENCES', 'SELECT', 'TRIGGER', 'TRUNCATE', 'UPDATE']);
20+
SELECT table_privs_are('test_priv', 'postgres', array['DELETE', 'INSERT', 'REFERENCES', 'SELECT', 'TRIGGER', 'TRUNCATE', 'UPDATE']);
1421
SELECT table_privs_are('test_priv', 'anon', array['DELETE', 'INSERT', 'REFERENCES', 'SELECT', 'TRIGGER', 'TRUNCATE', 'UPDATE']);
1522
SELECT table_privs_are('test_priv', 'authenticated', array['DELETE', 'INSERT', 'REFERENCES', 'SELECT', 'TRIGGER', 'TRUNCATE', 'UPDATE']);
1623
SELECT table_privs_are('test_priv', 'service_role', array['DELETE', 'INSERT', 'REFERENCES', 'SELECT', 'TRIGGER', 'TRUNCATE', 'UPDATE']);
17-
SELECT table_privs_are('test_priv', 'postgres', array['DELETE', 'INSERT', 'REFERENCES', 'SELECT', 'TRIGGER', 'TRUNCATE', 'UPDATE']);
1824
reset role;
25+
26+
-- Verify extensions schema privileges
27+
SELECT schema_privs_are('extensions', 'postgres', array['CREATE', 'USAGE']);
28+
SELECT schema_privs_are('extensions', 'anon', array['USAGE']);
29+
SELECT schema_privs_are('extensions', 'authenticated', array['USAGE']);
30+
SELECT schema_privs_are('extensions', 'service_role', array['USAGE']);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
11
BEGIN;
22
create extension if not exists postgis_sfcgal with schema "extensions" cascade;
33
ROLLBACK;
4+
5+
BEGIN;
6+
-- create postgis tiger as supabase_admin
7+
create extension if not exists address_standardizer with schema extensions;
8+
create extension if not exists postgis_tiger_geocoder cascade;
9+
10+
-- \ir ansible/files/postgresql_extension_custom_scripts/postgis_tiger_geocoder/after-create.sql
11+
grant usage on schema tiger, tiger_data to postgres with grant option;
12+
grant all privileges on all tables in schema tiger, tiger_data to postgres with grant option;
13+
grant all privileges on all routines in schema tiger, tiger_data to postgres with grant option;
14+
grant all privileges on all sequences in schema tiger, tiger_data to postgres with grant option;
15+
alter default privileges in schema tiger, tiger_data grant all on tables to postgres with grant option;
16+
alter default privileges in schema tiger, tiger_data grant all on routines to postgres with grant option;
17+
alter default privileges in schema tiger, tiger_data grant all on sequences to postgres with grant option;
18+
19+
-- postgres role should have access
20+
set local role postgres;
21+
select tiger.pprint_addy(tiger.pagc_normalize_address('710 E Ben White Blvd, Austin, TX 78704'));
22+
23+
-- other roles can be granted access
24+
grant usage on schema tiger, tiger_data to authenticated;
25+
grant select on all tables in schema tiger, tiger_data to authenticated;
26+
grant execute on all routines in schema tiger, tiger_data to authenticated;
27+
28+
-- authenticated role should have access now
29+
set local role authenticated;
30+
select tiger.pprint_addy(tiger.pagc_normalize_address('710 E Ben White Blvd, Austin, TX 78704'));
31+
ROLLBACK;
32+
33+
BEGIN;
34+
-- address standardizer creates a table in extensions schema, owned by supabase_admin
35+
create extension if not exists address_standardizer_data_us with schema extensions;
36+
-- postgres role should have access
37+
set local role postgres;
38+
select * from extensions.us_lex;
39+
ROLLBACK;

migrations/tests/test.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CREATE EXTENSION IF NOT EXISTS pgtap;
55

66
BEGIN;
77

8-
SELECT plan(25);
8+
SELECT plan(33);
99

1010
\ir fixtures.sql
1111
\ir database/test.sql

0 commit comments

Comments
 (0)