| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Eike Frost <[email protected]> |
| 4 |
# Copyright (c) 2021, Christophe Gilles <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or |
| 6 |
# https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: keycloak_realm_key |
| 12 |
|
| 13 |
short_description: Allows administration of Keycloak realm keys using Keycloak API |
| 14 |
|
| 15 |
version_added: 7.5.0 |
| 16 |
|
| 17 |
description: |
| 18 |
- This module allows the administration of Keycloak realm keys using the Keycloak REST API. It requires access to the REST |
| 19 |
API using OpenID Connect; the user connecting and the realm being used must have the requisite access rights. In a default |
| 20 |
Keycloak installation, admin-cli and an admin user would work, as would a separate realm definition with the scope tailored |
| 21 |
to your needs and a user having the expected roles. |
| 22 |
- The names of module options are snake_cased versions of the camelCase ones found in the Keycloak API and its documentation |
| 23 |
at U(https://www.keycloak.org/docs-api/8.0/rest-api/index.html). Aliases are provided so camelCased versions can be used |
| 24 |
as well. |
| 25 |
- This module is unable to detect changes to the actual cryptographic key after importing it. However, if some other property |
| 26 |
is changed alongside the cryptographic key, then the key also changes as a side-effect, as the JSON payload needs to include |
| 27 |
the private key. This can be considered either a bug or a feature, as the alternative would be to always update the realm |
| 28 |
key whether it has changed or not. |
| 29 |
attributes: |
| 30 |
check_mode: |
| 31 |
support: full |
| 32 |
diff_mode: |
| 33 |
support: partial |
| 34 |
action_group: |
| 35 |
version_added: 10.2.0 |
| 36 |
|
| 37 |
options: |
| 38 |
state: |
| 39 |
description: |
| 40 |
- State of the keycloak realm key. |
| 41 |
- On V(present), the realm key is created (or updated if it exists already). |
| 42 |
- On V(absent), the realm key is removed if it exists. |
| 43 |
choices: ['present', 'absent'] |
| 44 |
default: 'present' |
| 45 |
type: str |
| 46 |
name: |
| 47 |
description: |
| 48 |
- Name of the realm key to create. |
| 49 |
type: str |
| 50 |
required: true |
| 51 |
force: |
| 52 |
description: |
| 53 |
- Enforce the state of the private key and certificate. This is not automatically the case as this module is unable |
| 54 |
to determine the current state of the private key and thus cannot trigger an update based on an actual divergence. |
| 55 |
That said, a private key update may happen even if force is false as a side-effect of other changes. |
| 56 |
default: false |
| 57 |
type: bool |
| 58 |
parent_id: |
| 59 |
description: |
| 60 |
- The parent_id of the realm key. In practice the name of the realm. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
provider_id: |
| 64 |
description: |
| 65 |
- The name of the "provider ID" for the key. |
| 66 |
- The value V(rsa-enc) has been added in community.general 8.2.0. |
| 67 |
- The value V(java-keystore) has been added in community.general 12.4.0. This provider imports keys from |
| 68 |
a Java Keystore (JKS or PKCS12) file located on the Keycloak server filesystem. |
| 69 |
- The values V(rsa-generated), V(hmac-generated), V(aes-generated), and V(ecdsa-generated) have been added in |
| 70 |
community.general 12.4.0. These are auto-generated key providers where Keycloak manages the key material. |
| 71 |
- The values V(rsa-enc-generated), V(ecdh-generated), and V(eddsa-generated) have been added in |
| 72 |
community.general 12.4.0. These correspond to the auto-generated key providers available in Keycloak 26. |
| 73 |
choices: |
| 74 |
- rsa |
| 75 |
- rsa-enc |
| 76 |
- java-keystore |
| 77 |
- rsa-generated |
| 78 |
- rsa-enc-generated |
| 79 |
- hmac-generated |
| 80 |
- aes-generated |
| 81 |
- ecdsa-generated |
| 82 |
- ecdh-generated |
| 83 |
- eddsa-generated |
| 84 |
default: 'rsa' |
| 85 |
type: str |
| 86 |
config: |
| 87 |
description: |
| 88 |
- Dict specifying the key and its properties. |
| 89 |
type: dict |
| 90 |
suboptions: |
| 91 |
active: |
| 92 |
description: |
| 93 |
- Whether they key is active or inactive. Not to be confused with the state of the Ansible resource managed by the |
| 94 |
O(state) parameter. |
| 95 |
default: true |
| 96 |
type: bool |
| 97 |
enabled: |
| 98 |
description: |
| 99 |
- Whether the key is enabled or disabled. Not to be confused with the state of the Ansible resource managed by the |
| 100 |
O(state) parameter. |
| 101 |
default: true |
| 102 |
type: bool |
| 103 |
priority: |
| 104 |
description: |
| 105 |
- The priority of the key. |
| 106 |
type: int |
| 107 |
required: true |
| 108 |
algorithm: |
| 109 |
description: |
| 110 |
- Key algorithm. |
| 111 |
- The values V(RS384), V(RS512), V(PS256), V(PS384), V(PS512), V(RSA1_5), V(RSA-OAEP), V(RSA-OAEP-256) have been |
| 112 |
added in community.general 8.2.0. |
| 113 |
- The values V(HS256), V(HS384), V(HS512) (for HMAC), V(ES256), V(ES384), V(ES512) (for ECDSA), and V(AES) |
| 114 |
have been added in community.general 12.4.0. |
| 115 |
- The values V(ECDH_ES), V(ECDH_ES_A128KW), V(ECDH_ES_A192KW), V(ECDH_ES_A256KW) (for ECDH key exchange), |
| 116 |
and V(Ed25519), V(Ed448) (for EdDSA signing) have been added in community.general 12.4.0. |
| 117 |
- For O(provider_id=rsa), O(provider_id=rsa-generated), and O(provider_id=java-keystore), defaults to V(RS256). |
| 118 |
- For O(provider_id=rsa-enc) and O(provider_id=rsa-enc-generated), must be one of V(RSA1_5), V(RSA-OAEP), V(RSA-OAEP-256) (required, no default). |
| 119 |
- For O(provider_id=hmac-generated), must be one of V(HS256), V(HS384), V(HS512) (required, no default). |
| 120 |
- For O(provider_id=ecdsa-generated), must be one of V(ES256), V(ES384), V(ES512) (required, no default). |
| 121 |
- For O(provider_id=ecdh-generated), must be one of V(ECDH_ES), V(ECDH_ES_A128KW), V(ECDH_ES_A192KW), V(ECDH_ES_A256KW) (required, no default). |
| 122 |
- For O(provider_id=eddsa-generated), this option is not used (the algorithm is determined by O(config.elliptic_curve)). |
| 123 |
- For O(provider_id=aes-generated), this option is not used (AES is always used). |
| 124 |
choices: |
| 125 |
- RS256 |
| 126 |
- RS384 |
| 127 |
- RS512 |
| 128 |
- PS256 |
| 129 |
- PS384 |
| 130 |
- PS512 |
| 131 |
- RSA1_5 |
| 132 |
- RSA-OAEP |
| 133 |
- RSA-OAEP-256 |
| 134 |
- HS256 |
| 135 |
- HS384 |
| 136 |
- HS512 |
| 137 |
- ES256 |
| 138 |
- ES384 |
| 139 |
- ES512 |
| 140 |
- AES |
| 141 |
- ECDH_ES |
| 142 |
- ECDH_ES_A128KW |
| 143 |
- ECDH_ES_A192KW |
| 144 |
- ECDH_ES_A256KW |
| 145 |
- Ed25519 |
| 146 |
- Ed448 |
| 147 |
default: RS256 |
| 148 |
type: str |
| 149 |
private_key: |
| 150 |
description: |
| 151 |
- The private key as an ASCII string. Contents of the key must match O(config.algorithm) and O(provider_id). |
| 152 |
- Please note that the module cannot detect whether the private key specified differs from the current state's private |
| 153 |
key. Use O(force=true) to force the module to update the private key if you expect it to be updated. |
| 154 |
- Required when O(provider_id) is V(rsa) or V(rsa-enc). Not used for auto-generated providers. |
| 155 |
type: str |
| 156 |
certificate: |
| 157 |
description: |
| 158 |
- A certificate signed with the private key as an ASCII string. Contents of the key must match O(config.algorithm) |
| 159 |
and O(provider_id). |
| 160 |
- If you want Keycloak to automatically generate a certificate using your private key then set this to an empty |
| 161 |
string. |
| 162 |
- Required when O(provider_id) is V(rsa) or V(rsa-enc). Not used for auto-generated providers. |
| 163 |
type: str |
| 164 |
secret_size: |
| 165 |
description: |
| 166 |
- The size of the generated secret key in bytes. |
| 167 |
- Only applicable to O(provider_id=hmac-generated) and O(provider_id=aes-generated). |
| 168 |
- Valid values are V(16), V(24), V(32), V(64), V(128), V(256), V(512). |
| 169 |
- Default is V(64) for HMAC, V(16) for AES. |
| 170 |
type: int |
| 171 |
version_added: 12.4.0 |
| 172 |
key_size: |
| 173 |
description: |
| 174 |
- The size of the generated key in bits. |
| 175 |
- Only applicable to O(provider_id=rsa-generated) and O(provider_id=rsa-enc-generated). |
| 176 |
- Valid values are V(1024), V(2048), V(4096). Default is V(2048). |
| 177 |
type: int |
| 178 |
version_added: 12.4.0 |
| 179 |
elliptic_curve: |
| 180 |
description: |
| 181 |
- The elliptic curve to use for ECDSA, ECDH, or EdDSA keys. |
| 182 |
- For O(provider_id=ecdsa-generated) and O(provider_id=ecdh-generated), valid values are V(P-256), V(P-384), V(P-521). Default is V(P-256). |
| 183 |
- For O(provider_id=eddsa-generated), valid values are V(Ed25519), V(Ed448). Default is V(Ed25519). |
| 184 |
type: str |
| 185 |
choices: ['P-256', 'P-384', 'P-521', 'Ed25519', 'Ed448'] |
| 186 |
version_added: 12.4.0 |
| 187 |
keystore: |
| 188 |
description: |
| 189 |
- Path to the Java Keystore file on the Keycloak server filesystem. |
| 190 |
- Required when O(provider_id=java-keystore). |
| 191 |
type: str |
| 192 |
version_added: 12.4.0 |
| 193 |
keystore_password: |
| 194 |
description: |
| 195 |
- Password for the Java Keystore. |
| 196 |
- Required when O(provider_id=java-keystore). |
| 197 |
type: str |
| 198 |
version_added: 12.4.0 |
| 199 |
key_alias: |
| 200 |
description: |
| 201 |
- Alias of the key within the keystore. |
| 202 |
- Required when O(provider_id=java-keystore). |
| 203 |
type: str |
| 204 |
version_added: 12.4.0 |
| 205 |
key_password: |
| 206 |
description: |
| 207 |
- Password for the key within the keystore. |
| 208 |
- If not specified, the O(config.keystore_password) is used. |
| 209 |
- Only applicable to O(provider_id=java-keystore). |
| 210 |
type: str |
| 211 |
version_added: 12.4.0 |
| 212 |
update_password: |
| 213 |
description: |
| 214 |
- Controls when passwords are sent to Keycloak for V(java-keystore) provider. |
| 215 |
- V(always) - Always send passwords. Keycloak will update the component even if passwords |
| 216 |
have not changed. Use when you need to ensure passwords are updated. |
| 217 |
- V(on_create) - Only send passwords when creating a new component. When updating an |
| 218 |
existing component, send the masked value to preserve existing passwords. This makes |
| 219 |
the module idempotent for password fields. |
| 220 |
- This is necessary because Keycloak masks passwords in API responses (returns C(**********)), |
| 221 |
making comparison impossible. |
| 222 |
- Has no effect for providers other than V(java-keystore). |
| 223 |
type: str |
| 224 |
choices: ['always', 'on_create'] |
| 225 |
default: always |
| 226 |
version_added: 12.4.0 |
| 227 |
notes: |
| 228 |
- Current value of the private key cannot be fetched from Keycloak. Therefore comparing its desired state to the current |
| 229 |
state is not possible. |
| 230 |
- If O(config.certificate) is not explicitly provided it is dynamically created by Keycloak. Therefore comparing the current |
| 231 |
state of the certificate to the desired state (which may be empty) is not possible. |
| 232 |
- Due to the private key and certificate options the module is B(not fully idempotent). You can use O(force=true) to force |
| 233 |
the module to ensure updating if you know that the private key might have changed. |
| 234 |
- For auto-generated providers (V(rsa-generated), V(rsa-enc-generated), V(hmac-generated), V(aes-generated), V(ecdsa-generated), |
| 235 |
V(ecdh-generated), V(eddsa-generated)), Keycloak manages the key material automatically. The O(config.private_key) and |
| 236 |
O(config.certificate) options are not used. |
| 237 |
- For V(java-keystore) provider, the O(config.keystore_password) and O(config.key_password) values are returned masked by |
| 238 |
Keycloak. Therefore comparing their current state to the desired state is not possible. Use O(update_password=on_create) |
| 239 |
for idempotent playbooks, or use O(update_password=always) (default) if you need to ensure passwords are updated. |
| 240 |
extends_documentation_fragment: |
| 241 |
- community.general._keycloak |
| 242 |
- community.general._keycloak.actiongroup_keycloak |
| 243 |
- community.general._attributes |
| 244 |
|
| 245 |
author: |
| 246 |
- Samuli Seppänen (@mattock) |
| 247 |
- Ivan Kokalović (@koke1997) |
| 248 |
""" |
| 249 |
|
| 250 |
EXAMPLES = r""" |
| 251 |
- name: Manage Keycloak realm key (certificate autogenerated by Keycloak) |
| 252 |
community.general.keycloak_realm_key: |
| 253 |
name: custom |
| 254 |
state: present |
| 255 |
parent_id: master |
| 256 |
provider_id: rsa |
| 257 |
auth_keycloak_url: http://localhost:8080/auth |
| 258 |
auth_username: keycloak |
| 259 |
auth_password: keycloak |
| 260 |
auth_realm: master |
| 261 |
config: |
| 262 |
private_key: "{{ private_key }}" |
| 263 |
certificate: "" |
| 264 |
enabled: true |
| 265 |
active: true |
| 266 |
priority: 120 |
| 267 |
algorithm: RS256 |
| 268 |
|
| 269 |
- name: Manage Keycloak realm key and certificate |
| 270 |
community.general.keycloak_realm_key: |
| 271 |
name: custom |
| 272 |
state: present |
| 273 |
parent_id: master |
| 274 |
provider_id: rsa |
| 275 |
auth_keycloak_url: http://localhost:8080/auth |
| 276 |
auth_username: keycloak |
| 277 |
auth_password: keycloak |
| 278 |
auth_realm: master |
| 279 |
config: |
| 280 |
private_key: "{{ private_key }}" |
| 281 |
certificate: "{{ certificate }}" |
| 282 |
enabled: true |
| 283 |
active: true |
| 284 |
priority: 120 |
| 285 |
algorithm: RS256 |
| 286 |
|
| 287 |
- name: Create HMAC signing key (auto-generated) |
| 288 |
community.general.keycloak_realm_key: |
| 289 |
name: hmac-custom |
| 290 |
state: present |
| 291 |
parent_id: master |
| 292 |
provider_id: hmac-generated |
| 293 |
auth_keycloak_url: http://localhost:8080/auth |
| 294 |
auth_username: keycloak |
| 295 |
auth_password: keycloak |
| 296 |
auth_realm: master |
| 297 |
config: |
| 298 |
enabled: true |
| 299 |
active: true |
| 300 |
priority: 100 |
| 301 |
algorithm: HS256 |
| 302 |
secret_size: 64 |
| 303 |
|
| 304 |
- name: Create AES encryption key (auto-generated) |
| 305 |
community.general.keycloak_realm_key: |
| 306 |
name: aes-custom |
| 307 |
state: present |
| 308 |
parent_id: master |
| 309 |
provider_id: aes-generated |
| 310 |
auth_keycloak_url: http://localhost:8080/auth |
| 311 |
auth_username: keycloak |
| 312 |
auth_password: keycloak |
| 313 |
auth_realm: master |
| 314 |
config: |
| 315 |
enabled: true |
| 316 |
active: true |
| 317 |
priority: 100 |
| 318 |
secret_size: 16 |
| 319 |
|
| 320 |
- name: Create ECDSA signing key (auto-generated) |
| 321 |
community.general.keycloak_realm_key: |
| 322 |
name: ecdsa-custom |
| 323 |
state: present |
| 324 |
parent_id: master |
| 325 |
provider_id: ecdsa-generated |
| 326 |
auth_keycloak_url: http://localhost:8080/auth |
| 327 |
auth_username: keycloak |
| 328 |
auth_password: keycloak |
| 329 |
auth_realm: master |
| 330 |
config: |
| 331 |
enabled: true |
| 332 |
active: true |
| 333 |
priority: 100 |
| 334 |
algorithm: ES256 |
| 335 |
elliptic_curve: P-256 |
| 336 |
|
| 337 |
- name: Create RSA signing key (auto-generated) |
| 338 |
community.general.keycloak_realm_key: |
| 339 |
name: rsa-auto |
| 340 |
state: present |
| 341 |
parent_id: master |
| 342 |
provider_id: rsa-generated |
| 343 |
auth_keycloak_url: http://localhost:8080/auth |
| 344 |
auth_username: keycloak |
| 345 |
auth_password: keycloak |
| 346 |
auth_realm: master |
| 347 |
config: |
| 348 |
enabled: true |
| 349 |
active: true |
| 350 |
priority: 100 |
| 351 |
algorithm: RS256 |
| 352 |
key_size: 2048 |
| 353 |
|
| 354 |
- name: Remove default HMAC key |
| 355 |
community.general.keycloak_realm_key: |
| 356 |
name: hmac-generated |
| 357 |
state: absent |
| 358 |
parent_id: myrealm |
| 359 |
provider_id: hmac-generated |
| 360 |
auth_keycloak_url: http://localhost:8080/auth |
| 361 |
auth_username: keycloak |
| 362 |
auth_password: keycloak |
| 363 |
auth_realm: master |
| 364 |
config: |
| 365 |
priority: 100 |
| 366 |
|
| 367 |
- name: Create RSA encryption key (auto-generated) |
| 368 |
community.general.keycloak_realm_key: |
| 369 |
name: rsa-enc-auto |
| 370 |
state: present |
| 371 |
parent_id: master |
| 372 |
provider_id: rsa-enc-generated |
| 373 |
auth_keycloak_url: http://localhost:8080/auth |
| 374 |
auth_username: keycloak |
| 375 |
auth_password: keycloak |
| 376 |
auth_realm: master |
| 377 |
config: |
| 378 |
enabled: true |
| 379 |
active: true |
| 380 |
priority: 100 |
| 381 |
algorithm: RSA-OAEP |
| 382 |
key_size: 2048 |
| 383 |
|
| 384 |
- name: Create ECDH key exchange key (auto-generated) |
| 385 |
community.general.keycloak_realm_key: |
| 386 |
name: ecdh-custom |
| 387 |
state: present |
| 388 |
parent_id: master |
| 389 |
provider_id: ecdh-generated |
| 390 |
auth_keycloak_url: http://localhost:8080/auth |
| 391 |
auth_username: keycloak |
| 392 |
auth_password: keycloak |
| 393 |
auth_realm: master |
| 394 |
config: |
| 395 |
enabled: true |
| 396 |
active: true |
| 397 |
priority: 100 |
| 398 |
algorithm: ECDH_ES |
| 399 |
elliptic_curve: P-256 |
| 400 |
|
| 401 |
- name: Create EdDSA signing key (auto-generated) |
| 402 |
community.general.keycloak_realm_key: |
| 403 |
name: eddsa-custom |
| 404 |
state: present |
| 405 |
parent_id: master |
| 406 |
provider_id: eddsa-generated |
| 407 |
auth_keycloak_url: http://localhost:8080/auth |
| 408 |
auth_username: keycloak |
| 409 |
auth_password: keycloak |
| 410 |
auth_realm: master |
| 411 |
config: |
| 412 |
enabled: true |
| 413 |
active: true |
| 414 |
priority: 100 |
| 415 |
elliptic_curve: Ed25519 |
| 416 |
|
| 417 |
- name: Import key from Java Keystore (always update passwords) |
| 418 |
community.general.keycloak_realm_key: |
| 419 |
name: jks-imported |
| 420 |
state: present |
| 421 |
parent_id: master |
| 422 |
provider_id: java-keystore |
| 423 |
auth_keycloak_url: http://localhost:8080/auth |
| 424 |
auth_username: keycloak |
| 425 |
auth_password: keycloak |
| 426 |
auth_realm: master |
| 427 |
# update_password: always is the default - passwords are always sent to Keycloak |
| 428 |
config: |
| 429 |
enabled: true |
| 430 |
active: true |
| 431 |
priority: 100 |
| 432 |
algorithm: RS256 |
| 433 |
keystore: /opt/keycloak/conf/keystore.jks |
| 434 |
keystore_password: "{{ keystore_password }}" |
| 435 |
key_alias: mykey |
| 436 |
key_password: "{{ key_password }}" |
| 437 |
|
| 438 |
- name: Import key from Java Keystore (idempotent - only set password on create) |
| 439 |
community.general.keycloak_realm_key: |
| 440 |
name: jks-idempotent |
| 441 |
state: present |
| 442 |
parent_id: master |
| 443 |
provider_id: java-keystore |
| 444 |
auth_keycloak_url: http://localhost:8080/auth |
| 445 |
auth_username: keycloak |
| 446 |
auth_password: keycloak |
| 447 |
auth_realm: master |
| 448 |
update_password: on_create # Only send passwords when creating, preserve existing on update |
| 449 |
config: |
| 450 |
enabled: true |
| 451 |
active: true |
| 452 |
priority: 100 |
| 453 |
algorithm: RS256 |
| 454 |
keystore: /opt/keycloak/conf/keystore.jks |
| 455 |
keystore_password: "{{ keystore_password }}" |
| 456 |
key_alias: mykey |
| 457 |
key_password: "{{ key_password }}" |
| 458 |
""" |
| 459 |
|
| 460 |
RETURN = r""" |
| 461 |
msg: |
| 462 |
description: Message as to what action was taken. |
| 463 |
returned: always |
| 464 |
type: str |
| 465 |
|
| 466 |
end_state: |
| 467 |
description: Representation of the keycloak_realm_key after module execution. |
| 468 |
returned: on success |
| 469 |
type: dict |
| 470 |
contains: |
| 471 |
id: |
| 472 |
description: ID of the realm key. |
| 473 |
type: str |
| 474 |
returned: when O(state=present) |
| 475 |
sample: 5b7ec13f-99da-46ad-8326-ab4c73cf4ce4 |
| 476 |
name: |
| 477 |
description: Name of the realm key. |
| 478 |
type: str |
| 479 |
returned: when O(state=present) |
| 480 |
sample: mykey |
| 481 |
parentId: |
| 482 |
description: ID of the realm this key belongs to. |
| 483 |
type: str |
| 484 |
returned: when O(state=present) |
| 485 |
sample: myrealm |
| 486 |
providerId: |
| 487 |
description: The ID of the key provider. |
| 488 |
type: str |
| 489 |
returned: when O(state=present) |
| 490 |
sample: rsa |
| 491 |
providerType: |
| 492 |
description: The type of provider. |
| 493 |
type: str |
| 494 |
returned: when O(state=present) |
| 495 |
config: |
| 496 |
description: Realm key configuration. |
| 497 |
type: dict |
| 498 |
returned: when O(state=present) |
| 499 |
sample: |
| 500 |
{ |
| 501 |
"active": [ |
| 502 |
"true" |
| 503 |
], |
| 504 |
"algorithm": [ |
| 505 |
"RS256" |
| 506 |
], |
| 507 |
"enabled": [ |
| 508 |
"true" |
| 509 |
], |
| 510 |
"priority": [ |
| 511 |
"140" |
| 512 |
] |
| 513 |
} |
| 514 |
key_info: |
| 515 |
description: |
| 516 |
- Cryptographic key metadata fetched from the realm keys endpoint. |
| 517 |
- Only returned for V(java-keystore) provider when O(state=present) and not in check mode. |
| 518 |
- This includes the key ID (kid) and certificate fingerprint, which can be used to detect |
| 519 |
if the actual cryptographic key changed. |
| 520 |
type: dict |
| 521 |
returned: when O(provider_id=java-keystore) and O(state=present) |
| 522 |
version_added: 12.4.0 |
| 523 |
contains: |
| 524 |
kid: |
| 525 |
description: The key ID (kid) - unique identifier for the cryptographic key. |
| 526 |
type: str |
| 527 |
sample: bN7p5Nc_V2M7N_-mb5vVSRVPKq5qD_OuARInB9ofsJ0 |
| 528 |
certificate_fingerprint: |
| 529 |
description: SHA256 fingerprint of the certificate in colon-separated hex format. |
| 530 |
type: str |
| 531 |
sample: "A1:B2:C3:D4:E5:F6:..." |
| 532 |
status: |
| 533 |
description: The key status (ACTIVE, PASSIVE, DISABLED). |
| 534 |
type: str |
| 535 |
sample: ACTIVE |
| 536 |
valid_to: |
| 537 |
description: Certificate expiration timestamp in milliseconds since epoch. |
| 538 |
type: int |
| 539 |
sample: 1801789047000 |
| 540 |
""" |
| 541 |
|
| 542 |
import base64 |
| 543 |
import binascii |
| 544 |
import hashlib |
| 545 |
from copy import deepcopy |
| 546 |
from urllib.parse import urlencode |
| 547 |
|
| 548 |
from ansible.module_utils.basic import AnsibleModule |
| 549 |
|
| 550 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 551 |
KeycloakAPI, |
| 552 |
KeycloakError, |
| 553 |
camel, |
| 554 |
get_token, |
| 555 |
keycloak_argument_spec, |
| 556 |
) |
| 557 |
|
| 558 |
# Provider IDs that require private_key and certificate |
| 559 |
IMPORTED_KEY_PROVIDERS = ["rsa", "rsa-enc"] |
| 560 |
# Provider IDs that import keys from Java Keystore |
| 561 |
KEYSTORE_PROVIDERS = ["java-keystore"] |
| 562 |
# Provider IDs that auto-generate keys |
| 563 |
GENERATED_KEY_PROVIDERS = [ |
| 564 |
"rsa-generated", |
| 565 |
"rsa-enc-generated", |
| 566 |
"hmac-generated", |
| 567 |
"aes-generated", |
| 568 |
"ecdsa-generated", |
| 569 |
"ecdh-generated", |
| 570 |
"eddsa-generated", |
| 571 |
] |
| 572 |
|
| 573 |
# Mapping of Ansible parameter names to Keycloak config property names |
| 574 |
# for cases where camel() conversion doesn't produce the correct result. |
| 575 |
# Each provider type may use a different config key for elliptic curve. |
| 576 |
CONFIG_PARAM_MAPPING = { |
| 577 |
"elliptic_curve": "ecdsaEllipticCurveKey", |
| 578 |
} |
| 579 |
|
| 580 |
# Provider-specific config key names for elliptic_curve parameter |
| 581 |
# ECDSA and ECDH both use the same curves (P-256, P-384, P-521) but different config keys |
| 582 |
# EdDSA uses different curves (Ed25519, Ed448) with its own config key |
| 583 |
ELLIPTIC_CURVE_CONFIG_KEYS = { |
| 584 |
"ecdsa-generated": "ecdsaEllipticCurveKey", |
| 585 |
"ecdh-generated": "ecdhEllipticCurveKey", |
| 586 |
"eddsa-generated": "eddsaEllipticCurveKey", |
| 587 |
} |
| 588 |
|
| 589 |
# Valid algorithm choices per provider type |
| 590 |
# Note: aes-generated and eddsa-generated don't use algorithm config |
| 591 |
PROVIDER_ALGORITHMS = { |
| 592 |
"rsa": ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512"], |
| 593 |
"rsa-enc": ["RSA1_5", "RSA-OAEP", "RSA-OAEP-256"], |
| 594 |
"java-keystore": ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512"], |
| 595 |
"rsa-generated": ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512"], |
| 596 |
"rsa-enc-generated": ["RSA1_5", "RSA-OAEP", "RSA-OAEP-256"], |
| 597 |
"hmac-generated": ["HS256", "HS384", "HS512"], |
| 598 |
"ecdsa-generated": ["ES256", "ES384", "ES512"], |
| 599 |
"ecdh-generated": ["ECDH_ES", "ECDH_ES_A128KW", "ECDH_ES_A192KW", "ECDH_ES_A256KW"], |
| 600 |
} |
| 601 |
|
| 602 |
# Providers that don't use the algorithm config parameter |
| 603 |
# eddsa-generated: algorithm is determined by the elliptic curve (Ed25519 or Ed448) |
| 604 |
# aes-generated: always uses AES algorithm |
| 605 |
PROVIDERS_WITHOUT_ALGORITHM = ["aes-generated", "eddsa-generated"] |
| 606 |
|
| 607 |
# Providers where the RS256 default is valid (for backward compatibility) |
| 608 |
PROVIDERS_WITH_RS256_DEFAULT = ["rsa", "rsa-generated", "java-keystore"] |
| 609 |
|
| 610 |
# Config keys that cannot be compared and must be removed from changesets/diffs. |
| 611 |
# privateKey/certificate: Keycloak doesn't return private keys, certificates are generated dynamically. |
| 612 |
# keystorePassword/keyPassword: Keycloak masks these with "**********" in API responses. |
| 613 |
SENSITIVE_CONFIG_KEYS = ["privateKey", "certificate", "keystorePassword", "keyPassword"] |
| 614 |
|
| 615 |
|
| 616 |
def remove_sensitive_config_keys(config): |
| 617 |
for key in SENSITIVE_CONFIG_KEYS: |
| 618 |
config.pop(key, None) |
| 619 |
|
| 620 |
|
| 621 |
def get_keycloak_config_key(param_name, provider_id=None): |
| 622 |
"""Convert Ansible parameter name to Keycloak config key. |
| 623 |
|
| 624 |
Uses explicit mapping if available, otherwise applies camelCase conversion. |
| 625 |
For elliptic_curve, the config key depends on the provider type. |
| 626 |
""" |
| 627 |
# Handle elliptic_curve specially - each provider uses a different config key |
| 628 |
if param_name == "elliptic_curve" and provider_id in ELLIPTIC_CURVE_CONFIG_KEYS: |
| 629 |
return ELLIPTIC_CURVE_CONFIG_KEYS[param_name] |
| 630 |
if param_name in CONFIG_PARAM_MAPPING: |
| 631 |
return CONFIG_PARAM_MAPPING[param_name] |
| 632 |
return camel(param_name) |
| 633 |
|
| 634 |
|
| 635 |
def compute_certificate_fingerprint(certificate_pem): |
| 636 |
try: |
| 637 |
cert_der = base64.b64decode(certificate_pem) |
| 638 |
fingerprint = hashlib.sha256(cert_der).hexdigest().upper() |
| 639 |
return ":".join(fingerprint[i : i + 2] for i in range(0, len(fingerprint), 2)) |
| 640 |
except (ValueError, binascii.Error, TypeError): |
| 641 |
return None |
| 642 |
|
| 643 |
|
| 644 |
def get_key_info_for_component(kc, realm, component_id): |
| 645 |
try: |
| 646 |
keys_response = kc.get_realm_keys_metadata_by_id(realm) |
| 647 |
if not keys_response or "keys" not in keys_response: |
| 648 |
return None |
| 649 |
|
| 650 |
for key in keys_response.get("keys", []): |
| 651 |
if key.get("providerId") == component_id: |
| 652 |
return { |
| 653 |
"kid": key.get("kid"), |
| 654 |
"certificate_fingerprint": compute_certificate_fingerprint(key.get("certificate")), |
| 655 |
"public_key": key.get("publicKey"), |
| 656 |
"valid_to": key.get("validTo"), |
| 657 |
"status": key.get("status"), |
| 658 |
"algorithm": key.get("algorithm"), |
| 659 |
"type": key.get("type"), |
| 660 |
} |
| 661 |
return None |
| 662 |
except (KeyError, TypeError): |
| 663 |
return None |
| 664 |
|
| 665 |
|
| 666 |
def main(): |
| 667 |
""" |
| 668 |
Module execution |
| 669 |
|
| 670 |
:return: |
| 671 |
""" |
| 672 |
argument_spec = keycloak_argument_spec() |
| 673 |
|
| 674 |
meta_args = dict( |
| 675 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 676 |
name=dict(type="str", required=True), |
| 677 |
force=dict(type="bool", default=False), |
| 678 |
parent_id=dict(type="str", required=True), |
| 679 |
provider_id=dict( |
| 680 |
type="str", |
| 681 |
default="rsa", |
| 682 |
choices=[ |
| 683 |
"rsa", |
| 684 |
"rsa-enc", |
| 685 |
"java-keystore", |
| 686 |
"rsa-generated", |
| 687 |
"rsa-enc-generated", |
| 688 |
"hmac-generated", |
| 689 |
"aes-generated", |
| 690 |
"ecdsa-generated", |
| 691 |
"ecdh-generated", |
| 692 |
"eddsa-generated", |
| 693 |
], |
| 694 |
), |
| 695 |
config=dict( |
| 696 |
type="dict", |
| 697 |
options=dict( |
| 698 |
active=dict(type="bool", default=True), |
| 699 |
enabled=dict(type="bool", default=True), |
| 700 |
priority=dict(type="int", required=True), |
| 701 |
algorithm=dict( |
| 702 |
type="str", |
| 703 |
default="RS256", |
| 704 |
choices=[ |
| 705 |
"RS256", |
| 706 |
"RS384", |
| 707 |
"RS512", |
| 708 |
"PS256", |
| 709 |
"PS384", |
| 710 |
"PS512", |
| 711 |
"RSA1_5", |
| 712 |
"RSA-OAEP", |
| 713 |
"RSA-OAEP-256", |
| 714 |
"HS256", |
| 715 |
"HS384", |
| 716 |
"HS512", |
| 717 |
"ES256", |
| 718 |
"ES384", |
| 719 |
"ES512", |
| 720 |
"AES", |
| 721 |
"ECDH_ES", |
| 722 |
"ECDH_ES_A128KW", |
| 723 |
"ECDH_ES_A192KW", |
| 724 |
"ECDH_ES_A256KW", |
| 725 |
"Ed25519", |
| 726 |
"Ed448", |
| 727 |
], |
| 728 |
), |
| 729 |
private_key=dict(type="str", no_log=True), |
| 730 |
certificate=dict(type="str"), |
| 731 |
secret_size=dict(type="int", no_log=False), |
| 732 |
key_size=dict(type="int"), |
| 733 |
elliptic_curve=dict(type="str", choices=["P-256", "P-384", "P-521", "Ed25519", "Ed448"]), |
| 734 |
keystore=dict(type="str", no_log=False), |
| 735 |
keystore_password=dict(type="str", no_log=True), |
| 736 |
key_alias=dict(type="str", no_log=False), |
| 737 |
key_password=dict(type="str", no_log=True), |
| 738 |
), |
| 739 |
), |
| 740 |
update_password=dict( |
| 741 |
type="str", |
| 742 |
default="always", |
| 743 |
choices=["always", "on_create"], |
| 744 |
no_log=False, |
| 745 |
), |
| 746 |
) |
| 747 |
|
| 748 |
argument_spec.update(meta_args) |
| 749 |
|
| 750 |
module = AnsibleModule( |
| 751 |
argument_spec=argument_spec, |
| 752 |
supports_check_mode=True, |
| 753 |
required_one_of=( |
| 754 |
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]] |
| 755 |
), |
| 756 |
required_together=([["auth_username", "auth_password"]]), |
| 757 |
required_by={"refresh_token": "auth_realm"}, |
| 758 |
) |
| 759 |
|
| 760 |
provider_id = module.params["provider_id"] |
| 761 |
config = module.params["config"] or {} |
| 762 |
state = module.params["state"] |
| 763 |
|
| 764 |
# Validate that imported key providers have the required parameters |
| 765 |
if state == "present" and provider_id in IMPORTED_KEY_PROVIDERS: |
| 766 |
if not config.get("private_key"): |
| 767 |
module.fail_json(msg=f"config.private_key is required for provider_id '{provider_id}'") |
| 768 |
if config.get("certificate") is None: |
| 769 |
module.fail_json( |
| 770 |
msg=f"config.certificate is required for provider_id '{provider_id}' (use empty string for auto-generation)" |
| 771 |
) |
| 772 |
|
| 773 |
# Validate that java-keystore providers have the required parameters |
| 774 |
if state == "present" and provider_id in KEYSTORE_PROVIDERS: |
| 775 |
required_params = ["keystore", "keystore_password", "key_alias"] |
| 776 |
missing = [p for p in required_params if not config.get(p)] |
| 777 |
if missing: |
| 778 |
module.fail_json( |
| 779 |
msg=f"For provider_id=java-keystore, the following config options are required: {', '.join(missing)}" |
| 780 |
) |
| 781 |
|
| 782 |
# Validate algorithm for providers that use it |
| 783 |
if state == "present": |
| 784 |
algorithm = config.get("algorithm") |
| 785 |
if provider_id in PROVIDER_ALGORITHMS: |
| 786 |
valid_algorithms = PROVIDER_ALGORITHMS[provider_id] |
| 787 |
if algorithm not in valid_algorithms: |
| 788 |
msg = f"algorithm '{algorithm}' is not valid for provider_id '{provider_id}'." |
| 789 |
if algorithm == "RS256" and provider_id not in PROVIDERS_WITH_RS256_DEFAULT: |
| 790 |
msg += " The default 'RS256' is not valid for this provider." |
| 791 |
msg += f" Valid choices are: {', '.join(valid_algorithms)}" |
| 792 |
module.fail_json(msg=msg) |
| 793 |
elif provider_id in PROVIDERS_WITHOUT_ALGORITHM and algorithm is not None and algorithm != "RS256": |
| 794 |
# aes-generated and eddsa-generated don't use algorithm - only warn if user explicitly set a non-default value |
| 795 |
module.warn(f"algorithm is ignored for provider_id '{provider_id}'") |
| 796 |
|
| 797 |
# Validate elliptic curve for providers that use it |
| 798 |
if state == "present": |
| 799 |
elliptic_curve = config.get("elliptic_curve") |
| 800 |
if provider_id in ["ecdsa-generated", "ecdh-generated"] and elliptic_curve is not None: |
| 801 |
valid_curves = ["P-256", "P-384", "P-521"] |
| 802 |
if elliptic_curve not in valid_curves: |
| 803 |
module.fail_json( |
| 804 |
msg=f"elliptic_curve '{elliptic_curve}' is not valid for provider_id '{provider_id}'. " |
| 805 |
f"Valid choices are: {', '.join(valid_curves)}" |
| 806 |
) |
| 807 |
elif provider_id == "eddsa-generated" and elliptic_curve is not None: |
| 808 |
valid_curves = ["Ed25519", "Ed448"] |
| 809 |
if elliptic_curve not in valid_curves: |
| 810 |
module.fail_json( |
| 811 |
msg=f"elliptic_curve '{elliptic_curve}' is not valid for provider_id '{provider_id}'. " |
| 812 |
f"Valid choices are: {', '.join(valid_curves)}" |
| 813 |
) |
| 814 |
|
| 815 |
result = dict(changed=False, msg="", end_state={}, diff=dict(before={}, after={})) |
| 816 |
|
| 817 |
# This will include the current state of the realm key if it is already |
| 818 |
# present. This is only used for diff-mode. |
| 819 |
before_realm_key = {} |
| 820 |
before_realm_key["config"] = {} |
| 821 |
|
| 822 |
# Obtain access token, initialize API |
| 823 |
try: |
| 824 |
connection_header = get_token(module.params) |
| 825 |
except KeycloakError as e: |
| 826 |
module.fail_json(msg=str(e)) |
| 827 |
|
| 828 |
kc = KeycloakAPI(module, connection_header) |
| 829 |
|
| 830 |
params_to_ignore = list(keycloak_argument_spec().keys()) + ["state", "force", "parent_id", "update_password"] |
| 831 |
|
| 832 |
# Filter and map the parameters names that apply to the role |
| 833 |
component_params = [x for x in module.params if x not in params_to_ignore and module.params.get(x) is not None] |
| 834 |
|
| 835 |
# We only support one component provider type in this module |
| 836 |
provider_type = "org.keycloak.keys.KeyProvider" |
| 837 |
|
| 838 |
# Build a proposed changeset from parameters given to this module |
| 839 |
changeset = {} |
| 840 |
changeset["config"] = {} |
| 841 |
|
| 842 |
# Generate a JSON payload for Keycloak Admin API from the module |
| 843 |
# parameters. Parameters that do not belong to the JSON payload (e.g. |
| 844 |
# "state" or "auth_keycloal_url") have been filtered away earlier (see |
| 845 |
# above). |
| 846 |
# |
| 847 |
# This loop converts Ansible module parameters (snake-case) into |
| 848 |
# Keycloak-compatible format (camel-case). For example private_key |
| 849 |
# becomes privateKey. |
| 850 |
# |
| 851 |
# It also converts bool, str and int parameters into lists with a single |
| 852 |
# entry of 'str' type. Bool values are also lowercased. This is required |
| 853 |
# by Keycloak. |
| 854 |
# |
| 855 |
for component_param in component_params: |
| 856 |
if component_param == "config": |
| 857 |
for config_param in module.params["config"]: |
| 858 |
raw_value = module.params["config"][config_param] |
| 859 |
# Optional params (secret_size, key_size, elliptic_curve) default to None. |
| 860 |
# Skip them to avoid sending str(None) = "None" as a config value to Keycloak. |
| 861 |
if raw_value is None: |
| 862 |
continue |
| 863 |
# Use custom mapping if available, otherwise camelCase |
| 864 |
# Pass provider_id for elliptic_curve which uses different config keys per provider |
| 865 |
keycloak_key = get_keycloak_config_key(config_param, provider_id) |
| 866 |
changeset["config"][keycloak_key] = [] |
| 867 |
if isinstance(raw_value, bool): |
| 868 |
value = str(raw_value).lower() |
| 869 |
else: |
| 870 |
value = str(raw_value) |
| 871 |
|
| 872 |
changeset["config"][keycloak_key].append(value) |
| 873 |
else: |
| 874 |
# No need for camelcase in here as these are one word parameters |
| 875 |
new_param_value = module.params[component_param] |
| 876 |
changeset[camel(component_param)] = new_param_value |
| 877 |
|
| 878 |
# As provider_type is not a module parameter we have to add it to the |
| 879 |
# changeset explicitly. |
| 880 |
changeset["providerType"] = provider_type |
| 881 |
|
| 882 |
# Make a deep copy of the changeset. This is use when determining |
| 883 |
# changes to the current state. |
| 884 |
changeset_copy = deepcopy(changeset) |
| 885 |
|
| 886 |
# Remove keys that cannot be compared: privateKey/certificate (not returned |
| 887 |
# by Keycloak API) and keystore passwords (masked with "**********"). |
| 888 |
# The actual values remain in 'changeset' for the API payload. |
| 889 |
remove_sensitive_config_keys(changeset_copy["config"]) |
| 890 |
|
| 891 |
name = module.params["name"] |
| 892 |
force = module.params["force"] |
| 893 |
parent_id = module.params["parent_id"] |
| 894 |
|
| 895 |
# Get a list of all Keycloak components that are of keyprovider type. |
| 896 |
realm_keys = kc.get_components(urlencode(dict(type=provider_type)), parent_id) |
| 897 |
|
| 898 |
# If this component is present get its key ID. Confusingly the key ID is |
| 899 |
# also known as the Provider ID. |
| 900 |
key_id = None |
| 901 |
|
| 902 |
# Track individual parameter changes |
| 903 |
changes = "" |
| 904 |
|
| 905 |
# This tells Ansible whether the key was changed (added, removed, modified) |
| 906 |
result["changed"] = False |
| 907 |
|
| 908 |
# Loop through the list of components. If we encounter a component whose |
| 909 |
# name matches the value of the name parameter then assume the key is |
| 910 |
# already present. |
| 911 |
for key in realm_keys: |
| 912 |
if key["name"] == name: |
| 913 |
key_id = key["id"] |
| 914 |
changeset["id"] = key_id |
| 915 |
changeset_copy["id"] = key_id |
| 916 |
|
| 917 |
# Compare top-level parameters |
| 918 |
for param in changeset: |
| 919 |
before_realm_key[param] = key[param] |
| 920 |
|
| 921 |
if changeset_copy[param] != key[param] and param != "config": |
| 922 |
changes += f"{param}: {key[param]} -> {changeset_copy[param]}, " |
| 923 |
result["changed"] = True |
| 924 |
|
| 925 |
# Compare parameters under the "config" key |
| 926 |
# Note: Keycloak API may not return all config fields for default keys |
| 927 |
# (e.g., 'active', 'enabled', 'algorithm' may be missing). Handle this |
| 928 |
# gracefully by using .get() with defaults. |
| 929 |
for p, v in changeset_copy["config"].items(): |
| 930 |
# Get the current value, defaulting to our expected value if not present |
| 931 |
# This handles the case where Keycloak doesn't return certain fields |
| 932 |
# for default/generated keys |
| 933 |
current_value = key["config"].get(p, v) |
| 934 |
before_realm_key["config"][p] = current_value |
| 935 |
if v != current_value: |
| 936 |
changes += f"config.{p}: {current_value} -> {v}, " |
| 937 |
result["changed"] = True |
| 938 |
|
| 939 |
# For java-keystore provider, also fetch and compare key info (kid) |
| 940 |
# This detects if the actual cryptographic key changed even when |
| 941 |
# other config parameters remain the same |
| 942 |
if provider_id in KEYSTORE_PROVIDERS: |
| 943 |
current_key_info = get_key_info_for_component(kc, parent_id, key_id) |
| 944 |
if current_key_info: |
| 945 |
before_realm_key["key_info"] = { |
| 946 |
"kid": current_key_info.get("kid"), |
| 947 |
"certificate_fingerprint": current_key_info.get("certificate_fingerprint"), |
| 948 |
} |
| 949 |
|
| 950 |
# Sanitize linefeeds for the privateKey and certificate (only for imported providers). |
| 951 |
# Without this the JSON payload will be invalid. |
| 952 |
if "privateKey" in changeset["config"]: |
| 953 |
changeset["config"]["privateKey"][0] = changeset["config"]["privateKey"][0].replace("\\n", "\n") |
| 954 |
if "certificate" in changeset["config"]: |
| 955 |
changeset["config"]["certificate"][0] = changeset["config"]["certificate"][0].replace("\\n", "\n") |
| 956 |
|
| 957 |
# For java-keystore provider: handle update_password parameter |
| 958 |
# When update_password=on_create and we're updating an existing component, |
| 959 |
# replace actual passwords with the masked value ("**********") that Keycloak |
| 960 |
# returns in API responses. When Keycloak receives this masked value, it |
| 961 |
# preserves the existing password instead of updating it. |
| 962 |
# This makes the module idempotent for password fields. |
| 963 |
update_password = module.params["update_password"] |
| 964 |
if provider_id in KEYSTORE_PROVIDERS and key_id and update_password == "on_create": |
| 965 |
SECRET_VALUE = "**********" |
| 966 |
if "keystorePassword" in changeset["config"]: |
| 967 |
changeset["config"]["keystorePassword"] = [SECRET_VALUE] |
| 968 |
if "keyPassword" in changeset["config"]: |
| 969 |
changeset["config"]["keyPassword"] = [SECRET_VALUE] |
| 970 |
|
| 971 |
# Check all the possible states of the resource and do what is needed to |
| 972 |
# converge current state with desired state (create, update or delete |
| 973 |
# the key). |
| 974 |
if key_id and state == "present": |
| 975 |
if result["changed"]: |
| 976 |
if module._diff: |
| 977 |
remove_sensitive_config_keys(before_realm_key["config"]) |
| 978 |
result["diff"] = dict(before=before_realm_key, after=changeset_copy) |
| 979 |
|
| 980 |
if module.check_mode: |
| 981 |
result["msg"] = f"Realm key {name} would be changed: {changes.strip(', ')}" |
| 982 |
else: |
| 983 |
kc.update_component(changeset, parent_id) |
| 984 |
result["msg"] = f"Realm key {name} changed: {changes.strip(', ')}" |
| 985 |
elif not result["changed"] and force: |
| 986 |
kc.update_component(changeset, parent_id) |
| 987 |
result["changed"] = True |
| 988 |
result["msg"] = f"Realm key {name} was forcibly updated" |
| 989 |
else: |
| 990 |
result["msg"] = f"Realm key {name} was in sync" |
| 991 |
|
| 992 |
result["end_state"] = changeset_copy |
| 993 |
|
| 994 |
# For java-keystore provider, include key info in end_state |
| 995 |
if provider_id in KEYSTORE_PROVIDERS: |
| 996 |
if not module.check_mode: |
| 997 |
key_info = get_key_info_for_component(kc, parent_id, key_id) |
| 998 |
if key_info: |
| 999 |
result["end_state"]["key_info"] = { |
| 1000 |
"kid": key_info.get("kid"), |
| 1001 |
"certificate_fingerprint": key_info.get("certificate_fingerprint"), |
| 1002 |
"status": key_info.get("status"), |
| 1003 |
"valid_to": key_info.get("valid_to"), |
| 1004 |
} |
| 1005 |
else: |
| 1006 |
module.warn( |
| 1007 |
f"Key component '{name}' exists but no active key was found. " |
| 1008 |
"This may indicate an incorrect keystore password, path, or alias." |
| 1009 |
) |
| 1010 |
elif key_id and state == "absent": |
| 1011 |
if module._diff: |
| 1012 |
remove_sensitive_config_keys(before_realm_key["config"]) |
| 1013 |
result["diff"] = dict(before=before_realm_key, after={}) |
| 1014 |
|
| 1015 |
if module.check_mode: |
| 1016 |
result["changed"] = True |
| 1017 |
result["msg"] = f"Realm key {name} would be deleted" |
| 1018 |
else: |
| 1019 |
kc.delete_component(key_id, parent_id) |
| 1020 |
result["changed"] = True |
| 1021 |
result["msg"] = f"Realm key {name} deleted" |
| 1022 |
|
| 1023 |
result["end_state"] = {} |
| 1024 |
elif not key_id and state == "present": |
| 1025 |
if module._diff: |
| 1026 |
result["diff"] = dict(before={}, after=changeset_copy) |
| 1027 |
|
| 1028 |
if module.check_mode: |
| 1029 |
result["changed"] = True |
| 1030 |
result["msg"] = f"Realm key {name} would be created" |
| 1031 |
else: |
| 1032 |
kc.create_component(changeset, parent_id) |
| 1033 |
result["changed"] = True |
| 1034 |
result["msg"] = f"Realm key {name} created" |
| 1035 |
|
| 1036 |
# For java-keystore provider, fetch and include key info after creation |
| 1037 |
if provider_id in KEYSTORE_PROVIDERS: |
| 1038 |
# We need to get the component ID first (it was just created) |
| 1039 |
realm_keys_after = kc.get_components(urlencode(dict(type=provider_type)), parent_id) |
| 1040 |
for k in realm_keys_after: |
| 1041 |
if k["name"] == name: |
| 1042 |
new_key_id = k["id"] |
| 1043 |
key_info = get_key_info_for_component(kc, parent_id, new_key_id) |
| 1044 |
if key_info: |
| 1045 |
changeset_copy["key_info"] = { |
| 1046 |
"kid": key_info.get("kid"), |
| 1047 |
"certificate_fingerprint": key_info.get("certificate_fingerprint"), |
| 1048 |
"status": key_info.get("status"), |
| 1049 |
"valid_to": key_info.get("valid_to"), |
| 1050 |
} |
| 1051 |
else: |
| 1052 |
module.warn( |
| 1053 |
f"Key component '{name}' was created but no active key was found. " |
| 1054 |
"This may indicate an incorrect keystore password, path, or alias." |
| 1055 |
) |
| 1056 |
break |
| 1057 |
|
| 1058 |
result["end_state"] = changeset_copy |
| 1059 |
elif not key_id and state == "absent": |
| 1060 |
result["changed"] = False |
| 1061 |
result["msg"] = f"Realm key {name} not present" |
| 1062 |
result["end_state"] = {} |
| 1063 |
|
| 1064 |
module.exit_json(**result) |
| 1065 |
|
| 1066 |
|
| 1067 |
if __name__ == "__main__": |
| 1068 |
main() |