|
| 1 | +-- Lists each column's privileges in the form of: |
| 2 | +-- |
| 3 | +-- [ |
| 4 | +-- { |
| 5 | +-- "column_id": "12345.1", |
| 6 | +-- "relation_schema": "public", |
| 7 | +-- "relation_name": "mytable", |
| 8 | +-- "column_name": "mycolumn", |
| 9 | +-- "privileges": [ |
| 10 | +-- { |
| 11 | +-- "grantor": "postgres", |
| 12 | +-- "grantee": "myrole", |
| 13 | +-- "privilege_type": "SELECT", |
| 14 | +-- "is_grantable": false |
| 15 | +-- }, |
| 16 | +-- ... |
| 17 | +-- ] |
| 18 | +-- }, |
| 19 | +-- ... |
| 20 | +-- ] |
| 21 | +-- |
| 22 | +-- Modified from information_schema.column_privileges. We try to be as close as |
| 23 | +-- possible to the view definition, obtained from: |
| 24 | +-- |
| 25 | +-- select pg_get_viewdef('information_schema.column_privileges'); |
| 26 | +-- |
| 27 | +-- The main differences are: |
| 28 | +-- - we include column privileges for materialized views |
| 29 | +-- (reason for exclusion in information_schema.column_privileges: |
| 30 | +-- https://www.postgresql.org/message-id/9136.1502740844%40sss.pgh.pa.us) |
| 31 | +-- - we query a.attrelid and a.attnum to generate `column_id` |
| 32 | +-- - `table_catalog` is omitted |
| 33 | +-- - table_schema -> relation_schema, table_name -> relation_name |
| 34 | +-- |
| 35 | +-- Column privileges are intertwined with table privileges in that table |
| 36 | +-- privileges override column privileges. E.g. if we do: |
| 37 | +-- |
| 38 | +-- grant all on mytable to myrole; |
| 39 | +-- |
| 40 | +-- Then `myrole` is granted privileges for ALL columns. Likewise, if we do: |
| 41 | +-- |
| 42 | +-- grant all (id) on mytable to myrole; |
| 43 | +-- revoke all on mytable from myrole; |
| 44 | +-- |
| 45 | +-- Then the grant on the `id` column is revoked. |
| 46 | +-- |
| 47 | +-- This is unlike how grants for schemas and tables interact, where you need |
| 48 | +-- privileges for BOTH the schema the table is in AND the table itself in order |
| 49 | +-- to access the table. |
| 50 | + |
| 51 | +select (x.attrelid || '.' || x.attnum) as column_id, |
| 52 | + nc.nspname as relation_schema, |
| 53 | + x.relname as relation_name, |
| 54 | + x.attname as column_name, |
| 55 | + coalesce( |
| 56 | + jsonb_agg( |
| 57 | + jsonb_build_object( |
| 58 | + 'grantor', u_grantor.rolname, |
| 59 | + 'grantee', grantee.rolname, |
| 60 | + 'privilege_type', x.prtype, |
| 61 | + 'is_grantable', x.grantable |
| 62 | + ) |
| 63 | + ), |
| 64 | + '[]' |
| 65 | + ) as privileges |
| 66 | +from |
| 67 | + (select pr_c.grantor, |
| 68 | + pr_c.grantee, |
| 69 | + a.attrelid, |
| 70 | + a.attnum, |
| 71 | + a.attname, |
| 72 | + pr_c.relname, |
| 73 | + pr_c.relnamespace, |
| 74 | + pr_c.prtype, |
| 75 | + pr_c.grantable, |
| 76 | + pr_c.relowner |
| 77 | + from |
| 78 | + (select pg_class.oid, |
| 79 | + pg_class.relname, |
| 80 | + pg_class.relnamespace, |
| 81 | + pg_class.relowner, |
| 82 | + (aclexplode(coalesce(pg_class.relacl, acldefault('r', pg_class.relowner)))).grantor as grantor, |
| 83 | + (aclexplode(coalesce(pg_class.relacl, acldefault('r', pg_class.relowner)))).grantee as grantee, |
| 84 | + (aclexplode(coalesce(pg_class.relacl, acldefault('r', pg_class.relowner)))).privilege_type as privilege_type, |
| 85 | + (aclexplode(coalesce(pg_class.relacl, acldefault('r', pg_class.relowner)))).is_grantable as is_grantable |
| 86 | + from pg_class |
| 87 | + where (pg_class.relkind = any (array['r', |
| 88 | + 'v', |
| 89 | + 'm', |
| 90 | + 'f', |
| 91 | + 'p'])) ) pr_c(oid, relname, relnamespace, relowner, grantor, grantee, prtype, grantable), |
| 92 | + pg_attribute a |
| 93 | + where ((a.attrelid = pr_c.oid) |
| 94 | + and (a.attnum > 0) |
| 95 | + and (not a.attisdropped)) |
| 96 | + union select pr_a.grantor, |
| 97 | + pr_a.grantee, |
| 98 | + pr_a.attrelid, |
| 99 | + pr_a.attnum, |
| 100 | + pr_a.attname, |
| 101 | + c.relname, |
| 102 | + c.relnamespace, |
| 103 | + pr_a.prtype, |
| 104 | + pr_a.grantable, |
| 105 | + c.relowner |
| 106 | + from |
| 107 | + (select a.attrelid, |
| 108 | + a.attnum, |
| 109 | + a.attname, |
| 110 | + (aclexplode(coalesce(a.attacl, acldefault('c', cc.relowner)))).grantor as grantor, |
| 111 | + (aclexplode(coalesce(a.attacl, acldefault('c', cc.relowner)))).grantee as grantee, |
| 112 | + (aclexplode(coalesce(a.attacl, acldefault('c', cc.relowner)))).privilege_type as privilege_type, |
| 113 | + (aclexplode(coalesce(a.attacl, acldefault('c', cc.relowner)))).is_grantable as is_grantable |
| 114 | + from (pg_attribute a |
| 115 | + join pg_class cc on ((a.attrelid = cc.oid))) |
| 116 | + where ((a.attnum > 0) |
| 117 | + and (not a.attisdropped))) pr_a(attrelid, attnum, attname, grantor, grantee, prtype, grantable), |
| 118 | + pg_class c |
| 119 | + where ((pr_a.attrelid = c.oid) |
| 120 | + and (c.relkind = any (ARRAY['r', |
| 121 | + 'v', |
| 122 | + 'm', |
| 123 | + 'f', |
| 124 | + 'p'])))) x, |
| 125 | + pg_namespace nc, |
| 126 | + pg_authid u_grantor, |
| 127 | + (select pg_authid.oid, |
| 128 | + pg_authid.rolname |
| 129 | + from pg_authid |
| 130 | + union all select (0)::oid as oid, |
| 131 | + 'PUBLIC') grantee(oid, rolname) |
| 132 | +where ((x.relnamespace = nc.oid) |
| 133 | + and (x.grantee = grantee.oid) |
| 134 | + and (x.grantor = u_grantor.oid) |
| 135 | + and (x.prtype = any (ARRAY['INSERT', |
| 136 | + 'SELECT', |
| 137 | + 'UPDATE', |
| 138 | + 'REFERENCES'])) |
| 139 | + and (pg_has_role(u_grantor.oid, 'USAGE') |
| 140 | + or pg_has_role(grantee.oid, 'USAGE') |
| 141 | + or (grantee.rolname = 'PUBLIC'))) |
| 142 | +group by column_id, |
| 143 | + nc.nspname, |
| 144 | + x.relname, |
| 145 | + x.attname |
0 commit comments