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
12 changes: 8 additions & 4 deletions Dockerfile-multigres
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,15 @@ ENV POSTGRES_CONFIG_TEMPLATE_PATH=/etc/pgctld-custom/postgresql.conf.tmpl
# Strip extensions absent from pg17 vanilla build
RUN sed -i 's/ timescaledb,//g; s/ plv8,//g' /etc/postgresql-custom/supautils.conf

COPY --chown=postgres:postgres docker/pgctld/multigres-roles.sql /docker-entrypoint-initdb.d/multigres-roles.sql

# Generate a single SQL manifest that pgctld runs via --init-db-sql-file after initdb.
Comment thread
niconosenzo marked this conversation as resolved.
Resolved
# Creates the postgres role, runs init-scripts as postgres (matching migrate.sh),
# then runs migrations as supabase_admin.
# Creates the postgres and multigres_probe roles (multigres-roles.sql), runs
# init-scripts as postgres (matching migrate.sh), then runs migrations as supabase_admin.
RUN set -e && \
manifest=/docker-entrypoint-initdb.d/multigres-init.sql && \
printf -- "-- Auto-generated: run init-scripts and migrations after initdb\n" > "$manifest" && \
printf "DO \$\$ BEGIN\n IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'postgres') THEN\n CREATE ROLE postgres SUPERUSER LOGIN;\n END IF;\nEND \$\$;\n" >> "$manifest" && \
printf '\\ir multigres-roles.sql\n' >> "$manifest" && \
printf "ALTER DATABASE postgres OWNER TO postgres;\n\n" >> "$manifest" && \
printf "SET SESSION AUTHORIZATION postgres;\n" >> "$manifest" && \
for f in $(ls /docker-entrypoint-initdb.d/init-scripts/*.sql 2>/dev/null | sort); do \
Expand Down Expand Up @@ -270,11 +272,13 @@ COPY docker/pgctld/orioledb-postgresql.conf.tmpl /etc/pgctld-custom/orioledb-pos
COPY --chmod=755 docker/pgctld/pgctld-wrapper.sh /usr/local/bin/pgctld
ENV POSTGRES_CONFIG_TEMPLATE_PATH=/etc/pgctld-custom/orioledb-postgresql.conf.tmpl

COPY --chown=postgres:postgres docker/pgctld/multigres-roles.sql /docker-entrypoint-initdb.d/multigres-roles.sql

# Regenerate manifest after orioledb added 00-pre-init.sql to init-scripts/
RUN set -e && \
manifest=/docker-entrypoint-initdb.d/multigres-init.sql && \
printf -- "-- Auto-generated: run init-scripts and migrations after initdb\n" > "$manifest" && \
printf "DO \$\$ BEGIN\n IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'postgres') THEN\n CREATE ROLE postgres SUPERUSER LOGIN;\n END IF;\nEND \$\$;\n" >> "$manifest" && \
printf '\\ir multigres-roles.sql\n' >> "$manifest" && \
printf "ALTER DATABASE postgres OWNER TO postgres;\n\n" >> "$manifest" && \
printf "SET SESSION AUTHORIZATION postgres;\n" >> "$manifest" && \
for f in $(ls /docker-entrypoint-initdb.d/init-scripts/*.sql 2>/dev/null | sort); do \
Expand Down
2 changes: 1 addition & 1 deletion ansible/files/postgresql_config/supautils.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ 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_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, multigres_probe'
supautils.hint_roles = 'anon, authenticated, service_role'
17 changes: 17 additions & 0 deletions docker/pgctld/multigres-roles.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- multigres_probe gets no password here. The deployment sets one after boot,
-- and pg_hba is scram-sha-256 only, so the role can't log in before that.

DO $$ BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'postgres') THEN
CREATE ROLE postgres SUPERUSER LOGIN;
END IF;
END $$;

DO $$ BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'multigres_probe') THEN
CREATE ROLE multigres_probe;
END IF;
END $$;

ALTER ROLE multigres_probe LOGIN NOBYPASSRLS CONNECTION LIMIT 2;
ALTER ROLE multigres_probe SET statement_timeout = '2s';
40 changes: 37 additions & 3 deletions nix/packages/docker-image-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,42 @@ writeShellApplication {
fi
log_info " ✓ supautils.conf policy loaded (policy_grants set)"

# 7. multigres_probe must come out of the init manifest with the
# attributes the probe relies on: LOGIN, no BYPASSRLS, connection
# limit 2, statement_timeout 2s, and the name must be reserved so
# non-superusers cannot alter or drop it.
local probe_role
probe_role=$(docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" "$container" sh -c "
psql -U $POSTGRES_USER -d postgres -h $pooler_dir/pg_sockets \
-tAc \"SELECT rolcanlogin, rolsuper, rolbypassrls, rolconnlimit FROM pg_roles WHERE rolname = 'multigres_probe';\" 2>&1") || true
if [[ "$probe_role" != "t|f|f|2" ]]; then
log_error " multigres_probe attributes wrong (got: '$probe_role', want: 't|f|f|2')"
exit 1
fi
log_info " ✓ multigres_probe is LOGIN NOSUPERUSER NOBYPASSRLS CONNECTION LIMIT 2"

local probe_timeout
probe_timeout=$(docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" "$container" sh -c "
psql -U $POSTGRES_USER -d postgres -h $pooler_dir/pg_sockets \
-tAc \"SELECT setconfig FROM pg_db_role_setting WHERE setrole = 'multigres_probe'::regrole AND setdatabase = 0;\" 2>&1") || true
if [[ "$probe_timeout" != "{statement_timeout=2s}" ]]; then
log_error " multigres_probe statement_timeout not set (got: '$probe_timeout')"
exit 1
fi
log_info " ✓ multigres_probe statement_timeout = 2s"

local reserved_roles
reserved_roles=$(docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" "$container" sh -c "
psql -U $POSTGRES_USER -d postgres -h $pooler_dir/pg_sockets \
-tAc \"SHOW supautils.reserved_roles;\" 2>&1") || true
if ! echo "$reserved_roles" | grep -qw "multigres_probe"; then
log_error " multigres_probe not in supautils.reserved_roles (got: $reserved_roles)"
exit 1
fi
log_info " ✓ multigres_probe is in supautils.reserved_roles"

if [[ "$version" == "multigres-orioledb-17" ]]; then
# 7. /usr/local/bin/pgctld must be a wrapper script (not a plain symlink)
# 8. /usr/local/bin/pgctld must be a wrapper script (not a plain symlink)
local first_line
first_line=$(docker exec "$container" sh -c "head -1 /usr/local/bin/pgctld")
if [[ "$first_line" != "#!/bin/sh" ]]; then
Expand All @@ -378,7 +412,7 @@ writeShellApplication {
fi
log_info " ✓ /usr/local/bin/pgctld is a wrapper script"

# 8. shared_preload_libraries must include orioledb — injected by wrapper, no flags needed
# 9. shared_preload_libraries must include orioledb — injected by wrapper, no flags needed
local spl
spl=$(docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" "$container" sh -c "
psql -U $POSTGRES_USER -d postgres -h $pooler_dir/pg_sockets \
Expand All @@ -390,7 +424,7 @@ writeShellApplication {
fi
log_info " ✓ shared_preload_libraries contains orioledb"

# 9. default_table_access_method must be orioledb
# 10. default_table_access_method must be orioledb
local tam
tam=$(docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" "$container" sh -c "
psql -U $POSTGRES_USER -d postgres -h $pooler_dir/pg_sockets \
Expand Down
17 changes: 17 additions & 0 deletions nix/tests/expected/supautils_reserved_roles.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@ SELECT rolname FROM pg_roles WHERE rolname = 'supabase_privileged_role';
(1 row)

ROLLBACK;
-- verify non-superuser postgres role cannot claim the reserved multigres_probe
-- name. The role only exists on Multigres images, so CREATE must be refused
-- by name before the role exists anywhere else.
BEGIN;
SET ROLE postgres;
SAVEPOINT before_create;
CREATE ROLE multigres_probe;
ERROR: "multigres_probe" is a reserved role, only superusers can modify it
ROLLBACK TO SAVEPOINT before_create;
RESET ROLE;
SELECT count(*) FROM pg_roles WHERE rolname = 'multigres_probe';
count
-------
0
(1 row)

ROLLBACK;
17 changes: 17 additions & 0 deletions nix/tests/sql/supautils_reserved_roles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ RESET ROLE;
SELECT rolname FROM pg_roles WHERE rolname = 'supabase_privileged_role';

ROLLBACK;

-- verify non-superuser postgres role cannot claim the reserved multigres_probe
-- name. The role only exists on Multigres images, so CREATE must be refused
-- by name before the role exists anywhere else.
BEGIN;

SET ROLE postgres;

SAVEPOINT before_create;
CREATE ROLE multigres_probe;
ROLLBACK TO SAVEPOINT before_create;

RESET ROLE;

SELECT count(*) FROM pg_roles WHERE rolname = 'multigres_probe';

ROLLBACK;
Loading