| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Andrew Klychkov (@Andersson007) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: postgresql_membership |
| 14 |
short_description: Add or remove PostgreSQL roles from groups |
| 15 |
description: |
| 16 |
- Adds or removes PostgreSQL roles from groups (other roles). |
| 17 |
- Users are roles with login privilege. |
| 18 |
- Groups are PostgreSQL roles usually without LOGIN privilege. |
| 19 |
- "Common use case:" |
| 20 |
- 1) add a new group (groups) by M(community.postgresql.postgresql_user) module with I(role_attr_flags=NOLOGIN) |
| 21 |
- 2) grant them desired privileges by M(community.postgresql.postgresql_privs) module |
| 22 |
- 3) add desired PostgreSQL users to the new group (groups) by this module |
| 23 |
options: |
| 24 |
groups: |
| 25 |
description: |
| 26 |
- The list of groups (roles) that need to be granted to or revoked from I(target_roles). |
| 27 |
required: true |
| 28 |
type: list |
| 29 |
elements: str |
| 30 |
aliases: |
| 31 |
- group |
| 32 |
- source_role |
| 33 |
- source_roles |
| 34 |
target_roles: |
| 35 |
description: |
| 36 |
- The list of target roles (groups will be granted to them). |
| 37 |
required: true |
| 38 |
type: list |
| 39 |
elements: str |
| 40 |
aliases: |
| 41 |
- target_role |
| 42 |
- users |
| 43 |
- user |
| 44 |
fail_on_role: |
| 45 |
description: |
| 46 |
- If C(true), fail when group or target_role doesn't exist. If C(false), just warn and continue. |
| 47 |
default: true |
| 48 |
type: bool |
| 49 |
state: |
| 50 |
description: |
| 51 |
- Membership state. |
| 52 |
- I(state=present) implies the I(groups)must be granted to I(target_roles). |
| 53 |
- I(state=absent) implies the I(groups) must be revoked from I(target_roles). |
| 54 |
- I(state=exact) implies that I(target_roles) will be members of only the I(groups) |
| 55 |
(available since community.postgresql 2.2.0). |
| 56 |
Any other groups will be revoked from I(target_roles). |
| 57 |
type: str |
| 58 |
default: present |
| 59 |
choices: [ absent, exact, present ] |
| 60 |
login_db: |
| 61 |
description: |
| 62 |
- Name of database to connect to. |
| 63 |
- The V(db) alias is deprecated and will be removed in version 5.0.0. |
| 64 |
type: str |
| 65 |
aliases: |
| 66 |
- db |
| 67 |
session_role: |
| 68 |
description: |
| 69 |
- Switch to session_role after connecting. |
| 70 |
The specified session_role must be a role that the current login_user is a member of. |
| 71 |
- Permissions checking for SQL commands is carried out as though |
| 72 |
the session_role were the one that had logged in originally. |
| 73 |
type: str |
| 74 |
trust_input: |
| 75 |
description: |
| 76 |
- If C(false), check whether values of parameters I(groups), |
| 77 |
I(target_roles), I(session_role) are potentially dangerous. |
| 78 |
- It makes sense to use C(false) only when SQL injections via the parameters are possible. |
| 79 |
type: bool |
| 80 |
default: true |
| 81 |
version_added: '0.2.0' |
| 82 |
seealso: |
| 83 |
- module: community.postgresql.postgresql_user |
| 84 |
- module: community.postgresql.postgresql_privs |
| 85 |
- module: community.postgresql.postgresql_owner |
| 86 |
- name: PostgreSQL role membership reference |
| 87 |
description: Complete reference of the PostgreSQL role membership documentation. |
| 88 |
link: https://www.postgresql.org/docs/current/role-membership.html |
| 89 |
- name: PostgreSQL role attributes reference |
| 90 |
description: Complete reference of the PostgreSQL role attributes documentation. |
| 91 |
link: https://www.postgresql.org/docs/current/role-attributes.html |
| 92 |
|
| 93 |
attributes: |
| 94 |
check_mode: |
| 95 |
support: full |
| 96 |
|
| 97 |
author: |
| 98 |
- Andrew Klychkov (@Andersson007) |
| 99 |
|
| 100 |
extends_documentation_fragment: |
| 101 |
- community.postgresql.postgres |
| 102 |
''' |
| 103 |
|
| 104 |
EXAMPLES = r''' |
| 105 |
- name: Grant role read_only to alice and bob |
| 106 |
community.postgresql.postgresql_membership: |
| 107 |
group: read_only |
| 108 |
target_roles: |
| 109 |
- alice |
| 110 |
- bob |
| 111 |
state: present |
| 112 |
|
| 113 |
# you can also use target_roles: alice,bob,etc to pass the role list |
| 114 |
|
| 115 |
- name: Revoke role read_only and exec_func from bob. Ignore if roles don't exist |
| 116 |
community.postgresql.postgresql_membership: |
| 117 |
groups: |
| 118 |
- read_only |
| 119 |
- exec_func |
| 120 |
target_role: bob |
| 121 |
fail_on_role: false |
| 122 |
state: absent |
| 123 |
|
| 124 |
- name: > |
| 125 |
Make sure alice and bob are members only of marketing and sales. |
| 126 |
If they are members of other groups, they will be removed from those groups |
| 127 |
community.postgresql.postgresql_membership: |
| 128 |
group: |
| 129 |
- marketing |
| 130 |
- sales |
| 131 |
target_roles: |
| 132 |
- alice |
| 133 |
- bob |
| 134 |
state: exact |
| 135 |
|
| 136 |
- name: Make sure alice and bob do not belong to any groups |
| 137 |
community.postgresql.postgresql_membership: |
| 138 |
group: [] |
| 139 |
target_roles: |
| 140 |
- alice |
| 141 |
- bob |
| 142 |
state: exact |
| 143 |
''' |
| 144 |
|
| 145 |
RETURN = r''' |
| 146 |
queries: |
| 147 |
description: List of executed queries. |
| 148 |
returned: success |
| 149 |
type: str |
| 150 |
sample: [ "GRANT \"user_ro\" TO \"alice\"" ] |
| 151 |
granted: |
| 152 |
description: Dict of granted groups and roles. |
| 153 |
returned: if I(state=present) |
| 154 |
type: dict |
| 155 |
sample: { "ro_group": [ "alice", "bob" ] } |
| 156 |
revoked: |
| 157 |
description: Dict of revoked groups and roles. |
| 158 |
returned: if I(state=absent) |
| 159 |
type: dict |
| 160 |
sample: { "ro_group": [ "alice", "bob" ] } |
| 161 |
state: |
| 162 |
description: Membership state that tried to be set. |
| 163 |
returned: success |
| 164 |
type: str |
| 165 |
sample: "present" |
| 166 |
''' |
| 167 |
|
| 168 |
from ansible.module_utils.basic import AnsibleModule |
| 169 |
from ansible_collections.community.postgresql.plugins.module_utils.database import \ |
| 170 |
check_input |
| 171 |
from ansible_collections.community.postgresql.plugins.module_utils.postgres import ( |
| 172 |
PgMembership, |
| 173 |
connect_to_db, |
| 174 |
ensure_required_libs, |
| 175 |
get_conn_params, |
| 176 |
pg_cursor_args, |
| 177 |
postgres_common_argument_spec, |
| 178 |
) |
| 179 |
|
| 180 |
# =========================================== |
| 181 |
# Module execution. |
| 182 |
# |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
argument_spec = postgres_common_argument_spec() |
| 187 |
argument_spec.update( |
| 188 |
groups=dict(type='list', elements='str', required=True, aliases=['group', 'source_role', 'source_roles']), |
| 189 |
target_roles=dict(type='list', elements='str', required=True, aliases=['target_role', 'user', 'users']), |
| 190 |
fail_on_role=dict(type='bool', default=True), |
| 191 |
state=dict(type='str', default='present', choices=['absent', 'exact', 'present']), |
| 192 |
login_db=dict(type='str', aliases=['db'], deprecated_aliases=[ |
| 193 |
{ |
| 194 |
'name': 'db', |
| 195 |
'version': '5.0.0', |
| 196 |
'collection_name': 'community.postgresql', |
| 197 |
}], |
| 198 |
), |
| 199 |
session_role=dict(type='str'), |
| 200 |
trust_input=dict(type='bool', default=True), |
| 201 |
) |
| 202 |
|
| 203 |
module = AnsibleModule( |
| 204 |
argument_spec=argument_spec, |
| 205 |
supports_check_mode=True, |
| 206 |
) |
| 207 |
|
| 208 |
groups = module.params['groups'] |
| 209 |
target_roles = module.params['target_roles'] |
| 210 |
fail_on_role = module.params['fail_on_role'] |
| 211 |
state = module.params['state'] |
| 212 |
session_role = module.params['session_role'] |
| 213 |
trust_input = module.params['trust_input'] |
| 214 |
if not trust_input: |
| 215 |
# Check input for potentially dangerous elements: |
| 216 |
check_input(module, groups, target_roles, session_role) |
| 217 |
|
| 218 |
# Ensure psycopg libraries are available before connecting to DB: |
| 219 |
ensure_required_libs(module) |
| 220 |
conn_params = get_conn_params(module, module.params, warn_db_default=False) |
| 221 |
db_connection, dummy = connect_to_db(module, conn_params, autocommit=False) |
| 222 |
cursor = db_connection.cursor(**pg_cursor_args) |
| 223 |
|
| 224 |
############## |
| 225 |
# Create the object and do main job: |
| 226 |
|
| 227 |
pg_membership = PgMembership(module, cursor, groups, target_roles, fail_on_role) |
| 228 |
|
| 229 |
if state == 'present': |
| 230 |
pg_membership.grant() |
| 231 |
|
| 232 |
elif state == 'exact': |
| 233 |
pg_membership.match() |
| 234 |
|
| 235 |
elif state == 'absent': |
| 236 |
pg_membership.revoke() |
| 237 |
|
| 238 |
# Rollback if it's possible and check_mode: |
| 239 |
if module.check_mode: |
| 240 |
db_connection.rollback() |
| 241 |
else: |
| 242 |
db_connection.commit() |
| 243 |
|
| 244 |
cursor.close() |
| 245 |
db_connection.close() |
| 246 |
|
| 247 |
# Make return values: |
| 248 |
return_dict = dict( |
| 249 |
changed=pg_membership.changed, |
| 250 |
state=state, |
| 251 |
groups=pg_membership.groups, |
| 252 |
target_roles=pg_membership.target_roles, |
| 253 |
queries=pg_membership.executed_queries, |
| 254 |
) |
| 255 |
|
| 256 |
if state == 'present': |
| 257 |
return_dict['granted'] = pg_membership.granted |
| 258 |
elif state == 'absent': |
| 259 |
return_dict['revoked'] = pg_membership.revoked |
| 260 |
elif state == 'exact': |
| 261 |
return_dict['granted'] = pg_membership.granted |
| 262 |
return_dict['revoked'] = pg_membership.revoked |
| 263 |
|
| 264 |
module.exit_json(**return_dict) |
| 265 |
|
| 266 |
|
| 267 |
if __name__ == '__main__': |
| 268 |
main() |