| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Eike Frost <[email protected]> |
| 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_clienttemplate |
| 11 |
|
| 12 |
short_description: Allows administration of Keycloak client templates using Keycloak API |
| 13 |
|
| 14 |
description: |
| 15 |
- This module allows the administration of Keycloak client templates using the Keycloak REST API. It requires access to |
| 16 |
the REST API using OpenID Connect; the user connecting and the client being used must have the requisite access rights. |
| 17 |
In a default Keycloak installation, admin-cli and an admin user would work, as would a separate client definition with |
| 18 |
the scope tailored to your needs and a user having the expected roles. |
| 19 |
- The names of module options are snake_cased versions of the camelCase ones found in the Keycloak API and its documentation |
| 20 |
at U(https://www.keycloak.org/docs-api/8.0/rest-api/index.html). |
| 21 |
- The Keycloak API does not always enforce for only sensible settings to be used -- you can set SAML-specific settings on |
| 22 |
an OpenID Connect client for instance and the other way around. Be careful. If you do not specify a setting, usually a |
| 23 |
sensible default is chosen. |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: full |
| 29 |
action_group: |
| 30 |
version_added: 10.2.0 |
| 31 |
|
| 32 |
options: |
| 33 |
state: |
| 34 |
description: |
| 35 |
- State of the client template. |
| 36 |
- On V(present), the client template is created (or updated if it exists already). |
| 37 |
- On V(absent), the client template is removed if it exists. |
| 38 |
choices: ['present', 'absent'] |
| 39 |
default: 'present' |
| 40 |
type: str |
| 41 |
|
| 42 |
id: |
| 43 |
description: |
| 44 |
- ID of client template to be worked on. This is usually a UUID. |
| 45 |
type: str |
| 46 |
|
| 47 |
realm: |
| 48 |
description: |
| 49 |
- Realm this client template is found in. |
| 50 |
type: str |
| 51 |
default: master |
| 52 |
|
| 53 |
name: |
| 54 |
description: |
| 55 |
- Name of the client template. |
| 56 |
type: str |
| 57 |
|
| 58 |
description: |
| 59 |
description: |
| 60 |
- Description of the client template in Keycloak. |
| 61 |
type: str |
| 62 |
|
| 63 |
protocol: |
| 64 |
description: |
| 65 |
- Type of client template. |
| 66 |
- The V(docker-v2) value was added in community.general 8.6.0. |
| 67 |
choices: ['openid-connect', 'saml', 'docker-v2'] |
| 68 |
type: str |
| 69 |
|
| 70 |
full_scope_allowed: |
| 71 |
description: |
| 72 |
- Is the "Full Scope Allowed" feature set for this client template or not. This is C(fullScopeAllowed) in the Keycloak |
| 73 |
REST API. |
| 74 |
type: bool |
| 75 |
|
| 76 |
protocol_mappers: |
| 77 |
description: |
| 78 |
- A list of dicts defining protocol mappers for this client template. This is C(protocolMappers) in the Keycloak REST |
| 79 |
API. |
| 80 |
type: list |
| 81 |
elements: dict |
| 82 |
suboptions: |
| 83 |
consentRequired: |
| 84 |
description: |
| 85 |
- Specifies whether a user needs to provide consent to a client for this mapper to be active. |
| 86 |
type: bool |
| 87 |
|
| 88 |
consentText: |
| 89 |
description: |
| 90 |
- The human-readable name of the consent the user is presented to accept. |
| 91 |
type: str |
| 92 |
|
| 93 |
id: |
| 94 |
description: |
| 95 |
- Usually a UUID specifying the internal ID of this protocol mapper instance. |
| 96 |
type: str |
| 97 |
|
| 98 |
name: |
| 99 |
description: |
| 100 |
- The name of this protocol mapper. |
| 101 |
type: str |
| 102 |
|
| 103 |
protocol: |
| 104 |
description: |
| 105 |
- This specifies for which protocol this protocol mapper is active. |
| 106 |
choices: ['openid-connect', 'saml', 'docker-v2'] |
| 107 |
type: str |
| 108 |
|
| 109 |
protocolMapper: |
| 110 |
description: |
| 111 |
- 'The Keycloak-internal name of the type of this protocol-mapper. While an exhaustive list is impossible to provide |
| 112 |
since this may be extended through SPIs by the user of Keycloak, by default Keycloak as of 3.4 ships with at least:' |
| 113 |
- V(docker-v2-allow-all-mapper). |
| 114 |
- V(oidc-address-mapper). |
| 115 |
- V(oidc-full-name-mapper). |
| 116 |
- V(oidc-group-membership-mapper). |
| 117 |
- V(oidc-hardcoded-claim-mapper). |
| 118 |
- V(oidc-hardcoded-role-mapper). |
| 119 |
- V(oidc-role-name-mapper). |
| 120 |
- V(oidc-script-based-protocol-mapper). |
| 121 |
- V(oidc-sha256-pairwise-sub-mapper). |
| 122 |
- V(oidc-usermodel-attribute-mapper). |
| 123 |
- V(oidc-usermodel-client-role-mapper). |
| 124 |
- V(oidc-usermodel-property-mapper). |
| 125 |
- V(oidc-usermodel-realm-role-mapper). |
| 126 |
- V(oidc-usersessionmodel-note-mapper). |
| 127 |
- V(saml-group-membership-mapper). |
| 128 |
- V(saml-hardcode-attribute-mapper). |
| 129 |
- V(saml-hardcode-role-mapper). |
| 130 |
- V(saml-role-list-mapper). |
| 131 |
- V(saml-role-name-mapper). |
| 132 |
- V(saml-user-attribute-mapper). |
| 133 |
- V(saml-user-property-mapper). |
| 134 |
- V(saml-user-session-note-mapper). |
| 135 |
- An exhaustive list of available mappers on your installation can be obtained on the admin console by going to |
| 136 |
Server Info -> Providers and looking under 'protocol-mapper'. |
| 137 |
type: str |
| 138 |
|
| 139 |
config: |
| 140 |
description: |
| 141 |
- Dict specifying the configuration options for the protocol mapper; the contents differ depending on the value |
| 142 |
of O(protocol_mappers[].protocolMapper) and are not documented other than by the source of the mappers and its |
| 143 |
parent class(es). An example is given below. It is easiest to obtain valid config values by dumping an already-existing |
| 144 |
protocol mapper configuration through check-mode in the RV(existing) field. |
| 145 |
type: dict |
| 146 |
|
| 147 |
attributes: |
| 148 |
description: |
| 149 |
- A dict of further attributes for this client template. This can contain various configuration settings, though in |
| 150 |
the default installation of Keycloak as of 3.4, none are documented or known, so this is usually empty. |
| 151 |
type: dict |
| 152 |
|
| 153 |
notes: |
| 154 |
- The Keycloak REST API defines further fields (namely C(bearerOnly), C(consentRequired), C(standardFlowEnabled), C(implicitFlowEnabled), |
| 155 |
C(directAccessGrantsEnabled), C(serviceAccountsEnabled), C(publicClient), and C(frontchannelLogout)) which, while available |
| 156 |
with keycloak_client, do not have any effect on Keycloak client-templates and are discarded if supplied with an API request |
| 157 |
changing client-templates. As such, they are not available through this module. |
| 158 |
extends_documentation_fragment: |
| 159 |
- community.general._keycloak |
| 160 |
- community.general._keycloak.actiongroup_keycloak |
| 161 |
- community.general._attributes |
| 162 |
|
| 163 |
author: |
| 164 |
- Eike Frost (@eikef) |
| 165 |
""" |
| 166 |
|
| 167 |
EXAMPLES = r""" |
| 168 |
- name: Create or update Keycloak client template (minimal), authentication with credentials |
| 169 |
community.general.keycloak_client: |
| 170 |
auth_client_id: admin-cli |
| 171 |
auth_keycloak_url: https://auth.example.com/auth |
| 172 |
auth_realm: master |
| 173 |
auth_username: USERNAME |
| 174 |
auth_password: PASSWORD |
| 175 |
realm: master |
| 176 |
name: this_is_a_test |
| 177 |
delegate_to: localhost |
| 178 |
|
| 179 |
- name: Create or update Keycloak client template (minimal), authentication with token |
| 180 |
community.general.keycloak_clienttemplate: |
| 181 |
auth_client_id: admin-cli |
| 182 |
auth_keycloak_url: https://auth.example.com/auth |
| 183 |
auth_realm: master |
| 184 |
token: TOKEN |
| 185 |
realm: master |
| 186 |
name: this_is_a_test |
| 187 |
delegate_to: localhost |
| 188 |
|
| 189 |
- name: Delete Keycloak client template |
| 190 |
community.general.keycloak_client: |
| 191 |
auth_client_id: admin-cli |
| 192 |
auth_keycloak_url: https://auth.example.com/auth |
| 193 |
auth_realm: master |
| 194 |
auth_username: USERNAME |
| 195 |
auth_password: PASSWORD |
| 196 |
realm: master |
| 197 |
state: absent |
| 198 |
name: test01 |
| 199 |
delegate_to: localhost |
| 200 |
|
| 201 |
- name: Create or update Keycloak client template (with a protocol mapper) |
| 202 |
community.general.keycloak_client: |
| 203 |
auth_client_id: admin-cli |
| 204 |
auth_keycloak_url: https://auth.example.com/auth |
| 205 |
auth_realm: master |
| 206 |
auth_username: USERNAME |
| 207 |
auth_password: PASSWORD |
| 208 |
realm: master |
| 209 |
name: this_is_a_test |
| 210 |
protocol_mappers: |
| 211 |
- config: |
| 212 |
access.token.claim: true |
| 213 |
claim.name: "family_name" |
| 214 |
id.token.claim: true |
| 215 |
jsonType.label: String |
| 216 |
user.attribute: lastName |
| 217 |
userinfo.token.claim: true |
| 218 |
consentRequired: true |
| 219 |
consentText: "${familyName}" |
| 220 |
name: family name |
| 221 |
protocol: openid-connect |
| 222 |
protocolMapper: oidc-usermodel-property-mapper |
| 223 |
full_scope_allowed: false |
| 224 |
id: bce6f5e9-d7d3-4955-817e-c5b7f8d65b3f |
| 225 |
delegate_to: localhost |
| 226 |
""" |
| 227 |
|
| 228 |
RETURN = r""" |
| 229 |
msg: |
| 230 |
description: Message as to what action was taken. |
| 231 |
returned: always |
| 232 |
type: str |
| 233 |
sample: "Client template testclient has been updated" |
| 234 |
|
| 235 |
proposed: |
| 236 |
description: Representation of proposed client template. |
| 237 |
returned: always |
| 238 |
type: dict |
| 239 |
sample: {"name": "test01"} |
| 240 |
|
| 241 |
existing: |
| 242 |
description: Representation of existing client template (sample is truncated). |
| 243 |
returned: always |
| 244 |
type: dict |
| 245 |
sample: |
| 246 |
{ |
| 247 |
"description": "test01", |
| 248 |
"fullScopeAllowed": false, |
| 249 |
"id": "9c3712ab-decd-481e-954f-76da7b006e5f", |
| 250 |
"name": "test01", |
| 251 |
"protocol": "saml" |
| 252 |
} |
| 253 |
|
| 254 |
end_state: |
| 255 |
description: Representation of client template after module execution (sample is truncated). |
| 256 |
returned: on success |
| 257 |
type: dict |
| 258 |
sample: |
| 259 |
{ |
| 260 |
"description": "test01", |
| 261 |
"fullScopeAllowed": false, |
| 262 |
"id": "9c3712ab-decd-481e-954f-76da7b006e5f", |
| 263 |
"name": "test01", |
| 264 |
"protocol": "saml" |
| 265 |
} |
| 266 |
""" |
| 267 |
|
| 268 |
from ansible.module_utils.basic import AnsibleModule |
| 269 |
|
| 270 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 271 |
KeycloakAPI, |
| 272 |
KeycloakError, |
| 273 |
camel, |
| 274 |
get_token, |
| 275 |
keycloak_argument_spec, |
| 276 |
) |
| 277 |
|
| 278 |
|
| 279 |
def main(): |
| 280 |
""" |
| 281 |
Module execution |
| 282 |
|
| 283 |
:return: |
| 284 |
""" |
| 285 |
argument_spec = keycloak_argument_spec() |
| 286 |
|
| 287 |
protmapper_spec = dict( |
| 288 |
consentRequired=dict(type="bool"), |
| 289 |
consentText=dict(type="str"), |
| 290 |
id=dict(type="str"), |
| 291 |
name=dict(type="str"), |
| 292 |
protocol=dict(type="str", choices=["openid-connect", "saml", "docker-v2"]), |
| 293 |
protocolMapper=dict(type="str"), |
| 294 |
config=dict(type="dict"), |
| 295 |
) |
| 296 |
|
| 297 |
meta_args = dict( |
| 298 |
realm=dict(type="str", default="master"), |
| 299 |
state=dict(default="present", choices=["present", "absent"]), |
| 300 |
id=dict(type="str"), |
| 301 |
name=dict(type="str"), |
| 302 |
description=dict(type="str"), |
| 303 |
protocol=dict(type="str", choices=["openid-connect", "saml", "docker-v2"]), |
| 304 |
attributes=dict(type="dict"), |
| 305 |
full_scope_allowed=dict(type="bool"), |
| 306 |
protocol_mappers=dict(type="list", elements="dict", options=protmapper_spec), |
| 307 |
) |
| 308 |
|
| 309 |
argument_spec.update(meta_args) |
| 310 |
|
| 311 |
module = AnsibleModule( |
| 312 |
argument_spec=argument_spec, |
| 313 |
supports_check_mode=True, |
| 314 |
required_one_of=( |
| 315 |
[ |
| 316 |
["id", "name"], |
| 317 |
["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"], |
| 318 |
] |
| 319 |
), |
| 320 |
required_together=([["auth_username", "auth_password"]]), |
| 321 |
required_by={"refresh_token": "auth_realm"}, |
| 322 |
) |
| 323 |
|
| 324 |
result = dict(changed=False, msg="", diff={}, proposed={}, existing={}, end_state={}) |
| 325 |
|
| 326 |
# Obtain access token, initialize API |
| 327 |
try: |
| 328 |
connection_header = get_token(module.params) |
| 329 |
except KeycloakError as e: |
| 330 |
module.fail_json(msg=str(e)) |
| 331 |
|
| 332 |
kc = KeycloakAPI(module, connection_header) |
| 333 |
|
| 334 |
realm = module.params.get("realm") |
| 335 |
state = module.params.get("state") |
| 336 |
cid = module.params.get("id") |
| 337 |
|
| 338 |
# Filter and map the parameters names that apply to the client template |
| 339 |
clientt_params = [ |
| 340 |
x |
| 341 |
for x in module.params |
| 342 |
if x |
| 343 |
not in [ |
| 344 |
"state", |
| 345 |
"auth_keycloak_url", |
| 346 |
"auth_client_id", |
| 347 |
"auth_realm", |
| 348 |
"auth_client_secret", |
| 349 |
"auth_username", |
| 350 |
"auth_password", |
| 351 |
"validate_certs", |
| 352 |
"realm", |
| 353 |
] |
| 354 |
and module.params.get(x) is not None |
| 355 |
] |
| 356 |
|
| 357 |
# See if it already exists in Keycloak |
| 358 |
if cid is None: |
| 359 |
before_clientt = kc.get_client_template_by_name(module.params.get("name"), realm=realm) |
| 360 |
if before_clientt is not None: |
| 361 |
cid = before_clientt["id"] |
| 362 |
else: |
| 363 |
before_clientt = kc.get_client_template_by_id(cid, realm=realm) |
| 364 |
|
| 365 |
if before_clientt is None: |
| 366 |
before_clientt = {} |
| 367 |
|
| 368 |
result["existing"] = before_clientt |
| 369 |
|
| 370 |
# Build a proposed changeset from parameters given to this module |
| 371 |
changeset = {} |
| 372 |
|
| 373 |
for clientt_param in clientt_params: |
| 374 |
# lists in the Keycloak API are sorted |
| 375 |
new_param_value = module.params.get(clientt_param) |
| 376 |
if isinstance(new_param_value, list): |
| 377 |
try: |
| 378 |
new_param_value = sorted(new_param_value) |
| 379 |
except TypeError: |
| 380 |
pass |
| 381 |
changeset[camel(clientt_param)] = new_param_value |
| 382 |
|
| 383 |
# Prepare the desired values using the existing values (non-existence results in a dict that is save to use as a basis) |
| 384 |
desired_clientt = before_clientt.copy() |
| 385 |
desired_clientt.update(changeset) |
| 386 |
|
| 387 |
result["proposed"] = changeset |
| 388 |
|
| 389 |
# Cater for when it doesn't exist (an empty dict) |
| 390 |
if not before_clientt: |
| 391 |
if state == "absent": |
| 392 |
# Do nothing and exit |
| 393 |
if module._diff: |
| 394 |
result["diff"] = dict(before="", after="") |
| 395 |
result["changed"] = False |
| 396 |
result["end_state"] = {} |
| 397 |
result["msg"] = "Client template does not exist, doing nothing." |
| 398 |
module.exit_json(**result) |
| 399 |
|
| 400 |
# Process a creation |
| 401 |
result["changed"] = True |
| 402 |
|
| 403 |
if "name" not in desired_clientt: |
| 404 |
module.fail_json(msg="name needs to be specified when creating a new client") |
| 405 |
|
| 406 |
if module._diff: |
| 407 |
result["diff"] = dict(before="", after=desired_clientt) |
| 408 |
|
| 409 |
if module.check_mode: |
| 410 |
module.exit_json(**result) |
| 411 |
|
| 412 |
# create it |
| 413 |
kc.create_client_template(desired_clientt, realm=realm) |
| 414 |
after_clientt = kc.get_client_template_by_name(desired_clientt["name"], realm=realm) |
| 415 |
|
| 416 |
result["end_state"] = after_clientt |
| 417 |
|
| 418 |
result["msg"] = f"Client template {desired_clientt['name']} has been created." |
| 419 |
module.exit_json(**result) |
| 420 |
|
| 421 |
else: |
| 422 |
if state == "present": |
| 423 |
# Process an update |
| 424 |
|
| 425 |
result["changed"] = True |
| 426 |
if module.check_mode: |
| 427 |
# We can only compare the current client template with the proposed updates we have |
| 428 |
if module._diff: |
| 429 |
result["diff"] = dict(before=before_clientt, after=desired_clientt) |
| 430 |
|
| 431 |
module.exit_json(**result) |
| 432 |
|
| 433 |
# do the update |
| 434 |
kc.update_client_template(cid, desired_clientt, realm=realm) |
| 435 |
|
| 436 |
after_clientt = kc.get_client_template_by_id(cid, realm=realm) |
| 437 |
if before_clientt == after_clientt: |
| 438 |
result["changed"] = False |
| 439 |
|
| 440 |
result["end_state"] = after_clientt |
| 441 |
|
| 442 |
if module._diff: |
| 443 |
result["diff"] = dict(before=before_clientt, after=after_clientt) |
| 444 |
|
| 445 |
result["msg"] = f"Client template {desired_clientt['name']} has been updated." |
| 446 |
module.exit_json(**result) |
| 447 |
|
| 448 |
else: |
| 449 |
# Process a deletion (because state was not 'present') |
| 450 |
result["changed"] = True |
| 451 |
|
| 452 |
if module._diff: |
| 453 |
result["diff"] = dict(before=before_clientt, after="") |
| 454 |
|
| 455 |
if module.check_mode: |
| 456 |
module.exit_json(**result) |
| 457 |
|
| 458 |
# delete it |
| 459 |
kc.delete_client_template(cid, realm=realm) |
| 460 |
result["proposed"] = {} |
| 461 |
|
| 462 |
result["end_state"] = {} |
| 463 |
|
| 464 |
result["msg"] = f"Client template {before_clientt['name']} has been deleted." |
| 465 |
|
| 466 |
module.exit_json(**result) |
| 467 |
|
| 468 |
|
| 469 |
if __name__ == "__main__": |
| 470 |
main() |