| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) Ansible project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: keycloak_client_rolemapping |
| 11 |
|
| 12 |
short_description: Allows administration of Keycloak client_rolemapping with the Keycloak API |
| 13 |
|
| 14 |
version_added: 3.5.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- This module allows you to add, remove or modify Keycloak client_rolemapping with the Keycloak REST API. It requires access |
| 18 |
to the REST API using OpenID Connect; the user connecting and the client being used must have the requisite access rights. |
| 19 |
In a default Keycloak installation, admin-cli and an admin user would work, as would a separate client definition with |
| 20 |
the scope tailored to your needs and a user having the expected roles. |
| 21 |
- The names of module options are snake_cased versions of the camelCase ones found in the Keycloak API and its documentation |
| 22 |
at U(https://www.keycloak.org/docs-api/8.0/rest-api/index.html). |
| 23 |
- Attributes are multi-valued in the Keycloak API. All attributes are lists of individual values and are returned that way |
| 24 |
by this module. You may pass single values for attributes when calling the module, and this is translated into a list |
| 25 |
suitable for the API. |
| 26 |
- When updating a client_rolemapping, where possible provide the role ID to the module. This removes a lookup to the API |
| 27 |
to translate the name into the role ID. |
| 28 |
attributes: |
| 29 |
check_mode: |
| 30 |
support: full |
| 31 |
diff_mode: |
| 32 |
support: full |
| 33 |
action_group: |
| 34 |
version_added: 10.2.0 |
| 35 |
|
| 36 |
options: |
| 37 |
state: |
| 38 |
description: |
| 39 |
- State of the client_rolemapping. |
| 40 |
- On V(present), the client_rolemapping is created if it does not yet exist, or updated with the parameters |
| 41 |
you provide. |
| 42 |
- On V(absent), the client_rolemapping is removed if it exists. |
| 43 |
default: 'present' |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- present |
| 47 |
- absent |
| 48 |
|
| 49 |
realm: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- They Keycloak realm under which this role_representation resides. |
| 53 |
default: 'master' |
| 54 |
|
| 55 |
group_name: |
| 56 |
type: str |
| 57 |
description: |
| 58 |
- Name of the group to be mapped. |
| 59 |
- This parameter is required (can be replaced by gid for less API call). |
| 60 |
parents: |
| 61 |
version_added: "7.1.0" |
| 62 |
type: list |
| 63 |
description: |
| 64 |
- List of parent groups for the group to handle sorted top to bottom. |
| 65 |
- Set this if your group is a subgroup and you do not provide the GID in O(gid). |
| 66 |
elements: dict |
| 67 |
suboptions: |
| 68 |
id: |
| 69 |
type: str |
| 70 |
description: |
| 71 |
- Identify parent by ID. |
| 72 |
- Needs less API calls than using O(parents[].name). |
| 73 |
- A deep parent chain can be started at any point when first given parent is given as ID. |
| 74 |
- Note that in principle both ID and name can be specified at the same time but current implementation only always |
| 75 |
use just one of them, with ID being preferred. |
| 76 |
name: |
| 77 |
type: str |
| 78 |
description: |
| 79 |
- Identify parent by name. |
| 80 |
- Needs more internal API calls than using O(parents[].id) to map names to ID's under the hood. |
| 81 |
- When giving a parent chain with only names it must be complete up to the top. |
| 82 |
- Note that in principle both ID and name can be specified at the same time but current implementation only always |
| 83 |
use just one of them, with ID being preferred. |
| 84 |
gid: |
| 85 |
type: str |
| 86 |
description: |
| 87 |
- ID of the group to be mapped. |
| 88 |
- This parameter is not required for updating or deleting the rolemapping but providing it reduces the number of API |
| 89 |
calls required. |
| 90 |
client_id: |
| 91 |
type: str |
| 92 |
description: |
| 93 |
- Name of the client to be mapped (different than O(cid)). |
| 94 |
- This parameter is required (can be replaced by cid for less API call). |
| 95 |
cid: |
| 96 |
type: str |
| 97 |
description: |
| 98 |
- ID of the client to be mapped. |
| 99 |
- This parameter is not required for updating or deleting the rolemapping but providing it reduces the number of API |
| 100 |
calls required. |
| 101 |
roles: |
| 102 |
description: |
| 103 |
- Roles to be mapped to the group. |
| 104 |
type: list |
| 105 |
elements: dict |
| 106 |
suboptions: |
| 107 |
name: |
| 108 |
type: str |
| 109 |
description: |
| 110 |
- Name of the role_representation. |
| 111 |
- This parameter is required only when creating or updating the role_representation. |
| 112 |
id: |
| 113 |
type: str |
| 114 |
description: |
| 115 |
- The unique identifier for this role_representation. |
| 116 |
- This parameter is not required for updating or deleting a role_representation but providing it reduces the number |
| 117 |
of API calls required. |
| 118 |
extends_documentation_fragment: |
| 119 |
- community.general._keycloak |
| 120 |
- community.general._keycloak.actiongroup_keycloak |
| 121 |
- community.general._attributes |
| 122 |
|
| 123 |
author: |
| 124 |
- Gaƫtan Daubresse (@Gaetan2907) |
| 125 |
""" |
| 126 |
|
| 127 |
EXAMPLES = r""" |
| 128 |
- name: Map a client role to a group, authentication with credentials |
| 129 |
community.general.keycloak_client_rolemapping: |
| 130 |
realm: MyCustomRealm |
| 131 |
auth_client_id: admin-cli |
| 132 |
auth_keycloak_url: https://auth.example.com/auth |
| 133 |
auth_realm: master |
| 134 |
auth_username: USERNAME |
| 135 |
auth_password: PASSWORD |
| 136 |
state: present |
| 137 |
client_id: client1 |
| 138 |
group_name: group1 |
| 139 |
roles: |
| 140 |
- name: role_name1 |
| 141 |
id: role_id1 |
| 142 |
- name: role_name2 |
| 143 |
id: role_id2 |
| 144 |
delegate_to: localhost |
| 145 |
|
| 146 |
- name: Map a client role to a group, authentication with token |
| 147 |
community.general.keycloak_client_rolemapping: |
| 148 |
realm: MyCustomRealm |
| 149 |
auth_client_id: admin-cli |
| 150 |
auth_keycloak_url: https://auth.example.com/auth |
| 151 |
token: TOKEN |
| 152 |
state: present |
| 153 |
client_id: client1 |
| 154 |
group_name: group1 |
| 155 |
roles: |
| 156 |
- name: role_name1 |
| 157 |
id: role_id1 |
| 158 |
- name: role_name2 |
| 159 |
id: role_id2 |
| 160 |
delegate_to: localhost |
| 161 |
|
| 162 |
- name: Map a client role to a subgroup, authentication with token |
| 163 |
community.general.keycloak_client_rolemapping: |
| 164 |
realm: MyCustomRealm |
| 165 |
auth_client_id: admin-cli |
| 166 |
auth_keycloak_url: https://auth.example.com/auth |
| 167 |
token: TOKEN |
| 168 |
state: present |
| 169 |
client_id: client1 |
| 170 |
group_name: subgroup1 |
| 171 |
parents: |
| 172 |
- name: parent-group |
| 173 |
roles: |
| 174 |
- name: role_name1 |
| 175 |
id: role_id1 |
| 176 |
- name: role_name2 |
| 177 |
id: role_id2 |
| 178 |
delegate_to: localhost |
| 179 |
|
| 180 |
- name: Unmap client role from a group |
| 181 |
community.general.keycloak_client_rolemapping: |
| 182 |
realm: MyCustomRealm |
| 183 |
auth_client_id: admin-cli |
| 184 |
auth_keycloak_url: https://auth.example.com/auth |
| 185 |
auth_realm: master |
| 186 |
auth_username: USERNAME |
| 187 |
auth_password: PASSWORD |
| 188 |
state: absent |
| 189 |
client_id: client1 |
| 190 |
group_name: group1 |
| 191 |
roles: |
| 192 |
- name: role_name1 |
| 193 |
id: role_id1 |
| 194 |
- name: role_name2 |
| 195 |
id: role_id2 |
| 196 |
delegate_to: localhost |
| 197 |
""" |
| 198 |
|
| 199 |
RETURN = r""" |
| 200 |
msg: |
| 201 |
description: Message as to what action was taken. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
sample: "Role role1 assigned to group group1." |
| 205 |
|
| 206 |
proposed: |
| 207 |
description: Representation of proposed client role mapping. |
| 208 |
returned: always |
| 209 |
type: dict |
| 210 |
sample: {"clientId": "test"} |
| 211 |
|
| 212 |
existing: |
| 213 |
description: |
| 214 |
- Representation of existing client role mapping. |
| 215 |
- The sample is truncated. |
| 216 |
returned: always |
| 217 |
type: dict |
| 218 |
sample: |
| 219 |
{ |
| 220 |
"adminUrl": "http://www.example.com/admin_url", |
| 221 |
"attributes": { |
| 222 |
"request.object.signature.alg": "RS256" |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
end_state: |
| 227 |
description: |
| 228 |
- Representation of client role mapping after module execution. |
| 229 |
- The sample is truncated. |
| 230 |
returned: on success |
| 231 |
type: dict |
| 232 |
sample: |
| 233 |
{ |
| 234 |
"adminUrl": "http://www.example.com/admin_url", |
| 235 |
"attributes": { |
| 236 |
"request.object.signature.alg": "RS256" |
| 237 |
} |
| 238 |
} |
| 239 |
""" |
| 240 |
|
| 241 |
from ansible.module_utils.basic import AnsibleModule |
| 242 |
|
| 243 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 244 |
KeycloakAPI, |
| 245 |
KeycloakError, |
| 246 |
get_token, |
| 247 |
keycloak_argument_spec, |
| 248 |
) |
| 249 |
|
| 250 |
|
| 251 |
def main(): |
| 252 |
""" |
| 253 |
Module execution |
| 254 |
|
| 255 |
:return: |
| 256 |
""" |
| 257 |
argument_spec = keycloak_argument_spec() |
| 258 |
|
| 259 |
roles_spec = dict( |
| 260 |
name=dict(type="str"), |
| 261 |
id=dict(type="str"), |
| 262 |
) |
| 263 |
|
| 264 |
meta_args = dict( |
| 265 |
state=dict(default="present", choices=["present", "absent"]), |
| 266 |
realm=dict(default="master"), |
| 267 |
gid=dict(type="str"), |
| 268 |
group_name=dict(type="str"), |
| 269 |
parents=dict( |
| 270 |
type="list", |
| 271 |
elements="dict", |
| 272 |
options=dict(id=dict(type="str"), name=dict(type="str")), |
| 273 |
), |
| 274 |
cid=dict(type="str"), |
| 275 |
client_id=dict(type="str"), |
| 276 |
roles=dict(type="list", elements="dict", options=roles_spec), |
| 277 |
) |
| 278 |
|
| 279 |
argument_spec.update(meta_args) |
| 280 |
|
| 281 |
module = AnsibleModule( |
| 282 |
argument_spec=argument_spec, |
| 283 |
supports_check_mode=True, |
| 284 |
required_one_of=( |
| 285 |
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]] |
| 286 |
), |
| 287 |
required_together=([["auth_username", "auth_password"]]), |
| 288 |
required_by={"refresh_token": "auth_realm"}, |
| 289 |
) |
| 290 |
|
| 291 |
result = dict(changed=False, msg="", diff={}, proposed={}, existing={}, end_state={}) |
| 292 |
|
| 293 |
# Obtain access token, initialize API |
| 294 |
try: |
| 295 |
connection_header = get_token(module.params) |
| 296 |
except KeycloakError as e: |
| 297 |
module.fail_json(msg=str(e)) |
| 298 |
|
| 299 |
kc = KeycloakAPI(module, connection_header) |
| 300 |
|
| 301 |
realm = module.params.get("realm") |
| 302 |
state = module.params.get("state") |
| 303 |
cid = module.params.get("cid") |
| 304 |
client_id = module.params.get("client_id") |
| 305 |
gid = module.params.get("gid") |
| 306 |
group_name = module.params.get("group_name") |
| 307 |
roles = module.params.get("roles") |
| 308 |
parents = module.params.get("parents") |
| 309 |
|
| 310 |
# Check the parameters |
| 311 |
if cid is None and client_id is None: |
| 312 |
module.fail_json(msg="Either the `client_id` or `cid` has to be specified.") |
| 313 |
if gid is None and group_name is None: |
| 314 |
module.fail_json(msg="Either the `group_name` or `gid` has to be specified.") |
| 315 |
|
| 316 |
# Get the potential missing parameters |
| 317 |
if gid is None: |
| 318 |
group_rep = kc.get_group_by_name(group_name, realm=realm, parents=parents) |
| 319 |
if group_rep is not None: |
| 320 |
gid = group_rep["id"] |
| 321 |
else: |
| 322 |
module.fail_json(msg=f"Could not fetch group {group_name}:") |
| 323 |
if cid is None: |
| 324 |
cid = kc.get_client_id(client_id, realm=realm) |
| 325 |
if cid is None: |
| 326 |
module.fail_json(msg=f"Could not fetch client {client_id}:") |
| 327 |
if roles is None: |
| 328 |
module.exit_json(msg="Nothing to do (no roles specified).") |
| 329 |
else: |
| 330 |
for role in roles: |
| 331 |
if role["name"] is None and role["id"] is None: |
| 332 |
module.fail_json(msg="Either the `name` or `id` has to be specified on each role.") |
| 333 |
# Fetch missing role_id |
| 334 |
if role["id"] is None: |
| 335 |
role_id = kc.get_client_role_id_by_name(cid, role["name"], realm=realm) |
| 336 |
if role_id is not None: |
| 337 |
role["id"] = role_id |
| 338 |
else: |
| 339 |
module.fail_json(msg=f"Could not fetch role {role['name']}:") |
| 340 |
# Fetch missing role_name |
| 341 |
else: |
| 342 |
role["name"] = kc.get_client_group_rolemapping_by_id(gid, cid, role["id"], realm=realm)["name"] |
| 343 |
if role["name"] is None: |
| 344 |
module.fail_json(msg=f"Could not fetch role {role['id']}") |
| 345 |
|
| 346 |
# Get effective client-level role mappings |
| 347 |
available_roles_before = kc.get_client_group_available_rolemappings(gid, cid, realm=realm) |
| 348 |
assigned_roles_before = kc.get_client_group_composite_rolemappings(gid, cid, realm=realm) |
| 349 |
|
| 350 |
result["existing"] = assigned_roles_before |
| 351 |
result["proposed"] = list(assigned_roles_before) if assigned_roles_before else [] |
| 352 |
|
| 353 |
update_roles = [] |
| 354 |
for role in roles: |
| 355 |
# Fetch roles to assign if state present |
| 356 |
if state == "present": |
| 357 |
for available_role in available_roles_before: |
| 358 |
if role["name"] == available_role["name"]: |
| 359 |
update_roles.append( |
| 360 |
{ |
| 361 |
"id": role["id"], |
| 362 |
"name": role["name"], |
| 363 |
} |
| 364 |
) |
| 365 |
result["proposed"].append(available_role) |
| 366 |
# Fetch roles to remove if state absent |
| 367 |
else: |
| 368 |
for assigned_role in assigned_roles_before: |
| 369 |
if role["name"] == assigned_role["name"]: |
| 370 |
update_roles.append( |
| 371 |
{ |
| 372 |
"id": role["id"], |
| 373 |
"name": role["name"], |
| 374 |
} |
| 375 |
) |
| 376 |
if assigned_role in result["proposed"]: # Handle double removal |
| 377 |
result["proposed"].remove(assigned_role) |
| 378 |
|
| 379 |
if len(update_roles): |
| 380 |
if state == "present": |
| 381 |
# Assign roles |
| 382 |
result["changed"] = True |
| 383 |
if module._diff: |
| 384 |
result["diff"] = dict(before=assigned_roles_before, after=result["proposed"]) |
| 385 |
if module.check_mode: |
| 386 |
module.exit_json(**result) |
| 387 |
kc.add_group_rolemapping(gid, cid, update_roles, realm=realm) |
| 388 |
result["msg"] = f"Roles {update_roles} assigned to group {group_name}." |
| 389 |
assigned_roles_after = kc.get_client_group_composite_rolemappings(gid, cid, realm=realm) |
| 390 |
result["end_state"] = assigned_roles_after |
| 391 |
module.exit_json(**result) |
| 392 |
else: |
| 393 |
# Remove mapping of role |
| 394 |
result["changed"] = True |
| 395 |
if module._diff: |
| 396 |
result["diff"] = dict(before=assigned_roles_before, after=result["proposed"]) |
| 397 |
if module.check_mode: |
| 398 |
module.exit_json(**result) |
| 399 |
kc.delete_group_rolemapping(gid, cid, update_roles, realm=realm) |
| 400 |
result["msg"] = f"Roles {update_roles} removed from group {group_name}." |
| 401 |
assigned_roles_after = kc.get_client_group_composite_rolemappings(gid, cid, realm=realm) |
| 402 |
result["end_state"] = assigned_roles_after |
| 403 |
module.exit_json(**result) |
| 404 |
# Do nothing |
| 405 |
else: |
| 406 |
result["changed"] = False |
| 407 |
result["msg"] = ( |
| 408 |
f"Nothing to do, roles {roles} are {'mapped' if state == 'present' else 'not mapped'} with group {group_name}." |
| 409 |
) |
| 410 |
module.exit_json(**result) |
| 411 |
|
| 412 |
|
| 413 |
if __name__ == "__main__": |
| 414 |
main() |