Debian Codegraph @

ansible_collections/community/general/tests/integration/targets/keycloak_authz_permission/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: Remove keycloak client to avoid failures from previous failed runs
6 community.general.keycloak_client:
7 auth_keycloak_url: "{{ url }}"
8 auth_realm: "{{ admin_realm }}"
9 auth_username: "{{ admin_user }}"
10 auth_password: "{{ admin_password }}"
11 realm: "{{ realm }}"
12 client_id: "{{ client_id }}"
13 state: absent
14
15 - name: Create keycloak client with authorization services enabled
16 community.general.keycloak_client:
17 auth_keycloak_url: "{{ url }}"
18 auth_realm: "{{ admin_realm }}"
19 auth_username: "{{ admin_user }}"
20 auth_password: "{{ admin_password }}"
21 realm: "{{ realm }}"
22 client_id: "{{ client_id }}"
23 state: present
24 enabled: true
25 public_client: false
26 service_accounts_enabled: true
27 authorization_services_enabled: true
28
29 - name: Create file:create authorization scope
30 community.general.keycloak_authz_authorization_scope:
31 auth_keycloak_url: "{{ url }}"
32 auth_realm: "{{ admin_realm }}"
33 auth_username: "{{ admin_user }}"
34 auth_password: "{{ admin_password }}"
35 state: present
36 name: "file:create"
37 display_name: "File create"
38 icon_uri: "http://localhost/icon.png"
39 client_id: "{{ client_id }}"
40 realm: "{{ realm }}"
41 register: result
42
43 - name: Create file:delete authorization scope
44 community.general.keycloak_authz_authorization_scope:
45 auth_keycloak_url: "{{ url }}"
46 auth_realm: "{{ admin_realm }}"
47 auth_username: "{{ admin_user }}"
48 auth_password: "{{ admin_password }}"
49 state: present
50 name: "file:delete"
51 display_name: "File delete"
52 icon_uri: "http://localhost/icon.png"
53 client_id: "{{ client_id }}"
54 realm: "{{ realm }}"
55 register: result
56
57 - name: Create permission without type (test for failure)
58 community.general.keycloak_authz_permission:
59 auth_keycloak_url: "{{ url }}"
60 auth_realm: "{{ admin_realm }}"
61 auth_username: "{{ admin_user }}"
62 auth_password: "{{ admin_password }}"
63 state: present
64 name: "ScopePermission"
65 description: "Scope permission"
66 client_id: "{{ client_id }}"
67 realm: "{{ realm }}"
68 register: result
69 failed_when: result.msg.find('missing required arguments') == -1
70
71 - name: Create scope permission without scopes (test for failure)
72 community.general.keycloak_authz_permission:
73 auth_keycloak_url: "{{ url }}"
74 auth_realm: "{{ admin_realm }}"
75 auth_username: "{{ admin_user }}"
76 auth_password: "{{ admin_password }}"
77 state: present
78 name: "ScopePermission"
79 description: "Scope permission"
80 permission_type: scope
81 client_id: "{{ client_id }}"
82 realm: "{{ realm }}"
83 register: result
84 failed_when: result.msg.find('Scopes need to defined when permission type is set to scope!') == -1
85
86 - name: Create scope permission with multiple resources (test for failure)
87 community.general.keycloak_authz_permission:
88 auth_keycloak_url: "{{ url }}"
89 auth_realm: "{{ admin_realm }}"
90 auth_username: "{{ admin_user }}"
91 auth_password: "{{ admin_password }}"
92 state: present
93 name: "ScopePermission"
94 description: "Scope permission"
95 resources:
96 - "Default Resource"
97 - "Other Resource"
98 permission_type: scope
99 scopes:
100 - "file:delete"
101 client_id: "{{ client_id }}"
102 realm: "{{ realm }}"
103 register: result
104 failed_when: result.msg.find('Only one resource can be defined for a scope permission!') == -1
105
106 - name: Create scope permission with invalid policy name (test for failure)
107 community.general.keycloak_authz_permission:
108 auth_keycloak_url: "{{ url }}"
109 auth_realm: "{{ admin_realm }}"
110 auth_username: "{{ admin_user }}"
111 auth_password: "{{ admin_password }}"
112 state: present
113 name: "ScopePermission"
114 description: "Scope permission"
115 permission_type: scope
116 scopes:
117 - "file:delete"
118 policies:
119 - "Missing Policy"
120 client_id: "{{ client_id }}"
121 realm: "{{ realm }}"
122 register: result
123 failed_when: result.msg.find('Unable to find authorization policy with name') == -1
124
125 - name: Create scope permission
126 community.general.keycloak_authz_permission:
127 auth_keycloak_url: "{{ url }}"
128 auth_realm: "{{ admin_realm }}"
129 auth_username: "{{ admin_user }}"
130 auth_password: "{{ admin_password }}"
131 state: present
132 name: "ScopePermission"
133 description: "Scope permission"
134 permission_type: scope
135 scopes:
136 - "file:delete"
137 policies:
138 - "Default Policy"
139 client_id: "{{ client_id }}"
140 realm: "{{ realm }}"
141 register: result
142
143 - name: Assert that scope permission was created
144 assert:
145 that:
146 - result is changed
147 - result.end_state != {}
148 - result.end_state.name == "ScopePermission"
149 - result.end_state.description == "Scope permission"
150 - result.end_state.type == "scope"
151 - result.end_state.resources == []
152 - result.end_state.policies|length == 1
153 - result.end_state.scopes|length == 1
154
155 - name: Query state
156 community.general.keycloak_authz_permission_info:
157 auth_keycloak_url: "{{ url }}"
158 auth_realm: "{{ admin_realm }}"
159 auth_username: "{{ admin_user }}"
160 auth_password: "{{ admin_password }}"
161 name: "ScopePermission"
162 client_id: "{{ client_id }}"
163 realm: "{{ realm }}"
164 register: result
165
166 - name: Assert that queried state matches desired end state
167 assert:
168 that:
169 - result.queried_state.name == "ScopePermission"
170 - result.queried_state.description == "Scope permission"
171
172 - name: Create scope permission (test for idempotency)
173 community.general.keycloak_authz_permission:
174 auth_keycloak_url: "{{ url }}"
175 auth_realm: "{{ admin_realm }}"
176 auth_username: "{{ admin_user }}"
177 auth_password: "{{ admin_password }}"
178 state: present
179 name: "ScopePermission"
180 description: "Scope permission"
181 permission_type: scope
182 scopes:
183 - "file:delete"
184 policies:
185 - "Default Policy"
186 client_id: "{{ client_id }}"
187 realm: "{{ realm }}"
188 register: result
189
190 - name: Assert that nothing changed
191 assert:
192 that:
193 - result.end_state != {}
194 - result.end_state.name == "ScopePermission"
195 - result.end_state.description == "Scope permission"
196 - result.end_state.type == "scope"
197 - result.end_state.resources == []
198 - result.end_state.policies|length == 1
199 - result.end_state.scopes|length == 1
200
201 - name: Query state
202 community.general.keycloak_authz_permission_info:
203 auth_keycloak_url: "{{ url }}"
204 auth_realm: "{{ admin_realm }}"
205 auth_username: "{{ admin_user }}"
206 auth_password: "{{ admin_password }}"
207 name: "ScopePermission"
208 client_id: "{{ client_id }}"
209 realm: "{{ realm }}"
210 register: result
211
212 - name: Assert that queried state matches desired end state
213 assert:
214 that:
215 - result.queried_state.name == "ScopePermission"
216 - result.queried_state.description == "Scope permission"
217
218 - name: Update scope permission
219 community.general.keycloak_authz_permission:
220 auth_keycloak_url: "{{ url }}"
221 auth_realm: "{{ admin_realm }}"
222 auth_username: "{{ admin_user }}"
223 auth_password: "{{ admin_password }}"
224 state: present
225 name: "ScopePermission"
226 description: "Scope permission changed"
227 permission_type: scope
228 decision_strategy: 'AFFIRMATIVE'
229 scopes:
230 - "file:create"
231 - "file:delete"
232 policies: []
233 client_id: "{{ client_id }}"
234 realm: "{{ realm }}"
235 register: result
236
237 - name: Assert that scope permission was updated correctly
238 assert:
239 that:
240 - result.changed == True
241 - result.end_state != {}
242 - result.end_state.scopes|length == 2
243 - result.end_state.policies == []
244 - result.end_state.resources == []
245 - result.end_state.name == "ScopePermission"
246 - result.end_state.description == "Scope permission changed"
247
248 - name: Query state
249 community.general.keycloak_authz_permission_info:
250 auth_keycloak_url: "{{ url }}"
251 auth_realm: "{{ admin_realm }}"
252 auth_username: "{{ admin_user }}"
253 auth_password: "{{ admin_password }}"
254 name: "ScopePermission"
255 client_id: "{{ client_id }}"
256 realm: "{{ realm }}"
257 register: result
258
259 - name: Assert that queried state matches desired end state
260 assert:
261 that:
262 - result.queried_state.name == "ScopePermission"
263 - result.queried_state.description == "Scope permission changed"
264
265 - name: Update scope permission (test for idempotency)
266 community.general.keycloak_authz_permission:
267 auth_keycloak_url: "{{ url }}"
268 auth_realm: "{{ admin_realm }}"
269 auth_username: "{{ admin_user }}"
270 auth_password: "{{ admin_password }}"
271 state: present
272 name: "ScopePermission"
273 description: "Scope permission changed"
274 permission_type: scope
275 decision_strategy: 'AFFIRMATIVE'
276 scopes:
277 - "file:create"
278 - "file:delete"
279 policies: []
280 client_id: "{{ client_id }}"
281 realm: "{{ realm }}"
282 register: result
283
284 - name: Assert that nothing changed
285 assert:
286 that:
287 - result.changed == True
288 - result.end_state != {}
289 - result.end_state.scopes|length == 2
290 - result.end_state.policies == []
291 - result.end_state.resources == []
292 - result.end_state.name == "ScopePermission"
293 - result.end_state.description == "Scope permission changed"
294
295 - name: Query state
296 community.general.keycloak_authz_permission_info:
297 auth_keycloak_url: "{{ url }}"
298 auth_realm: "{{ admin_realm }}"
299 auth_username: "{{ admin_user }}"
300 auth_password: "{{ admin_password }}"
301 name: "ScopePermission"
302 client_id: "{{ client_id }}"
303 realm: "{{ realm }}"
304 register: result
305
306 - name: Assert that queried state matches desired end state
307 assert:
308 that:
309 - result.queried_state.name == "ScopePermission"
310 - result.queried_state.description == "Scope permission changed"
311
312 - name: Remove scope permission
313 community.general.keycloak_authz_permission:
314 auth_keycloak_url: "{{ url }}"
315 auth_realm: "{{ admin_realm }}"
316 auth_username: "{{ admin_user }}"
317 auth_password: "{{ admin_password }}"
318 state: absent
319 name: "ScopePermission"
320 permission_type: scope
321 client_id: "{{ client_id }}"
322 realm: "{{ realm }}"
323 register: result
324
325 - name: Assert that scope permission was removed
326 assert:
327 that:
328 - result is changed
329 - result.end_state == {}
330
331 - name: Remove scope permission (test for idempotency)
332 community.general.keycloak_authz_permission:
333 auth_keycloak_url: "{{ url }}"
334 auth_realm: "{{ admin_realm }}"
335 auth_username: "{{ admin_user }}"
336 auth_password: "{{ admin_password }}"
337 state: absent
338 name: "ScopePermission"
339 permission_type: scope
340 client_id: "{{ client_id }}"
341 realm: "{{ realm }}"
342 register: result
343
344 - name: Assert that nothing has changed
345 assert:
346 that:
347 - result is not changed
348 - result.end_state == {}
349
350 - name: Create resource permission without resources (test for failure)
351 community.general.keycloak_authz_permission:
352 auth_keycloak_url: "{{ url }}"
353 auth_realm: "{{ admin_realm }}"
354 auth_username: "{{ admin_user }}"
355 auth_password: "{{ admin_password }}"
356 state: present
357 name: "ResourcePermission"
358 description: "Resource permission"
359 permission_type: resource
360 policies:
361 - "Default Policy"
362 client_id: "{{ client_id }}"
363 realm: "{{ realm }}"
364 register: result
365 failed_when: result.msg.find('A resource need to defined when permission type is set to resource!') == -1
366
367 - name: Create resource permission with scopes (test for failure)
368 community.general.keycloak_authz_permission:
369 auth_keycloak_url: "{{ url }}"
370 auth_realm: "{{ admin_realm }}"
371 auth_username: "{{ admin_user }}"
372 auth_password: "{{ admin_password }}"
373 state: present
374 name: "ResourcePermission"
375 description: "Resource permission"
376 permission_type: resource
377 resources:
378 - "Default Resource"
379 policies:
380 - "Default Policy"
381 scopes:
382 - "file:delete"
383 client_id: "{{ client_id }}"
384 realm: "{{ realm }}"
385 register: result
386 failed_when: result.msg.find('Scopes cannot be defined when permission type is set to resource!') == -1
387
388 - name: Create resource permission
389 community.general.keycloak_authz_permission:
390 auth_keycloak_url: "{{ url }}"
391 auth_realm: "{{ admin_realm }}"
392 auth_username: "{{ admin_user }}"
393 auth_password: "{{ admin_password }}"
394 state: present
395 name: "ResourcePermission"
396 description: "Resource permission"
397 resources:
398 - "Default Resource"
399 permission_type: resource
400 policies:
401 - "Default Policy"
402 client_id: "{{ client_id }}"
403 realm: "{{ realm }}"
404 register: result
405
406 - name: Assert that resource permission was created
407 assert:
408 that:
409 - result is changed
410 - result.end_state != {}
411 - result.end_state.policies|length == 1
412 - result.end_state.resources|length == 1
413 - result.end_state.name == "ResourcePermission"
414 - result.end_state.description == "Resource permission"
415
416 - name: Query state
417 community.general.keycloak_authz_permission_info:
418 auth_keycloak_url: "{{ url }}"
419 auth_realm: "{{ admin_realm }}"
420 auth_username: "{{ admin_user }}"
421 auth_password: "{{ admin_password }}"
422 name: "ResourcePermission"
423 client_id: "{{ client_id }}"
424 realm: "{{ realm }}"
425 register: result
426
427 - name: Assert that queried state matches desired end state
428 assert:
429 that:
430 - result.queried_state.name == "ResourcePermission"
431 - result.queried_state.description == "Resource permission"
432
433 - name: Create resource permission (test for idempotency)
434 community.general.keycloak_authz_permission:
435 auth_keycloak_url: "{{ url }}"
436 auth_realm: "{{ admin_realm }}"
437 auth_username: "{{ admin_user }}"
438 auth_password: "{{ admin_password }}"
439 state: present
440 name: "ResourcePermission"
441 description: "Resource permission"
442 resources:
443 - "Default Resource"
444 permission_type: resource
445 policies:
446 - "Default Policy"
447 client_id: "{{ client_id }}"
448 realm: "{{ realm }}"
449 register: result
450
451 - name: Assert that nothing has changed
452 assert:
453 that:
454 - result.end_state != {}
455 - result.end_state.policies|length == 1
456 - result.end_state.resources|length == 1
457 - result.end_state.name == "ResourcePermission"
458 - result.end_state.description == "Resource permission"
459
460 - name: Query state
461 community.general.keycloak_authz_permission_info:
462 auth_keycloak_url: "{{ url }}"
463 auth_realm: "{{ admin_realm }}"
464 auth_username: "{{ admin_user }}"
465 auth_password: "{{ admin_password }}"
466 name: "ResourcePermission"
467 client_id: "{{ client_id }}"
468 realm: "{{ realm }}"
469 register: result
470
471 - name: Assert that queried state matches desired end state
472 assert:
473 that:
474 - result.queried_state.name == "ResourcePermission"
475 - result.queried_state.description == "Resource permission"
476
477 - name: Update resource permission
478 community.general.keycloak_authz_permission:
479 auth_keycloak_url: "{{ url }}"
480 auth_realm: "{{ admin_realm }}"
481 auth_username: "{{ admin_user }}"
482 auth_password: "{{ admin_password }}"
483 state: present
484 name: "ResourcePermission"
485 description: "Resource permission changed"
486 resources:
487 - "Default Resource"
488 permission_type: resource
489 policies: []
490 client_id: "{{ client_id }}"
491 realm: "{{ realm }}"
492 register: result
493
494 - name: Assert that resource permission was updated correctly
495 assert:
496 that:
497 - result.changed == True
498 - result.end_state != {}
499 - result.end_state.policies == []
500 - result.end_state.resources|length == 1
501 - result.end_state.name == "ResourcePermission"
502 - result.end_state.description == "Resource permission changed"
503
504 - name: Query state
505 community.general.keycloak_authz_permission_info:
506 auth_keycloak_url: "{{ url }}"
507 auth_realm: "{{ admin_realm }}"
508 auth_username: "{{ admin_user }}"
509 auth_password: "{{ admin_password }}"
510 name: "ResourcePermission"
511 client_id: "{{ client_id }}"
512 realm: "{{ realm }}"
513 register: result
514
515 - name: Assert that queried state matches desired end state
516 assert:
517 that:
518 - result.queried_state.name == "ResourcePermission"
519 - result.queried_state.description == "Resource permission changed"
520
521 - name: Remove resource permission
522 community.general.keycloak_authz_permission:
523 auth_keycloak_url: "{{ url }}"
524 auth_realm: "{{ admin_realm }}"
525 auth_username: "{{ admin_user }}"
526 auth_password: "{{ admin_password }}"
527 state: absent
528 name: "ResourcePermission"
529 permission_type: resource
530 client_id: "{{ client_id }}"
531 realm: "{{ realm }}"
532 register: result
533
534 - name: Assert that resource permission was removed
535 assert:
536 that:
537 - result is changed
538 - result.end_state == {}
539
540 - name: Remove resource permission (test for idempotency)
541 community.general.keycloak_authz_permission:
542 auth_keycloak_url: "{{ url }}"
543 auth_realm: "{{ admin_realm }}"
544 auth_username: "{{ admin_user }}"
545 auth_password: "{{ admin_password }}"
546 state: absent
547 name: "ResourcePermission"
548 permission_type: resource
549 client_id: "{{ client_id }}"
550 realm: "{{ realm }}"
551 register: result
552
553 - name: Assert that nothing has changed
554 assert:
555 that:
556 - result is not changed
557 - result.end_state == {}
558
559 - name: Remove keycloak client
560 community.general.keycloak_client:
561 auth_keycloak_url: "{{ url }}"
562 auth_realm: "{{ admin_realm }}"
563 auth_username: "{{ admin_user }}"
564 auth_password: "{{ admin_password }}"
565 realm: "{{ realm }}"
566 client_id: "{{ client_id }}"
567 state: absent