postgresai 0.18.0-dev.1 → 0.18.0-dev.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -17
- package/dist/bin/postgres-ai.js +7335 -8921
- package/dist/sql/03.permissions.sql +33 -23
- package/dist/sql/06.helpers.sql +13 -0
- package/dist/sql/uninit/01.helpers.sql +3 -0
- package/dist/sql/uninit/02.permissions.sql +77 -21
- package/dist/sql/uninit/03.role.sql +5 -2
- package/package.json +1 -1
- package/sql/03.permissions.sql +33 -23
- package/sql/06.helpers.sql +13 -0
- package/sql/uninit/01.helpers.sql +3 -0
- package/sql/uninit/02.permissions.sql +77 -21
- package/sql/uninit/03.role.sql +5 -2
|
@@ -7,14 +7,10 @@ grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
|
|
|
7
7
|
grant pg_monitor to {{ROLE_IDENT}};
|
|
8
8
|
grant select on pg_catalog.pg_index to {{ROLE_IDENT}};
|
|
9
9
|
|
|
10
|
-
--
|
|
11
|
-
--
|
|
12
|
-
--
|
|
13
|
-
--
|
|
14
|
-
-- in the pg_stat_statements metric. pg_monitor already implies pg_read_all_stats;
|
|
15
|
-
-- granting it explicitly documents the dependency and keeps the metric working if the
|
|
16
|
-
-- pg_monitor grant above is ever narrowed. Idempotent.
|
|
17
|
-
grant pg_read_all_stats to {{ROLE_IDENT}};
|
|
10
|
+
-- Cluster-wide pg_stat_statements collection needs pg_read_all_stats (without it, other
|
|
11
|
+
-- users' rows have queryid and query text redacted to NULL). pg_monitor includes it, so it
|
|
12
|
+
-- is not granted directly: on PostgreSQL 16+ that needs ADMIN OPTION on pg_read_all_stats,
|
|
13
|
+
-- which Supabase's `postgres` role does not have (it has ADMIN OPTION on pg_monitor only).
|
|
18
14
|
|
|
19
15
|
-- Create postgres_ai schema for our objects
|
|
20
16
|
-- Using IF NOT EXISTS for idempotency - prepare-db can be run multiple times
|
|
@@ -44,7 +40,7 @@ grant usage on schema public to {{ROLE_IDENT}};
|
|
|
44
40
|
-- Grant access to the schema where pg_stat_statements is installed.
|
|
45
41
|
-- Some providers (e.g., Supabase) install extensions in a separate 'extensions' schema
|
|
46
42
|
-- rather than pg_catalog. This DO block detects the schema and grants USAGE if needed.
|
|
47
|
-
do
|
|
43
|
+
do {{DO_QUOTE}}
|
|
48
44
|
declare
|
|
49
45
|
ext_schema text;
|
|
50
46
|
begin
|
|
@@ -55,32 +51,46 @@ begin
|
|
|
55
51
|
|
|
56
52
|
-- Only grant if extension exists and is in a non-standard schema
|
|
57
53
|
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
58
|
-
execute format('grant usage on schema %I to {{
|
|
54
|
+
execute format('grant usage on schema %I to %I', ext_schema, {{ROLE_LITERAL}});
|
|
59
55
|
end if;
|
|
60
|
-
end
|
|
56
|
+
end {{DO_QUOTE}};
|
|
61
57
|
|
|
62
|
-
-- [SEARCH_PATH_BLOCK_START]
|
|
63
|
-
--
|
|
64
|
-
|
|
58
|
+
-- [SEARCH_PATH_BLOCK_START] Resolve built-ins from pg_catalog first; keep pg_temp last.
|
|
59
|
+
-- Do not include the role-named schema: it may be writable by that role.
|
|
60
|
+
-- public stays for pg_stat_statements. Residual risk: a function in public with
|
|
61
|
+
-- an exact-type match (e.g. public.quote_ident(name)) still beats pg_catalog.
|
|
62
|
+
-- The path for this database includes its pg_stat_statements schema if it is in a non-standard location.
|
|
63
|
+
do {{DO_QUOTE}}
|
|
65
64
|
declare
|
|
66
65
|
ext_schema text;
|
|
67
|
-
|
|
66
|
+
role_path constant text := 'pg_catalog, postgres_ai, public, pg_temp';
|
|
67
|
+
db_path text := role_path;
|
|
68
68
|
begin
|
|
69
|
+
-- This block runs as the admin: resolve its operators and functions from pg_catalog only
|
|
70
|
+
-- (CVE-2018-1058). The pin is inside the block: a DO block always runs in a transaction,
|
|
71
|
+
-- also in psql autocommit.
|
|
72
|
+
set local search_path = pg_catalog, pg_temp;
|
|
69
73
|
-- Detect pg_stat_statements extension schema
|
|
70
74
|
select n.nspname into ext_schema
|
|
71
75
|
from pg_extension e
|
|
72
76
|
join pg_namespace n on e.extnamespace = n.oid
|
|
73
77
|
where e.extname = 'pg_stat_statements';
|
|
74
78
|
|
|
75
|
-
-- Build search_path: include extension schema if in non-standard location
|
|
76
79
|
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
77
|
-
|
|
78
|
-
else
|
|
79
|
-
sp := 'postgres_ai, "$user", public, pg_catalog';
|
|
80
|
+
db_path := 'pg_catalog, postgres_ai, ' || quote_ident(ext_schema) || ', public, pg_temp';
|
|
80
81
|
end if;
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
-- Role-wide value: for databases that prepare-db did not run in. All databases share
|
|
84
|
+
-- this one row, so write it only when it holds another value.
|
|
85
|
+
if not exists (
|
|
86
|
+
select from pg_catalog.pg_db_role_setting
|
|
87
|
+
where setrole = (select oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}})
|
|
88
|
+
and setdatabase = 0
|
|
89
|
+
and ('search_path=' || role_path) = any (setconfig)
|
|
90
|
+
) then
|
|
91
|
+
execute format('alter user %I set search_path = %s', {{ROLE_LITERAL}}, role_path);
|
|
92
|
+
end if;
|
|
93
|
+
-- This database's value: other databases keep their own pg_stat_statements schema.
|
|
94
|
+
execute format('alter user %I in database %I set search_path = %s', {{ROLE_LITERAL}}, pg_catalog.current_database(), db_path);
|
|
95
|
+
end {{DO_QUOTE}};
|
|
84
96
|
-- [SEARCH_PATH_BLOCK_END]
|
|
85
|
-
|
|
86
|
-
|
package/dist/sql/06.helpers.sql
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
-- Helper functions for postgres_ai monitoring user (template-filled by cli/lib/init.ts)
|
|
2
2
|
|
|
3
|
+
-- Remove the retired helper from databases prepared by older CLI versions.
|
|
4
|
+
-- Only its owner or a superuser can drop it: anyone else gets a warning here. Releases
|
|
5
|
+
-- installed table_describe with the same owner, so replacing it below then fails.
|
|
6
|
+
do {{DO_QUOTE}}
|
|
7
|
+
begin
|
|
8
|
+
-- Runs as the admin: resolve names from pg_catalog only (CVE-2018-1058).
|
|
9
|
+
set local search_path = pg_catalog, pg_temp;
|
|
10
|
+
drop function if exists postgres_ai.explain_generic(text, text, text);
|
|
11
|
+
exception when insufficient_privilege then
|
|
12
|
+
raise warning 'postgres_ai: legacy SECURITY DEFINER helper was NOT removed: %', sqlerrm
|
|
13
|
+
using hint = 'Its owner or a superuser must drop it, then re-run prepare-db.';
|
|
14
|
+
end {{DO_QUOTE}};
|
|
15
|
+
|
|
3
16
|
/*
|
|
4
17
|
* table_describe
|
|
5
18
|
*
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
-- Drop helper functions created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
-- Run before dropping the postgres_ai schema.
|
|
3
3
|
|
|
4
|
+
-- Objects in other schemas (for example, a planted public.format) must not run as the admin.
|
|
5
|
+
set local search_path = pg_catalog, pg_temp;
|
|
6
|
+
|
|
4
7
|
drop function if exists postgres_ai.explain_generic(text, text, text);
|
|
5
8
|
drop function if exists postgres_ai.table_describe(text);
|
|
@@ -1,30 +1,86 @@
|
|
|
1
1
|
-- Revoke permissions and drop objects created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
|
|
3
|
+
-- Objects in other schemas (for example, a planted public.=) must not run as the admin.
|
|
4
|
+
set local search_path = pg_catalog, pg_temp;
|
|
5
|
+
-- The CLI reads the "unprepare-db:" WARNINGs. A higher setting would hide them.
|
|
6
|
+
set local client_min_messages = warning;
|
|
7
|
+
|
|
3
8
|
-- Drop the postgres_ai.pg_statistic view
|
|
4
9
|
drop view if exists postgres_ai.pg_statistic;
|
|
5
10
|
|
|
6
|
-
--
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
--
|
|
10
|
-
--
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
end $$;
|
|
11
|
+
-- Revoke only the grants that the role holds. The postgres_ai schema stays
|
|
12
|
+
-- because it may contain customer objects. The pg_read_all_stats membership
|
|
13
|
+
-- comes from older prepare-db versions. The functions come from optional
|
|
14
|
+
-- permissions (04, 05). A non-superuser admin cannot revoke a grant that a
|
|
15
|
+
-- superuser made. Pass 1 revokes. Pass 2 sends one "grant left" WARNING for
|
|
16
|
+
-- each grant that stays. The CLI shows these WARNINGs and exits 1.
|
|
17
|
+
do {{DO_QUOTE}}
|
|
18
|
+
declare
|
|
19
|
+
role_oid oid;
|
|
20
|
+
stmt text;
|
|
21
|
+
begin
|
|
22
|
+
select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
|
|
23
|
+
if role_oid is null then
|
|
24
|
+
return; -- The monitoring role was already removed.
|
|
25
|
+
end if;
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
for pass in 1..2 loop
|
|
28
|
+
for stmt in
|
|
29
|
+
-- PG 16+ keeps one row for each grantor. A revoke without GRANTED BY
|
|
30
|
+
-- removes only the row of the current user. PG < 16 can keep the oid of
|
|
31
|
+
-- a dropped grantor; then the statement has no GRANTED BY.
|
|
32
|
+
select pg_catalog.format('revoke %s from %I', m.roleid::regrole, {{ROLE_LITERAL}})
|
|
33
|
+
|| coalesce(' granted by ' || pg_catalog.quote_ident(g.rolname), '')
|
|
34
|
+
from pg_catalog.pg_auth_members m
|
|
35
|
+
left join pg_catalog.pg_roles g on g.oid = m.grantor
|
|
36
|
+
where m.member = role_oid
|
|
37
|
+
and m.roleid in ('pg_monitor'::regrole, 'pg_read_all_stats'::regrole)
|
|
38
|
+
union
|
|
39
|
+
select pg_catalog.format('revoke select on pg_catalog.pg_index from %I', {{ROLE_LITERAL}})
|
|
40
|
+
from pg_catalog.pg_class c
|
|
41
|
+
where c.oid operator(pg_catalog.=) pg_catalog.to_regclass('pg_catalog.pg_index')::oid
|
|
42
|
+
and exists (
|
|
43
|
+
select from pg_catalog.aclexplode(c.relacl) a
|
|
44
|
+
where a.grantee = role_oid and a.privilege_type = 'SELECT')
|
|
45
|
+
union
|
|
46
|
+
select pg_catalog.format('revoke connect on database %I from %I', d.datname, {{ROLE_LITERAL}})
|
|
47
|
+
from pg_catalog.pg_database d
|
|
48
|
+
where d.datname = pg_catalog.current_database()
|
|
49
|
+
and exists (
|
|
50
|
+
select from pg_catalog.aclexplode(d.datacl) a
|
|
51
|
+
where a.grantee = role_oid and a.privilege_type = 'CONNECT')
|
|
52
|
+
union
|
|
53
|
+
select pg_catalog.format('revoke usage on schema postgres_ai from %I', {{ROLE_LITERAL}})
|
|
54
|
+
from pg_catalog.pg_namespace n
|
|
55
|
+
where n.nspname = 'postgres_ai'
|
|
56
|
+
and exists (
|
|
57
|
+
select from pg_catalog.aclexplode(n.nspacl) a
|
|
58
|
+
where a.grantee = role_oid and a.privilege_type = 'USAGE')
|
|
59
|
+
union
|
|
60
|
+
select pg_catalog.format('revoke execute on function %s from %I', p.oid::regprocedure, {{ROLE_LITERAL}})
|
|
61
|
+
from pg_catalog.pg_proc p
|
|
62
|
+
where p.oid in (
|
|
63
|
+
pg_catalog.to_regprocedure('rds_tools.pg_ls_multixactdir()'),
|
|
64
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text)'),
|
|
65
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text, boolean)'),
|
|
66
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text)'),
|
|
67
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text, boolean, boolean)'))
|
|
68
|
+
and exists (
|
|
69
|
+
select from pg_catalog.aclexplode(p.proacl) a
|
|
70
|
+
where a.grantee = role_oid and a.privilege_type = 'EXECUTE')
|
|
71
|
+
loop
|
|
72
|
+
if pass = 2 then
|
|
73
|
+
raise warning 'unprepare-db: grant left; a superuser can run: %', stmt;
|
|
74
|
+
else
|
|
75
|
+
begin
|
|
76
|
+
execute stmt;
|
|
77
|
+
exception when insufficient_privilege then
|
|
78
|
+
raise warning 'unprepare-db: % failed: %', stmt, sqlerrm;
|
|
79
|
+
end;
|
|
80
|
+
end if;
|
|
81
|
+
end loop;
|
|
82
|
+
end loop;
|
|
83
|
+
end {{DO_QUOTE}};
|
|
28
84
|
|
|
29
85
|
-- Note: USAGE on public is typically granted by default; we don't revoke it
|
|
30
86
|
-- to avoid breaking other applications that may rely on it.
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
-- Drop the monitoring role created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
-- This must run after revoking all permissions from the role.
|
|
3
3
|
|
|
4
|
+
-- Objects in other schemas (for example, a planted public.format) must not run as the admin.
|
|
5
|
+
set local search_path = pg_catalog, pg_temp;
|
|
6
|
+
|
|
4
7
|
-- Use a DO block to handle the case where the role doesn't exist
|
|
5
|
-
do
|
|
8
|
+
do {{DO_QUOTE}} begin
|
|
6
9
|
-- Reassign owned objects to current user before dropping
|
|
7
10
|
-- This handles any objects that might have been created by the role
|
|
8
11
|
begin
|
|
@@ -24,4 +27,4 @@ do $$ begin
|
|
|
24
27
|
exception when undefined_object then
|
|
25
28
|
null; -- Role doesn't exist, that's fine
|
|
26
29
|
end;
|
|
27
|
-
end
|
|
30
|
+
end {{DO_QUOTE}};
|
package/package.json
CHANGED
package/sql/03.permissions.sql
CHANGED
|
@@ -7,14 +7,10 @@ grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
|
|
|
7
7
|
grant pg_monitor to {{ROLE_IDENT}};
|
|
8
8
|
grant select on pg_catalog.pg_index to {{ROLE_IDENT}};
|
|
9
9
|
|
|
10
|
-
--
|
|
11
|
-
--
|
|
12
|
-
--
|
|
13
|
-
--
|
|
14
|
-
-- in the pg_stat_statements metric. pg_monitor already implies pg_read_all_stats;
|
|
15
|
-
-- granting it explicitly documents the dependency and keeps the metric working if the
|
|
16
|
-
-- pg_monitor grant above is ever narrowed. Idempotent.
|
|
17
|
-
grant pg_read_all_stats to {{ROLE_IDENT}};
|
|
10
|
+
-- Cluster-wide pg_stat_statements collection needs pg_read_all_stats (without it, other
|
|
11
|
+
-- users' rows have queryid and query text redacted to NULL). pg_monitor includes it, so it
|
|
12
|
+
-- is not granted directly: on PostgreSQL 16+ that needs ADMIN OPTION on pg_read_all_stats,
|
|
13
|
+
-- which Supabase's `postgres` role does not have (it has ADMIN OPTION on pg_monitor only).
|
|
18
14
|
|
|
19
15
|
-- Create postgres_ai schema for our objects
|
|
20
16
|
-- Using IF NOT EXISTS for idempotency - prepare-db can be run multiple times
|
|
@@ -44,7 +40,7 @@ grant usage on schema public to {{ROLE_IDENT}};
|
|
|
44
40
|
-- Grant access to the schema where pg_stat_statements is installed.
|
|
45
41
|
-- Some providers (e.g., Supabase) install extensions in a separate 'extensions' schema
|
|
46
42
|
-- rather than pg_catalog. This DO block detects the schema and grants USAGE if needed.
|
|
47
|
-
do
|
|
43
|
+
do {{DO_QUOTE}}
|
|
48
44
|
declare
|
|
49
45
|
ext_schema text;
|
|
50
46
|
begin
|
|
@@ -55,32 +51,46 @@ begin
|
|
|
55
51
|
|
|
56
52
|
-- Only grant if extension exists and is in a non-standard schema
|
|
57
53
|
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
58
|
-
execute format('grant usage on schema %I to {{
|
|
54
|
+
execute format('grant usage on schema %I to %I', ext_schema, {{ROLE_LITERAL}});
|
|
59
55
|
end if;
|
|
60
|
-
end
|
|
56
|
+
end {{DO_QUOTE}};
|
|
61
57
|
|
|
62
|
-
-- [SEARCH_PATH_BLOCK_START]
|
|
63
|
-
--
|
|
64
|
-
|
|
58
|
+
-- [SEARCH_PATH_BLOCK_START] Resolve built-ins from pg_catalog first; keep pg_temp last.
|
|
59
|
+
-- Do not include the role-named schema: it may be writable by that role.
|
|
60
|
+
-- public stays for pg_stat_statements. Residual risk: a function in public with
|
|
61
|
+
-- an exact-type match (e.g. public.quote_ident(name)) still beats pg_catalog.
|
|
62
|
+
-- The path for this database includes its pg_stat_statements schema if it is in a non-standard location.
|
|
63
|
+
do {{DO_QUOTE}}
|
|
65
64
|
declare
|
|
66
65
|
ext_schema text;
|
|
67
|
-
|
|
66
|
+
role_path constant text := 'pg_catalog, postgres_ai, public, pg_temp';
|
|
67
|
+
db_path text := role_path;
|
|
68
68
|
begin
|
|
69
|
+
-- This block runs as the admin: resolve its operators and functions from pg_catalog only
|
|
70
|
+
-- (CVE-2018-1058). The pin is inside the block: a DO block always runs in a transaction,
|
|
71
|
+
-- also in psql autocommit.
|
|
72
|
+
set local search_path = pg_catalog, pg_temp;
|
|
69
73
|
-- Detect pg_stat_statements extension schema
|
|
70
74
|
select n.nspname into ext_schema
|
|
71
75
|
from pg_extension e
|
|
72
76
|
join pg_namespace n on e.extnamespace = n.oid
|
|
73
77
|
where e.extname = 'pg_stat_statements';
|
|
74
78
|
|
|
75
|
-
-- Build search_path: include extension schema if in non-standard location
|
|
76
79
|
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
77
|
-
|
|
78
|
-
else
|
|
79
|
-
sp := 'postgres_ai, "$user", public, pg_catalog';
|
|
80
|
+
db_path := 'pg_catalog, postgres_ai, ' || quote_ident(ext_schema) || ', public, pg_temp';
|
|
80
81
|
end if;
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
-- Role-wide value: for databases that prepare-db did not run in. All databases share
|
|
84
|
+
-- this one row, so write it only when it holds another value.
|
|
85
|
+
if not exists (
|
|
86
|
+
select from pg_catalog.pg_db_role_setting
|
|
87
|
+
where setrole = (select oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}})
|
|
88
|
+
and setdatabase = 0
|
|
89
|
+
and ('search_path=' || role_path) = any (setconfig)
|
|
90
|
+
) then
|
|
91
|
+
execute format('alter user %I set search_path = %s', {{ROLE_LITERAL}}, role_path);
|
|
92
|
+
end if;
|
|
93
|
+
-- This database's value: other databases keep their own pg_stat_statements schema.
|
|
94
|
+
execute format('alter user %I in database %I set search_path = %s', {{ROLE_LITERAL}}, pg_catalog.current_database(), db_path);
|
|
95
|
+
end {{DO_QUOTE}};
|
|
84
96
|
-- [SEARCH_PATH_BLOCK_END]
|
|
85
|
-
|
|
86
|
-
|
package/sql/06.helpers.sql
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
-- Helper functions for postgres_ai monitoring user (template-filled by cli/lib/init.ts)
|
|
2
2
|
|
|
3
|
+
-- Remove the retired helper from databases prepared by older CLI versions.
|
|
4
|
+
-- Only its owner or a superuser can drop it: anyone else gets a warning here. Releases
|
|
5
|
+
-- installed table_describe with the same owner, so replacing it below then fails.
|
|
6
|
+
do {{DO_QUOTE}}
|
|
7
|
+
begin
|
|
8
|
+
-- Runs as the admin: resolve names from pg_catalog only (CVE-2018-1058).
|
|
9
|
+
set local search_path = pg_catalog, pg_temp;
|
|
10
|
+
drop function if exists postgres_ai.explain_generic(text, text, text);
|
|
11
|
+
exception when insufficient_privilege then
|
|
12
|
+
raise warning 'postgres_ai: legacy SECURITY DEFINER helper was NOT removed: %', sqlerrm
|
|
13
|
+
using hint = 'Its owner or a superuser must drop it, then re-run prepare-db.';
|
|
14
|
+
end {{DO_QUOTE}};
|
|
15
|
+
|
|
3
16
|
/*
|
|
4
17
|
* table_describe
|
|
5
18
|
*
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
-- Drop helper functions created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
-- Run before dropping the postgres_ai schema.
|
|
3
3
|
|
|
4
|
+
-- Objects in other schemas (for example, a planted public.format) must not run as the admin.
|
|
5
|
+
set local search_path = pg_catalog, pg_temp;
|
|
6
|
+
|
|
4
7
|
drop function if exists postgres_ai.explain_generic(text, text, text);
|
|
5
8
|
drop function if exists postgres_ai.table_describe(text);
|
|
@@ -1,30 +1,86 @@
|
|
|
1
1
|
-- Revoke permissions and drop objects created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
|
|
3
|
+
-- Objects in other schemas (for example, a planted public.=) must not run as the admin.
|
|
4
|
+
set local search_path = pg_catalog, pg_temp;
|
|
5
|
+
-- The CLI reads the "unprepare-db:" WARNINGs. A higher setting would hide them.
|
|
6
|
+
set local client_min_messages = warning;
|
|
7
|
+
|
|
3
8
|
-- Drop the postgres_ai.pg_statistic view
|
|
4
9
|
drop view if exists postgres_ai.pg_statistic;
|
|
5
10
|
|
|
6
|
-
--
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
--
|
|
10
|
-
--
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
end $$;
|
|
11
|
+
-- Revoke only the grants that the role holds. The postgres_ai schema stays
|
|
12
|
+
-- because it may contain customer objects. The pg_read_all_stats membership
|
|
13
|
+
-- comes from older prepare-db versions. The functions come from optional
|
|
14
|
+
-- permissions (04, 05). A non-superuser admin cannot revoke a grant that a
|
|
15
|
+
-- superuser made. Pass 1 revokes. Pass 2 sends one "grant left" WARNING for
|
|
16
|
+
-- each grant that stays. The CLI shows these WARNINGs and exits 1.
|
|
17
|
+
do {{DO_QUOTE}}
|
|
18
|
+
declare
|
|
19
|
+
role_oid oid;
|
|
20
|
+
stmt text;
|
|
21
|
+
begin
|
|
22
|
+
select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
|
|
23
|
+
if role_oid is null then
|
|
24
|
+
return; -- The monitoring role was already removed.
|
|
25
|
+
end if;
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
for pass in 1..2 loop
|
|
28
|
+
for stmt in
|
|
29
|
+
-- PG 16+ keeps one row for each grantor. A revoke without GRANTED BY
|
|
30
|
+
-- removes only the row of the current user. PG < 16 can keep the oid of
|
|
31
|
+
-- a dropped grantor; then the statement has no GRANTED BY.
|
|
32
|
+
select pg_catalog.format('revoke %s from %I', m.roleid::regrole, {{ROLE_LITERAL}})
|
|
33
|
+
|| coalesce(' granted by ' || pg_catalog.quote_ident(g.rolname), '')
|
|
34
|
+
from pg_catalog.pg_auth_members m
|
|
35
|
+
left join pg_catalog.pg_roles g on g.oid = m.grantor
|
|
36
|
+
where m.member = role_oid
|
|
37
|
+
and m.roleid in ('pg_monitor'::regrole, 'pg_read_all_stats'::regrole)
|
|
38
|
+
union
|
|
39
|
+
select pg_catalog.format('revoke select on pg_catalog.pg_index from %I', {{ROLE_LITERAL}})
|
|
40
|
+
from pg_catalog.pg_class c
|
|
41
|
+
where c.oid operator(pg_catalog.=) pg_catalog.to_regclass('pg_catalog.pg_index')::oid
|
|
42
|
+
and exists (
|
|
43
|
+
select from pg_catalog.aclexplode(c.relacl) a
|
|
44
|
+
where a.grantee = role_oid and a.privilege_type = 'SELECT')
|
|
45
|
+
union
|
|
46
|
+
select pg_catalog.format('revoke connect on database %I from %I', d.datname, {{ROLE_LITERAL}})
|
|
47
|
+
from pg_catalog.pg_database d
|
|
48
|
+
where d.datname = pg_catalog.current_database()
|
|
49
|
+
and exists (
|
|
50
|
+
select from pg_catalog.aclexplode(d.datacl) a
|
|
51
|
+
where a.grantee = role_oid and a.privilege_type = 'CONNECT')
|
|
52
|
+
union
|
|
53
|
+
select pg_catalog.format('revoke usage on schema postgres_ai from %I', {{ROLE_LITERAL}})
|
|
54
|
+
from pg_catalog.pg_namespace n
|
|
55
|
+
where n.nspname = 'postgres_ai'
|
|
56
|
+
and exists (
|
|
57
|
+
select from pg_catalog.aclexplode(n.nspacl) a
|
|
58
|
+
where a.grantee = role_oid and a.privilege_type = 'USAGE')
|
|
59
|
+
union
|
|
60
|
+
select pg_catalog.format('revoke execute on function %s from %I', p.oid::regprocedure, {{ROLE_LITERAL}})
|
|
61
|
+
from pg_catalog.pg_proc p
|
|
62
|
+
where p.oid in (
|
|
63
|
+
pg_catalog.to_regprocedure('rds_tools.pg_ls_multixactdir()'),
|
|
64
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text)'),
|
|
65
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text, boolean)'),
|
|
66
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text)'),
|
|
67
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text, boolean, boolean)'))
|
|
68
|
+
and exists (
|
|
69
|
+
select from pg_catalog.aclexplode(p.proacl) a
|
|
70
|
+
where a.grantee = role_oid and a.privilege_type = 'EXECUTE')
|
|
71
|
+
loop
|
|
72
|
+
if pass = 2 then
|
|
73
|
+
raise warning 'unprepare-db: grant left; a superuser can run: %', stmt;
|
|
74
|
+
else
|
|
75
|
+
begin
|
|
76
|
+
execute stmt;
|
|
77
|
+
exception when insufficient_privilege then
|
|
78
|
+
raise warning 'unprepare-db: % failed: %', stmt, sqlerrm;
|
|
79
|
+
end;
|
|
80
|
+
end if;
|
|
81
|
+
end loop;
|
|
82
|
+
end loop;
|
|
83
|
+
end {{DO_QUOTE}};
|
|
28
84
|
|
|
29
85
|
-- Note: USAGE on public is typically granted by default; we don't revoke it
|
|
30
86
|
-- to avoid breaking other applications that may rely on it.
|
package/sql/uninit/03.role.sql
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
-- Drop the monitoring role created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
-- This must run after revoking all permissions from the role.
|
|
3
3
|
|
|
4
|
+
-- Objects in other schemas (for example, a planted public.format) must not run as the admin.
|
|
5
|
+
set local search_path = pg_catalog, pg_temp;
|
|
6
|
+
|
|
4
7
|
-- Use a DO block to handle the case where the role doesn't exist
|
|
5
|
-
do
|
|
8
|
+
do {{DO_QUOTE}} begin
|
|
6
9
|
-- Reassign owned objects to current user before dropping
|
|
7
10
|
-- This handles any objects that might have been created by the role
|
|
8
11
|
begin
|
|
@@ -24,4 +27,4 @@ do $$ begin
|
|
|
24
27
|
exception when undefined_object then
|
|
25
28
|
null; -- Role doesn't exist, that's fine
|
|
26
29
|
end;
|
|
27
|
-
end
|
|
30
|
+
end {{DO_QUOTE}};
|