| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) Ansible project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: keycloak_client_rolescope |
| 11 |
|
| 12 |
short_description: Allows administration of Keycloak client roles scope to restrict the usage of certain roles to a other |
| 13 |
specific client applications |
| 14 |
|
| 15 |
version_added: 8.6.0 |
| 16 |
|
| 17 |
description: |
| 18 |
- This module allows you to add or remove Keycloak roles from clients scope using the Keycloak REST API. It requires access |
| 19 |
to the REST API using OpenID Connect; the user connecting and the client being used must have the requisite access rights. |
| 20 |
In a default Keycloak installation, admin-cli and an admin user would work, as would a separate client definition with |
| 21 |
the scope tailored to your needs and a user having the expected roles. |
| 22 |
- Client O(client_id) must have O(community.general.keycloak_client#module:full_scope_allowed) set to V(false). |
| 23 |
- Attributes are multi-valued in the Keycloak API. All attributes are lists of individual values and are returned that way |
| 24 |
by this module. You may pass single values for attributes when calling the module, and this is translated into a list |
| 25 |
suitable for the API. |
| 26 |
attributes: |
| 27 |
check_mode: |
| 28 |
support: full |
| 29 |
diff_mode: |
| 30 |
support: full |
| 31 |
action_group: |
| 32 |
version_added: 10.2.0 |
| 33 |
|
| 34 |
options: |
| 35 |
state: |
| 36 |
description: |
| 37 |
- State of the role mapping. |
| 38 |
- On V(present), all roles in O(role_names) are mapped if not exist yet. |
| 39 |
- On V(absent), all roles mapping in O(role_names) are removed if it exists. |
| 40 |
default: 'present' |
| 41 |
type: str |
| 42 |
choices: |
| 43 |
- present |
| 44 |
- absent |
| 45 |
|
| 46 |
realm: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- The Keycloak realm under which clients resides. |
| 50 |
default: 'master' |
| 51 |
|
| 52 |
client_id: |
| 53 |
type: str |
| 54 |
required: true |
| 55 |
description: |
| 56 |
- Roles provided in O(role_names) while be added to this client scope. |
| 57 |
client_scope_id: |
| 58 |
type: str |
| 59 |
description: |
| 60 |
- If the O(role_names) are client role, the client ID under which it resides. |
| 61 |
- If this parameter is absent, the roles are considered a realm role. |
| 62 |
role_names: |
| 63 |
required: true |
| 64 |
type: list |
| 65 |
elements: str |
| 66 |
description: |
| 67 |
- Names of roles to manipulate. |
| 68 |
- If O(client_scope_id) is present, all roles must be under this client. |
| 69 |
- If O(client_scope_id) is absent, all roles must be under the realm. |
| 70 |
extends_documentation_fragment: |
| 71 |
- community.general._keycloak |
| 72 |
- community.general._keycloak.actiongroup_keycloak |
| 73 |
- community.general._attributes |
| 74 |
|
| 75 |
author: |
| 76 |
- Andre Desrosiers (@desand01) |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
- name: Add roles to public client scope |
| 81 |
community.general.keycloak_client_rolescope: |
| 82 |
auth_keycloak_url: https://auth.example.com/auth |
| 83 |
auth_realm: master |
| 84 |
auth_username: USERNAME |
| 85 |
auth_password: PASSWORD |
| 86 |
realm: MyCustomRealm |
| 87 |
client_id: frontend-client-public |
| 88 |
client_scope_id: backend-client-private |
| 89 |
role_names: |
| 90 |
- backend-role-admin |
| 91 |
- backend-role-user |
| 92 |
|
| 93 |
- name: Remove roles from public client scope |
| 94 |
community.general.keycloak_client_rolescope: |
| 95 |
auth_keycloak_url: https://auth.example.com/auth |
| 96 |
auth_realm: master |
| 97 |
auth_username: USERNAME |
| 98 |
auth_password: PASSWORD |
| 99 |
realm: MyCustomRealm |
| 100 |
client_id: frontend-client-public |
| 101 |
client_scope_id: backend-client-private |
| 102 |
role_names: |
| 103 |
- backend-role-admin |
| 104 |
state: absent |
| 105 |
|
| 106 |
- name: Add realm roles to public client scope |
| 107 |
community.general.keycloak_client_rolescope: |
| 108 |
auth_keycloak_url: https://auth.example.com/auth |
| 109 |
auth_realm: master |
| 110 |
auth_username: USERNAME |
| 111 |
auth_password: PASSWORD |
| 112 |
realm: MyCustomRealm |
| 113 |
client_id: frontend-client-public |
| 114 |
role_names: |
| 115 |
- realm-role-admin |
| 116 |
- realm-role-user |
| 117 |
""" |
| 118 |
|
| 119 |
RETURN = r""" |
| 120 |
msg: |
| 121 |
description: Message as to what action was taken. |
| 122 |
returned: always |
| 123 |
type: str |
| 124 |
sample: "Client role scope for frontend-client-public has been updated" |
| 125 |
|
| 126 |
end_state: |
| 127 |
description: Representation of role role scope after module execution. |
| 128 |
returned: on success |
| 129 |
type: list |
| 130 |
elements: dict |
| 131 |
sample: |
| 132 |
[ |
| 133 |
{ |
| 134 |
"clientRole": false, |
| 135 |
"composite": false, |
| 136 |
"containerId": "MyCustomRealm", |
| 137 |
"id": "47293104-59a6-46f0-b460-2e9e3c9c424c", |
| 138 |
"name": "backend-role-admin" |
| 139 |
}, |
| 140 |
{ |
| 141 |
"clientRole": false, |
| 142 |
"composite": false, |
| 143 |
"containerId": "MyCustomRealm", |
| 144 |
"id": "39c62a6d-542c-4715-92d2-41021eb33967", |
| 145 |
"name": "backend-role-user" |
| 146 |
} |
| 147 |
] |
| 148 |
""" |
| 149 |
|
| 150 |
from ansible.module_utils.basic import AnsibleModule |
| 151 |
|
| 152 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 153 |
KeycloakAPI, |
| 154 |
KeycloakError, |
| 155 |
get_token, |
| 156 |
keycloak_argument_spec, |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
def main(): |
| 161 |
""" |
| 162 |
Module execution |
| 163 |
|
| 164 |
:return: |
| 165 |
""" |
| 166 |
argument_spec = keycloak_argument_spec() |
| 167 |
|
| 168 |
meta_args = dict( |
| 169 |
client_id=dict(type="str", required=True), |
| 170 |
client_scope_id=dict(type="str"), |
| 171 |
realm=dict(type="str", default="master"), |
| 172 |
role_names=dict(type="list", elements="str", required=True), |
| 173 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 174 |
) |
| 175 |
|
| 176 |
argument_spec.update(meta_args) |
| 177 |
|
| 178 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 179 |
|
| 180 |
result = dict(changed=False, msg="", diff={}, end_state={}) |
| 181 |
|
| 182 |
# Obtain access token, initialize API |
| 183 |
try: |
| 184 |
connection_header = get_token(module.params) |
| 185 |
except KeycloakError as e: |
| 186 |
module.fail_json(msg=str(e)) |
| 187 |
|
| 188 |
kc = KeycloakAPI(module, connection_header) |
| 189 |
|
| 190 |
realm = module.params.get("realm") |
| 191 |
clientid = module.params.get("client_id") |
| 192 |
client_scope_id = module.params.get("client_scope_id") |
| 193 |
role_names = module.params.get("role_names") |
| 194 |
state = module.params.get("state") |
| 195 |
|
| 196 |
objRealm = kc.get_realm_by_id(realm) |
| 197 |
if not objRealm: |
| 198 |
module.fail_json(msg=f"Failed to retrive realm '{realm}'") |
| 199 |
|
| 200 |
objClient = kc.get_client_by_clientid(clientid, realm) |
| 201 |
if not objClient: |
| 202 |
module.fail_json(msg=f"Failed to retrive client '{realm}.{clientid}'") |
| 203 |
if objClient["fullScopeAllowed"] and state == "present": |
| 204 |
module.fail_json(msg=f"FullScopeAllowed is active for Client '{realm}.{clientid}'") |
| 205 |
|
| 206 |
if client_scope_id: |
| 207 |
objClientScope = kc.get_client_by_clientid(client_scope_id, realm) |
| 208 |
if not objClientScope: |
| 209 |
module.fail_json(msg=f"Failed to retrive client '{realm}.{client_scope_id}'") |
| 210 |
before_role_mapping = kc.get_client_role_scope_from_client(objClient["id"], objClientScope["id"], realm) |
| 211 |
else: |
| 212 |
before_role_mapping = kc.get_client_role_scope_from_realm(objClient["id"], realm) |
| 213 |
|
| 214 |
if client_scope_id: |
| 215 |
# retrive all role from client_scope |
| 216 |
client_scope_roles_by_name = kc.get_client_roles_by_id(objClientScope["id"], realm) |
| 217 |
else: |
| 218 |
# retrive all role from realm |
| 219 |
client_scope_roles_by_name = kc.get_realm_roles(realm) |
| 220 |
|
| 221 |
# convert to indexed Dict by name |
| 222 |
client_scope_roles_by_name = {role["name"]: role for role in client_scope_roles_by_name} |
| 223 |
role_mapping_by_name = {role["name"]: role for role in before_role_mapping} |
| 224 |
role_mapping_to_manipulate = [] |
| 225 |
|
| 226 |
if state == "present": |
| 227 |
# update desired |
| 228 |
for role_name in role_names: |
| 229 |
if role_name not in client_scope_roles_by_name: |
| 230 |
if client_scope_id: |
| 231 |
module.fail_json(msg=f"Failed to retrive role '{realm}.{client_scope_id}.{role_name}'") |
| 232 |
else: |
| 233 |
module.fail_json(msg=f"Failed to retrive role '{realm}.{role_name}'") |
| 234 |
if role_name not in role_mapping_by_name: |
| 235 |
role_mapping_to_manipulate.append(client_scope_roles_by_name[role_name]) |
| 236 |
role_mapping_by_name[role_name] = client_scope_roles_by_name[role_name] |
| 237 |
else: |
| 238 |
# remove role if present |
| 239 |
for role_name in role_names: |
| 240 |
if role_name in role_mapping_by_name: |
| 241 |
role_mapping_to_manipulate.append(role_mapping_by_name[role_name]) |
| 242 |
del role_mapping_by_name[role_name] |
| 243 |
|
| 244 |
before_role_mapping = sorted(before_role_mapping, key=lambda d: d["name"]) |
| 245 |
desired_role_mapping = sorted(role_mapping_by_name.values(), key=lambda d: d["name"]) |
| 246 |
|
| 247 |
result["changed"] = len(role_mapping_to_manipulate) > 0 |
| 248 |
|
| 249 |
if result["changed"]: |
| 250 |
result["diff"] = dict(before=before_role_mapping, after=desired_role_mapping) |
| 251 |
|
| 252 |
if not result["changed"]: |
| 253 |
# no changes |
| 254 |
result["end_state"] = before_role_mapping |
| 255 |
result["msg"] = f"No changes required for client role scope {clientid}." |
| 256 |
elif state == "present": |
| 257 |
# doing update |
| 258 |
if module.check_mode: |
| 259 |
result["end_state"] = desired_role_mapping |
| 260 |
elif client_scope_id: |
| 261 |
result["end_state"] = kc.update_client_role_scope_from_client( |
| 262 |
role_mapping_to_manipulate, objClient["id"], objClientScope["id"], realm |
| 263 |
) |
| 264 |
else: |
| 265 |
result["end_state"] = kc.update_client_role_scope_from_realm( |
| 266 |
role_mapping_to_manipulate, objClient["id"], realm |
| 267 |
) |
| 268 |
result["msg"] = f"Client role scope for {clientid} has been updated" |
| 269 |
else: |
| 270 |
# doing delete |
| 271 |
if module.check_mode: |
| 272 |
result["end_state"] = desired_role_mapping |
| 273 |
elif client_scope_id: |
| 274 |
result["end_state"] = kc.delete_client_role_scope_from_client( |
| 275 |
role_mapping_to_manipulate, objClient["id"], objClientScope["id"], realm |
| 276 |
) |
| 277 |
else: |
| 278 |
result["end_state"] = kc.delete_client_role_scope_from_realm( |
| 279 |
role_mapping_to_manipulate, objClient["id"], realm |
| 280 |
) |
| 281 |
result["msg"] = f"Client role scope for {clientid} has been deleted" |
| 282 |
module.exit_json(**result) |
| 283 |
|
| 284 |
|
| 285 |
if __name__ == "__main__": |
| 286 |
main() |