From b0e395f495fa7680561146d8d88b0f974b2fe4e8 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Tue, 25 Apr 2023 01:49:56 -0500 Subject: [PATCH] nix: no SUPERUSER for connection role Change :USER to :PGUSER in SQL scripts --- nix/tools/withTools.nix | 17 ++++++++++++----- test/io/fixtures.sql | 4 ++-- test/load/fixtures.sql | 2 +- test/spec/SpecHelper.hs | 2 +- test/spec/fixtures/roles.sql | 3 +-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/nix/tools/withTools.nix b/nix/tools/withTools.nix index eba52d606..de36204d0 100644 --- a/nix/tools/withTools.nix +++ b/nix/tools/withTools.nix @@ -15,11 +15,14 @@ let withTmpDb = { name, postgresql }: - let commandName = "postgrest-with-${name}"; in + let + commandName = "postgrest-with-${name}"; + superuserRole = "postgres"; + in checkedShellScript { name = commandName; - docs = "Run the given command in a temporary database with ${name}"; + docs = "Run the given command in a temporary database with ${name}. If you wish to mutate the database, login with the '${superuserRole}' role."; args = [ "ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from], [test/spec/fixtures/load.sql])" @@ -66,7 +69,8 @@ let log "Initializing database cluster..." # We try to make the database cluster as independent as possible from the host # by specifying the timezone, locale and encoding. - PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER" --auth=trust \ + # initdb -U creates a superuser(man initdb) + PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "${superuserRole}" --auth=trust \ >> "$setuplog" log "Starting the database cluster..." @@ -82,8 +86,11 @@ let } trap stop EXIT - log "Loading fixtures..." - psql -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog" + log "Creating a minimally privileged $PGUSER connection role..." + createuser "$PGUSER" -U "${superuserRole}" --host="$tmpdir/socket" --no-createdb --no-inherit --no-superuser --no-createrole --no-replication --login + + log "Loading fixtures under the ${superuserRole} role..." + psql -U "${superuserRole}" -v PGUSER="$PGUSER" -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog" log "Done. Running command..." diff --git a/test/io/fixtures.sql b/test/io/fixtures.sql index f96237c74..638466d24 100644 --- a/test/io/fixtures.sql +++ b/test/io/fixtures.sql @@ -4,7 +4,7 @@ set search_path to public; CREATE ROLE postgrest_test_anonymous; -ALTER ROLE :USER SET pgrst.db_anon_role = 'postgrest_test_anonymous'; +ALTER ROLE :PGUSER SET pgrst.db_anon_role = 'postgrest_test_anonymous'; CREATE ROLE postgrest_test_author; @@ -14,7 +14,7 @@ alter role postgrest_test_serializable set default_transaction_isolation = 'seri CREATE ROLE postgrest_test_repeatable_read; alter role postgrest_test_repeatable_read set default_transaction_isolation = 'REPEATABLE READ'; -GRANT postgrest_test_anonymous, postgrest_test_author, postgrest_test_serializable, postgrest_test_repeatable_read TO :USER; +GRANT postgrest_test_anonymous, postgrest_test_author, postgrest_test_serializable, postgrest_test_repeatable_read TO :PGUSER; CREATE SCHEMA v1; GRANT USAGE ON SCHEMA v1 TO postgrest_test_anonymous; diff --git a/test/load/fixtures.sql b/test/load/fixtures.sql index 48862048f..72cf1e3ec 100644 --- a/test/load/fixtures.sql +++ b/test/load/fixtures.sql @@ -1,5 +1,5 @@ CREATE ROLE postgrest_test_anonymous; -GRANT postgrest_test_anonymous TO :USER; +GRANT postgrest_test_anonymous TO :PGUSER; CREATE SCHEMA test; -- PUT+PATCH target needs one record and column to modify diff --git a/test/spec/SpecHelper.hs b/test/spec/SpecHelper.hs index 64d09b3b3..1c7d49bbe 100644 --- a/test/spec/SpecHelper.hs +++ b/test/spec/SpecHelper.hs @@ -211,7 +211,7 @@ testObservabilityCfg = baseCfg { configServerTraceHeader = Just $ mk "X-Request- analyzeTable :: Text -> IO () analyzeTable tableName = - void $ readProcess "psql" ["--set", "ON_ERROR_STOP=1", "-a", "-c", toS $ "ANALYZE test.\"" <> tableName <> "\""] [] + void $ readProcess "psql" ["-U", "postgres", "--set", "ON_ERROR_STOP=1", "-a", "-c", toS $ "ANALYZE test.\"" <> tableName <> "\""] [] rangeHdrs :: ByteRange -> [Header] rangeHdrs r = [rangeUnit, (hRange, renderByteRange r)] diff --git a/test/spec/fixtures/roles.sql b/test/spec/fixtures/roles.sql index 93b962d83..a628bf1e0 100644 --- a/test/spec/fixtures/roles.sql +++ b/test/spec/fixtures/roles.sql @@ -1,7 +1,6 @@ -\set AUTHENTICATOR current_user DROP ROLE IF EXISTS postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author; CREATE ROLE postgrest_test_anonymous; CREATE ROLE postgrest_test_default_role; CREATE ROLE postgrest_test_author; -GRANT postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author TO :USER; +GRANT postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author TO :PGUSER;