| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Eike Frost <[email protected]> |
| 4 |
# Copyright (c) 2021, Christophe Gilles <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or |
| 6 |
# https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: keycloak_authentication_required_actions |
| 12 |
|
| 13 |
short_description: Allows administration of Keycloak authentication required actions |
| 14 |
|
| 15 |
description: |
| 16 |
- This module can register, update and delete required actions. |
| 17 |
- It also filters out any duplicate required actions by their alias. The first occurrence is preserved. |
| 18 |
version_added: 7.1.0 |
| 19 |
|
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: full |
| 25 |
action_group: |
| 26 |
version_added: 10.2.0 |
| 27 |
|
| 28 |
options: |
| 29 |
realm: |
| 30 |
description: |
| 31 |
- The name of the realm in which are the authentication required actions. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
required_actions: |
| 35 |
elements: dict |
| 36 |
description: |
| 37 |
- Authentication required action. |
| 38 |
suboptions: |
| 39 |
alias: |
| 40 |
description: |
| 41 |
- Unique name of the required action. |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
config: |
| 45 |
description: |
| 46 |
- Configuration for the required action. |
| 47 |
type: dict |
| 48 |
defaultAction: |
| 49 |
description: |
| 50 |
- Indicates whether new users have the required action assigned to them. |
| 51 |
type: bool |
| 52 |
enabled: |
| 53 |
description: |
| 54 |
- Indicates, if the required action is enabled or not. |
| 55 |
type: bool |
| 56 |
name: |
| 57 |
description: |
| 58 |
- Displayed name of the required action. Required for registration. |
| 59 |
type: str |
| 60 |
priority: |
| 61 |
description: |
| 62 |
- Priority of the required action. |
| 63 |
type: int |
| 64 |
providerId: |
| 65 |
description: |
| 66 |
- Provider ID of the required action. Required for registration. |
| 67 |
type: str |
| 68 |
type: list |
| 69 |
state: |
| 70 |
choices: ["absent", "present"] |
| 71 |
description: |
| 72 |
- Control if the realm authentication required actions are going to be registered/updated (V(present)) or deleted (V(absent)). |
| 73 |
required: true |
| 74 |
type: str |
| 75 |
|
| 76 |
extends_documentation_fragment: |
| 77 |
- community.general._keycloak |
| 78 |
- community.general._keycloak.actiongroup_keycloak |
| 79 |
- community.general._attributes |
| 80 |
|
| 81 |
author: |
| 82 |
- Skrekulko (@Skrekulko) |
| 83 |
""" |
| 84 |
|
| 85 |
EXAMPLES = r""" |
| 86 |
- name: Register a new required action. |
| 87 |
community.general.keycloak_authentication_required_actions: |
| 88 |
auth_client_id: "admin-cli" |
| 89 |
auth_keycloak_url: "http://localhost:8080" |
| 90 |
auth_password: "password" |
| 91 |
auth_realm: "master" |
| 92 |
auth_username: "admin" |
| 93 |
realm: "master" |
| 94 |
required_actions: |
| 95 |
- alias: "TERMS_AND_CONDITIONS" |
| 96 |
name: "Terms and conditions" |
| 97 |
providerId: "TERMS_AND_CONDITIONS" |
| 98 |
enabled: true |
| 99 |
state: "present" |
| 100 |
|
| 101 |
- name: Update the newly registered required action. |
| 102 |
community.general.keycloak_authentication_required_actions: |
| 103 |
auth_client_id: "admin-cli" |
| 104 |
auth_keycloak_url: "http://localhost:8080" |
| 105 |
auth_password: "password" |
| 106 |
auth_realm: "master" |
| 107 |
auth_username: "admin" |
| 108 |
realm: "master" |
| 109 |
required_actions: |
| 110 |
- alias: "TERMS_AND_CONDITIONS" |
| 111 |
enabled: false |
| 112 |
state: "present" |
| 113 |
|
| 114 |
- name: Delete the updated registered required action. |
| 115 |
community.general.keycloak_authentication_required_actions: |
| 116 |
auth_client_id: "admin-cli" |
| 117 |
auth_keycloak_url: "http://localhost:8080" |
| 118 |
auth_password: "password" |
| 119 |
auth_realm: "master" |
| 120 |
auth_username: "admin" |
| 121 |
realm: "master" |
| 122 |
required_actions: |
| 123 |
- alias: "TERMS_AND_CONDITIONS" |
| 124 |
state: "absent" |
| 125 |
""" |
| 126 |
|
| 127 |
RETURN = r""" |
| 128 |
msg: |
| 129 |
description: Message as to what action was taken. |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
|
| 133 |
end_state: |
| 134 |
description: Representation of the authentication required actions after module execution. |
| 135 |
returned: on success |
| 136 |
type: dict |
| 137 |
contains: |
| 138 |
alias: |
| 139 |
description: |
| 140 |
- Unique name of the required action. |
| 141 |
sample: test-provider-id |
| 142 |
type: str |
| 143 |
config: |
| 144 |
description: |
| 145 |
- Configuration for the required action. |
| 146 |
sample: {} |
| 147 |
type: dict |
| 148 |
defaultAction: |
| 149 |
description: |
| 150 |
- Indicates whether new users have the required action assigned to them. |
| 151 |
sample: false |
| 152 |
type: bool |
| 153 |
enabled: |
| 154 |
description: |
| 155 |
- Indicates, if the required action is enabled or not. |
| 156 |
sample: false |
| 157 |
type: bool |
| 158 |
name: |
| 159 |
description: |
| 160 |
- Displayed name of the required action. Required for registration. |
| 161 |
sample: Test provider ID |
| 162 |
type: str |
| 163 |
priority: |
| 164 |
description: |
| 165 |
- Priority of the required action. |
| 166 |
sample: 90 |
| 167 |
type: int |
| 168 |
providerId: |
| 169 |
description: |
| 170 |
- Provider ID of the required action. Required for registration. |
| 171 |
sample: test-provider-id |
| 172 |
type: str |
| 173 |
""" |
| 174 |
|
| 175 |
from ansible.module_utils.basic import AnsibleModule |
| 176 |
|
| 177 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 178 |
KeycloakAPI, |
| 179 |
KeycloakError, |
| 180 |
get_token, |
| 181 |
keycloak_argument_spec, |
| 182 |
) |
| 183 |
|
| 184 |
|
| 185 |
def sanitize_required_actions(objects): |
| 186 |
for obj in objects: |
| 187 |
alias = obj["alias"] |
| 188 |
name = obj["name"] |
| 189 |
provider_id = obj["providerId"] |
| 190 |
|
| 191 |
if not name: |
| 192 |
obj["name"] = alias |
| 193 |
|
| 194 |
if provider_id != alias: |
| 195 |
obj["providerId"] = alias |
| 196 |
|
| 197 |
return objects |
| 198 |
|
| 199 |
|
| 200 |
def filter_duplicates(objects): |
| 201 |
filtered_objects = {} |
| 202 |
|
| 203 |
for obj in objects: |
| 204 |
alias = obj["alias"] |
| 205 |
|
| 206 |
if alias not in filtered_objects: |
| 207 |
filtered_objects[alias] = obj |
| 208 |
|
| 209 |
return list(filtered_objects.values()) |
| 210 |
|
| 211 |
|
| 212 |
def main(): |
| 213 |
""" |
| 214 |
Module execution |
| 215 |
|
| 216 |
:return: |
| 217 |
""" |
| 218 |
argument_spec = keycloak_argument_spec() |
| 219 |
|
| 220 |
meta_args = dict( |
| 221 |
realm=dict(type="str", required=True), |
| 222 |
required_actions=dict( |
| 223 |
type="list", |
| 224 |
elements="dict", |
| 225 |
options=dict( |
| 226 |
alias=dict(type="str", required=True), |
| 227 |
config=dict(type="dict"), |
| 228 |
defaultAction=dict(type="bool"), |
| 229 |
enabled=dict(type="bool"), |
| 230 |
name=dict(type="str"), |
| 231 |
priority=dict(type="int"), |
| 232 |
providerId=dict(type="str"), |
| 233 |
), |
| 234 |
), |
| 235 |
state=dict(type="str", choices=["present", "absent"], required=True), |
| 236 |
) |
| 237 |
|
| 238 |
argument_spec.update(meta_args) |
| 239 |
|
| 240 |
module = AnsibleModule( |
| 241 |
argument_spec=argument_spec, |
| 242 |
supports_check_mode=True, |
| 243 |
required_one_of=( |
| 244 |
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]] |
| 245 |
), |
| 246 |
required_together=([["auth_username", "auth_password"]]), |
| 247 |
required_by={"refresh_token": "auth_realm"}, |
| 248 |
) |
| 249 |
|
| 250 |
result = dict(changed=False, msg="", end_state={}, diff=dict(before={}, after={})) |
| 251 |
|
| 252 |
# Obtain access token, initialize API |
| 253 |
try: |
| 254 |
connection_header = get_token(module.params) |
| 255 |
except KeycloakError as e: |
| 256 |
module.fail_json(msg=str(e)) |
| 257 |
|
| 258 |
kc = KeycloakAPI(module, connection_header) |
| 259 |
|
| 260 |
# Convenience variables |
| 261 |
realm = module.params.get("realm") |
| 262 |
desired_required_actions = module.params.get("required_actions") |
| 263 |
state = module.params.get("state") |
| 264 |
|
| 265 |
# Sanitize required actions |
| 266 |
desired_required_actions = sanitize_required_actions(desired_required_actions) |
| 267 |
|
| 268 |
# Filter out duplicate required actions |
| 269 |
desired_required_actions = filter_duplicates(desired_required_actions) |
| 270 |
|
| 271 |
# Get required actions |
| 272 |
before_required_actions = kc.get_required_actions(realm=realm) |
| 273 |
|
| 274 |
if state == "present": |
| 275 |
# Initialize empty lists to hold the required actions that need to be |
| 276 |
# registered, updated, and original ones of the updated one |
| 277 |
register_required_actions = [] |
| 278 |
before_updated_required_actions = [] |
| 279 |
updated_required_actions = [] |
| 280 |
|
| 281 |
# Loop through the desired required actions and check if they exist in the before required actions |
| 282 |
for desired_required_action in desired_required_actions: |
| 283 |
found = False |
| 284 |
|
| 285 |
# Loop through the before required actions and check if the aliases match |
| 286 |
for before_required_action in before_required_actions: |
| 287 |
if desired_required_action["alias"] == before_required_action["alias"]: |
| 288 |
update_required = False |
| 289 |
|
| 290 |
# Fill in the parameters |
| 291 |
for k, v in before_required_action.items(): |
| 292 |
if k not in desired_required_action or desired_required_action[k] is None: |
| 293 |
desired_required_action[k] = v |
| 294 |
|
| 295 |
# Loop through the keys of the desired and before required actions |
| 296 |
# and check if there are any differences between them |
| 297 |
for key in desired_required_action.keys(): |
| 298 |
if ( |
| 299 |
key in before_required_action |
| 300 |
and desired_required_action[key] != before_required_action[key] |
| 301 |
): |
| 302 |
update_required = True |
| 303 |
break |
| 304 |
|
| 305 |
# If there are differences, add the before and desired required actions |
| 306 |
# to their respective lists for updating |
| 307 |
if update_required: |
| 308 |
before_updated_required_actions.append(before_required_action) |
| 309 |
updated_required_actions.append(desired_required_action) |
| 310 |
found = True |
| 311 |
break |
| 312 |
# If the desired required action is not found in the before required actions, |
| 313 |
# add it to the list of required actions to register |
| 314 |
if not found: |
| 315 |
# Check if name is provided |
| 316 |
if "name" not in desired_required_action or desired_required_action["name"] is None: |
| 317 |
module.fail_json( |
| 318 |
msg=f"Unable to register required action {desired_required_action['alias']} in realm {realm}: name not included" |
| 319 |
) |
| 320 |
|
| 321 |
# Check if provider ID is provided |
| 322 |
if "providerId" not in desired_required_action or desired_required_action["providerId"] is None: |
| 323 |
module.fail_json( |
| 324 |
msg=f"Unable to register required action {desired_required_action['alias']} in realm {realm}: providerId not included" |
| 325 |
) |
| 326 |
|
| 327 |
register_required_actions.append(desired_required_action) |
| 328 |
|
| 329 |
# Handle diff |
| 330 |
if module._diff: |
| 331 |
diff_required_actions = updated_required_actions.copy() |
| 332 |
diff_required_actions.extend(register_required_actions) |
| 333 |
|
| 334 |
result["diff"] = dict(before=before_updated_required_actions, after=diff_required_actions) |
| 335 |
|
| 336 |
# Handle changed |
| 337 |
if register_required_actions or updated_required_actions: |
| 338 |
result["changed"] = True |
| 339 |
|
| 340 |
# Handle check mode |
| 341 |
if module.check_mode: |
| 342 |
if register_required_actions or updated_required_actions: |
| 343 |
result["change"] = True |
| 344 |
result["msg"] = "Required actions would be registered/updated" |
| 345 |
else: |
| 346 |
result["change"] = False |
| 347 |
result["msg"] = "Required actions would not be registered/updated" |
| 348 |
|
| 349 |
module.exit_json(**result) |
| 350 |
|
| 351 |
# Register required actions |
| 352 |
if register_required_actions: |
| 353 |
for register_required_action in register_required_actions: |
| 354 |
kc.register_required_action(realm=realm, rep=register_required_action) |
| 355 |
kc.update_required_action( |
| 356 |
alias=register_required_action["alias"], realm=realm, rep=register_required_action |
| 357 |
) |
| 358 |
|
| 359 |
# Update required actions |
| 360 |
if updated_required_actions: |
| 361 |
for updated_required_action in updated_required_actions: |
| 362 |
kc.update_required_action( |
| 363 |
alias=updated_required_action["alias"], realm=realm, rep=updated_required_action |
| 364 |
) |
| 365 |
|
| 366 |
# Initialize the final list of required actions |
| 367 |
final_required_actions = [] |
| 368 |
|
| 369 |
# Iterate over the before_required_actions |
| 370 |
for before_required_action in before_required_actions: |
| 371 |
# Check if there is an updated_required_action with the same alias |
| 372 |
updated_required_action_found = False |
| 373 |
|
| 374 |
for updated_required_action in updated_required_actions: |
| 375 |
if updated_required_action["alias"] == before_required_action["alias"]: |
| 376 |
# Merge the two dictionaries, favoring the values from updated_required_action |
| 377 |
merged_dict = {} |
| 378 |
for key in before_required_action.keys(): |
| 379 |
if key in updated_required_action: |
| 380 |
merged_dict[key] = updated_required_action[key] |
| 381 |
else: |
| 382 |
merged_dict[key] = before_required_action[key] |
| 383 |
|
| 384 |
for key in updated_required_action.keys(): |
| 385 |
if key not in before_required_action: |
| 386 |
merged_dict[key] = updated_required_action[key] |
| 387 |
|
| 388 |
# Add the merged dictionary to the final list of required actions |
| 389 |
final_required_actions.append(merged_dict) |
| 390 |
|
| 391 |
# Mark the updated_required_action as found |
| 392 |
updated_required_action_found = True |
| 393 |
|
| 394 |
# Stop looking for updated_required_action |
| 395 |
break |
| 396 |
|
| 397 |
# If no matching updated_required_action was found, add the before_required_action to the final list of required actions |
| 398 |
if not updated_required_action_found: |
| 399 |
final_required_actions.append(before_required_action) |
| 400 |
|
| 401 |
# Append any remaining updated_required_actions that were not merged |
| 402 |
for updated_required_action in updated_required_actions: |
| 403 |
if not any(updated_required_action["alias"] == action["alias"] for action in final_required_actions): |
| 404 |
final_required_actions.append(updated_required_action) |
| 405 |
|
| 406 |
# Append newly registered required actions |
| 407 |
final_required_actions.extend(register_required_actions) |
| 408 |
|
| 409 |
# Handle message and end state |
| 410 |
result["msg"] = "Required actions registered/updated" |
| 411 |
result["end_state"] = final_required_actions |
| 412 |
else: |
| 413 |
# Filter out the deleted required actions |
| 414 |
final_required_actions = [] |
| 415 |
delete_required_actions = [] |
| 416 |
|
| 417 |
for before_required_action in before_required_actions: |
| 418 |
delete_action = False |
| 419 |
|
| 420 |
for desired_required_action in desired_required_actions: |
| 421 |
if before_required_action["alias"] == desired_required_action["alias"]: |
| 422 |
delete_action = True |
| 423 |
break |
| 424 |
|
| 425 |
if not delete_action: |
| 426 |
final_required_actions.append(before_required_action) |
| 427 |
else: |
| 428 |
delete_required_actions.append(before_required_action) |
| 429 |
|
| 430 |
# Handle diff |
| 431 |
if module._diff: |
| 432 |
result["diff"] = dict(before=before_required_actions, after=final_required_actions) |
| 433 |
|
| 434 |
# Handle changed |
| 435 |
if delete_required_actions: |
| 436 |
result["changed"] = True |
| 437 |
|
| 438 |
# Handle check mode |
| 439 |
if module.check_mode: |
| 440 |
if final_required_actions: |
| 441 |
result["change"] = True |
| 442 |
result["msg"] = "Required actions would be deleted" |
| 443 |
else: |
| 444 |
result["change"] = False |
| 445 |
result["msg"] = "Required actions would not be deleted" |
| 446 |
|
| 447 |
module.exit_json(**result) |
| 448 |
|
| 449 |
# Delete required actions |
| 450 |
if delete_required_actions: |
| 451 |
for delete_required_action in delete_required_actions: |
| 452 |
kc.delete_required_action(alias=delete_required_action["alias"], realm=realm) |
| 453 |
|
| 454 |
# Handle message and end state |
| 455 |
result["msg"] = "Required actions deleted" |
| 456 |
result["end_state"] = final_required_actions |
| 457 |
|
| 458 |
module.exit_json(**result) |
| 459 |
|
| 460 |
|
| 461 |
if __name__ == "__main__": |
| 462 |
main() |