Shenanigator
Shenanigator

Reputation: 1066

Query permissions for a specific table in redshift (Groups and Users)

I am trying to find a query that lets me get the current permissions on a specific table in Redshift, for both groups and users. I know how to do the actual grant, but I am having a heck of a time finding the correct table(s) to query to get existing permissions.

Other information:

This will be used during a programmatic operation where the table is cascade dropped and re-created to ensure we re-apply the same permissions from before the drop. If there is an option to do this in Python without a conn.execute() query I am open to that as well.

Edit:

I did find the below query before and I tried a quick short version to see if I could get groups:

SELECT * 
FROM 
(
SELECT 
    schemaname
    ,objectname
    ,usename
        ,groname
    ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'select') AND HAS_TABLE_PRIVILEGE(grp.groname, fullobj, 'select') AND has_schema_privilege(usrs.usename, schemaname, 'usage')  AS sel
FROM
    (
    SELECT schemaname, 't' AS obj_type, tablename AS objectname, schemaname + '.' + tablename AS fullobj FROM pg_tables
    UNION
    SELECT schemaname, 'v' AS obj_type, viewname AS objectname, schemaname + '.' + viewname AS fullobj FROM pg_views
    ) AS objs
    ,(SELECT * FROM pg_user) AS usrs
        ,(SELECT * FROM pg_group) AS grp
ORDER BY fullobj
)
WHERE sel = true
and objectname = 'table_name'

This gives me an error saying it can't find the username which is a group name.

Upvotes: 2

Views: 5913

Answers (3)

Vivek Ramanathan
Vivek Ramanathan

Reputation: 470

To see schemas accessible by a group on redshift:

SELECT * FROM svv_schema_privileges WHERE identity_name = 'my_group_name';

Upvotes: 0

Shenanigator
Shenanigator

Reputation: 1066

I finally found something I was able to pick apart for groups and hash together something:

SELECT
    namespace, item, type, groname 
FROM
    (
    SELECT
        use.usename AS subject,
        nsp.nspname AS NAMESPACE,
        cls.relname AS item,
        cls.relkind AS TYPE,
        use2.usename AS OWNER,
        cls.relacl 
    FROM
        pg_user use
        CROSS JOIN pg_class cls
        LEFT JOIN pg_namespace nsp ON cls.relnamespace = nsp.oid
        LEFT JOIN pg_user use2 ON cls.relowner = use2.usesysid 
    WHERE
        cls.relowner = use.usesysid 
        AND nsp.nspname NOT IN ( 'pg_catalog', 'pg_toast', 'information_schema' ) 
        AND nsp.nspname IN ( 'schema' ) 
        AND relacl IS NOT NULL 
    ORDER BY
        subject,
        NAMESPACE,
        item 
    )
    JOIN pg_group pu ON array_to_string( relacl, '|' ) LIKE'%' || pu.groname || '%'

take apart from How to query user group privileges in postgresql?

Upvotes: 0

demircioglu
demircioglu

Reputation: 3455

You can use the following query with objectname filter to find out permissions for a specific table

SELECT * 
FROM 
(
SELECT 
    schemaname
    ,objectname
    ,usename
    ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'select') AND has_schema_privilege(usrs.usename, schemaname, 'usage')  AS sel
    ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'insert') AND has_schema_privilege(usrs.usename, schemaname, 'usage')  AS ins
    ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'update') AND has_schema_privilege(usrs.usename, schemaname, 'usage')  AS upd
    ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'delete') AND has_schema_privilege(usrs.usename, schemaname, 'usage')  AS del
    ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'references') AND has_schema_privilege(usrs.usename, schemaname, 'usage')  AS ref
FROM
    (
    SELECT schemaname, 't' AS obj_type, tablename AS objectname, schemaname + '.' + tablename AS fullobj FROM pg_tables
    UNION
    SELECT schemaname, 'v' AS obj_type, viewname AS objectname, schemaname + '.' + viewname AS fullobj FROM pg_views
    ) AS objs
    ,(SELECT * FROM pg_user) AS usrs
ORDER BY fullobj
)
WHERE (sel = true or ins = true or upd = true or del = true or ref = true)
and objectname = 'my_table'

Upvotes: 5

Related Questions