| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019, INSPQ (@elfelip) |
| 4 |
# GNU General Public License v3.0+ (see COPYING 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_user |
| 11 |
short_description: Create and configure a user in Keycloak |
| 12 |
description: |
| 13 |
- This module creates, removes, or updates Keycloak users. |
| 14 |
version_added: 7.1.0 |
| 15 |
options: |
| 16 |
auth_username: |
| 17 |
aliases: [] |
| 18 |
realm: |
| 19 |
description: |
| 20 |
- The name of the realm in which is the client. |
| 21 |
default: master |
| 22 |
type: str |
| 23 |
username: |
| 24 |
description: |
| 25 |
- Username for the user. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
id: |
| 29 |
description: |
| 30 |
- ID of the user on the Keycloak server if known. |
| 31 |
type: str |
| 32 |
enabled: |
| 33 |
description: |
| 34 |
- Enabled user. |
| 35 |
type: bool |
| 36 |
email_verified: |
| 37 |
description: |
| 38 |
- Check the validity of user email. |
| 39 |
default: false |
| 40 |
type: bool |
| 41 |
aliases: |
| 42 |
- emailVerified |
| 43 |
first_name: |
| 44 |
description: |
| 45 |
- The user's first name. |
| 46 |
type: str |
| 47 |
aliases: |
| 48 |
- firstName |
| 49 |
last_name: |
| 50 |
description: |
| 51 |
- The user's last name. |
| 52 |
type: str |
| 53 |
aliases: |
| 54 |
- lastName |
| 55 |
email: |
| 56 |
description: |
| 57 |
- User email. |
| 58 |
type: str |
| 59 |
federation_link: |
| 60 |
description: |
| 61 |
- Federation Link. |
| 62 |
type: str |
| 63 |
aliases: |
| 64 |
- federationLink |
| 65 |
service_account_client_id: |
| 66 |
description: |
| 67 |
- Description of the client Application. |
| 68 |
type: str |
| 69 |
aliases: |
| 70 |
- serviceAccountClientId |
| 71 |
client_consents: |
| 72 |
description: |
| 73 |
- Client Authenticator Type. |
| 74 |
type: list |
| 75 |
elements: dict |
| 76 |
default: [] |
| 77 |
aliases: |
| 78 |
- clientConsents |
| 79 |
suboptions: |
| 80 |
client_id: |
| 81 |
description: |
| 82 |
- Client ID of the client role. Not the technical ID of the client. |
| 83 |
type: str |
| 84 |
required: true |
| 85 |
aliases: |
| 86 |
- clientId |
| 87 |
roles: |
| 88 |
description: |
| 89 |
- List of client roles to assign to the user. |
| 90 |
type: list |
| 91 |
required: true |
| 92 |
elements: str |
| 93 |
groups: |
| 94 |
description: |
| 95 |
- List of groups for the user. |
| 96 |
- Groups can be referenced by their name, like V(staff), or their path, like V(/staff/engineering). The path syntax |
| 97 |
allows you to reference subgroups, which is not possible otherwise. |
| 98 |
- Using the path is possible since community.general 10.6.0. |
| 99 |
type: list |
| 100 |
elements: dict |
| 101 |
default: [] |
| 102 |
suboptions: |
| 103 |
name: |
| 104 |
description: |
| 105 |
- Name of the group. |
| 106 |
type: str |
| 107 |
state: |
| 108 |
description: |
| 109 |
- Control whether the user must be member of this group or not. |
| 110 |
choices: ["present", "absent"] |
| 111 |
default: present |
| 112 |
type: str |
| 113 |
credentials: |
| 114 |
description: |
| 115 |
- User credentials. |
| 116 |
default: [] |
| 117 |
type: list |
| 118 |
elements: dict |
| 119 |
suboptions: |
| 120 |
type: |
| 121 |
description: |
| 122 |
- Credential type. |
| 123 |
type: str |
| 124 |
required: true |
| 125 |
value: |
| 126 |
description: |
| 127 |
- Value of the credential. |
| 128 |
type: str |
| 129 |
required: true |
| 130 |
temporary: |
| 131 |
description: |
| 132 |
- If V(true), the users are required to reset their credentials at next login. |
| 133 |
type: bool |
| 134 |
default: false |
| 135 |
required_actions: |
| 136 |
description: |
| 137 |
- RequiredActions user Auth. |
| 138 |
default: [] |
| 139 |
type: list |
| 140 |
elements: str |
| 141 |
aliases: |
| 142 |
- requiredActions |
| 143 |
federated_identities: |
| 144 |
description: |
| 145 |
- List of IDPs of user. |
| 146 |
default: [] |
| 147 |
type: list |
| 148 |
elements: str |
| 149 |
aliases: |
| 150 |
- federatedIdentities |
| 151 |
attributes: |
| 152 |
description: |
| 153 |
- List of user attributes. |
| 154 |
type: list |
| 155 |
elements: dict |
| 156 |
suboptions: |
| 157 |
name: |
| 158 |
description: |
| 159 |
- Name of the attribute. |
| 160 |
type: str |
| 161 |
values: |
| 162 |
description: |
| 163 |
- Values for the attribute as list. |
| 164 |
type: list |
| 165 |
elements: str |
| 166 |
state: |
| 167 |
description: |
| 168 |
- Control whether the attribute must exists or not. |
| 169 |
choices: ["present", "absent"] |
| 170 |
default: present |
| 171 |
type: str |
| 172 |
access: |
| 173 |
description: |
| 174 |
- List user access. |
| 175 |
type: dict |
| 176 |
disableable_credential_types: |
| 177 |
description: |
| 178 |
- List user Credential Type. |
| 179 |
default: [] |
| 180 |
type: list |
| 181 |
elements: str |
| 182 |
aliases: |
| 183 |
- disableableCredentialTypes |
| 184 |
origin: |
| 185 |
description: |
| 186 |
- User origin. |
| 187 |
type: str |
| 188 |
self: |
| 189 |
description: |
| 190 |
- User self administration. |
| 191 |
type: str |
| 192 |
state: |
| 193 |
description: |
| 194 |
- Control whether the user should exists or not. |
| 195 |
choices: ["present", "absent"] |
| 196 |
default: present |
| 197 |
type: str |
| 198 |
force: |
| 199 |
description: |
| 200 |
- If V(true), allows to remove user and recreate it. |
| 201 |
type: bool |
| 202 |
default: false |
| 203 |
extends_documentation_fragment: |
| 204 |
- community.general._keycloak |
| 205 |
- community.general._keycloak.actiongroup_keycloak |
| 206 |
- community.general._attributes |
| 207 |
attributes: |
| 208 |
check_mode: |
| 209 |
support: full |
| 210 |
diff_mode: |
| 211 |
support: full |
| 212 |
action_group: |
| 213 |
version_added: 10.2.0 |
| 214 |
notes: |
| 215 |
- The module does not modify the user ID of an existing user. |
| 216 |
author: |
| 217 |
- Philippe Gauthier (@elfelip) |
| 218 |
""" |
| 219 |
|
| 220 |
EXAMPLES = r""" |
| 221 |
- name: Create a user user1 |
| 222 |
community.general.keycloak_user: |
| 223 |
auth_keycloak_url: http://localhost:8080/auth |
| 224 |
auth_username: admin |
| 225 |
auth_password: password |
| 226 |
realm: master |
| 227 |
username: user1 |
| 228 |
firstName: user1 |
| 229 |
lastName: user1 |
| 230 |
email: user1 |
| 231 |
enabled: true |
| 232 |
emailVerified: false |
| 233 |
credentials: |
| 234 |
- type: password |
| 235 |
value: password |
| 236 |
temporary: false |
| 237 |
attributes: |
| 238 |
- name: attr1 |
| 239 |
values: |
| 240 |
- value1 |
| 241 |
state: present |
| 242 |
- name: attr2 |
| 243 |
values: |
| 244 |
- value2 |
| 245 |
state: absent |
| 246 |
groups: |
| 247 |
- name: group1 |
| 248 |
state: present |
| 249 |
state: present |
| 250 |
|
| 251 |
- name: Re-create a User |
| 252 |
community.general.keycloak_user: |
| 253 |
auth_keycloak_url: http://localhost:8080/auth |
| 254 |
auth_username: admin |
| 255 |
auth_password: password |
| 256 |
realm: master |
| 257 |
username: user1 |
| 258 |
firstName: user1 |
| 259 |
lastName: user1 |
| 260 |
email: user1 |
| 261 |
enabled: true |
| 262 |
emailVerified: false |
| 263 |
credentials: |
| 264 |
- type: password |
| 265 |
value: password |
| 266 |
temporary: false |
| 267 |
attributes: |
| 268 |
- name: attr1 |
| 269 |
values: |
| 270 |
- value1 |
| 271 |
state: present |
| 272 |
- name: attr2 |
| 273 |
values: |
| 274 |
- value2 |
| 275 |
state: absent |
| 276 |
groups: |
| 277 |
- name: group1 |
| 278 |
state: present |
| 279 |
state: present |
| 280 |
|
| 281 |
- name: Re-create a User |
| 282 |
community.general.keycloak_user: |
| 283 |
auth_keycloak_url: http://localhost:8080/auth |
| 284 |
auth_username: admin |
| 285 |
auth_password: password |
| 286 |
realm: master |
| 287 |
username: user1 |
| 288 |
firstName: user1 |
| 289 |
lastName: user1 |
| 290 |
email: user1 |
| 291 |
enabled: true |
| 292 |
emailVerified: false |
| 293 |
credentials: |
| 294 |
- type: password |
| 295 |
value: password |
| 296 |
temporary: false |
| 297 |
attributes: |
| 298 |
- name: attr1 |
| 299 |
values: |
| 300 |
- value1 |
| 301 |
state: present |
| 302 |
- name: attr2 |
| 303 |
values: |
| 304 |
- value2 |
| 305 |
state: absent |
| 306 |
groups: |
| 307 |
- name: group1 |
| 308 |
state: present |
| 309 |
state: present |
| 310 |
force: true |
| 311 |
|
| 312 |
- name: Remove User |
| 313 |
community.general.keycloak_user: |
| 314 |
auth_keycloak_url: http://localhost:8080/auth |
| 315 |
auth_username: admin |
| 316 |
auth_password: password |
| 317 |
realm: master |
| 318 |
username: user1 |
| 319 |
state: absent |
| 320 |
""" |
| 321 |
|
| 322 |
RETURN = r""" |
| 323 |
proposed: |
| 324 |
description: Representation of the proposed user. |
| 325 |
returned: on success |
| 326 |
type: dict |
| 327 |
existing: |
| 328 |
description: Representation of the existing user. |
| 329 |
returned: on success |
| 330 |
type: dict |
| 331 |
end_state: |
| 332 |
description: Representation of the user after module execution. |
| 333 |
returned: on success |
| 334 |
type: dict |
| 335 |
user_created: |
| 336 |
description: Indicates whether a user was created. |
| 337 |
returned: in success |
| 338 |
type: bool |
| 339 |
version_added: 12.0.0 |
| 340 |
""" |
| 341 |
|
| 342 |
import copy |
| 343 |
|
| 344 |
from ansible.module_utils.basic import AnsibleModule |
| 345 |
|
| 346 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 347 |
KeycloakAPI, |
| 348 |
KeycloakError, |
| 349 |
camel, |
| 350 |
get_token, |
| 351 |
is_struct_included, |
| 352 |
keycloak_argument_spec, |
| 353 |
) |
| 354 |
|
| 355 |
|
| 356 |
def main(): |
| 357 |
argument_spec = keycloak_argument_spec() |
| 358 |
argument_spec["auth_username"]["aliases"] = [] |
| 359 |
credential_spec = dict( |
| 360 |
type=dict(type="str", required=True), |
| 361 |
value=dict(type="str", required=True, no_log=True), |
| 362 |
temporary=dict(type="bool", default=False), |
| 363 |
) |
| 364 |
client_consents_spec = dict( |
| 365 |
client_id=dict(type="str", required=True, aliases=["clientId"]), |
| 366 |
roles=dict(type="list", elements="str", required=True), |
| 367 |
) |
| 368 |
attributes_spec = dict( |
| 369 |
name=dict(type="str"), |
| 370 |
values=dict(type="list", elements="str"), |
| 371 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 372 |
) |
| 373 |
groups_spec = dict(name=dict(type="str"), state=dict(type="str", choices=["present", "absent"], default="present")) |
| 374 |
meta_args = dict( |
| 375 |
realm=dict(type="str", default="master"), |
| 376 |
self=dict(type="str"), |
| 377 |
id=dict(type="str"), |
| 378 |
username=dict(type="str", required=True), |
| 379 |
first_name=dict(type="str", aliases=["firstName"]), |
| 380 |
last_name=dict(type="str", aliases=["lastName"]), |
| 381 |
email=dict(type="str"), |
| 382 |
enabled=dict(type="bool"), |
| 383 |
email_verified=dict(type="bool", default=False, aliases=["emailVerified"]), |
| 384 |
federation_link=dict(type="str", aliases=["federationLink"]), |
| 385 |
service_account_client_id=dict(type="str", aliases=["serviceAccountClientId"]), |
| 386 |
attributes=dict(type="list", elements="dict", options=attributes_spec), |
| 387 |
access=dict(type="dict"), |
| 388 |
groups=dict(type="list", default=[], elements="dict", options=groups_spec), |
| 389 |
disableable_credential_types=dict( |
| 390 |
type="list", default=[], aliases=["disableableCredentialTypes"], elements="str" |
| 391 |
), |
| 392 |
required_actions=dict(type="list", default=[], aliases=["requiredActions"], elements="str"), |
| 393 |
credentials=dict(type="list", default=[], elements="dict", options=credential_spec), |
| 394 |
federated_identities=dict(type="list", default=[], aliases=["federatedIdentities"], elements="str"), |
| 395 |
client_consents=dict( |
| 396 |
type="list", default=[], aliases=["clientConsents"], elements="dict", options=client_consents_spec |
| 397 |
), |
| 398 |
origin=dict(type="str"), |
| 399 |
state=dict(choices=["absent", "present"], default="present"), |
| 400 |
force=dict(type="bool", default=False), |
| 401 |
) |
| 402 |
argument_spec.update(meta_args) |
| 403 |
|
| 404 |
module = AnsibleModule( |
| 405 |
argument_spec=argument_spec, |
| 406 |
supports_check_mode=True, |
| 407 |
required_one_of=( |
| 408 |
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]] |
| 409 |
), |
| 410 |
required_together=([["auth_username", "auth_password"]]), |
| 411 |
required_by={"refresh_token": "auth_realm"}, |
| 412 |
) |
| 413 |
|
| 414 |
result = dict(changed=False, msg="", diff={}, proposed={}, existing={}, end_state={}) |
| 415 |
|
| 416 |
# Obtain access token, initialize API |
| 417 |
try: |
| 418 |
connection_header = get_token(module.params) |
| 419 |
except KeycloakError as e: |
| 420 |
module.fail_json(msg=str(e)) |
| 421 |
|
| 422 |
kc = KeycloakAPI(module, connection_header) |
| 423 |
|
| 424 |
realm = module.params.get("realm") |
| 425 |
state = module.params.get("state") |
| 426 |
force = module.params.get("force") |
| 427 |
username = module.params.get("username") |
| 428 |
groups = module.params.get("groups") |
| 429 |
|
| 430 |
# Filter and map the parameters names that apply to the user |
| 431 |
user_params = [ |
| 432 |
x |
| 433 |
for x in module.params |
| 434 |
if x not in list(keycloak_argument_spec().keys()) + ["state", "realm", "force", "groups"] |
| 435 |
and module.params.get(x) is not None |
| 436 |
] |
| 437 |
|
| 438 |
before_user = kc.get_user_by_username(username=username, realm=realm) |
| 439 |
|
| 440 |
if before_user is None: |
| 441 |
before_user = {} |
| 442 |
|
| 443 |
changeset = {} |
| 444 |
|
| 445 |
for param in user_params: |
| 446 |
new_param_value = module.params.get(param) |
| 447 |
if param == "attributes" and param in before_user: |
| 448 |
old_value = kc.convert_keycloak_user_attributes_dict_to_module_list(attributes=before_user["attributes"]) |
| 449 |
else: |
| 450 |
old_value = before_user[param] if param in before_user else None |
| 451 |
if new_param_value != old_value: |
| 452 |
if old_value is not None and param == "attributes": |
| 453 |
for old_attribute in old_value: |
| 454 |
old_attribute_found = False |
| 455 |
for new_attribute in new_param_value: |
| 456 |
if new_attribute["name"] == old_attribute["name"]: |
| 457 |
old_attribute_found = True |
| 458 |
if not old_attribute_found: |
| 459 |
new_param_value.append(copy.deepcopy(old_attribute)) |
| 460 |
if isinstance(new_param_value, dict): |
| 461 |
changeset[camel(param)] = copy.deepcopy(new_param_value) |
| 462 |
else: |
| 463 |
changeset[camel(param)] = new_param_value |
| 464 |
# Prepare the desired values using the existing values (non-existence results in a dict that is save to use as a basis) |
| 465 |
desired_user = copy.deepcopy(before_user) |
| 466 |
desired_user.update(changeset) |
| 467 |
|
| 468 |
result["proposed"] = changeset |
| 469 |
result["existing"] = before_user |
| 470 |
# Default values for user_created |
| 471 |
result["user_created"] = False |
| 472 |
changed = False |
| 473 |
|
| 474 |
# Cater for when it doesn't exist (an empty dict) |
| 475 |
if state == "absent": |
| 476 |
if not before_user: |
| 477 |
# Do nothing and exit |
| 478 |
if module._diff: |
| 479 |
result["diff"] = dict(before="", after="") |
| 480 |
result["changed"] = False |
| 481 |
result["end_state"] = {} |
| 482 |
result["msg"] = "Role does not exist, doing nothing." |
| 483 |
module.exit_json(**result) |
| 484 |
else: |
| 485 |
# Delete user |
| 486 |
kc.delete_user(user_id=before_user["id"], realm=realm) |
| 487 |
result["msg"] = f"User {before_user['username']} deleted" |
| 488 |
changed = True |
| 489 |
|
| 490 |
else: |
| 491 |
after_user = {} |
| 492 |
if force and before_user: # If the force option is set to true |
| 493 |
# Delete the existing user |
| 494 |
kc.delete_user(user_id=before_user["id"], realm=realm) |
| 495 |
|
| 496 |
if not before_user or force: |
| 497 |
# Process a creation |
| 498 |
changed = True |
| 499 |
|
| 500 |
if username is None: |
| 501 |
module.fail_json(msg="username must be specified when creating a new user") |
| 502 |
|
| 503 |
if module._diff: |
| 504 |
result["diff"] = dict(before="", after=desired_user) |
| 505 |
|
| 506 |
if module.check_mode: |
| 507 |
# Set user_created flag explicit for check_mode |
| 508 |
# create_user could have failed, but we don't know for sure until we try to create the user.' |
| 509 |
result["user_created"] = True |
| 510 |
module.exit_json(**result) |
| 511 |
|
| 512 |
# Create the user |
| 513 |
after_user = kc.create_user(userrep=desired_user, realm=realm) |
| 514 |
result["msg"] = f"User {desired_user['username']} created" |
| 515 |
# Add user ID to new representation |
| 516 |
desired_user["id"] = after_user["id"] |
| 517 |
# Set user_created flag |
| 518 |
result["user_created"] = True |
| 519 |
else: |
| 520 |
excludes = [ |
| 521 |
"access", |
| 522 |
"notBefore", |
| 523 |
"createdTimestamp", |
| 524 |
"totp", |
| 525 |
"credentials", |
| 526 |
"disableableCredentialTypes", |
| 527 |
"groups", |
| 528 |
"clientConsents", |
| 529 |
"federatedIdentities", |
| 530 |
"requiredActions", |
| 531 |
] |
| 532 |
# Add user ID to new representation |
| 533 |
desired_user["id"] = before_user["id"] |
| 534 |
|
| 535 |
# Compare users |
| 536 |
if not ( |
| 537 |
is_struct_included(desired_user, before_user, excludes) |
| 538 |
): # If the new user does not introduce a change to the existing user |
| 539 |
# Update the user |
| 540 |
after_user = kc.update_user(userrep=desired_user, realm=realm) |
| 541 |
changed = True |
| 542 |
|
| 543 |
# set user groups |
| 544 |
if kc.update_user_groups_membership(userrep=desired_user, groups=groups, realm=realm): |
| 545 |
changed = True |
| 546 |
# Get the user groups |
| 547 |
after_user["groups"] = kc.get_user_groups(user_id=desired_user["id"], realm=realm) |
| 548 |
result["end_state"] = after_user |
| 549 |
if changed: |
| 550 |
result["msg"] = f"User {desired_user['username']} updated" |
| 551 |
else: |
| 552 |
result["msg"] = f"No changes made for user {desired_user['username']}" |
| 553 |
|
| 554 |
result["changed"] = changed |
| 555 |
module.exit_json(**result) |
| 556 |
|
| 557 |
|
| 558 |
if __name__ == "__main__": |
| 559 |
main() |