-
Notifications
You must be signed in to change notification settings - Fork 5
Converge privileges between a fresh install and an updated one #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jnasbyupgrade
wants to merge
2
commits into
master
Choose a base branch
from
perm-normalize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+350
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Converge privileges between a fresh install and an updated one, down to | ||
| * pg_dump output. https://github.com/Postgres-Extensions/cat_tools/issues/93 | ||
| * | ||
| * The install script's ALTER DEFAULT PRIVILEGES grants USAGE on our types | ||
| * implicitly, and an implicit grant is never snapshotted into pg_init_privs, | ||
| * while the same grant written out in an update script is -- identical ACLs, | ||
| * different dumps. Issuing the same explicit statements at the end of both | ||
| * scripts removes the asymmetry by construction. | ||
| */ | ||
|
|
||
| /* | ||
| * One row per object cat_tools issues privileges on, plus the privileges it | ||
| * intends that object to have. object_kind is the keyword GRANT and REVOKE name | ||
| * the object with; grant_privilege is what the usage role gets, or NULL for | ||
| * nothing. privilege__normalize() below is a loop over this. | ||
| * | ||
| * Only the grantees cat_tools itself issues to -- PUBLIC and the usage role -- | ||
| * are described here. Grants an administrator made to other roles are left | ||
| * alone: an update must not silently revoke them. | ||
| */ | ||
| CREATE OR REPLACE VIEW _cat_tools.privilege_target_v AS | ||
| WITH ext AS ( | ||
| SELECT oid AS extoid | ||
| , extnamespace | ||
| FROM pg_catalog.pg_extension | ||
| WHERE extname = 'cat_tools' | ||
| ) | ||
| , member AS ( | ||
| SELECT classid | ||
| , objid | ||
| FROM pg_catalog.pg_depend | ||
| WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass | ||
| AND refobjid = (SELECT extoid FROM ext) | ||
| AND deptype = 'e' | ||
| ) | ||
| /* | ||
| * The extension's own schema is created by CREATE EXTENSION itself, so it is | ||
| * not a member and has to be unioned in. | ||
| */ | ||
| SELECT 'SCHEMA'::text AS object_kind | ||
| , n.oid::pg_catalog.regnamespace::text AS object_identity | ||
| , true AS revoke_public | ||
| , true AS revoke_role | ||
| , 'USAGE'::text AS grant_privilege | ||
| FROM pg_catalog.pg_namespace n | ||
| WHERE n.oid IN ( | ||
| SELECT extnamespace FROM ext | ||
| UNION ALL | ||
| SELECT objid FROM member WHERE classid = 'pg_catalog.pg_namespace'::pg_catalog.regclass | ||
| ) | ||
| UNION ALL | ||
| /* | ||
| * PUBLIC keeps the USAGE Postgres gives it on every type: schema USAGE is what | ||
| * actually gates our types, so revoking here would tighten the documented | ||
| * model rather than normalize it. | ||
| */ | ||
| SELECT 'TYPE' | ||
| , t.oid::pg_catalog.regtype::text | ||
| , false | ||
| , true | ||
| , 'USAGE' | ||
| FROM pg_catalog.pg_type t | ||
| WHERE t.oid IN (SELECT objid FROM member WHERE classid = 'pg_catalog.pg_type'::pg_catalog.regclass) | ||
| AND t.typtype <> 'p' | ||
| -- Array types and relation rowtypes carry no ACL of their own | ||
| AND NOT EXISTS( SELECT FROM pg_catalog.pg_type e WHERE e.typarray = t.oid ) | ||
| AND ( | ||
| t.typrelid = 0 | ||
| OR 'c' = (SELECT c.relkind FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid) | ||
| ) | ||
| UNION ALL | ||
| /* | ||
| * Relations in the extension's own schema are the public API; anything in | ||
| * _cat_tools is internal and gets no grant. Sequences (relkind 'S') are | ||
| * excluded because their privilege set is unlike a table's and cat_tools has | ||
| * none; adding one means adding a branch here. | ||
| * | ||
| * Column-level privileges (pg_attribute.attacl) are likewise out of scope: | ||
| * cat_tools grants nothing at column granularity and ALTER DEFAULT PRIVILEGES | ||
| * has no column granularity either, so the drift this exists to prevent cannot | ||
| * reach them. A table-level REVOKE never subsumes column-level privileges, so | ||
| * granting at column granularity in future means handling them here too. | ||
| */ | ||
| SELECT 'TABLE' | ||
| , c.oid::pg_catalog.regclass::text | ||
| , true | ||
| , true | ||
| , CASE WHEN c.relnamespace = (SELECT extnamespace FROM ext) THEN 'SELECT' END | ||
| FROM pg_catalog.pg_class c | ||
| WHERE c.oid IN (SELECT objid FROM member WHERE classid = 'pg_catalog.pg_class'::pg_catalog.regclass) | ||
| AND c.relkind IN ('r', 'p', 'v', 'm', 'f') | ||
| UNION ALL | ||
| /* | ||
| * Which routines the usage role may execute is a per-routine decision made at | ||
| * the creation site, where __cat_tools.create_function already issues | ||
| * REVOKE-then-GRANT, so only the REVOKE from PUBLIC is asserted here. | ||
| */ | ||
| SELECT 'ROUTINE' | ||
| , p.oid::pg_catalog.regprocedure::text | ||
| , true | ||
| , false | ||
| , NULL::text | ||
| FROM pg_catalog.pg_proc p | ||
| WHERE p.oid IN (SELECT objid FROM member WHERE classid = 'pg_catalog.pg_proc'::pg_catalog.regclass) | ||
| ; | ||
| COMMENT ON VIEW _cat_tools.privilege_target_v IS 'Every object cat_tools issues privileges on, and the privileges it intends.'; | ||
|
|
||
| CREATE OR REPLACE FUNCTION _cat_tools.privilege__normalize( | ||
| ) RETURNS void LANGUAGE plpgsql AS $body$ | ||
| DECLARE | ||
| /* | ||
| * Single point of definition for the grantee; nothing else below names a | ||
| * role. https://github.com/Postgres-Extensions/cat_tools/issues/88 may change | ||
| * how it is chosen. | ||
| */ | ||
| c_usage_role CONSTANT pg_catalog.name := 'cat_tools__usage'; | ||
|
|
||
| /* | ||
| * A non-superuser install may be unable to create the role | ||
| * (https://github.com/Postgres-Extensions/cat_tools/issues/88), so its absence | ||
| * skips the role-specific statements instead of failing. | ||
| */ | ||
| c_have_role CONSTANT boolean := EXISTS( | ||
| SELECT FROM pg_catalog.pg_roles WHERE rolname = c_usage_role | ||
| ); | ||
|
|
||
| r record; | ||
| BEGIN | ||
| IF NOT c_have_role THEN | ||
| RAISE DEBUG 'role % does not exist; normalizing PUBLIC privileges only', c_usage_role; | ||
| END IF; | ||
|
|
||
| FOR r IN | ||
| SELECT * | ||
| FROM _cat_tools.privilege_target_v | ||
| -- Deterministic statement order, so both paths build ACL entries alike | ||
| ORDER BY object_kind, object_identity | ||
| LOOP | ||
| /* | ||
| * REVOKE first, rather than just topping the GRANT up: an ALTER DEFAULT | ||
| * PRIVILEGES configured outside this extension reaches every object a fresh | ||
| * install creates and nothing an update script touches, so whatever it added | ||
| * would otherwise be a permanent divergence. | ||
| */ | ||
| IF r.revoke_public THEN | ||
| EXECUTE format('REVOKE ALL ON %s %s FROM PUBLIC', r.object_kind, r.object_identity); | ||
| END IF; | ||
|
|
||
| CONTINUE WHEN NOT c_have_role; | ||
|
|
||
| IF r.revoke_role THEN | ||
| EXECUTE format('REVOKE ALL ON %s %s FROM %I', r.object_kind, r.object_identity, c_usage_role); | ||
| END IF; | ||
|
|
||
| IF r.grant_privilege IS NOT NULL THEN | ||
| EXECUTE format('GRANT %s ON %s %s TO %I', r.grant_privilege, r.object_kind, r.object_identity, c_usage_role); | ||
| END IF; | ||
| END LOOP; | ||
| END | ||
| $body$; | ||
| COMMENT ON FUNCTION _cat_tools.privilege__normalize() IS 'Reset every privilege cat_tools issues to its intended state.'; | ||
|
|
||
| SELECT _cat_tools.privilege__normalize(); | ||
|
|
||
| -- vi: expandtab ts=2 sw=2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,6 +136,8 @@ | |
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
26 lines of comment here is overkill. Again, IMPORTANT details about individual steps/things belong as comments with the relevant code. A header comment like this should only hit high-level things, and only if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Header is down to 9 lines: what this achieves, plus the one non-obvious reason it is needed (implicit ALTER DEFAULT PRIVILEGES grants never reach
pg_init_privs, explicit ones do). Everything else moved to the code it explains — the REVOKE-before-GRANT rationale sits on the REVOKE in the loop, the column-ACL exclusion sits on the TABLE branch it would apply to, and the grantee scope sits on the view that defines it. Same trim applied to the copy insql/cat_tools.sql.in.