postgresai 0.18.0-dev.9 → 0.18.0-rc.2

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.
@@ -1,30 +1,98 @@
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
+ -- Printed plans may run in psql autocommit, where SET LOCAL is ignored.
5
+ set search_path = pg_catalog, pg_temp;
6
+ -- The CLI reads the "unprepare-db:" WARNINGs. A higher setting would hide them.
7
+ set client_min_messages = warning;
8
+
3
9
  -- Drop the postgres_ai.pg_statistic view
4
10
  drop view if exists postgres_ai.pg_statistic;
5
11
 
6
- -- Drop the postgres_ai schema (CASCADE to handle any remaining objects)
7
- drop schema if exists postgres_ai cascade;
8
-
9
- -- Revoke permissions from the monitoring role
10
- -- Use a DO block to handle the case where the role doesn't exist
11
- do $$ begin
12
- revoke pg_monitor from {{ROLE_IDENT}};
13
- exception when undefined_object then
14
- null; -- Role doesn't exist, nothing to revoke
15
- end $$;
16
-
17
- do $$ begin
18
- revoke select on pg_catalog.pg_index from {{ROLE_IDENT}};
19
- exception when undefined_object then
20
- null; -- Role doesn't exist
21
- end $$;
12
+ -- Revoke only the grants that the role holds. The postgres_ai schema stays
13
+ -- because it may contain customer objects. The pg_read_all_stats membership
14
+ -- comes from older prepare-db versions. The functions come from optional
15
+ -- permissions (04, 05). A non-superuser admin cannot revoke a grant that a
16
+ -- superuser made. Pass 1 revokes. Pass 2 sends one "grant left" WARNING for
17
+ -- each grant that stays. The CLI shows these WARNINGs and exits 1.
18
+ do {{DO_QUOTE}}
19
+ declare
20
+ role_oid oid;
21
+ stmt text;
22
+ begin
23
+ select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
24
+ if role_oid is null then
25
+ return; -- The monitoring role was already removed.
26
+ end if;
22
27
 
23
- do $$ begin
24
- revoke connect on database {{DB_IDENT}} from {{ROLE_IDENT}};
25
- exception when undefined_object then
26
- null; -- Role doesn't exist
27
- end $$;
28
+ for pass in 1..2 loop
29
+ for stmt in
30
+ -- PG 16+ keeps one row for each grantor. A revoke without GRANTED BY
31
+ -- removes only the row of the current user. PG < 16 can keep the oid of
32
+ -- a dropped grantor; then the statement has no GRANTED BY.
33
+ select pg_catalog.format('revoke %s from %I', m.roleid::regrole, {{ROLE_LITERAL}})
34
+ || coalesce(' granted by ' || pg_catalog.quote_ident(g.rolname), '')
35
+ from pg_catalog.pg_auth_members m
36
+ left join pg_catalog.pg_roles g on g.oid = m.grantor
37
+ where m.member = role_oid
38
+ and m.roleid in ('pg_monitor'::regrole, 'pg_read_all_stats'::regrole)
39
+ union
40
+ select pg_catalog.format('revoke select on pg_catalog.pg_index from %I', {{ROLE_LITERAL}})
41
+ from pg_catalog.pg_class c
42
+ where c.oid operator(pg_catalog.=) pg_catalog.to_regclass('pg_catalog.pg_index')::oid
43
+ and exists (
44
+ select from pg_catalog.aclexplode(c.relacl) a
45
+ where a.grantee = role_oid and a.privilege_type = 'SELECT')
46
+ union
47
+ select pg_catalog.format('revoke connect on database %I from %I', d.datname, {{ROLE_LITERAL}})
48
+ from pg_catalog.pg_database d
49
+ where d.datname = pg_catalog.current_database()
50
+ and exists (
51
+ select from pg_catalog.aclexplode(d.datacl) a
52
+ where a.grantee = role_oid and a.privilege_type = 'CONNECT')
53
+ union
54
+ select pg_catalog.format('revoke usage on schema postgres_ai from %I', {{ROLE_LITERAL}})
55
+ from pg_catalog.pg_namespace n
56
+ where n.nspname = 'postgres_ai'
57
+ and exists (
58
+ select from pg_catalog.aclexplode(n.nspacl) a
59
+ where a.grantee = role_oid and a.privilege_type = 'USAGE')
60
+ union
61
+ -- prepare-db grants USAGE when pg_stat_statements lives in a custom
62
+ -- extension schema. Retaining the login role must remove that grant too.
63
+ select pg_catalog.format('revoke usage on schema %I from %I', n.nspname, {{ROLE_LITERAL}})
64
+ from pg_catalog.pg_extension e
65
+ join pg_catalog.pg_namespace n on n.oid = e.extnamespace
66
+ where e.extname = 'pg_stat_statements'
67
+ and n.nspname not in ('pg_catalog', 'public')
68
+ and exists (
69
+ select from pg_catalog.aclexplode(n.nspacl) a
70
+ where a.grantee = role_oid and a.privilege_type = 'USAGE')
71
+ union
72
+ select pg_catalog.format('revoke execute on function %s from %I', p.oid::regprocedure, {{ROLE_LITERAL}})
73
+ from pg_catalog.pg_proc p
74
+ where p.oid in (
75
+ pg_catalog.to_regprocedure('rds_tools.pg_ls_multixactdir()'),
76
+ pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text)'),
77
+ pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text, boolean)'),
78
+ pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text)'),
79
+ pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text, boolean, boolean)'))
80
+ and exists (
81
+ select from pg_catalog.aclexplode(p.proacl) a
82
+ where a.grantee = role_oid and a.privilege_type = 'EXECUTE')
83
+ loop
84
+ if pass = 2 then
85
+ raise warning 'unprepare-db: grant left; a superuser can run: %', stmt;
86
+ else
87
+ begin
88
+ execute stmt;
89
+ exception when insufficient_privilege then
90
+ raise warning 'unprepare-db: % failed: %', stmt, sqlerrm;
91
+ end;
92
+ end if;
93
+ end loop;
94
+ end loop;
95
+ end {{DO_QUOTE}};
28
96
 
29
- -- Note: USAGE on public is typically granted by default; we don't revoke it
30
- -- to avoid breaking other applications that may rely on it.
97
+ -- Step 02 keeps a direct USAGE grant on public for --keep-role. Step 03
98
+ -- revokes every direct schema USAGE grant only inside the DROP ROLE transaction.
@@ -1,27 +1,45 @@
1
- -- Drop the monitoring role created by prepare-db (template-filled by cli/lib/init.ts)
2
- -- This must run after revoking all permissions from the role.
1
+ -- Drop the monitoring role only when it has no remaining dependencies.
2
+ -- A reused role may own customer objects. Do not transfer or delete those
3
+ -- objects on the role's behalf; PostgreSQL will reject the drop instead.
3
4
 
4
- -- Use a DO block to handle the case where the role doesn't exist
5
- do $$ begin
6
- -- Reassign owned objects to current user before dropping
7
- -- This handles any objects that might have been created by the role
8
- begin
9
- execute format('reassign owned by %I to current_user', {{ROLE_LITERAL}});
10
- exception when undefined_object then
11
- null; -- Role doesn't exist, nothing to reassign
12
- end;
5
+ -- Objects in other schemas (for example, a planted public.format) must not run as the admin.
6
+ -- Printed plans may run in psql autocommit, where SET LOCAL is ignored.
7
+ set search_path = pg_catalog, pg_temp;
13
8
 
14
- -- Drop owned objects (in case reassign didn't work for some objects)
15
- begin
16
- execute format('drop owned by %I', {{ROLE_LITERAL}});
17
- exception when undefined_object then
18
- null; -- Role doesn't exist
19
- end;
9
+ -- prepare-db grants schema USAGE, but the extension can move or be dropped
10
+ -- before cleanup. Revoke every direct schema USAGE grant to the role in the
11
+ -- role-drop transaction; unrelated dependencies roll all revokes back.
12
+ -- prepare-db also grants CONNECT on the target database, which can differ
13
+ -- from the database of this session. Step 02 revokes it only on the current
14
+ -- one, so revoke direct CONNECT grants on every database here too.
15
+ do {{DO_QUOTE}}
16
+ declare
17
+ role_oid oid;
18
+ schema_name name;
19
+ database_name name;
20
+ begin
21
+ select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
22
+ if role_oid is null then
23
+ return;
24
+ end if;
20
25
 
21
- -- Drop the role
22
- begin
23
- execute format('drop role %I', {{ROLE_LITERAL}});
24
- exception when undefined_object then
25
- null; -- Role doesn't exist, that's fine
26
- end;
27
- end $$;
26
+ for schema_name in
27
+ select distinct n.nspname
28
+ from pg_catalog.pg_namespace n,
29
+ pg_catalog.aclexplode(n.nspacl) a
30
+ where a.grantee = role_oid and a.privilege_type = 'USAGE'
31
+ loop
32
+ execute pg_catalog.format('revoke usage on schema %I from %I', schema_name, {{ROLE_LITERAL}});
33
+ end loop;
34
+
35
+ for database_name in
36
+ select distinct d.datname
37
+ from pg_catalog.pg_database d,
38
+ pg_catalog.aclexplode(d.datacl) a
39
+ where a.grantee = role_oid and a.privilege_type = 'CONNECT'
40
+ loop
41
+ execute pg_catalog.format('revoke connect on database %I from %I', database_name, {{ROLE_LITERAL}});
42
+ end loop;
43
+ end {{DO_QUOTE}};
44
+
45
+ drop role if exists {{ROLE_IDENT}};