Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update postgresql_privs.py to include ALL x IN SCHEMA functionality for PostgreSQL versions 9.x + #282

Merged
merged 17 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/all_in_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Add functionallity to postgresql_privs.py when the PostgreSQL version is 9.0.0 or greater to incorporate `ALL x IN SCHEMA` syntax. Please see the official documentation for details regarding grants (https://www.postgresql.org/docs/9.0/sql-grant.html).
jchancojr marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 29 additions & 11 deletions plugins/modules/postgresql_privs.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ def get_type_acls(self, schema, types):
# Manipulating privileges

# WARNING: usage_on_types has been deprecated and will be removed in community.postgresql 3.0.0, please use an obj_type of 'type' instead.
def manipulate_privs(self, obj_type, privs, objs, roles, target_roles,
def manipulate_privs(self, obj_type, privs, objs, orig_objs, roles, target_roles,
state, grant_option, schema_qualifier=None, fail_on_role=True, usage_on_types=True):
"""Manipulate database object privileges.

Expand All @@ -719,6 +719,7 @@ def manipulate_privs(self, obj_type, privs, objs, roles, target_roles,
or None if type is "group".
:param objs: List of database objects to grant/revoke
privileges for.
:param orig_objs: ALL_IN_SCHEMA or null
jchancojr marked this conversation as resolved.
Show resolved Hide resolved
:param roles: Either a list of role names or "PUBLIC"
for the implicitly defined "PUBLIC" group
:param target_roles: List of role names to grant/revoke
Expand Down Expand Up @@ -791,8 +792,10 @@ def manipulate_privs(self, obj_type, privs, objs, roles, target_roles,
# Note: obj_type has been checked against a set of string literals
# and privs was escaped when it was parsed
# Note: Underscores are replaced with spaces to support multi-word obj_type
set_what = '%s ON %s %s' % (','.join(privs), obj_type.replace('_', ' '),
','.join(obj_ids))
if orig_objs is not None:
set_what = '%s ON %s %s' % (','.join(privs), orig_objs, schema_qualifier)
else:
set_what = '%s ON %s %s' % (','.join(privs), obj_type.replace('_', ' '), ','.join(obj_ids))

# for_whom: SQL-fragment specifying for whom to set the above
if roles == 'PUBLIC':
Expand Down Expand Up @@ -1093,14 +1096,28 @@ def main():
else:
privs = None
# objs:
if p.type == 'table' and p.objs == 'ALL_IN_SCHEMA':
objs = conn.get_all_tables_in_schema(p.schema)
elif p.type == 'sequence' and p.objs == 'ALL_IN_SCHEMA':
objs = conn.get_all_sequences_in_schema(p.schema)
elif p.type == 'function' and p.objs == 'ALL_IN_SCHEMA':
objs = conn.get_all_functions_in_schema(p.schema)
elif p.type == 'procedure' and p.objs == 'ALL_IN_SCHEMA':
objs = conn.get_all_procedures_in_schema(p.schema)
if p.objs == 'ALL_IN_SCHEMA':
jchancojr marked this conversation as resolved.
Show resolved Hide resolved
if p.type == 'table':
objs = conn.get_all_tables_in_schema(p.schema)
elif p.type == 'sequence':
objs = conn.get_all_sequences_in_schema(p.schema)
elif p.type == 'function':
objs = conn.get_all_functions_in_schema(p.schema)
elif p.type == 'procedure':
objs = conn.get_all_procedures_in_schema(p.schema)

if conn.pg_version >= 90000:
if p.type == 'table':
orig_objs = 'ALL TABLES IN SCHEMA'
elif p.type == 'sequence':
orig_objs = 'ALL SEQUENCES IN SCHEMA'
elif p.type == 'function':
orig_objs = 'ALL FUNCTIONS IN SCHEMA'
elif p.type == 'procedure':
orig_objs = 'ALL PROCEDURES IN SCHEMA'
else:
orig_objs = None
jchancojr marked this conversation as resolved.
Show resolved Hide resolved

elif p.type == 'default_privs':
if p.objs == 'ALL_DEFAULT':
objs = frozenset(VALID_DEFAULT_OBJS.keys())
Expand Down Expand Up @@ -1150,6 +1167,7 @@ def main():
obj_type=p.type,
privs=privs,
objs=objs,
orig_objs=orig_objs,
roles=roles,
target_roles=target_roles,
state=p.state,
Expand Down