Debian Codegraph @

ansible_collections/community/general/tests/integration/targets/keycloak_client/tasks/main.yml

raw
1 ---
2 # Copyright (c) Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 - name: Install required packages
6 pip:
7 name:
8 - jmespath
9 register: result
10 until: result is success
11
12 - name: Wait for Keycloak
13 uri:
14 url: "{{ url }}/admin/"
15 status_code: 200
16 validate_certs: false
17 register: result
18 until: result.status == 200
19 retries: 10
20 delay: 10
21
22 - name: Delete realm
23 community.general.keycloak_realm:
24 auth_keycloak_url: "{{ url }}"
25 auth_realm: "{{ admin_realm }}"
26 auth_username: "{{ admin_user }}"
27 auth_password: "{{ admin_password }}"
28 id: "{{ realm }}"
29 realm: "{{ realm }}"
30 state: absent
31
32 - name: Create realm
33 community.general.keycloak_realm:
34 auth_keycloak_url: "{{ url }}"
35 auth_realm: "{{ admin_realm }}"
36 auth_username: "{{ admin_user }}"
37 auth_password: "{{ admin_password }}"
38 id: "{{ realm }}"
39 realm: "{{ realm }}"
40 state: present
41
42 - name: Desire client
43 community.general.keycloak_client:
44 auth_keycloak_url: "{{ url }}"
45 auth_realm: "{{ admin_realm }}"
46 auth_username: "{{ admin_user }}"
47 auth_password: "{{ admin_password }}"
48 realm: "{{ realm }}"
49 client_id: "{{ client_id }}"
50 state: present
51 redirect_uris: '{{redirect_uris1}}'
52 attributes: '{{client_attributes1}}'
53 protocol_mappers: '{{protocol_mappers1}}'
54 register: desire_client_not_present
55
56 - name: Desire client again with same props
57 community.general.keycloak_client:
58 auth_keycloak_url: "{{ url }}"
59 auth_realm: "{{ admin_realm }}"
60 auth_username: "{{ admin_user }}"
61 auth_password: "{{ admin_password }}"
62 realm: "{{ realm }}"
63 client_id: "{{ client_id }}"
64 state: present
65 redirect_uris: '{{redirect_uris1}}'
66 attributes: '{{client_attributes1}}'
67 protocol_mappers: '{{protocol_mappers1}}'
68 register: desire_client_when_present_and_same
69
70 - name: Check client again with same props
71 community.general.keycloak_client:
72 auth_keycloak_url: "{{ url }}"
73 auth_realm: "{{ admin_realm }}"
74 auth_username: "{{ admin_user }}"
75 auth_password: "{{ admin_password }}"
76 realm: "{{ realm }}"
77 client_id: "{{ client_id }}"
78 state: present
79 redirect_uris: '{{redirect_uris1}}'
80 attributes: '{{client_attributes1}}'
81 protocol_mappers: '{{protocol_mappers2_unordered}}'
82 authorization_services_enabled: false
83 check_mode: true
84 register: check_client_when_present_and_same
85
86 - name: Assert changes not detected in last two tasks (desire when same, and check)
87 assert:
88 that:
89 - desire_client_when_present_and_same is not changed
90 - check_client_when_present_and_same is not changed
91
92 - name: Check client again with changed props
93 community.general.keycloak_client:
94 auth_keycloak_url: "{{ url }}"
95 auth_realm: "{{ admin_realm }}"
96 auth_username: "{{ admin_user }}"
97 auth_password: "{{ admin_password }}"
98 realm: "{{ realm }}"
99 client_id: "{{ client_id }}"
100 state: present
101 redirect_uris: '{{redirect_uris1}}'
102 attributes: '{{client_attributes1}}'
103 protocol_mappers: '{{protocol_mappers1}}'
104 authorization_services_enabled: false
105 service_accounts_enabled: true
106 check_mode: true
107 register: check_client_when_present_and_changed
108
109 - name: Assert changes detected in last tasks
110 assert:
111 that:
112 - check_client_when_present_and_changed is changed
113
114 - name: Check client with modified protocol_mappers idempotence
115 community.general.keycloak_client:
116 auth_keycloak_url: "{{ url }}"
117 auth_realm: "{{ admin_realm }}"
118 auth_username: "{{ admin_user }}"
119 auth_password: "{{ admin_password }}"
120 realm: "{{ realm }}"
121 client_id: "{{ client_id }}"
122 state: present
123 redirect_uris: '{{redirect_uris1}}'
124 attributes: '{{client_attributes1}}'
125 protocol_mappers: '{{protocol_mappers3_modifed}}'
126 authorization_services_enabled: false
127 service_accounts_enabled: true
128 register: check_client_protocol_mappers_idempotence
129
130 - name: Assert idempotence changes to protocol_mappers
131 assert:
132 that:
133 - check_client_protocol_mappers_idempotence is changed
134 - end_state.protocolMappers | length == 3
135 - end_state.protocolMappers | community.general.json_query("[?name == 'email_verified']") | length == 0
136 - end_state.protocolMappers | community.general.json_query("[?name == 'address']") | length == 1
137 - end_state.protocolMappers | community.general.json_query("[?name == 'email']") | length == 1
138 - end_state.protocolMappers | community.general.json_query("[?name == 'family_name']") | length == 1
139 - email.config is defined
140 - email.config['access.token.claim'] == "false"
141 vars:
142 end_state: "{{ check_client_protocol_mappers_idempotence.end_state }}"
143 email: "{{ end_state.protocolMappers | community.general.json_query('[?name == `email`]') | first | d({}) }}"
144
145 - name: Desire client with flow binding overrides
146 community.general.keycloak_client:
147 auth_keycloak_url: "{{ url }}"
148 auth_realm: "{{ admin_realm }}"
149 auth_username: "{{ admin_user }}"
150 auth_password: "{{ admin_password }}"
151 realm: "{{ realm }}"
152 client_id: "{{ client_id }}"
153 state: present
154 redirect_uris: '{{redirect_uris1}}'
155 attributes: '{{client_attributes1}}'
156 protocol_mappers: '{{protocol_mappers1}}'
157 authentication_flow_binding_overrides:
158 browser_name: browser
159 direct_grant_name: direct grant
160 register: desire_client_with_flow_binding_overrides
161
162 - name: Assert flows are set
163 assert:
164 that:
165 - desire_client_with_flow_binding_overrides is changed
166 - "'authenticationFlowBindingOverrides' in desire_client_with_flow_binding_overrides.end_state"
167 - desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.browser | length > 0
168 - desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.direct_grant | length > 0
169
170 - name: Backup flow UUIDs
171 set_fact:
172 flow_browser_uuid: "{{ desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.browser }}"
173 flow_direct_grant_uuid: "{{ desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.direct_grant }}"
174
175 - name: Desire client with flow binding overrides remove direct_grant_name
176 community.general.keycloak_client:
177 auth_keycloak_url: "{{ url }}"
178 auth_realm: "{{ admin_realm }}"
179 auth_username: "{{ admin_user }}"
180 auth_password: "{{ admin_password }}"
181 realm: "{{ realm }}"
182 client_id: "{{ client_id }}"
183 state: present
184 redirect_uris: '{{redirect_uris1}}'
185 attributes: '{{client_attributes1}}'
186 protocol_mappers: '{{protocol_mappers1}}'
187 authentication_flow_binding_overrides:
188 browser_name: browser
189 register: desire_client_with_flow_binding_overrides
190
191 - name: Assert flows are updated
192 assert:
193 that:
194 - desire_client_with_flow_binding_overrides is changed
195 - "'authenticationFlowBindingOverrides' in desire_client_with_flow_binding_overrides.end_state"
196 - desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.browser | length > 0
197 - "'direct_grant' not in desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides"
198
199 - name: Desire client with flow binding overrides remove browser add direct_grant
200 community.general.keycloak_client:
201 auth_keycloak_url: "{{ url }}"
202 auth_realm: "{{ admin_realm }}"
203 auth_username: "{{ admin_user }}"
204 auth_password: "{{ admin_password }}"
205 realm: "{{ realm }}"
206 client_id: "{{ client_id }}"
207 state: present
208 redirect_uris: '{{redirect_uris1}}'
209 attributes: '{{client_attributes1}}'
210 protocol_mappers: '{{protocol_mappers1}}'
211 authentication_flow_binding_overrides:
212 direct_grant_name: direct grant
213 register: desire_client_with_flow_binding_overrides
214
215 - name: Assert flows are updated
216 assert:
217 that:
218 - desire_client_with_flow_binding_overrides is changed
219 - "'authenticationFlowBindingOverrides' in desire_client_with_flow_binding_overrides.end_state"
220 - "'browser' not in desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides"
221 - desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.direct_grant | length > 0
222
223 - name: Desire client with flow binding overrides with UUIDs
224 community.general.keycloak_client:
225 auth_keycloak_url: "{{ url }}"
226 auth_realm: "{{ admin_realm }}"
227 auth_username: "{{ admin_user }}"
228 auth_password: "{{ admin_password }}"
229 realm: "{{ realm }}"
230 client_id: "{{ client_id }}"
231 state: present
232 redirect_uris: '{{redirect_uris1}}'
233 attributes: '{{client_attributes1}}'
234 protocol_mappers: '{{protocol_mappers1}}'
235 authentication_flow_binding_overrides:
236 browser: "{{ flow_browser_uuid }}"
237 direct_grant: "{{ flow_direct_grant_uuid }}"
238 register: desire_client_with_flow_binding_overrides
239
240 - name: Assert flows are updated
241 assert:
242 that:
243 - desire_client_with_flow_binding_overrides is changed
244 - "'authenticationFlowBindingOverrides' in desire_client_with_flow_binding_overrides.end_state"
245 - desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.browser == flow_browser_uuid
246 - desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides.direct_grant == flow_direct_grant_uuid
247
248 - name: Unset flow binding overrides
249 community.general.keycloak_client:
250 auth_keycloak_url: "{{ url }}"
251 auth_realm: "{{ admin_realm }}"
252 auth_username: "{{ admin_user }}"
253 auth_password: "{{ admin_password }}"
254 realm: "{{ realm }}"
255 client_id: "{{ client_id }}"
256 state: present
257 redirect_uris: '{{redirect_uris1}}'
258 attributes: '{{client_attributes1}}'
259 protocol_mappers: '{{protocol_mappers1}}'
260 authentication_flow_binding_overrides:
261 browser: "{{ None }}"
262 direct_grant: null
263 register: desire_client_with_flow_binding_overrides
264
265 - name: Assert flows are removed
266 assert:
267 that:
268 - desire_client_with_flow_binding_overrides is changed
269 - "'authenticationFlowBindingOverrides' in desire_client_with_flow_binding_overrides.end_state"
270 - "'browser' not in desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides"
271 - "'direct_grant' not in desire_client_with_flow_binding_overrides.end_state.authenticationFlowBindingOverrides"
272
273 - name: Create a scope1 client scope
274 community.general.keycloak_clientscope:
275 auth_keycloak_url: "{{ url }}"
276 auth_realm: "{{ admin_realm }}"
277 auth_username: "{{ admin_user }}"
278 auth_password: "{{ admin_password }}"
279 realm: "{{ realm }}"
280 name: scope1
281 description: "test 1"
282 protocol: openid-connect
283
284 - name: Create a scope2 client scope
285 community.general.keycloak_clientscope:
286 auth_keycloak_url: "{{ url }}"
287 auth_realm: "{{ admin_realm }}"
288 auth_username: "{{ admin_user }}"
289 auth_password: "{{ admin_password }}"
290 realm: "{{ realm }}"
291 name: scope2
292 description: "test 2"
293 protocol: openid-connect
294
295 - name: Create a scope3 client scope
296 community.general.keycloak_clientscope:
297 auth_keycloak_url: "{{ url }}"
298 auth_realm: "{{ admin_realm }}"
299 auth_username: "{{ admin_user }}"
300 auth_password: "{{ admin_password }}"
301 realm: "{{ realm }}"
302 name: scope3
303 description: "test 3"
304 protocol: openid-connect
305
306 - name: Create Keycloak client with default_client_scopes (idempotent behavior)
307 community.general.keycloak_client:
308 auth_keycloak_url: "{{ url }}"
309 auth_realm: "{{ admin_realm }}"
310 auth_username: "{{ admin_user }}"
311 auth_password: "{{ admin_password }}"
312 realm: "{{ realm }}"
313 client_scopes_behavior: idempotent
314 default_client_scopes: ['scope1']
315 optional_client_scopes: ['scope2']
316 client_id: testSD-bug
317 state: present
318 register: desire_client_with_default_client_scopes
319
320 - name: Assert default_client_scopes and optional_client_scopes are set correctly
321 assert:
322 that:
323 - desire_client_with_default_client_scopes is changed
324 - '"scope1" in end_state.defaultClientScopes'
325 - '"scope2" in end_state.optionalClientScopes'
326 - end_state.defaultClientScopes | length == 1
327 - end_state.optionalClientScopes | length == 1
328 vars:
329 end_state: "{{ desire_client_with_default_client_scopes.end_state }}"
330
331 - name: Update Keycloak client with new scopes (ignore behavior, check mode)
332 community.general.keycloak_client:
333 auth_keycloak_url: "{{ url }}"
334 auth_realm: "{{ admin_realm }}"
335 auth_username: "{{ admin_user }}"
336 auth_password: "{{ admin_password }}"
337 realm: "{{ realm }}"
338 default_client_scopes: ['scope3']
339 optional_client_scopes: ['scope3']
340 client_id: testSD-bug
341 state: present
342 check_mode: true
343 register: desire_client_with_default_client_scopes
344
345 - name: Assert client scopes remain unchanged with ignore behavior
346 assert:
347 that:
348 - desire_client_with_default_client_scopes is not changed
349 - end_state.defaultClientScopes | length == 1
350 - end_state.optionalClientScopes | length == 1
351 - '"scope1" in end_state.defaultClientScopes'
352 - '"scope2" in end_state.optionalClientScopes'
353 vars:
354 end_state: "{{ desire_client_with_default_client_scopes.end_state }}"
355
356 - name: Update Keycloak client with conflicting scopes (patch behavior, should fail)
357 community.general.keycloak_client:
358 auth_keycloak_url: "{{ url }}"
359 auth_realm: "{{ admin_realm }}"
360 auth_username: "{{ admin_user }}"
361 auth_password: "{{ admin_password }}"
362 realm: "{{ realm }}"
363 client_scopes_behavior: patch
364 default_client_scopes: ['scope3']
365 optional_client_scopes: ['scope1', 'scope2', 'scope3']
366 client_id: testSD-bug
367 state: present
368 ignore_errors: true
369 register: desire_client_with_default_client_scopes
370
371 - name: Assert patch behavior fails when scope is both default and optional
372 assert:
373 that:
374 - desire_client_with_default_client_scopes is failed
375 - "'scope3' in desire_client_with_default_client_scopes.msg"
376
377 - name: Update Keycloak client with new scopes (patch behavior)
378 community.general.keycloak_client:
379 auth_keycloak_url: "{{ url }}"
380 auth_realm: "{{ admin_realm }}"
381 auth_username: "{{ admin_user }}"
382 auth_password: "{{ admin_password }}"
383 realm: "{{ realm }}"
384 client_scopes_behavior: patch
385 default_client_scopes: ['scope1', 'scope3']
386 optional_client_scopes: []
387 client_id: testSD-bug
388 state: present
389 register: desire_client_with_default_client_scopes
390
391 - name: Assert client scopes are patched correctly
392 assert:
393 that:
394 - desire_client_with_default_client_scopes is changed
395 - end_state.defaultClientScopes | length == 2
396 - end_state.optionalClientScopes | length == 1
397 - '"scope1" in end_state.defaultClientScopes'
398 - '"scope3" in end_state.defaultClientScopes'
399 - '"scope2" in end_state.optionalClientScopes'
400 vars:
401 end_state: "{{ desire_client_with_default_client_scopes.end_state }}"
402
403 - name: Update Keycloak client with empty default_client_scopes (idempotent behavior)
404 community.general.keycloak_client:
405 auth_keycloak_url: "{{ url }}"
406 auth_realm: "{{ admin_realm }}"
407 auth_username: "{{ admin_user }}"
408 auth_password: "{{ admin_password }}"
409 realm: "{{ realm }}"
410 client_scopes_behavior: idempotent
411 default_client_scopes: []
412 optional_client_scopes: ['scope3']
413 client_id: testSD-bug
414 state: present
415 register: desire_client_with_default_client_scopes
416
417 - name: Assert idempotent behavior with empty default_client_scopes
418 assert:
419 that:
420 - desire_client_with_default_client_scopes is changed
421 - end_state.defaultClientScopes | length == 0
422 - end_state.optionalClientScopes | length == 1
423 - '"scope3" in end_state.optionalClientScopes'
424 vars:
425 end_state: "{{ desire_client_with_default_client_scopes.end_state }}"
426
427 - name: Create client with initial attributes
428 community.general.keycloak_client:
429 auth_keycloak_url: "{{ url }}"
430 auth_realm: "{{ admin_realm }}"
431 auth_username: "{{ admin_user }}"
432 auth_password: "{{ admin_password }}"
433 realm: "{{ realm }}"
434 client_id: "{{ client_id_2 }}"
435 state: present
436 attributes: '{{ client_attributes1 }}'
437 register: check_client_when_present_and_attributes_modified
438
439 - name: Update client attributes
440 community.general.keycloak_client:
441 auth_keycloak_url: "{{ url }}"
442 auth_realm: "{{ admin_realm }}"
443 auth_username: "{{ admin_user }}"
444 auth_password: "{{ admin_password }}"
445 realm: "{{ realm }}"
446 client_id: "{{ client_id_2 }}"
447 state: present
448 attributes: '{{ client_attributes2 }}'
449 register: check_client_when_present_and_attributes_modified
450
451 - name: Assert client attributes are updated
452 assert:
453 that:
454 - check_client_when_present_and_attributes_modified is changed
455 - end_state.attributes["backchannel.logout.revoke.offline.tokens"] == 'false'
456 - end_state.attributes["backchannel.logout.session.required"] == 'false'
457 - end_state.attributes["oauth2.device.authorization.grant.enabled"] == 'false'
458 vars:
459 end_state: "{{ check_client_when_present_and_attributes_modified.end_state }}"
460
461 # ---- Tests for valid_post_logout_redirect_uris and backchannel_logout_url ----
462
463 - name: Create client with post logout redirect URIs and backchannel logout URL
464 community.general.keycloak_client:
465 auth_keycloak_url: "{{ url }}"
466 auth_realm: "{{ admin_realm }}"
467 auth_username: "{{ admin_user }}"
468 auth_password: "{{ admin_password }}"
469 realm: "{{ realm }}"
470 client_id: logout-test-client
471 valid_post_logout_redirect_uris: "{{ post_logout_redirect_uris }}"
472 backchannel_logout_url: "{{ backchannel_logout_url }}"
473 state: present
474 register: result_create_logout_client
475
476 - name: Assert logout client is created with correct attributes
477 assert:
478 that:
479 - result_create_logout_client is changed
480 - result_create_logout_client.end_state.attributes["post.logout.redirect.uris"] is defined
481 - result_create_logout_client.end_state.attributes["backchannel.logout.url"] == backchannel_logout_url
482
483 - name: Re-create client with same logout fields (idempotency)
484 community.general.keycloak_client:
485 auth_keycloak_url: "{{ url }}"
486 auth_realm: "{{ admin_realm }}"
487 auth_username: "{{ admin_user }}"
488 auth_password: "{{ admin_password }}"
489 realm: "{{ realm }}"
490 client_id: logout-test-client
491 valid_post_logout_redirect_uris: "{{ post_logout_redirect_uris }}"
492 backchannel_logout_url: "{{ backchannel_logout_url }}"
493 state: present
494 register: result_idempotent_logout_client
495
496 - name: Assert logout client is idempotent
497 assert:
498 that:
499 - result_idempotent_logout_client is not changed
500
501 - name: Update client logout fields
502 community.general.keycloak_client:
503 auth_keycloak_url: "{{ url }}"
504 auth_realm: "{{ admin_realm }}"
505 auth_username: "{{ admin_user }}"
506 auth_password: "{{ admin_password }}"
507 realm: "{{ realm }}"
508 client_id: logout-test-client
509 valid_post_logout_redirect_uris:
510 - "https://example.com/new-logout"
511 backchannel_logout_url: "https://example.com/new-backchannel"
512 state: present
513 register: result_update_logout_client
514
515 - name: Assert logout client fields are updated
516 assert:
517 that:
518 - result_update_logout_client is changed
519 - result_update_logout_client.end_state.attributes["backchannel.logout.url"] == "https://example.com/new-backchannel"
520
521 - name: Delete logout test client
522 community.general.keycloak_client:
523 auth_keycloak_url: "{{ url }}"
524 auth_realm: "{{ admin_realm }}"
525 auth_username: "{{ admin_user }}"
526 auth_password: "{{ admin_password }}"
527 realm: "{{ realm }}"
528 client_id: logout-test-client
529 state: absent