diff --git a/molecule/keycloak_modules/keycloak_realm/verify.yml b/molecule/keycloak_modules/keycloak_realm/verify.yml index 4081e52..fae5d6b 100644 --- a/molecule/keycloak_modules/keycloak_realm/verify.yml +++ b/molecule/keycloak_modules/keycloak_realm/verify.yml @@ -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 diff --git a/plugins/modules/keycloak_realm.py b/plugins/modules/keycloak_realm.py index f0340a5..a2ce3fa 100644 --- a/plugins/modules/keycloak_realm.py +++ b/plugins/modules/keycloak_realm.py @@ -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: