mirror of
https://github.com/ansible-middleware/keycloak.git
synced 2026-11-24 18:03:48 +00:00
keycloak_user: fix check mode reporting changed=True for every existing user
In check mode, the desired group memberships were built as a set and compared with the stored memberships, a list, through is_struct_included(). It has no branch for sets and falls back to comparing their string forms, which never match, so check mode reported a change for every existing user, with or without groups. Check mode now decides like normal mode: a group given by name or by path counts as joined when the user is already a member. That rule moves from update_user_groups_membership() into filter_groups_to_add_to_and_remove_from_user(), which both modes use, and the groups reported in check mode no longer list a group given by path twice. Add check mode tests, including groups given by path. Fixes #442 Signed-off-by: Filipe Rosa Capela <Filipe.Capela-CIC.Netherlands1@ibm.com>
This commit is contained in:
@@ -219,3 +219,129 @@
|
|||||||
that:
|
that:
|
||||||
- create_result.changed
|
- create_result.changed
|
||||||
- create_result.end_state.requiredActions == []
|
- create_result.end_state.requiredActions == []
|
||||||
|
|
||||||
|
- name: Create user for check mode tests
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Re-run with identical input in check mode
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
state: present
|
||||||
|
check_mode: true
|
||||||
|
register: check_mode_result
|
||||||
|
|
||||||
|
- name: Assert check mode reports no change for identical input
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- check_mode_result is not changed
|
||||||
|
|
||||||
|
- name: Add the user to a group
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
groups:
|
||||||
|
- name: test
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Re-run with identical groups in check mode
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
groups:
|
||||||
|
- name: test
|
||||||
|
state: present
|
||||||
|
check_mode: true
|
||||||
|
register: check_mode_result
|
||||||
|
|
||||||
|
- name: Assert check mode reports no change for identical groups
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- check_mode_result is not changed
|
||||||
|
|
||||||
|
- name: Join a second group in check mode
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
groups:
|
||||||
|
- name: test
|
||||||
|
- name: test2
|
||||||
|
state: present
|
||||||
|
check_mode: true
|
||||||
|
register: check_mode_result
|
||||||
|
|
||||||
|
- name: Assert check mode reports joining a group as a change
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- check_mode_result is changed
|
||||||
|
|
||||||
|
- name: Create a parent group and a subgroup for check mode tests
|
||||||
|
middleware_automation.keycloak.keycloak_group:
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
name: "{{ item.name }}"
|
||||||
|
parents: "{{ item.parents | default(omit) }}"
|
||||||
|
state: present
|
||||||
|
loop:
|
||||||
|
- name: check-mode-parent
|
||||||
|
- name: check-mode-child
|
||||||
|
parents:
|
||||||
|
- name: check-mode-parent
|
||||||
|
|
||||||
|
- name: Add the user to the subgroup by path
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
groups:
|
||||||
|
- name: test
|
||||||
|
- name: /check-mode-parent/check-mode-child
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Re-run with identical groups given by path in check mode
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
groups:
|
||||||
|
- name: test
|
||||||
|
- name: /check-mode-parent/check-mode-child
|
||||||
|
state: present
|
||||||
|
check_mode: true
|
||||||
|
register: check_mode_result
|
||||||
|
|
||||||
|
- name: Assert check mode reports no change for identical groups given by path
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- check_mode_result is not changed
|
||||||
|
- check_mode_result.end_state.groups | sort == ['check-mode-child', 'test']
|
||||||
|
|
||||||
|
- name: Leave the subgroup by path in check mode
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
groups:
|
||||||
|
- name: test
|
||||||
|
- name: /check-mode-parent/check-mode-child
|
||||||
|
state: absent
|
||||||
|
state: present
|
||||||
|
check_mode: true
|
||||||
|
register: check_mode_result
|
||||||
|
|
||||||
|
- name: Assert check mode reports leaving a group given by path as a change
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- check_mode_result is changed
|
||||||
|
- check_mode_result.end_state.groups == ['test']
|
||||||
|
|
||||||
|
- name: Delete user for check mode tests
|
||||||
|
middleware_automation.keycloak.keycloak_user:
|
||||||
|
username: check-mode-user
|
||||||
|
realm: "{{ ephemeral_realm }}"
|
||||||
|
state: absent
|
||||||
|
register: check_mode_delete_result
|
||||||
|
|
||||||
|
- name: Assert user for check mode tests is deleted
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- check_mode_delete_result.changed
|
||||||
|
|||||||
@@ -3153,19 +3153,9 @@ class KeycloakAPI:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
user_groups = self.get_user_group_details(user_id=userrep["id"], realm=realm)
|
user_groups = self.get_user_group_details(user_id=userrep["id"], realm=realm)
|
||||||
user_group_names = [user_group["name"] for user_group in user_groups if "name" in user_group]
|
groups_to_add, groups_to_remove = self.filter_groups_to_add_to_and_remove_from_user(
|
||||||
user_group_paths = [user_group["path"] for user_group in user_groups if "path" in user_group]
|
user_groups, groups_to_add, groups_to_remove
|
||||||
|
)
|
||||||
groups_to_add = [
|
|
||||||
group_to_add
|
|
||||||
for group_to_add in groups_to_add
|
|
||||||
if group_to_add not in user_group_names and group_to_add not in user_group_paths
|
|
||||||
]
|
|
||||||
groups_to_remove = [
|
|
||||||
group_to_remove
|
|
||||||
for group_to_remove in groups_to_remove
|
|
||||||
if group_to_remove in user_group_names or group_to_remove in user_group_paths
|
|
||||||
]
|
|
||||||
if not groups_to_add and not groups_to_remove:
|
if not groups_to_add and not groups_to_remove:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -3206,6 +3196,29 @@ class KeycloakAPI:
|
|||||||
groups_to_remove.append(group_name)
|
groups_to_remove.append(group_name)
|
||||||
return groups_to_add, groups_to_remove
|
return groups_to_add, groups_to_remove
|
||||||
|
|
||||||
|
def filter_groups_to_add_to_and_remove_from_user(self, user_groups, groups_to_add, groups_to_remove):
|
||||||
|
"""
|
||||||
|
Keep only the group membership changes that a user still needs.
|
||||||
|
:param user_groups: The user's current groups, as returned by get_user_group_details.
|
||||||
|
:param groups_to_add: Names or paths of the groups the user must be in.
|
||||||
|
:param groups_to_remove: Names or paths of the groups the user must not be in.
|
||||||
|
:return: The groups the user is not in yet, and the groups the user is in and must leave.
|
||||||
|
"""
|
||||||
|
user_group_names = [user_group["name"] for user_group in user_groups if "name" in user_group]
|
||||||
|
user_group_paths = [user_group["path"] for user_group in user_groups if "path" in user_group]
|
||||||
|
|
||||||
|
groups_to_add = [
|
||||||
|
group_to_add
|
||||||
|
for group_to_add in groups_to_add
|
||||||
|
if group_to_add not in user_group_names and group_to_add not in user_group_paths
|
||||||
|
]
|
||||||
|
groups_to_remove = [
|
||||||
|
group_to_remove
|
||||||
|
for group_to_remove in groups_to_remove
|
||||||
|
if group_to_remove in user_group_names or group_to_remove in user_group_paths
|
||||||
|
]
|
||||||
|
return groups_to_add, groups_to_remove
|
||||||
|
|
||||||
def find_group_by_path(self, target, realm: str = "master"):
|
def find_group_by_path(self, target, realm: str = "master"):
|
||||||
"""
|
"""
|
||||||
Finds a realm group by path, e.g. '/my/group'.
|
Finds a realm group by path, e.g. '/my/group'.
|
||||||
|
|||||||
@@ -490,10 +490,10 @@ def main():
|
|||||||
desired_user.update(changeset)
|
desired_user.update(changeset)
|
||||||
|
|
||||||
if before_user:
|
if before_user:
|
||||||
before_groups = kc.get_user_groups(user_id=before_user["id"], realm=realm)
|
before_group_details = kc.get_user_group_details(user_id=before_user["id"], realm=realm)
|
||||||
before_user["groups"] = before_groups
|
before_user["groups"] = [group["name"] for group in before_group_details if "name" in group]
|
||||||
else:
|
else:
|
||||||
before_groups = []
|
before_group_details = []
|
||||||
|
|
||||||
result["proposed"] = changeset
|
result["proposed"] = changeset
|
||||||
result["existing"] = before_user
|
result["existing"] = before_user
|
||||||
@@ -579,16 +579,17 @@ def main():
|
|||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
changed |= kc.update_user_groups_membership(userrep=desired_user, groups=groups, realm=realm)
|
changed |= kc.update_user_groups_membership(userrep=desired_user, groups=groups, realm=realm)
|
||||||
|
|
||||||
present_groups = [g["name"] for g in groups if g["state"] == "present"]
|
|
||||||
absent_groups = [g["name"] for g in groups if g["state"] == "absent"]
|
|
||||||
|
|
||||||
desired_user["groups"] = (set(before_groups) | set(present_groups)) - set(absent_groups)
|
|
||||||
|
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
# check if group meberships would have changed
|
# check if group meberships would have changed
|
||||||
changed |= not is_struct_included(
|
groups_to_add, groups_to_remove = kc.filter_groups_to_add_to_and_remove_from_user(
|
||||||
desired_user["groups"], before_groups, user_compare_excludes, empty_list_result=False
|
before_group_details, *kc.extract_groups_to_add_to_and_remove_from_user(groups)
|
||||||
)
|
)
|
||||||
|
desired_user["groups"] = [
|
||||||
|
group["name"]
|
||||||
|
for group in before_group_details
|
||||||
|
if "name" in group and group["name"] not in groups_to_remove and group.get("path") not in groups_to_remove
|
||||||
|
] + groups_to_add
|
||||||
|
changed |= bool(groups_to_add or groups_to_remove)
|
||||||
else:
|
else:
|
||||||
after_user["groups"] = kc.get_user_groups(user_id=desired_user["id"], realm=realm)
|
after_user["groups"] = kc.get_user_groups(user_id=desired_user["id"], realm=realm)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user