Redshift中特定表的查询权限(组和用户)

时间:2019-02-12 20:23:19

标签: amazon-redshift

我正在尝试查找一个查询,该查询使我可以在Redshift中获取针对组和用户的特定表的当前权限。我知道如何进行实际的授予,但是我很难找一个正确的表进行查询以获取现有权限。

其他信息:

这将在程序操作中使用,在该操作中表被级联删除并重新创建,以确保我们从删除之前重新应用相同的权限。如果可以选择在不使用conn.execute()查询的情况下在Python中执行此操作,那么我也愿意这样做。

编辑:

我之前确实找到了以下查询,并且尝试了一个简短的简短版本,看看是否可以得到组:

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'

这给了我一个错误,提示它找不到作为组名的用户名。

2 个答案:

答案 0 :(得分:2)

您可以使用以下带有objectname过滤器的查询来查找特定表的权限

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'

答案 1 :(得分:0)

我终于找到了可以分解为组并哈希在一起的东西:

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 || '%'

How to query user group privileges in postgresql?