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

Add keycloak_realm_rolemapping module to map realm roles to groups #7663

Merged
merged 16 commits into from
Dec 28, 2023
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 .github/BOTMETA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ files:
maintainers: laurpaum
$modules/keycloak_user_rolemapping.py:
maintainers: bratwurzt
$modules/keycloak_realm_rolemapping.py:
maintainers: agross mhuysamen Gaetan2907
$modules/keyring.py:
maintainers: ahussey-redhat
$modules/keyring_info.py:
Expand Down
34 changes: 34 additions & 0 deletions plugins/module_utils/identity/keycloak/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
URL_CLIENT_USER_ROLEMAPPINGS_AVAILABLE = "{url}/admin/realms/{realm}/users/{id}/role-mappings/clients/{client}/available"
URL_CLIENT_USER_ROLEMAPPINGS_COMPOSITE = "{url}/admin/realms/{realm}/users/{id}/role-mappings/clients/{client}/composite"

URL_REALM_GROUP_ROLEMAPPINGS = "{url}/admin/realms/{realm}/groups/{group}/role-mappings/realm"

URL_CLIENTSECRET = "{url}/admin/realms/{realm}/clients/{id}/client-secret"

URL_AUTHENTICATION_FLOWS = "{url}/admin/realms/{realm}/authentication/flows"
Expand Down Expand Up @@ -626,6 +628,38 @@ def add_client_roles_by_id_composite_rolemapping(self, rid, roles_rep, realm="ma
self.fail_open_url(e, msg="Could not assign roles to composite role %s and realm %s: %s"
% (rid, realm, str(e)))

def add_group_realm_rolemapping(self, gid, role_rep, realm="master"):
""" Add the specified realm role to specified group on the Keycloak server.

:param gid: ID of the group to add the role mapping.
:param role_rep: Representation of the role to assign.
:param realm: Realm from which to obtain the rolemappings.
:return: None.
"""
url = URL_REALM_GROUP_ROLEMAPPINGS.format(url=self.baseurl, realm=realm, group=gid)
try:
open_url(url, method="POST", http_agent=self.http_agent, headers=self.restheaders, data=json.dumps(role_rep),
validate_certs=self.validate_certs, timeout=self.connection_timeout)
except Exception as e:
self.fail_open_url(e, msg="Could add realm role mappings for group %s, realm %s: %s"
% (gid, realm, str(e)))
agross marked this conversation as resolved.
Show resolved Hide resolved

def delete_group_realm_rolemapping(self, gid, role_rep, realm="master"):
""" Delete the specified realm role from the specified group on the Keycloak server.

:param gid: ID of the group from which to obtain the rolemappings.
:param role_rep: Representation of the role to assign.
:param realm: Realm from which to obtain the rolemappings.
:return: None.
"""
url = URL_REALM_GROUP_ROLEMAPPINGS.format(url=self.baseurl, realm=realm, group=gid)
try:
open_url(url, method="DELETE", http_agent=self.http_agent, headers=self.restheaders, data=json.dumps(role_rep),
validate_certs=self.validate_certs, timeout=self.connection_timeout)
except Exception as e:
self.fail_open_url(e, msg="Could not delete realm role mappings for group %s, realm %s: %s"
% (gid, realm, str(e)))

def add_group_rolemapping(self, gid, cid, role_rep, realm="master"):
""" Fetch the composite role of a client in a specified group on the Keycloak server.

Expand Down
Loading