Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keycloak_group: fix subgroup creation in Keycloak ≥23 #8979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8979-keycloak_group-fix-subgroups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- keycloak_group - fix crash caused in subgroup creation. The crash was caused by a missing or empty ``subGroups`` property in Keycloak ≥23 (https://github.com/ansible-collections/community.general/issues/8788, https://github.com/ansible-collections/community.general/pull/8979).
russoz marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 19 additions & 2 deletions plugins/module_utils/identity/keycloak/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,23 @@ def get_group_by_groupid(self, gid, realm="master"):
self.module.fail_json(msg="Could not fetch group %s in realm %s: %s"
% (gid, realm, str(e)))

def get_subgroups(self, parent, realm="master"):
if 'subGroupCount' in parent:
# Since version 23, when GETting a group Keycloak does not
# return subGroups but only a subGroupCount.
# Children must be fetched in a second request.
if parent['subGroupCount'] == 0:
group_children = []
else:
group_children_url = URL_GROUP_CHILDREN.format(url=self.baseurl, realm=realm, groupid=parent['id'])
group_children = json.loads(to_native(open_url(group_children_url, method="GET", http_agent=self.http_agent, headers=self.restheaders,
timeout=self.connection_timeout,
validate_certs=self.validate_certs).read()))
subgroups = group_children
else:
subgroups = parent['subGroups']
return subgroups

def get_group_by_name(self, name, realm="master", parents=None):
""" Fetch a keycloak group within a realm based on its name.

Expand All @@ -1519,7 +1536,7 @@ def get_group_by_name(self, name, realm="master", parents=None):
if not parent:
return None

all_groups = parent['subGroups']
all_groups = self.get_subgroups(parent, realm)
else:
all_groups = self.get_groups(realm=realm)

Expand Down Expand Up @@ -1568,7 +1585,7 @@ def get_subgroup_by_chain(self, name_chain, realm="master"):
return None

for p in name_chain[1:]:
for sg in tmp['subGroups']:
for sg in self.get_subgroups(tmp):
pv, is_id = self._get_normed_group_parent(p)

if is_id:
Expand Down