keycloak_realm: fix check mode always reporting changed

Check mode compared the realm returned by Keycloak with a plain
dict.update() of the module parameters. Keycloak stores attributes,
browserSecurityHeaders and smtpServer as string maps, merges attributes
and browserSecurityHeaders into the existing values and masks the SMTP
password, so the comparison never matched and check mode always
reported a change while a live run reported ok.

Predict the realm Keycloak would return for those maps in check mode
and add molecule checks for unchanged and changed settings.

Fixes #447

Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
This commit is contained in:
sarankumar1325
2026-10-30 13:12:39 +00:00
parent 1ea6285f95
commit dc8a6cb02d
2 changed files with 139 additions and 1 deletions
@@ -118,3 +118,93 @@
that:
- result is failed
- "'nonexistent-scope' in result.msg"
- name: Configure realm attributes, SMTP server and browser security headers
middleware_automation.keycloak.keycloak_realm: &realm_map_settings
id: "{{ ephemeral_realm }}"
realm: "{{ ephemeral_realm }}"
attributes:
molecule-number: 123
molecule-flag: true
smtp_server:
host: smtp.example.com
port: 587
from: noreply@example.com
auth: true
user: molecule
password: molecule-smtp-password
starttls: true
browser_security_headers:
xFrameOptions: DENY
state: present
register: result
- name: Assert realm map settings applied
ansible.builtin.assert:
that:
- result is changed
- result.end_state.attributes['molecule-number'] == '123'
- result.end_state.smtpServer.port == '587'
- result.end_state.browserSecurityHeaders.xFrameOptions == 'DENY'
- name: Re-apply realm map settings in check mode
middleware_automation.keycloak.keycloak_realm: *realm_map_settings
check_mode: true
register: result
- name: Assert check mode reports no change for unchanged map settings
ansible.builtin.assert:
that:
- result is not changed
- name: Change a realm attribute in check mode
middleware_automation.keycloak.keycloak_realm:
id: "{{ ephemeral_realm }}"
realm: "{{ ephemeral_realm }}"
attributes:
molecule-number: 456
molecule-flag: true
state: present
check_mode: true
register: result
- name: Assert check mode reports a changed realm attribute
ansible.builtin.assert:
that:
- result is changed
- name: Change the SMTP port in check mode
middleware_automation.keycloak.keycloak_realm:
id: "{{ ephemeral_realm }}"
realm: "{{ ephemeral_realm }}"
smtp_server:
host: smtp.example.com
port: 25
from: noreply@example.com
auth: true
user: molecule
password: molecule-smtp-password
starttls: true
state: present
check_mode: true
register: result
- name: Assert check mode reports a changed SMTP server
ansible.builtin.assert:
that:
- result is changed
- name: Change a browser security header in check mode
middleware_automation.keycloak.keycloak_realm:
id: "{{ ephemeral_realm }}"
realm: "{{ ephemeral_realm }}"
browser_security_headers:
xRobotsTag: all
state: present
check_mode: true
register: result
- name: Assert check mode reports a changed browser security header
ansible.builtin.assert:
that:
- result is changed
+49 -1
View File
@@ -906,6 +906,54 @@ def normalise_cr(realmrep):
return realmrep
# Keycloak masks the stored SMTP password with this value when returning a realm representation
SECRET_MASK = "**********"
def _stringify_map_value(value):
"""Convert a value to the string form Keycloak uses for string-to-string map entries."""
if isinstance(value, bool):
return "true" if value else "false"
return str(value)
def _check_mode_desired_realm(before_realm, changeset):
"""Predict the realm representation Keycloak would return after applying changeset.
Keycloak stores realm ``attributes``, ``browserSecurityHeaders`` and ``smtpServer`` as
string-to-string maps and merges ``attributes`` and ``browserSecurityHeaders`` into the
existing values, while ``smtpServer`` is replaced as a whole and returned with its
password masked. A plain ``dict.update()`` of the module parameters therefore never
matches the realm Keycloak returns, so check mode always reported a change.
:param before_realm: the realm representation currently stored in Keycloak
:param changeset: the proposed changes built from the module parameters
:return: the predicted realm representation
"""
desired = before_realm.copy()
desired.update(changeset)
for key in ("attributes", "browserSecurityHeaders"):
if isinstance(changeset.get(key), dict):
merged = dict(before_realm.get(key) or {})
for name, value in changeset[key].items():
if value is None:
merged.pop(name, None)
else:
merged[name] = _stringify_map_value(value)
desired[key] = merged
if isinstance(changeset.get("smtpServer"), dict):
smtp = dict((name, _stringify_map_value(value)) for name, value in changeset["smtpServer"].items() if value is not None)
before_password = (before_realm.get("smtpServer") or {}).get("password")
if "password" in smtp and before_password == SECRET_MASK:
# The stored password cannot be read back, so it cannot be compared
smtp["password"] = before_password
desired["smtpServer"] = smtp
return desired
def sanitize_cr(realmrep):
"""Removes probably sensitive details from a realm representation.
@@ -1212,7 +1260,7 @@ def main():
if module.check_mode:
# We can only compare the current realm with the proposed updates we have
before_norm = normalise_cr(before_realm)
desired_norm = normalise_cr(desired_realm)
desired_norm = normalise_cr(_check_mode_desired_realm(before_realm, changeset))
_inject_scope_names(before_norm, before_scope_names)
_inject_scope_names(desired_norm, {st: sorted(v) for st, v in desired_scopes.items()})
if module._diff: