Skip to content

Commit ef851d1

Browse files
authored
fix: evtrigs ownership (#1489)
Fixes #1437 + Moves migrations/db/init-scripts to migrations/db/migrations. + Make initial migrations idempotent. + Adds test for event triggers. + explicit alter default privs using the postgres role
1 parent 948b5fe commit ef851d1

14 files changed

+239
-116
lines changed

flake.nix

+4-4
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,6 @@
807807
exit 1
808808
fi
809809
810-
echo "Running migrations tests"
811-
pg_prove -p 5435 -U supabase_admin -h localhost -d postgres -v ${./migrations/tests}/test.sql
812-
813810
mkdir -p $out/regression_output
814811
if ! pg_regress \
815812
--use-existing \
@@ -825,12 +822,15 @@
825822
exit 1
826823
fi
827824
825+
echo "Running migrations tests"
826+
pg_prove -p 5435 -U supabase_admin -h localhost -d postgres -v ${./migrations/tests}/test.sql
827+
828828
# Copy logs to output
829829
for logfile in $(find /tmp -name postgresql.log -type f); do
830830
cp "$logfile" $out/postgresql.log
831831
done
832832
exit 0
833-
'';
833+
'';
834834
in
835835
rec {
836836
# The list of all packages that can be built with 'nix build'. The list

migrations/README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ nix run github:supabase/postgres/mybranch#dbmate-tool -- --version 15
4242

4343
aiming to provide a single source of truth for migrations on the platform that can be depended upon by those components. For more information on goals see [the RFC](https://www.notion.so/supabase/Centralize-SQL-Migrations-cd3847ae027d4f2bba9defb2cc82f69a)
4444

45-
46-
4745
## How it was Created
4846

4947
Migrations were pulled (in order) from:
@@ -53,9 +51,8 @@ Migrations were pulled (in order) from:
5351

5452
For compatibility with hosted projects, we include [migrate.sh](migrate.sh) that executes migrations in the same order as ami build:
5553

56-
1. Run all `db/init-scripts` with `postgres` superuser role.
57-
2. Run all `db/migrations` with `supabase_admin` superuser role.
58-
3. Finalize role passwords with `/etc/postgresql.schema.sql` if present.
54+
1. Run all `db/migrations` with `supabase_admin` superuser role.
55+
2. Finalize role passwords with `/etc/postgresql.schema.sql` if present.
5956

6057
Additionally, [supabase/postgres](https://github.com/supabase/postgres/blob/develop/ansible/playbook-docker.yml#L9) image contains several migration scripts to configure default extensions. These are run first by docker entrypoint and included in ami by ansible.
6158

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

-57
This file was deleted.

migrations/db/migrate.sh

-7
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ begin
3838
end if;
3939
end \$\$
4040
EOSQL
41-
# run init scripts as postgres user
42-
for sql in "$db"/init-scripts/*.sql; do
43-
echo "$0: running $sql"
44-
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -f "$sql"
45-
done
4641
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
4742
# run migrations as super user - postgres user demoted in post-setup
4843
for sql in "$db"/migrations/*.sql; do
@@ -54,8 +49,6 @@ else
5449
create role postgres superuser login password '$PGPASSWORD';
5550
alter database postgres owner to postgres;
5651
EOSQL
57-
# run init scripts as postgres user
58-
DBMATE_MIGRATIONS_DIR="$db/init-scripts" DATABASE_URL="postgres://postgres:$connect" dbmate --no-dump-schema migrate
5952
psql -v ON_ERROR_STOP=1 --no-password --no-psqlrc -U postgres -c "ALTER USER supabase_admin WITH PASSWORD '$PGPASSWORD'"
6053
# run migrations as super user - postgres user demoted in post-setup
6154
DBMATE_MIGRATIONS_DIR="$db/migrations" DATABASE_URL="postgres://supabase_admin:$connect" dbmate --no-dump-schema migrate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
-- migrate:up
2+
3+
-- Set up realtime
4+
-- 1. Create publication supabase_realtime if it doesn't already exist
5+
do $$
6+
begin
7+
if not exists (
8+
select 1 from pg_catalog.pg_publication
9+
where pubname = 'supabase_realtime'
10+
)
11+
then
12+
create publication supabase_realtime;
13+
end if;
14+
end
15+
$$;
16+
17+
-- Supabase super admin
18+
alter user supabase_admin with superuser createdb createrole replication bypassrls;
19+
20+
-- Supabase replication user
21+
do $$
22+
begin
23+
if not exists (
24+
select 1 from pg_roles
25+
where rolname = 'supabase_replication_admin'
26+
)
27+
then
28+
create user supabase_replication_admin with
29+
login
30+
replication;
31+
end if;
32+
end
33+
$$;
34+
35+
-- Supabase read-only user
36+
do $$
37+
begin
38+
if not exists (
39+
select 1 from pg_roles
40+
where rolname = 'supabase_read_only_user'
41+
)
42+
then
43+
create role supabase_read_only_user with
44+
login
45+
bypassrls;
46+
end if;
47+
end
48+
$$;
49+
grant pg_read_all_data to supabase_read_only_user;
50+
51+
-- Extension namespacing
52+
create schema if not exists extensions;
53+
create extension if not exists "uuid-ossp" with schema extensions;
54+
create extension if not exists pgcrypto with schema extensions;
55+
create extension if not exists pgjwt with schema extensions;
56+
57+
-- Set up auth roles for the developer
58+
do $$
59+
begin
60+
if not exists (
61+
select 1 from pg_roles
62+
where rolname = 'anon'
63+
)
64+
then
65+
create role anon nologin noinherit;
66+
end if;
67+
end
68+
$$;
69+
70+
-- "logged in" user: web_user, app_user, etc
71+
do $$
72+
begin
73+
if not exists (
74+
select 1 from pg_roles
75+
where rolname = 'authenticated'
76+
)
77+
then
78+
create role authenticated nologin noinherit;
79+
end if;
80+
end
81+
$$;
82+
83+
-- allow developers to create JWT's that bypass their policies
84+
do $$
85+
begin
86+
if not exists (
87+
select 1 from pg_roles
88+
where rolname = 'service_role'
89+
)
90+
then
91+
create role service_role nologin noinherit bypassrls;
92+
end if;
93+
end
94+
$$;
95+
96+
do $$
97+
begin
98+
if not exists (
99+
select 1 from pg_roles
100+
where rolname = 'authenticator'
101+
)
102+
then
103+
create role authenticator login noinherit;
104+
end if;
105+
end
106+
$$;
107+
108+
109+
grant anon to authenticator;
110+
grant authenticated to authenticator;
111+
grant service_role to authenticator;
112+
grant supabase_admin to authenticator;
113+
114+
-- These are required so that the users receive grants whenever "postgres" creates tables/function
115+
grant usage on schema public to postgres, anon, authenticated, service_role;
116+
alter default privileges for role postgres in schema public grant all on tables to postgres, anon, authenticated, service_role;
117+
alter default privileges for role postgres in schema public grant all on functions to postgres, anon, authenticated, service_role;
118+
alter default privileges for role postgres in schema public grant all on sequences to postgres, anon, authenticated, service_role;
119+
120+
-- Allow Extensions to be used in the API
121+
grant usage on schema extensions to postgres, anon, authenticated, service_role;
122+
123+
-- Set up namespacing
124+
alter user supabase_admin SET search_path TO public, extensions; -- don't include the "auth" schema
125+
126+
-- These are required so that the users receive grants whenever "supabase_admin" creates tables/function
127+
alter default privileges for user supabase_admin in schema public grant all
128+
on sequences to postgres, anon, authenticated, service_role;
129+
alter default privileges for user supabase_admin in schema public grant all
130+
on tables to postgres, anon, authenticated, service_role;
131+
alter default privileges for user supabase_admin in schema public grant all
132+
on functions to postgres, anon, authenticated, service_role;
133+
134+
-- Set short statement/query timeouts for API roles
135+
alter role anon set statement_timeout = '3s';
136+
alter role authenticated set statement_timeout = '8s';
137+
138+
-- migrate:down

migrations/db/init-scripts/00000000000001-auth-schema.sql migrations/db/migrations/00000000000001-auth-schema.sql

+26-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION supabase_admin;
44

55
-- auth.users definition
66

7-
CREATE TABLE auth.users (
7+
CREATE TABLE IF NOT EXISTS auth.users (
88
instance_id uuid NULL,
99
id uuid NOT NULL UNIQUE,
1010
aud varchar(255) NULL,
@@ -28,13 +28,13 @@ CREATE TABLE auth.users (
2828
updated_at timestamptz NULL,
2929
CONSTRAINT users_pkey PRIMARY KEY (id)
3030
);
31-
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
32-
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
31+
CREATE INDEX IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
32+
CREATE INDEX IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
3333
comment on table auth.users is 'Auth: Stores user login data within a secure schema.';
3434

3535
-- auth.refresh_tokens definition
3636

37-
CREATE TABLE auth.refresh_tokens (
37+
CREATE TABLE IF NOT EXISTS auth.refresh_tokens (
3838
instance_id uuid NULL,
3939
id bigserial NOT NULL,
4040
"token" varchar(255) NULL,
@@ -44,14 +44,14 @@ CREATE TABLE auth.refresh_tokens (
4444
updated_at timestamptz NULL,
4545
CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id)
4646
);
47-
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
48-
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
49-
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
47+
CREATE INDEX IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
48+
CREATE INDEX IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
49+
CREATE INDEX IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
5050
comment on table auth.refresh_tokens is 'Auth: Store of tokens used to refresh JWT tokens once they expire.';
5151

5252
-- auth.instances definition
5353

54-
CREATE TABLE auth.instances (
54+
CREATE TABLE IF NOT EXISTS auth.instances (
5555
id uuid NOT NULL,
5656
uuid uuid NULL,
5757
raw_base_config text NULL,
@@ -63,32 +63,34 @@ comment on table auth.instances is 'Auth: Manages users across multiple sites.';
6363

6464
-- auth.audit_log_entries definition
6565

66-
CREATE TABLE auth.audit_log_entries (
66+
CREATE TABLE IF NOT EXISTS auth.audit_log_entries (
6767
instance_id uuid NULL,
6868
id uuid NOT NULL,
6969
payload json NULL,
7070
created_at timestamptz NULL,
7171
CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id)
7272
);
73-
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
73+
CREATE INDEX IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
7474
comment on table auth.audit_log_entries is 'Auth: Audit trail for user actions.';
7575

7676
-- auth.schema_migrations definition
7777

78-
CREATE TABLE auth.schema_migrations (
78+
CREATE TABLE IF NOT EXISTS auth.schema_migrations (
7979
"version" varchar(255) NOT NULL,
8080
CONSTRAINT schema_migrations_pkey PRIMARY KEY ("version")
8181
);
8282
comment on table auth.schema_migrations is 'Auth: Manages updates to the auth system.';
8383

84+
-- insert migrations if they do not yet exist
8485
INSERT INTO auth.schema_migrations (version)
8586
VALUES ('20171026211738'),
8687
('20171026211808'),
8788
('20171026211834'),
8889
('20180103212743'),
8990
('20180108183307'),
9091
('20180119214651'),
91-
('20180125194653');
92+
('20180125194653')
93+
ON CONFLICT DO NOTHING;
9294

9395
-- Gets the User ID from the request cookie
9496
create or replace function auth.uid() returns uuid as $$
@@ -109,8 +111,18 @@ $$ language sql stable;
109111
GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role;
110112

111113
-- Supabase super admin
112-
CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
113-
GRANT ALL PRIVILEGES ON SCHEMA auth TO supabase_auth_admin;
114+
do $$
115+
begin
116+
if not exists (
117+
select 1 from pg_roles
118+
where rolname = 'supabase_auth_admin'
119+
)
120+
then
121+
CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
122+
end if;
123+
end
124+
$$;
125+
114126
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO supabase_auth_admin;
115127
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO supabase_auth_admin;
116128
ALTER USER supabase_auth_admin SET search_path = "auth";

0 commit comments

Comments
 (0)