从 .sql 文件填充后,以编程方式测试 Postgres 数据库的正确权限
Testing Postgres DB for right permissions programmatically, after populating from .sql file
我有一个项目,其中 postgres 数据库在 ci 测试服务器中使用 .sql 文件作为种子。这将作为 docker 图像构建和发布。
现在,我不会是唯一一个将这些 .sql 文件推送到服务器的人。
所以我需要检查所有 postgres 用户(可能还有未来用户)对所有数据库的权限,由这个文件引入。
示例:
用户A和B。
用户 A 是超级用户并拥有所有数据库。
用户 B 应该只能读取数据库 X 中的读取权限。
等等
有没有办法以编程方式测试这个?除了“尝试从 Y 读取为 B -> 错误?
到目前为止我找到的所有其他解决方案都是手动解决方案或 'try/error' 解决方案。
您可以通过 access privilege inquiry functions. These all work at the level of individual objects, so for any kind of database-wide checks, you will need to combine them with catalog queries. You'll also need to consult the catalog for any ownership information. OID types 检查各种类型的数据库对象的授权,这在编写这些查询时非常有用。
检查用户 b
是否可以 read-only 访问架构 p
中的所有 table:
SELECT EVERY(
has_table_privilege('b', oid, 'SELECT') AND
NOT has_table_privilege('b', oid, 'INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER')
)
FROM pg_class
WHERE relkind = 'r'
AND relnamespace = 'p'::regnamespace
(请注意,table 特权已被 column-level 授权所取代,因此您可能还想检查这些。)
检查 a
是否是超级用户:
SELECT rolsuper
FROM pg_roles
WHERE rolname = 'a'
检查 a
是否拥有所有数据库(除了由 initdb
创建的数据库):
SELECT EVERY(datdba = 'a'::regrole)
FROM pg_database
WHERE datname NOT IN ('postgres', 'template0', 'template1')
我有一个项目,其中 postgres 数据库在 ci 测试服务器中使用 .sql 文件作为种子。这将作为 docker 图像构建和发布。
现在,我不会是唯一一个将这些 .sql 文件推送到服务器的人。 所以我需要检查所有 postgres 用户(可能还有未来用户)对所有数据库的权限,由这个文件引入。
示例: 用户A和B。 用户 A 是超级用户并拥有所有数据库。 用户 B 应该只能读取数据库 X 中的读取权限。 等等
有没有办法以编程方式测试这个?除了“尝试从 Y 读取为 B -> 错误?
到目前为止我找到的所有其他解决方案都是手动解决方案或 'try/error' 解决方案。
您可以通过 access privilege inquiry functions. These all work at the level of individual objects, so for any kind of database-wide checks, you will need to combine them with catalog queries. You'll also need to consult the catalog for any ownership information. OID types 检查各种类型的数据库对象的授权,这在编写这些查询时非常有用。
检查用户 b
是否可以 read-only 访问架构 p
中的所有 table:
SELECT EVERY(
has_table_privilege('b', oid, 'SELECT') AND
NOT has_table_privilege('b', oid, 'INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER')
)
FROM pg_class
WHERE relkind = 'r'
AND relnamespace = 'p'::regnamespace
(请注意,table 特权已被 column-level 授权所取代,因此您可能还想检查这些。)
检查 a
是否是超级用户:
SELECT rolsuper
FROM pg_roles
WHERE rolname = 'a'
检查 a
是否拥有所有数据库(除了由 initdb
创建的数据库):
SELECT EVERY(datdba = 'a'::regrole)
FROM pg_database
WHERE datname NOT IN ('postgres', 'template0', 'template1')