Debian Codegraph @

ansible_collections/community/clickhouse/tests/integration/targets/clickhouse_grants/tasks/initial.yml

raw
1 ####################################################################
2 # WARNING: These are designed specifically for Ansible tests #
3 # and should not be used as examples of how to write Ansible roles #
4 ####################################################################
5
6 - name: Create a test dabase
7 community.clickhouse.clickhouse_client:
8 execute: CREATE DATABASE IF NOT EXISTS foo
9
10 - name: Create a test table
11 register: result
12 community.clickhouse.clickhouse_client:
13 login_host: localhost
14 execute: >
15 CREATE TABLE IF NOT EXISTS foo.test_table1
16 (x String) ENGINE = Memory
17 execute_kwargs:
18 types_check: false
19
20 - name: Insert into test table using named args
21 register: result
22 community.clickhouse.clickhouse_client:
23 execute: "INSERT INTO foo.test_table1 (x) VALUES (%(a)s), (%(b)s), (%(c)s)"
24 execute_kwargs:
25 params:
26 a: one
27 b: two
28 c: three
29
30 - name: Create a test table 2
31 register: result
32 community.clickhouse.clickhouse_client:
33 login_host: localhost
34 execute: >
35 CREATE TABLE IF NOT EXISTS foo.test_table2
36 (y String, z String) ENGINE = Memory
37 execute_kwargs:
38 types_check: false
39
40 - name: Insert into test table using named args
41 register: result
42 community.clickhouse.clickhouse_client:
43 execute: "INSERT INTO foo.test_table2 (y, z) VALUES (%(a)s, %(b)s)"
44 execute_kwargs:
45 params:
46 a: one
47 b: two
48
49 - name: Create a test user
50 community.clickhouse.clickhouse_user:
51 state: present
52 name: alice
53 password: querty
54
55 - name: Check the user exists
56 register: result
57 community.clickhouse.clickhouse_info:
58 login_host: localhost
59 limit:
60 - users
61
62 - name: Check user's grants, they are empty
63 ansible.builtin.assert:
64 that:
65 - result["users"]["alice"]["grants"] == []
66
67 - name: Grant privs to non-existent grantee, must fail
68 register: result
69 ignore_errors: true
70 community.clickhouse.clickhouse_grants:
71 state: present
72 grantee: notexists
73 privileges:
74 - object: '*.*'
75 privs:
76 TEST: false
77
78 - name: Check that it failed
79 ansible.builtin.assert:
80 that:
81 - result is failed
82 - result.msg is search("does not exist")
83
84 - name: Grant initial privileges to alice
85 register: result
86 community.clickhouse.clickhouse_grants:
87 state: present
88 grantee: alice
89 privileges:
90 - object: 'foo.*'
91 privs:
92 "SELECT": true
93 "INSERT": false
94 - object: '*.*'
95 privs:
96 "CREATE USER": false
97
98 - name: Check initial grant
99 ansible.builtin.assert:
100 that:
101 - result is changed
102 - result.executed_statements | length == 3
103
104 - name: Query database directly to verify grants
105 register: db_grants
106 community.clickhouse.clickhouse_client:
107 execute: "SHOW GRANTS FOR alice"
108
109 - name: Verify grants exist in database
110 ansible.builtin.assert:
111 that:
112 - db_grants.result | length > 0
113 - "'SELECT' in db_grants.result | string"
114 - "'INSERT' in db_grants.result | string"
115 - "'CREATE USER' in db_grants.result | string"
116
117 - name: Run the same grant again to check for idempotency
118 register: result
119 community.clickhouse.clickhouse_grants:
120 state: present
121 grantee: alice
122 privileges:
123 - object: 'foo.*'
124 privs:
125 "SELECT": true
126 "INSERT": false
127 - object: '*.*'
128 privs:
129 "CREATE USER": false
130
131 - name: Check idempotency
132 ansible.builtin.assert:
133 that:
134 - result is not changed
135 - result.executed_statements | length == 0
136
137 - name: Append privileges with exclusive=false (default)
138 register: result
139 community.clickhouse.clickhouse_grants:
140 state: present
141 grantee: alice
142 privileges:
143 - object: 'foo.*'
144 privs:
145 "SELECT": true
146 "INSERT": false
147 "DELETE": true
148 - object: '*.*'
149 privs:
150 "CREATE USER": false
151 "ALTER USER": true
152
153 - name: Check appended privileges
154 ansible.builtin.assert:
155 that:
156 - result is changed
157 - >
158 result.executed_statements | sort ==
159 [
160 "GRANT ALTER USER ON *.* TO 'alice' WITH GRANT OPTION",
161 "GRANT DELETE ON foo.* TO 'alice' WITH GRANT OPTION"
162 ] | sort
163
164 - name: Query database to verify appended grants
165 register: db_grants_appended
166 community.clickhouse.clickhouse_client:
167 execute: "SHOW GRANTS FOR 'alice'"
168
169 - name: Verify DELETE and ALTER USER were appended in database
170 ansible.builtin.assert:
171 that:
172 - db_grants_appended.result | length > 0
173 - "'DELETE' in db_grants_appended.result | string"
174 - "'ALTER USER' in db_grants_appended.result | string"
175 - "'SELECT' in db_grants_appended.result | string"
176 - "'INSERT' in db_grants_appended.result | string"
177
178 - name: Grant privileges with exclusive=true
179 register: result
180 community.clickhouse.clickhouse_grants:
181 state: present
182 grantee: alice
183 exclusive: true
184 privileges:
185 - object: 'foo.test_table1'
186 privs:
187 "SELECT(x)": false
188
189 - name: Check exclusive grant
190 ansible.builtin.assert:
191 that:
192 - result is changed
193 # 2 revokes and 1 grant
194 - result.executed_statements | length == 3
195
196 - name: Query database to verify exclusive grant replaced everything
197 register: db_grants_exclusive
198 community.clickhouse.clickhouse_client:
199 execute: "SHOW GRANTS FOR alice"
200
201 - name: Verify only SELECT(x) on foo.test_table1 exists in database
202 ansible.builtin.assert:
203 that:
204 - db_grants_exclusive.result | length == 1
205 - db_grants_exclusive.result[0] is sequence
206 - db_grants_exclusive.result[0][0] is string
207 - "'SELECT' in db_grants_exclusive.result[0][0]"
208 - "'foo.test_table1' in db_grants_exclusive.result[0][0]"
209 - "'DELETE' not in db_grants_exclusive.result | string"
210 - "'ALTER USER' not in db_grants_exclusive.result | string"
211
212 - name: Run exclusive grant again for idempotency
213 register: result
214 community.clickhouse.clickhouse_grants:
215 state: present
216 grantee: alice
217 exclusive: true
218 privileges:
219 - object: 'foo.test_table1'
220 privs:
221 "SELECT(x)": false
222
223 - name: Check exclusive grant idempotency
224 ansible.builtin.assert:
225 that:
226 - result is not changed
227 - result.executed_statements | length == 0
228
229 - name: Revoke all from alice for cleanup
230 community.clickhouse.clickhouse_client:
231 execute: "REVOKE ALL ON *.* FROM alice"
232
233 - name: Grant privileges in check mode
234 register: result
235 check_mode: true
236 community.clickhouse.clickhouse_grants:
237 state: present
238 grantee: alice
239 privileges:
240 - object: 'foo.*'
241 privs:
242 "SELECT": false
243
244 - name: Check grant in check mode
245 ansible.builtin.assert:
246 that:
247 - result is changed
248 - result.executed_statements | length == 1
249 - result.executed_statements[0] == "GRANT SELECT ON foo.* TO 'alice'"
250
251 - name: Get user info to verify no changes were made
252 register: result_info
253 community.clickhouse.clickhouse_info:
254 login_host: localhost
255 limit:
256 - users
257
258 - name: Assert no grants for alice after check mode run
259 ansible.builtin.assert:
260 that:
261 - result_info["users"]["alice"]["grants"] == []
262
263 # Test appending in check mode with existing grants
264 - name: Grant some initial privileges to alice for check mode append test
265 community.clickhouse.clickhouse_grants:
266 state: present
267 grantee: alice
268 privileges:
269 - object: 'foo.*'
270 privs:
271 "SELECT": true
272 "INSERT": false
273
274 - name: Append privileges with exclusive=false in check mode
275 register: result
276 check_mode: true
277 community.clickhouse.clickhouse_grants:
278 state: present
279 grantee: alice
280 exclusive: false
281 privileges:
282 - object: 'foo.*'
283 privs:
284 "SELECT": true
285 "INSERT": false
286 "ALTER DELETE": true
287
288 - name: Check append in check mode
289 ansible.builtin.assert:
290 that:
291 - result is changed
292 - result.executed_statements | length == 1
293 - result.executed_statements[0] == "GRANT ALTER DELETE ON foo.* TO 'alice' WITH GRANT OPTION"
294
295 - name: Get user info to verify check mode didn't actually change grants
296 register: result_info
297 community.clickhouse.clickhouse_info:
298 login_host: localhost
299 limit:
300 - users
301
302 - name: Assert alice still has only the initial grants (ALTER DELETE not added)
303 ansible.builtin.assert:
304 that:
305 - result_info["users"]["alice"]["grants"] | length == 2
306 - "'ALTER DELETE' not in result_info['users']['alice']['grants'] | join(' ')"
307
308 - name: Run the append again in check mode (idempotency test)
309 register: result
310 check_mode: true
311 community.clickhouse.clickhouse_grants:
312 state: present
313 grantee: alice
314 exclusive: false
315 privileges:
316 - object: 'foo.*'
317 privs:
318 "SELECT": true
319 "INSERT": false
320 "ALTER DELETE": true
321
322 - name: Check append idempotency in check mode
323 ansible.builtin.assert:
324 that:
325 - result is changed
326 - result.executed_statements | length == 1
327
328 # Test exclusive mode in check mode
329 - name: Replace all privileges with exclusive=true in check mode
330 register: result
331 check_mode: true
332 community.clickhouse.clickhouse_grants:
333 state: present
334 grantee: alice
335 exclusive: true
336 privileges:
337 - object: 'foo.test_table2'
338 privs:
339 "SELECT(y)": false
340
341 - name: Check exclusive mode in check mode
342 ansible.builtin.assert:
343 that:
344 - result is changed
345 # Should revoke SELECT and INSERT on foo.*, then grant SELECT(y) on foo.test_table2
346 - result.executed_statements | length == 2
347
348 - name: Get user info to verify check mode didn't actually change grants
349 register: result_info
350 community.clickhouse.clickhouse_info:
351 login_host: localhost
352 limit:
353 - users
354
355 - name: Assert alice still has the original grants (not replaced)
356 ansible.builtin.assert:
357 that:
358 - result_info["users"]["alice"]["grants"] | length == 2
359 - "'foo.*' in result_info['users']['alice']['grants'] | join(' ')"
360
361 # Test actual append (not check mode) to verify it works after check mode
362 - name: Actually append ALTER DELETE privilege
363 register: result
364 community.clickhouse.clickhouse_grants:
365 state: present
366 grantee: alice
367 exclusive: false
368 privileges:
369 - object: 'foo.*'
370 privs:
371 "SELECT": true
372 "INSERT": false
373 "ALTER DELETE": true
374
375 - name: Check actual append worked
376 ansible.builtin.assert:
377 that:
378 - result is changed
379 - result.executed_statements | length == 1
380 - result.executed_statements[0] == "GRANT ALTER DELETE ON foo.* TO 'alice' WITH GRANT OPTION"
381
382 - name: Run append again for idempotency check
383 register: result
384 community.clickhouse.clickhouse_grants:
385 state: present
386 grantee: alice
387 exclusive: false
388 privileges:
389 - object: 'foo.*'
390 privs:
391 "SELECT": true
392 "INSERT": false
393 "ALTER DELETE": true
394
395 - name: Check append idempotency
396 ansible.builtin.assert:
397 that:
398 - result is not changed
399 - result.executed_statements | length == 0
400
401 # Test actual exclusive mode (not check mode)
402 - name: Replace all privileges with exclusive=true
403 register: result
404 community.clickhouse.clickhouse_grants:
405 state: present
406 grantee: alice
407 exclusive: true
408 privileges:
409 - object: 'foo.test_table2'
410 privs:
411 "SELECT(z)": true
412
413 - name: Check exclusive mode worked
414 ansible.builtin.assert:
415 that:
416 - result is changed
417 # Should revoke all existing privileges and grant the new one
418 - result.executed_statements | length == 2
419
420 - name: Run exclusive grant again for idempotency check
421 register: result
422 community.clickhouse.clickhouse_grants:
423 state: present
424 grantee: alice
425 exclusive: true
426 privileges:
427 - object: 'foo.test_table2'
428 privs:
429 "SELECT(z)": true
430
431 - name: Check exclusive mode idempotency
432 ansible.builtin.assert:
433 that:
434 - result is not changed
435 - result.executed_statements | length == 0
436
437 # Test revoking all privileges with state=absent
438 - name: Grant some privileges to alice for revoke test
439 community.clickhouse.clickhouse_grants:
440 state: present
441 grantee: alice
442 privileges:
443 - object: 'foo.*'
444 privs:
445 "SELECT": true
446 "INSERT": false
447 - object: '*.*'
448 privs:
449 "CREATE USER": false
450
451 - name: Verify alice has grants
452 register: result_info
453 community.clickhouse.clickhouse_info:
454 login_host: localhost
455 limit:
456 - users
457
458 - name: Assert alice has grants before revoke
459 ansible.builtin.assert:
460 that:
461 - result_info["users"]["alice"]["grants"] | length > 0
462
463 - name: Revoke all privileges with state=absent
464 register: result
465 community.clickhouse.clickhouse_grants:
466 state: absent
467 grantee: alice
468
469 - name: Check revoke worked
470 ansible.builtin.assert:
471 that:
472 - result is changed
473 - result.executed_statements | length > 0
474
475 - name: Verify alice has no grants after revoke
476 register: result_info
477 community.clickhouse.clickhouse_info:
478 login_host: localhost
479 limit:
480 - users
481
482 - name: Assert alice has no grants after revoke
483 ansible.builtin.assert:
484 that:
485 - result_info["users"]["alice"]["grants"] == []
486
487 - name: Query database to confirm no grants exist
488 register: db_grants_after_revoke
489 community.clickhouse.clickhouse_client:
490 execute: "SHOW GRANTS FOR alice"
491
492 - name: Verify database shows no grants for alice
493 ansible.builtin.assert:
494 that:
495 - db_grants_after_revoke.result | length == 0
496
497 - name: Run revoke again for idempotency test
498 register: result
499 community.clickhouse.clickhouse_grants:
500 state: absent
501 grantee: alice
502
503 - name: Check revoke idempotency
504 ansible.builtin.assert:
505 that:
506 - result is not changed
507 - result.executed_statements | length == 0
508
509 # Test revoking in check mode
510 - name: Grant some privileges to alice for check mode revoke test
511 community.clickhouse.clickhouse_grants:
512 state: present
513 grantee: alice
514 privileges:
515 - object: 'foo.*'
516 privs:
517 "SELECT": false
518 "INSERT": true
519
520 - name: Revoke all privileges with state=absent in check mode
521 register: result
522 check_mode: true
523 community.clickhouse.clickhouse_grants:
524 state: absent
525 grantee: alice
526
527 - name: Check revoke in check mode
528 ansible.builtin.assert:
529 that:
530 - result is changed
531 - result.executed_statements | length > 0
532
533 - name: Verify alice still has grants after check mode revoke
534 register: result_info
535 community.clickhouse.clickhouse_info:
536 login_host: localhost
537 limit:
538 - users
539
540 - name: Assert alice still has grants (check mode didn't actually revoke)
541 ansible.builtin.assert:
542 that:
543 - result_info["users"]["alice"]["grants"] | length > 0
544
545 - name: Run revoke in check mode again for idempotency test
546 register: result
547 check_mode: true
548 community.clickhouse.clickhouse_grants:
549 state: absent
550 grantee: alice
551
552 - name: Check revoke idempotency in check mode
553 ansible.builtin.assert:
554 that:
555 - result is changed
556 - result.executed_statements | length > 0
557
558 # Test actual revoke after check mode to clean up
559 - name: Actually revoke all privileges from alice
560 register: result
561 community.clickhouse.clickhouse_grants:
562 state: absent
563 grantee: alice
564
565 - name: Check actual revoke worked
566 ansible.builtin.assert:
567 that:
568 - result is changed
569 - result.executed_statements | length > 0
570
571 - name: Verify alice has no grants after actual revoke
572 register: result_info
573 community.clickhouse.clickhouse_info:
574 login_host: localhost
575 limit:
576 - users
577
578 - name: Assert alice has no grants
579 ansible.builtin.assert:
580 that:
581 - result_info["users"]["alice"]["grants"] == []
582
583 # Test grant_option parameter (overrides individual privilege settings)
584 - name: Grant privileges using grant_option parameter
585 register: result
586 community.clickhouse.clickhouse_grants:
587 state: present
588 grantee: alice
589 privileges:
590 - object: 'foo.*'
591 grant_option: true
592 privs:
593 "SELECT": false # Should be overridden to true
594 "INSERT": false # Should be overridden to true
595
596 - name: Check grant_option parameter worked
597 ansible.builtin.assert:
598 that:
599 - result is changed
600 - result.executed_statements | length == 1
601 - "'WITH GRANT OPTION' in result.executed_statements[0]"
602
603 - name: Verify grants were applied with grant option
604 register: result_info
605 community.clickhouse.clickhouse_info:
606 login_host: localhost
607 limit:
608 - users
609
610 - name: Assert alice has grants with grant option
611 ansible.builtin.assert:
612 that:
613 - result_info["users"]["alice"]["grants"] | length > 0
614 - "'WITH GRANT OPTION' in result_info['users']['alice']['grants'] | join(' ')"
615
616 - name: Query database to verify WITH GRANT OPTION
617 register: db_grants_with_option
618 community.clickhouse.clickhouse_client:
619 execute: "SHOW GRANTS FOR alice"
620
621 - name: Verify WITH GRANT OPTION is in database
622 ansible.builtin.assert:
623 that:
624 - db_grants_with_option.result | length > 0
625 - "'WITH GRANT OPTION' in db_grants_with_option.result | string"
626 - "'SELECT' in db_grants_with_option.result | string"
627 - "'INSERT' in db_grants_with_option.result | string"
628
629 - name: Run same grant again for idempotency
630 register: result
631 community.clickhouse.clickhouse_grants:
632 state: present
633 grantee: alice
634 privileges:
635 - object: 'foo.*'
636 grant_option: true
637 privs:
638 "SELECT": false
639 "INSERT": false
640
641 - name: Check grant_option idempotency
642 ansible.builtin.assert:
643 that:
644 - result is not changed
645 - result.executed_statements | length == 0
646
647 # Test grant_option in check mode
648 - name: Revoke all for clean state
649 community.clickhouse.clickhouse_grants:
650 state: absent
651 grantee: alice
652
653 - name: Grant some privileges without grant option
654 community.clickhouse.clickhouse_grants:
655 state: present
656 grantee: alice
657 privileges:
658 - object: 'foo.*'
659 privs:
660 "SELECT": false
661 "INSERT": false
662
663 - name: Get current state before check mode
664 register: before_check
665 community.clickhouse.clickhouse_info:
666 login_host: localhost
667 limit:
668 - users
669
670 - name: Update grants using grant_option in check mode
671 register: result
672 check_mode: true
673 community.clickhouse.clickhouse_grants:
674 state: present
675 grantee: alice
676 exclusive: true
677 privileges:
678 - object: 'foo.*'
679 grant_option: true
680 privs:
681 "SELECT": false
682 "INSERT": false
683
684 - name: Check that changes were detected in check mode
685 ansible.builtin.assert:
686 that:
687 - result is changed
688 - result.executed_statements | length > 0
689
690 - name: Get current state after check mode
691 register: after_check
692 community.clickhouse.clickhouse_info:
693 login_host: localhost
694 limit:
695 - users
696
697 - name: Assert grants are UNCHANGED after check mode (no WITH GRANT OPTION added)
698 ansible.builtin.assert:
699 that:
700 - before_check["users"]["alice"]["grants"] == after_check["users"]["alice"]["grants"]
701 - "'WITH GRANT OPTION' not in after_check['users']['alice']['grants'] | join(' ')"
702
703 # Test grant_option=false explicitly
704 - name: Revoke all for clean state
705 community.clickhouse.clickhouse_grants:
706 state: absent
707 grantee: alice
708
709 - name: Grant privileges with explicit grant_option=false
710 register: result
711 community.clickhouse.clickhouse_grants:
712 state: present
713 grantee: alice
714 privileges:
715 - object: 'foo.*'
716 grant_option: false
717 privs:
718 "SELECT": true # Should be overridden to false
719 "INSERT": true # Should be overridden to false
720
721 - name: Check grant_option=false worked
722 ansible.builtin.assert:
723 that:
724 - result is changed
725
726 - name: Verify grants were applied without grant option
727 register: result_info
728 community.clickhouse.clickhouse_info:
729 login_host: localhost
730 limit:
731 - users
732
733 - name: Assert alice has grants without grant option
734 ansible.builtin.assert:
735 that:
736 - result_info["users"]["alice"]["grants"] | length > 0
737 - "'WITH GRANT OPTION' not in result_info['users']['alice']['grants'] | join(' ')"
738
739 # Test granting privileges to user with dashes in name
740 - name: Create a test user with dashes in name
741 community.clickhouse.clickhouse_user:
742 state: present
743 name: alice-with-dash
744 password: querty
745
746 - name: Check the user exists
747 register: result
748 community.clickhouse.clickhouse_info:
749 login_host: localhost
750 limit:
751 - users
752
753 - name: Check user's grants, they are empty
754 ansible.builtin.assert:
755 that:
756 - result["users"]["alice-with-dash"]["grants"] == []
757
758 - name: Grant initial privileges to alice-with-dash
759 register: result
760 community.clickhouse.clickhouse_grants:
761 state: present
762 grantee: alice-with-dash
763 privileges:
764 - object: 'foo.*'
765 privs:
766 "SELECT": true
767 "INSERT": false
768 - object: '*.*'
769 privs:
770 "CREATE USER": false
771
772 - name: Check initial grant for alice-with-dash
773 ansible.builtin.assert:
774 that:
775 - result is changed
776 - result.executed_statements | length == 3
777
778 - name: Query database directly to verify grants
779 register: db_grants
780 community.clickhouse.clickhouse_client:
781 execute: "SHOW GRANTS FOR 'alice-with-dash'"
782
783 - name: Verify grants exist in database
784 ansible.builtin.assert:
785 that:
786 - db_grants.result | length > 0
787 - "'SELECT' in db_grants.result | string"
788 - "'INSERT' in db_grants.result | string"
789 - "'CREATE USER' in db_grants.result | string"
790
791 # Cleanup user with dashes
792 - name: Drop alice-with-dash user
793 community.clickhouse.clickhouse_client:
794 execute: "DROP USER IF EXISTS 'alice-with-dash'"
795
796 # Test that check mode NEVER modifies actual state
797 - name: Revoke all for clean state
798 community.clickhouse.clickhouse_grants:
799 state: absent
800 grantee: alice
801
802 - name: Get empty state
803 register: empty_state
804 community.clickhouse.clickhouse_info:
805 login_host: localhost
806 limit:
807 - users
808
809 - name: Assert alice has no grants initially
810 ansible.builtin.assert:
811 that:
812 - empty_state["users"]["alice"]["grants"] == []
813
814 - name: Try to grant in check mode
815 register: result
816 check_mode: true
817 community.clickhouse.clickhouse_grants:
818 state: present
819 grantee: alice
820 privileges:
821 - object: 'foo.*'
822 privs:
823 "SELECT": true
824
825 - name: Verify check mode reported change
826 ansible.builtin.assert:
827 that:
828 - result is changed
829
830 - name: Get state after check mode grant
831 register: after_check_grant
832 community.clickhouse.clickhouse_info:
833 login_host: localhost
834 limit:
835 - users
836
837 - name: Assert NOTHING was actually granted in check mode
838 ansible.builtin.assert:
839 that:
840 - after_check_grant["users"]["alice"]["grants"] == []
841 - empty_state["users"]["alice"]["grants"] == after_check_grant["users"]["alice"]["grants"]
842
843 ##############################################################################
844 # Test diff functionality
845 ##############################################################################
846
847 - name: Clean up alice for diff tests
848 community.clickhouse.clickhouse_grants:
849 state: absent
850 grantee: alice
851
852 # Test 1: diff in check_mode when granting new privileges
853 - name: Grant privileges in check mode (should return diff)
854 register: result_check_diff
855 check_mode: true
856 community.clickhouse.clickhouse_grants:
857 state: present
858 grantee: alice
859 privileges:
860 - object: 'foo.*'
861 privs:
862 "SELECT": true
863 "INSERT": false
864
865 - name: Verify diff is returned in check_mode for new grants
866 ansible.builtin.assert:
867 that:
868 - result_check_diff is changed
869 - result_check_diff.diff is defined
870 - result_check_diff.diff.before is defined
871 - result_check_diff.diff.after is defined
872 - result_check_diff.diff.before == {}
873 - result_check_diff.diff.after.keys() | list == ["foo.*"]
874 - result_check_diff.diff.after["foo.*"]["SELECT"] == true
875 - result_check_diff.diff.after["foo.*"]["INSERT"] == false
876
877 # Test 2: Grant actual privileges, then test diff with append
878 - name: Revoke all first for clean state
879 community.clickhouse.clickhouse_grants:
880 state: absent
881 grantee: alice
882
883 - name: Grant initial privileges to alice
884 community.clickhouse.clickhouse_grants:
885 state: present
886 grantee: alice
887 privileges:
888 - object: 'bar.*'
889 privs:
890 "SELECT": true
891
892 - name: Append privileges in check mode (should show diff)
893 register: result_append_diff
894 check_mode: true
895 community.clickhouse.clickhouse_grants:
896 state: present
897 grantee: alice
898 exclusive: false
899 privileges:
900 - object: 'bar.*'
901 privs:
902 "SELECT": true
903 "INSERT": false
904
905 - name: Verify diff is present for append
906 ansible.builtin.assert:
907 that:
908 - result_append_diff.diff is defined
909 - result_append_diff.diff.before is defined
910 - result_append_diff.diff.after is defined
911
912 # Test 3: Test diff with exclusive mode
913 - name: Grant privileges with exclusive mode in check mode
914 register: result_exclusive_diff
915 check_mode: true
916 community.clickhouse.clickhouse_grants:
917 state: present
918 grantee: alice
919 exclusive: true
920 privileges:
921 - object: 'baz.*'
922 privs:
923 "DELETE": false
924
925 - name: Verify diff is present for exclusive mode
926 ansible.builtin.assert:
927 that:
928 - result_exclusive_diff is changed
929 - result_exclusive_diff.diff is defined
930 - result_exclusive_diff.diff.before is defined
931 - result_exclusive_diff.diff.after is defined
932
933 # Test 4: Test diff with revoke (state: absent)
934 - name: Revoke all privileges in check mode
935 register: result_revoke_diff
936 check_mode: true
937 community.clickhouse.clickhouse_grants:
938 state: absent
939 grantee: alice
940
941 - name: Verify diff shows grants being removed
942 ansible.builtin.assert:
943 that:
944 - result_revoke_diff is changed
945 - result_revoke_diff.diff is defined
946 - result_revoke_diff.diff.before != {}
947 - result_revoke_diff.diff.after == {}
948
949 # Test 5: Test diff when no changes are made (idempotency)
950 - name: Actually grant privileges (not check mode)
951 community.clickhouse.clickhouse_grants:
952 state: present
953 grantee: alice
954 privileges:
955 - object: 'qux.*'
956 privs:
957 "SELECT": true
958
959 - name: Try to grant same privileges again in check mode
960 register: result_nochange_diff
961 check_mode: true
962 community.clickhouse.clickhouse_grants:
963 state: present
964 grantee: alice
965 privileges:
966 - object: 'qux.*'
967 privs:
968 "SELECT": true
969
970 - name: Verify diff shows no changes when idempotent
971 ansible.builtin.assert:
972 that:
973 - result_nochange_diff is not changed
974 - result_nochange_diff.diff is defined
975 - result_nochange_diff.diff.before == result_nochange_diff.diff.after
976
977 # Test 6: Test diff with multiple objects
978 - name: Clean and grant privileges on multiple objects
979 community.clickhouse.clickhouse_grants:
980 state: present
981 grantee: alice
982 exclusive: true
983 privileges:
984 - object: 'db1.*'
985 privs:
986 "SELECT": true
987 - object: 'db2.*'
988 privs:
989 "INSERT": false
990
991 - name: Add more privileges in check mode
992 register: result_multi_diff
993 check_mode: true
994 community.clickhouse.clickhouse_grants:
995 state: present
996 grantee: alice
997 exclusive: false
998 privileges:
999 - object: 'db1.*'
1000 privs:
1001 "SELECT": true
1002 "DELETE": true
1003 - object: 'db3.*'
1004 privs:
1005 "ALTER": false
1006
1007 - name: Verify diff is present for multiple objects
1008 ansible.builtin.assert:
1009 that:
1010 - result_multi_diff is changed
1011 - result_multi_diff.diff is defined
1012 - result_multi_diff.diff.before is defined
1013 - result_multi_diff.diff.after is defined
1014
1015 # Cleanup
1016 - name: Final cleanup - revoke all grants from alice
1017 community.clickhouse.clickhouse_grants:
1018 state: absent
1019 grantee: alice
1020
1021 ##############################################################################
1022 # Test granting privileges to ROLES (not just users)
1023 ##############################################################################
1024
1025 - name: Create a test role
1026 community.clickhouse.clickhouse_client:
1027 execute: "CREATE ROLE IF NOT EXISTS test_role"
1028
1029 - name: Verify role exists in database
1030 register: db_roles
1031 community.clickhouse.clickhouse_client:
1032 execute: "SELECT name FROM system.roles WHERE name = 'test_role'"
1033
1034 - name: Assert role was created
1035 ansible.builtin.assert:
1036 that:
1037 - db_roles.result | length == 1
1038 - db_roles.result[0][0] == 'test_role'
1039
1040 # Test 1: Grant privileges to a role
1041 - name: Grant privileges to test_role
1042 register: result
1043 community.clickhouse.clickhouse_grants:
1044 state: present
1045 grantee: test_role
1046 privileges:
1047 - object: 'foo.*'
1048 privs:
1049 "SELECT": true
1050 "INSERT": false
1051 - object: '*.*'
1052 privs:
1053 "CREATE TABLE": false
1054
1055 - name: Check grant to role worked
1056 ansible.builtin.assert:
1057 that:
1058 - result is changed
1059 - result.executed_statements | length > 0
1060
1061 - name: Query database to verify role grants
1062 register: db_role_grants
1063 community.clickhouse.clickhouse_client:
1064 execute: "SHOW GRANTS FOR test_role"
1065
1066 - name: Verify grants exist for role in database
1067 ansible.builtin.assert:
1068 that:
1069 - db_role_grants.result | length > 0
1070 - "'SELECT' in db_role_grants.result | string"
1071 - "'INSERT' in db_role_grants.result | string"
1072 - "'CREATE TABLE' in db_role_grants.result | string"
1073
1074 # Test 2: Append privileges to role
1075 - name: Append more privileges to test_role
1076 register: result
1077 community.clickhouse.clickhouse_grants:
1078 state: present
1079 grantee: test_role
1080 exclusive: false
1081 privileges:
1082 - object: 'foo.*'
1083 privs:
1084 "DELETE": true
1085
1086 - name: Check append to role worked
1087 ansible.builtin.assert:
1088 that:
1089 - result is changed
1090 - "'DELETE' in result.executed_statements | string"
1091
1092 - name: Query database to verify appended role grants
1093 register: db_role_grants_appended
1094 community.clickhouse.clickhouse_client:
1095 execute: "SHOW GRANTS FOR test_role"
1096
1097 - name: Verify DELETE was appended to role
1098 ansible.builtin.assert:
1099 that:
1100 - db_role_grants_appended.result | length > 0
1101 - "'DELETE' in db_role_grants_appended.result | string"
1102 - "'SELECT' in db_role_grants_appended.result | string"
1103
1104 # Test 3: Test exclusive mode with role
1105 - name: Replace all role privileges with exclusive=true
1106 register: result
1107 community.clickhouse.clickhouse_grants:
1108 state: present
1109 grantee: test_role
1110 exclusive: true
1111 privileges:
1112 - object: 'bar.*'
1113 privs:
1114 "ALTER": false
1115
1116 - name: Check exclusive grant to role worked
1117 ansible.builtin.assert:
1118 that:
1119 - result is changed
1120
1121 - name: Query database to verify exclusive grant replaced role privileges
1122 register: db_role_grants_exclusive
1123 community.clickhouse.clickhouse_client:
1124 execute: "SHOW GRANTS FOR test_role"
1125
1126 - name: Verify only ALTER on bar.* exists for role
1127 ansible.builtin.assert:
1128 that:
1129 - "'ALTER' in db_role_grants_exclusive.result | string"
1130 - "'bar.*' in db_role_grants_exclusive.result | string"
1131 - "'foo.*' not in db_role_grants_exclusive.result | string"
1132
1133 # Test 4: Revoke all privileges from role
1134 - name: Revoke all privileges from test_role
1135 register: result
1136 community.clickhouse.clickhouse_grants:
1137 state: absent
1138 grantee: test_role
1139
1140 - name: Check revoke from role worked
1141 ansible.builtin.assert:
1142 that:
1143 - result is changed
1144
1145 - name: Query database to verify role has no grants
1146 register: db_role_grants_after_revoke
1147 community.clickhouse.clickhouse_client:
1148 execute: "SHOW GRANTS FOR test_role"
1149
1150 - name: Verify role has no grants
1151 ansible.builtin.assert:
1152 that:
1153 - db_role_grants_after_revoke.result | length == 0
1154
1155 # Test 5: Test role with check_mode
1156 - name: Grant privileges to role in check mode
1157 register: result
1158 check_mode: true
1159 community.clickhouse.clickhouse_grants:
1160 state: present
1161 grantee: test_role
1162 privileges:
1163 - object: 'foo.*'
1164 privs:
1165 "SELECT": false
1166
1167 - name: Verify check mode reported change for role
1168 ansible.builtin.assert:
1169 that:
1170 - result is changed
1171 - result.diff is defined
1172 - result.diff.before == {}
1173 - result.diff.after["foo.*"]["SELECT"] == false
1174
1175 - name: Query database to verify check mode didn't grant to role
1176 register: db_role_grants_check
1177 community.clickhouse.clickhouse_client:
1178 execute: "SHOW GRANTS FOR test_role"
1179
1180 - name: Verify role still has no grants after check mode
1181 ansible.builtin.assert:
1182 that:
1183 - db_role_grants_check.result | length == 0
1184
1185 # Test 6: Grant role to user and verify privilege inheritance
1186 - name: Actually grant privileges to role (not check mode)
1187 community.clickhouse.clickhouse_grants:
1188 state: present
1189 grantee: test_role
1190 privileges:
1191 - object: 'foo.*'
1192 privs:
1193 "SELECT": true
1194 "INSERT": true
1195
1196 - name: Grant test_role to alice
1197 community.clickhouse.clickhouse_client:
1198 execute: "GRANT test_role TO 'alice'"
1199
1200 - name: Query alice's grants (should show role assignment)
1201 register: db_alice_with_role
1202 community.clickhouse.clickhouse_client:
1203 execute: "SHOW GRANTS FOR 'alice'"
1204
1205 - name: Verify alice has the role granted
1206 ansible.builtin.assert:
1207 that:
1208 - "'test_role' in db_alice_with_role.result | string"
1209
1210 - name: Query role's grants
1211 register: db_role_final_grants
1212 community.clickhouse.clickhouse_client:
1213 execute: "SHOW GRANTS FOR test_role"
1214
1215 - name: Verify role has its privileges
1216 ansible.builtin.assert:
1217 that:
1218 - db_role_final_grants.result | length > 0
1219 - "'SELECT' in db_role_final_grants.result | string"
1220 - "'INSERT' in db_role_final_grants.result | string"
1221
1222 # Cleanup roles
1223 - name: Revoke role from alice
1224 community.clickhouse.clickhouse_client:
1225 execute: "REVOKE test_role FROM alice"
1226
1227 - name: Drop test role
1228 community.clickhouse.clickhouse_client:
1229 execute: "DROP ROLE IF EXISTS test_role"