| 1 |
# !/usr/bin/python |
| 2 |
# Copyright Jakub Danek <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or |
| 4 |
# 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_realm_localization |
| 11 |
|
| 12 |
short_description: Allows management of Keycloak realm localization overrides via the Keycloak API |
| 13 |
|
| 14 |
version_added: 12.4.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- This module allows you to manage per-locale message overrides for a Keycloak realm using the Keycloak Admin REST API. |
| 18 |
- Requires access via OpenID Connect; the connecting user/client must have sufficient privileges. |
| 19 |
- The names of module options are snake_cased versions of the names found in the Keycloak API. |
| 20 |
|
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: full |
| 26 |
|
| 27 |
options: |
| 28 |
force: |
| 29 |
description: |
| 30 |
- If V(false), only the keys listed in the O(overrides) are modified by this module. Any other pre-existing |
| 31 |
keys are ignored. |
| 32 |
- If V(true), all locale overrides are made to match configuration of this module. For example any keys |
| 33 |
missing from the O(overrides) are removed regardless of O(state) value. |
| 34 |
type: bool |
| 35 |
default: false |
| 36 |
locale: |
| 37 |
description: |
| 38 |
- Locale code for which the overrides apply (for example, V(en), V(fi), V(de)). |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
parent_id: |
| 42 |
description: |
| 43 |
- Name of the realm that owns the locale overrides. |
| 44 |
type: str |
| 45 |
required: true |
| 46 |
state: |
| 47 |
description: |
| 48 |
- Desired state of localization overrides for the given locale. |
| 49 |
- On V(present), the set of overrides for the locale are made to match O(overrides). |
| 50 |
If O(force) is V(true) keys not listed in O(overrides) are removed, |
| 51 |
and the listed keys are created or updated. |
| 52 |
If O(force) is V(false) keys not listed in O(overrides) are ignored, |
| 53 |
and the listed keys are created or updated. |
| 54 |
- On V(absent), overrides for the locale is removed. If O(force) is V(true), all keys are removed. |
| 55 |
If O(force) is V(false), only the keys listed in O(overrides) are removed. |
| 56 |
type: str |
| 57 |
choices: ['present', 'absent'] |
| 58 |
default: present |
| 59 |
overrides: |
| 60 |
description: |
| 61 |
- List of overrides to ensure for the locale when O(state=present). Each item is a mapping with |
| 62 |
the record's O(overrides[].key) and its O(overrides[].value). |
| 63 |
- Ignored when O(state=absent). |
| 64 |
type: list |
| 65 |
elements: dict |
| 66 |
default: [] |
| 67 |
suboptions: |
| 68 |
key: |
| 69 |
description: |
| 70 |
- The message key to override. |
| 71 |
type: str |
| 72 |
required: true |
| 73 |
value: |
| 74 |
description: |
| 75 |
- The override value for the message key. If omitted, value defaults to an empty string. |
| 76 |
type: str |
| 77 |
default: "" |
| 78 |
required: false |
| 79 |
|
| 80 |
seealso: |
| 81 |
- module: community.general.keycloak_realm |
| 82 |
description: You can specify list of supported locales using O(community.general.keycloak_realm#module:supported_locales). |
| 83 |
|
| 84 |
extends_documentation_fragment: |
| 85 |
- community.general._keycloak |
| 86 |
- community.general._keycloak.actiongroup_keycloak |
| 87 |
- community.general._attributes |
| 88 |
|
| 89 |
author: Jakub Danek (@danekja) |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Replace all overrides for locale "en" (credentials auth) |
| 94 |
community.general.keycloak_realm_localization: |
| 95 |
auth_client_id: admin-cli |
| 96 |
auth_keycloak_url: https://auth.example.com/auth |
| 97 |
auth_realm: master |
| 98 |
auth_username: USERNAME |
| 99 |
auth_password: PASSWORD |
| 100 |
parent_id: my-realm |
| 101 |
locale: en |
| 102 |
state: present |
| 103 |
force: true |
| 104 |
overrides: |
| 105 |
- key: greeting |
| 106 |
value: "Hello" |
| 107 |
- key: farewell |
| 108 |
value: "Bye" |
| 109 |
delegate_to: localhost |
| 110 |
|
| 111 |
- name: Replace listed overrides for locale "en" (credentials auth) |
| 112 |
community.general.keycloak_realm_localization: |
| 113 |
auth_client_id: admin-cli |
| 114 |
auth_keycloak_url: https://auth.example.com/auth |
| 115 |
auth_realm: master |
| 116 |
auth_username: USERNAME |
| 117 |
auth_password: PASSWORD |
| 118 |
parent_id: my-realm |
| 119 |
locale: en |
| 120 |
state: present |
| 121 |
force: false |
| 122 |
overrides: |
| 123 |
- key: greeting |
| 124 |
value: "Hello" |
| 125 |
- key: farewell |
| 126 |
value: "Bye" |
| 127 |
delegate_to: localhost |
| 128 |
|
| 129 |
- name: Ensure only one override exists for locale "fi" (token auth) |
| 130 |
community.general.keycloak_realm_localization: |
| 131 |
auth_client_id: admin-cli |
| 132 |
auth_keycloak_url: https://auth.example.com/auth |
| 133 |
token: TOKEN |
| 134 |
parent_id: my-realm |
| 135 |
locale: fi |
| 136 |
state: present |
| 137 |
force: true |
| 138 |
overrides: |
| 139 |
- key: app.title |
| 140 |
value: "Sovellukseni" |
| 141 |
delegate_to: localhost |
| 142 |
|
| 143 |
- name: Remove all overrides for locale "de" |
| 144 |
community.general.keycloak_realm_localization: |
| 145 |
auth_client_id: admin-cli |
| 146 |
auth_keycloak_url: https://auth.example.com/auth |
| 147 |
auth_realm: master |
| 148 |
auth_username: USERNAME |
| 149 |
auth_password: PASSWORD |
| 150 |
parent_id: my-realm |
| 151 |
locale: de |
| 152 |
state: absent |
| 153 |
force: true |
| 154 |
delegate_to: localhost |
| 155 |
|
| 156 |
- name: Remove only the listed overrides for locale "de" |
| 157 |
community.general.keycloak_realm_localization: |
| 158 |
auth_client_id: admin-cli |
| 159 |
auth_keycloak_url: https://auth.example.com/auth |
| 160 |
auth_realm: master |
| 161 |
auth_username: USERNAME |
| 162 |
auth_password: PASSWORD |
| 163 |
parent_id: my-realm |
| 164 |
locale: de |
| 165 |
state: absent |
| 166 |
force: false |
| 167 |
overrides: |
| 168 |
- key: app.title |
| 169 |
- key: foo |
| 170 |
- key: bar |
| 171 |
delegate_to: localhost |
| 172 |
""" |
| 173 |
|
| 174 |
RETURN = r""" |
| 175 |
end_state: |
| 176 |
description: |
| 177 |
- Final state of localization overrides for the locale after module execution. |
| 178 |
- Contains the O(locale) and the list of O(overrides) as key/value items. |
| 179 |
returned: on success |
| 180 |
type: dict |
| 181 |
contains: |
| 182 |
locale: |
| 183 |
description: The locale code affected. |
| 184 |
type: str |
| 185 |
sample: en |
| 186 |
overrides: |
| 187 |
description: The list of overrides that exist after execution. |
| 188 |
type: list |
| 189 |
elements: dict |
| 190 |
sample: |
| 191 |
- key: greeting |
| 192 |
value: Hello |
| 193 |
- key: farewell |
| 194 |
value: Bye |
| 195 |
""" |
| 196 |
|
| 197 |
from copy import deepcopy |
| 198 |
|
| 199 |
from ansible.module_utils.basic import AnsibleModule |
| 200 |
|
| 201 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 202 |
KeycloakAPI, |
| 203 |
KeycloakError, |
| 204 |
get_token, |
| 205 |
keycloak_argument_spec, |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
def _normalize_overrides(current: dict | None) -> list[dict]: |
| 210 |
""" |
| 211 |
Accepts: |
| 212 |
- dict: {'k1': 'v1', ...} |
| 213 |
Return a sorted list of {'key', 'value'}. |
| 214 |
|
| 215 |
This helper provides a consistent shape for downstream comparison/diff logic. |
| 216 |
""" |
| 217 |
if not current: |
| 218 |
return [] |
| 219 |
|
| 220 |
return [{"key": k, "value": v} for k, v in sorted(current.items())] |
| 221 |
|
| 222 |
|
| 223 |
def main(): |
| 224 |
argument_spec = keycloak_argument_spec() |
| 225 |
|
| 226 |
# Single override record structure |
| 227 |
overrides_spec = dict( |
| 228 |
key=dict(type="str", no_log=False, required=True), |
| 229 |
value=dict(type="str", default=""), |
| 230 |
) |
| 231 |
|
| 232 |
meta_args = dict( |
| 233 |
locale=dict(type="str", required=True), |
| 234 |
parent_id=dict(type="str", required=True), |
| 235 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 236 |
overrides=dict(type="list", elements="dict", options=overrides_spec, default=[]), |
| 237 |
force=dict(type="bool", default=False), |
| 238 |
) |
| 239 |
|
| 240 |
argument_spec.update(meta_args) |
| 241 |
|
| 242 |
module = AnsibleModule( |
| 243 |
argument_spec=argument_spec, |
| 244 |
supports_check_mode=True, |
| 245 |
required_one_of=([["token", "auth_realm", "auth_username", "auth_password"]]), |
| 246 |
required_together=([["auth_realm", "auth_username", "auth_password"]]), |
| 247 |
) |
| 248 |
|
| 249 |
result = dict(changed=False, msg="", end_state={}, diff=dict(before={}, after={})) |
| 250 |
|
| 251 |
# Obtain access token, initialize API |
| 252 |
try: |
| 253 |
connection_header = get_token(module.params) |
| 254 |
except KeycloakError as e: |
| 255 |
module.fail_json(msg=str(e)) |
| 256 |
|
| 257 |
kc = KeycloakAPI(module, connection_header) |
| 258 |
|
| 259 |
# Convenience locals for frequently used parameters |
| 260 |
locale = module.params["locale"] |
| 261 |
state = module.params["state"] |
| 262 |
parent_id = module.params["parent_id"] |
| 263 |
force = module.params["force"] |
| 264 |
|
| 265 |
desired_raw = module.params["overrides"] |
| 266 |
desired_overrides = _normalize_overrides({r["key"]: r.get("value") for r in desired_raw}) |
| 267 |
|
| 268 |
old_overrides = _normalize_overrides(kc.get_localization_values(locale, parent_id) or {}) |
| 269 |
before = { |
| 270 |
"locale": locale, |
| 271 |
"overrides": deepcopy(old_overrides), |
| 272 |
} |
| 273 |
|
| 274 |
# Proposed state used for diff reporting |
| 275 |
changeset = { |
| 276 |
"locale": locale, |
| 277 |
"overrides": [], |
| 278 |
} |
| 279 |
|
| 280 |
result["changed"] = False |
| 281 |
|
| 282 |
if state == "present": |
| 283 |
changeset["overrides"] = deepcopy(desired_overrides) |
| 284 |
|
| 285 |
# Compute two sets: |
| 286 |
# - to_update: keys missing or with different values |
| 287 |
# - to_remove: keys existing in current state but not in desired |
| 288 |
to_update = [] |
| 289 |
to_remove = deepcopy(old_overrides) |
| 290 |
|
| 291 |
# Mark updates and remove matched ones from to_remove |
| 292 |
for record in desired_overrides: |
| 293 |
override_found = False |
| 294 |
|
| 295 |
for override in to_remove: |
| 296 |
if override["key"] == record["key"]: |
| 297 |
override_found = True |
| 298 |
|
| 299 |
# Value differs -> update needed |
| 300 |
if override["value"] != record["value"]: |
| 301 |
result["changed"] = True |
| 302 |
to_update.append(record) |
| 303 |
|
| 304 |
# Remove processed item so what's left in to_remove are deletions |
| 305 |
to_remove.remove(override) |
| 306 |
break |
| 307 |
|
| 308 |
if not override_found: |
| 309 |
# New key, must be created |
| 310 |
to_update.append(record) |
| 311 |
result["changed"] = True |
| 312 |
|
| 313 |
# ignore any left-overs in to_remove, force is false |
| 314 |
if not force: |
| 315 |
changeset["overrides"].extend(to_remove) |
| 316 |
to_remove = [] |
| 317 |
|
| 318 |
if to_remove: |
| 319 |
result["changed"] = True |
| 320 |
|
| 321 |
if result["changed"]: |
| 322 |
if module._diff: |
| 323 |
result["diff"] = dict(before=before, after=changeset) |
| 324 |
|
| 325 |
if module.check_mode: |
| 326 |
result["msg"] = f"Locale {locale} overrides would be updated." |
| 327 |
|
| 328 |
else: |
| 329 |
for override in to_remove: |
| 330 |
kc.delete_localization_value(locale, override["key"], parent_id) |
| 331 |
|
| 332 |
for override in to_update: |
| 333 |
kc.set_localization_value(locale, override["key"], override["value"], parent_id) |
| 334 |
|
| 335 |
result["msg"] = f"Locale {locale} overrides have been updated." |
| 336 |
|
| 337 |
else: |
| 338 |
result["msg"] = f"Locale {locale} overrides are in sync." |
| 339 |
|
| 340 |
# For accurate end_state, read back from API unless we are in check_mode |
| 341 |
if not module.check_mode: |
| 342 |
final_overrides = _normalize_overrides(kc.get_localization_values(locale, parent_id) or {}) |
| 343 |
|
| 344 |
else: |
| 345 |
final_overrides = ["overrides"] |
| 346 |
|
| 347 |
result["end_state"] = {"locale": locale, "overrides": final_overrides} |
| 348 |
|
| 349 |
elif state == "absent": |
| 350 |
if force: |
| 351 |
to_remove = old_overrides |
| 352 |
|
| 353 |
else: |
| 354 |
# touch only overrides listed in parameters, leave the rest be |
| 355 |
to_remove = deepcopy(desired_overrides) |
| 356 |
to_keep = deepcopy(old_overrides) |
| 357 |
|
| 358 |
for override in to_remove: |
| 359 |
found = False |
| 360 |
for keep in to_keep: |
| 361 |
if override["key"] == keep["key"]: |
| 362 |
to_keep.remove(keep) |
| 363 |
found = True |
| 364 |
break |
| 365 |
|
| 366 |
if not found: |
| 367 |
to_remove.remove(override) |
| 368 |
|
| 369 |
changeset["overrides"] = to_keep |
| 370 |
|
| 371 |
if to_remove: |
| 372 |
result["changed"] = True |
| 373 |
|
| 374 |
if module._diff: |
| 375 |
result["diff"] = dict(before=before, after=changeset) |
| 376 |
|
| 377 |
if module.check_mode: |
| 378 |
if result["changed"]: |
| 379 |
result["msg"] = f"{len(to_remove)} overrides for locale {locale} would be deleted." |
| 380 |
else: |
| 381 |
result["msg"] = f"No overrides for locale {locale} to be deleted." |
| 382 |
|
| 383 |
else: |
| 384 |
for override in to_remove: |
| 385 |
kc.delete_localization_value(locale, override["key"], parent_id) |
| 386 |
|
| 387 |
if result["changed"]: |
| 388 |
result["msg"] = f"{len(to_remove)} overrides for locale {locale} deleted." |
| 389 |
else: |
| 390 |
result["msg"] = f"No overrides for locale {locale} to be deleted." |
| 391 |
|
| 392 |
result["end_state"] = changeset |
| 393 |
|
| 394 |
module.exit_json(**result) |
| 395 |
|
| 396 |
|
| 397 |
if __name__ == "__main__": |
| 398 |
main() |