amazon-redshift

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.


Solution

  • 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?