Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/secretsmanager_secret/tasks/basic.yml

raw
1 ---
2 ############################################################
3 # This Integration test is currently very minimal
4 # It would be good if someone would update the tests to run through the standard 4-step checks for
5 # all of the module parameters
6 #
7 # 1) Set Value (Check mode)
8 # - changed is True
9 # - object *NOT* updated
10 # 2) Set Value
11 # - changed is True
12 # - object updated
13 # 3) Set same Value (Check mode)
14 # - changed is False
15 # - object *NOT* updated
16 # - Tests idemoptency
17 # 4) Set same Value
18 # - changed is False
19 # - object *NOT* updated
20 # - Tests idemoptency
21
22 - set_fact:
23 # As a lookup plugin we won't have access to module_defaults
24 connection_args:
25 region: "{{ aws_region }}"
26 access_key: "{{ aws_access_key }}"
27 secret_key: "{{ aws_secret_key }}"
28 session_token: "{{ security_token | default(omit) }}"
29 no_log: True
30
31 - vars:
32 first_tags:
33 'Key with Spaces': Value with spaces
34 CamelCaseKey: CamelCaseValue
35 pascalCaseKey: pascalCaseValue
36 snake_case_key: snake_case_value
37 second_tags:
38 'New Key with Spaces': Value with spaces
39 NewCamelCaseKey: CamelCaseValue
40 newPascalCaseKey: pascalCaseValue
41 new_snake_case_key: snake_case_value
42 third_tags:
43 'Key with Spaces': Value with spaces
44 CamelCaseKey: CamelCaseValue
45 pascalCaseKey: pascalCaseValue
46 snake_case_key: snake_case_value
47 'New Key with Spaces': Updated Value with spaces
48 final_tags:
49 'Key with Spaces': Value with spaces
50 CamelCaseKey: CamelCaseValue
51 pascalCaseKey: pascalCaseValue
52 snake_case_key: snake_case_value
53 'New Key with Spaces': Updated Value with spaces
54 NewCamelCaseKey: CamelCaseValue
55 newPascalCaseKey: pascalCaseValue
56 new_snake_case_key: snake_case_value
57 block:
58 # ============================================================
59 # Preparation
60 # ============================================================
61 - name: 'Retrieve caller facts'
62 aws_caller_info:
63 register: caller_info
64
65 - name: 'Generate the ARN pattern to search for'
66 vars:
67 _caller_info: '{{ caller_info.arn.split(":") }}'
68 _base_arn: 'arn:{{_caller_info[1]}}:secretsmanager:{{ aws_region }}'
69 set_fact:
70 account_arn: '{{_base_arn}}:{{_caller_info[4]}}:secret:'
71
72 # ============================================================
73 # Creation testing
74 # ============================================================
75 - name: add secret to AWS Secrets Manager
76 secretsmanager_secret:
77 name: "{{ secret_name }}"
78 state: present
79 secret_type: 'string'
80 secret: "{{ super_secret_string }}"
81 register: result
82
83 - name: assert correct keys are returned
84 assert:
85 that:
86 - result.changed
87 - '"arn" in result.secret'
88 # - '"last_accessed_date" in result.secret'
89 - '"last_changed_date" in result.secret'
90 - '"name" in result.secret'
91 - '"version_ids_to_stages" in result.secret'
92 - result.secret.arn.startswith(account_arn)
93 # ARN includes a random string after the name
94 - secret_name in result.secret.arn
95 - result.secret.name == secret_name
96 - result.secret.version_ids_to_stages | length == 1
97
98 - name: save the ARN for later comparison
99 set_fact:
100 secret_arn: '{{ result.secret.arn }}'
101
102 - name: no changes to secret
103 secretsmanager_secret:
104 name: "{{ secret_name }}"
105 state: present
106 secret_type: 'string'
107 secret: "{{ super_secret_string }}"
108 register: result
109
110 - name: assert correct keys are returned
111 assert:
112 that:
113 - not result.changed
114 - '"arn" in result.secret'
115 - '"created_date" in result.secret'
116 - '"last_accessed_date" in result.secret'
117 - '"last_changed_date" in result.secret'
118 - '"name" in result.secret'
119 - '"version_ids_to_stages" in result.secret'
120 - result.secret.arn == secret_arn
121 - result.secret.name == secret_name
122 - result.secret.version_ids_to_stages | length == 1
123
124 - name: Set secret description
125 secretsmanager_secret:
126 name: "{{ secret_name }}"
127 description: 'this is a change to this secret'
128 state: present
129 secret_type: 'string'
130 secret: "{{ super_secret_string }}"
131 register: result
132
133 - name: assert correct keys are returned
134 assert:
135 that:
136 - result.changed
137 - '"arn" in result.secret'
138 - '"created_date" in result.secret'
139 - '"description" in result.secret'
140 - '"last_accessed_date" in result.secret'
141 - '"last_changed_date" in result.secret'
142 - '"name" in result.secret'
143 - '"version_ids_to_stages" in result.secret'
144 - result.secret.description == "this is a change to this secret"
145 - result.secret.arn == secret_arn
146 - result.secret.name == secret_name
147 - result.secret.version_ids_to_stages | length == 2
148
149 ###############################################################
150 # Tagging
151 ###############################################################
152
153 - name: Set tags (CHECK_MODE)
154 secretsmanager_secret:
155 name: "{{ secret_name }}"
156 description: 'this is a change to this secret'
157 state: present
158 secret_type: 'string'
159 secret: "{{ super_secret_string }}"
160 tags: "{{ first_tags }}"
161 register: result
162 check_mode: True
163
164 - name: assert changed
165 assert:
166 that:
167 - result is changed
168
169 - name: Set tags
170 secretsmanager_secret:
171 name: "{{ secret_name }}"
172 description: 'this is a change to this secret'
173 state: present
174 secret_type: 'string'
175 secret: "{{ super_secret_string }}"
176 tags: "{{ first_tags }}"
177 register: result
178
179 - name: assert correct keys are returned
180 assert:
181 that:
182 - result is changed
183 - '"arn" in result.secret'
184 - '"created_date" in result.secret'
185 - '"description" in result.secret'
186 - '"last_accessed_date" in result.secret'
187 - '"last_changed_date" in result.secret'
188 - '"name" in result.secret'
189 - '"tags" in result.secret'
190 - '"tags_dict" in result.secret'
191 - '"version_ids_to_stages" in result.secret'
192 - result.secret.description == "this is a change to this secret"
193 - result.secret.arn == secret_arn
194 - result.secret.name == secret_name
195 - result.secret.tags | length == 4
196 - result.secret.tags_dict == first_tags
197 - result.secret.version_ids_to_stages | length == 2
198
199 - name: Set tags - idempotency (CHECK_MODE)
200 secretsmanager_secret:
201 name: "{{ secret_name }}"
202 description: 'this is a change to this secret'
203 state: present
204 secret_type: 'string'
205 secret: "{{ super_secret_string }}"
206 tags: "{{ first_tags }}"
207 register: result
208 check_mode: True
209
210 - name: assert changed
211 assert:
212 that:
213 - result is not changed
214
215 - name: Set tags - idempotency
216 secretsmanager_secret:
217 name: "{{ secret_name }}"
218 description: 'this is a change to this secret'
219 state: present
220 secret_type: 'string'
221 secret: "{{ super_secret_string }}"
222 tags: "{{ first_tags }}"
223 register: result
224
225 - name: assert correct keys are returned
226 assert:
227 that:
228 - result is not changed
229 - '"arn" in result.secret'
230 - '"created_date" in result.secret'
231 - '"description" in result.secret'
232 - '"last_accessed_date" in result.secret'
233 - '"last_changed_date" in result.secret'
234 - '"name" in result.secret'
235 - '"tags" in result.secret'
236 - '"tags_dict" in result.secret'
237 - '"version_ids_to_stages" in result.secret'
238 - result.secret.description == "this is a change to this secret"
239 - result.secret.arn == secret_arn
240 - result.secret.name == secret_name
241 - result.secret.tags | length == 4
242 - result.secret.tags_dict == first_tags
243 - result.secret.version_ids_to_stages | length == 2
244
245 ###
246
247 - name: Update tags with purge (CHECK_MODE)
248 secretsmanager_secret:
249 name: "{{ secret_name }}"
250 description: 'this is a change to this secret'
251 state: present
252 secret_type: 'string'
253 secret: "{{ super_secret_string }}"
254 tags: "{{ second_tags }}"
255 register: result
256 check_mode: True
257
258 - name: assert changed
259 assert:
260 that:
261 - result is changed
262
263 - name: Update tags with purge
264 secretsmanager_secret:
265 name: "{{ secret_name }}"
266 description: 'this is a change to this secret'
267 state: present
268 secret_type: 'string'
269 secret: "{{ super_secret_string }}"
270 tags: "{{ second_tags }}"
271 register: result
272
273 - name: assert correct keys are returned
274 assert:
275 that:
276 - result is changed
277 - '"arn" in result.secret'
278 - '"created_date" in result.secret'
279 - '"description" in result.secret'
280 - '"last_accessed_date" in result.secret'
281 - '"last_changed_date" in result.secret'
282 - '"name" in result.secret'
283 - '"tags" in result.secret'
284 - '"tags_dict" in result.secret'
285 - '"version_ids_to_stages" in result.secret'
286 - result.secret.description == "this is a change to this secret"
287 - result.secret.arn == secret_arn
288 - result.secret.name == secret_name
289 - result.secret.tags | length == 4
290 - result.secret.tags_dict == second_tags
291 - result.secret.version_ids_to_stages | length == 2
292
293 - name: Update tags with purge - idempotency (CHECK_MODE)
294 secretsmanager_secret:
295 name: "{{ secret_name }}"
296 description: 'this is a change to this secret'
297 state: present
298 secret_type: 'string'
299 secret: "{{ super_secret_string }}"
300 tags: "{{ second_tags }}"
301 register: result
302 check_mode: True
303
304 - name: assert changed
305 assert:
306 that:
307 - result is not changed
308
309 - name: Update tags with purge - idempotency
310 secretsmanager_secret:
311 name: "{{ secret_name }}"
312 description: 'this is a change to this secret'
313 state: present
314 secret_type: 'string'
315 secret: "{{ super_secret_string }}"
316 tags: "{{ second_tags }}"
317 register: result
318
319 - name: assert correct keys are returned
320 assert:
321 that:
322 - result is not changed
323 - '"arn" in result.secret'
324 - '"created_date" in result.secret'
325 - '"description" in result.secret'
326 - '"last_accessed_date" in result.secret'
327 - '"last_changed_date" in result.secret'
328 - '"name" in result.secret'
329 - '"tags" in result.secret'
330 - '"tags_dict" in result.secret'
331 - '"version_ids_to_stages" in result.secret'
332 - result.secret.description == "this is a change to this secret"
333 - result.secret.arn == secret_arn
334 - result.secret.name == secret_name
335 - result.secret.tags | length == 4
336 - result.secret.tags_dict == second_tags
337 - result.secret.version_ids_to_stages | length == 2
338
339 ###
340
341 - name: Update tags without purge (CHECK_MODE)
342 secretsmanager_secret:
343 name: "{{ secret_name }}"
344 description: 'this is a change to this secret'
345 state: present
346 secret_type: 'string'
347 secret: "{{ super_secret_string }}"
348 tags: "{{ third_tags }}"
349 purge_tags: false
350 register: result
351 check_mode: True
352
353 - name: assert changed
354 assert:
355 that:
356 - result is changed
357
358 - name: Update tags without purge
359 secretsmanager_secret:
360 name: "{{ secret_name }}"
361 description: 'this is a change to this secret'
362 state: present
363 secret_type: 'string'
364 secret: "{{ super_secret_string }}"
365 tags: "{{ third_tags }}"
366 purge_tags: false
367 register: result
368
369 - name: assert correct keys are returned
370 assert:
371 that:
372 - result is changed
373 - '"arn" in result.secret'
374 - '"created_date" in result.secret'
375 - '"description" in result.secret'
376 - '"last_accessed_date" in result.secret'
377 - '"last_changed_date" in result.secret'
378 - '"name" in result.secret'
379 - '"tags" in result.secret'
380 - '"tags_dict" in result.secret'
381 - '"version_ids_to_stages" in result.secret'
382 - result.secret.description == "this is a change to this secret"
383 - result.secret.arn == secret_arn
384 - result.secret.name == secret_name
385 - result.secret.tags | length == 8
386 - result.secret.tags_dict == final_tags
387 - result.secret.version_ids_to_stages | length == 2
388
389 - name: Update tags without purge - idempotency (CHECK_MODE)
390 secretsmanager_secret:
391 name: "{{ secret_name }}"
392 description: 'this is a change to this secret'
393 state: present
394 secret_type: 'string'
395 secret: "{{ super_secret_string }}"
396 tags: "{{ third_tags }}"
397 purge_tags: false
398 register: result
399 check_mode: True
400
401 - name: assert changed
402 assert:
403 that:
404 - result is not changed
405
406 - name: Update tags without purge - idempotency
407 secretsmanager_secret:
408 name: "{{ secret_name }}"
409 description: 'this is a change to this secret'
410 state: present
411 secret_type: 'string'
412 secret: "{{ super_secret_string }}"
413 tags: "{{ third_tags }}"
414 purge_tags: false
415 register: result
416
417 - name: assert correct keys are returned
418 assert:
419 that:
420 - result is not changed
421 - '"arn" in result.secret'
422 - '"created_date" in result.secret'
423 - '"description" in result.secret'
424 - '"last_accessed_date" in result.secret'
425 - '"last_changed_date" in result.secret'
426 - '"name" in result.secret'
427 - '"tags" in result.secret'
428 - '"tags_dict" in result.secret'
429 - '"version_ids_to_stages" in result.secret'
430 - result.secret.description == "this is a change to this secret"
431 - result.secret.arn == secret_arn
432 - result.secret.name == secret_name
433 - result.secret.tags | length == 8
434 - result.secret.tags_dict == final_tags
435 - result.secret.version_ids_to_stages | length == 2
436
437 ###
438
439 - name: Tags not set - idempotency (CHECK_MODE)
440 secretsmanager_secret:
441 name: "{{ secret_name }}"
442 description: 'this is a change to this secret'
443 state: present
444 secret_type: 'string'
445 secret: "{{ super_secret_string }}"
446 register: result
447 check_mode: True
448
449 - name: assert changed
450 assert:
451 that:
452 - result is not changed
453
454 - name: Tags not set - idempotency
455 secretsmanager_secret:
456 name: "{{ secret_name }}"
457 description: 'this is a change to this secret'
458 state: present
459 secret_type: 'string'
460 secret: "{{ super_secret_string }}"
461 register: result
462
463 - name: assert correct keys are returned
464 assert:
465 that:
466 - result is not changed
467 - '"arn" in result.secret'
468 - '"created_date" in result.secret'
469 - '"description" in result.secret'
470 - '"last_accessed_date" in result.secret'
471 - '"last_changed_date" in result.secret'
472 - '"name" in result.secret'
473 - '"tags" in result.secret'
474 - '"tags_dict" in result.secret'
475 - '"version_ids_to_stages" in result.secret'
476 - result.secret.description == "this is a change to this secret"
477 - result.secret.arn == secret_arn
478 - result.secret.name == secret_name
479 - result.secret.tags | length == 8
480 - result.secret.tags_dict == final_tags
481 - result.secret.version_ids_to_stages | length == 2
482
483 ###
484
485 - name: remove all tags from secret (CHECK_MODE)
486 secretsmanager_secret:
487 name: "{{ secret_name }}"
488 description: 'this is a change to this secret'
489 state: present
490 secret_type: 'string'
491 secret: "{{ super_secret_string }}"
492 tags: {}
493 register: result
494 check_mode: true
495
496 - name: assert correct keys are returned
497 assert:
498 that:
499 - result is changed
500
501 - name: remove all tags from secret
502 secretsmanager_secret:
503 name: "{{ secret_name }}"
504 description: 'this is a change to this secret'
505 state: present
506 secret_type: 'string'
507 secret: "{{ super_secret_string }}"
508 tags: {}
509 register: result
510
511 - name: assert correct keys are returned
512 assert:
513 that:
514 - result is changed
515 - '"arn" in result.secret'
516 - '"created_date" in result.secret'
517 - '"description" in result.secret'
518 - '"last_accessed_date" in result.secret'
519 - '"last_changed_date" in result.secret'
520 - '"name" in result.secret'
521 - '"tags" in result.secret'
522 - '"tags_dict" in result.secret'
523 - '"version_ids_to_stages" in result.secret'
524 - result.secret.description == "this is a change to this secret"
525 - result.secret.arn == secret_arn
526 - result.secret.name == secret_name
527 - result.secret.tags | length == 0
528 - result.secret.tags_dict | length == 0
529 - result.secret.version_ids_to_stages | length == 2
530
531 - name: remove all tags from secret - idempotency (CHECK_MODE)
532 secretsmanager_secret:
533 name: "{{ secret_name }}"
534 description: 'this is a change to this secret'
535 state: present
536 secret_type: 'string'
537 secret: "{{ super_secret_string }}"
538 tags: {}
539 register: result
540 check_mode: true
541
542 - name: assert correct keys are returned
543 assert:
544 that:
545 - result is not changed
546
547 - name: remove all tags from secret
548 secretsmanager_secret:
549 name: "{{ secret_name }}"
550 description: 'this is a change to this secret'
551 state: present
552 secret_type: 'string'
553 secret: "{{ super_secret_string }}"
554 tags: {}
555 register: result
556
557 - name: assert correct keys are returned - idempotency
558 assert:
559 that:
560 - result is not changed
561 - '"arn" in result.secret'
562 - '"created_date" in result.secret'
563 - '"description" in result.secret'
564 - '"last_accessed_date" in result.secret'
565 - '"last_changed_date" in result.secret'
566 - '"name" in result.secret'
567 - '"tags" in result.secret'
568 - '"tags_dict" in result.secret'
569 - '"version_ids_to_stages" in result.secret'
570 - result.secret.description == "this is a change to this secret"
571 - result.secret.arn == secret_arn
572 - result.secret.name == secret_name
573 - result.secret.tags | length == 0
574 - result.secret.tags_dict | length == 0
575 - result.secret.version_ids_to_stages | length == 2
576
577 ###############################################################
578 # Resource policy
579 ###############################################################
580
581 - name: add resource policy to secret
582 secretsmanager_secret:
583 name: "{{ secret_name }}"
584 description: 'this is a change to this secret'
585 state: present
586 secret_type: 'string'
587 secret: "{{ super_secret_string }}"
588 resource_policy: "{{ lookup('template', 'secret-policy.j2', convert_data=False) | string }}"
589 register: result
590
591 - name: assert correct keys are returned
592 assert:
593 that:
594 - result.changed
595
596 - name: remove existing resource policy from secret
597 secretsmanager_secret:
598 name: "{{ secret_name }}"
599 description: 'this is a change to this secret'
600 state: present
601 secret_type: 'string'
602 secret: "{{ super_secret_string }}"
603 register: result
604
605 - name: assert correct keys are returned
606 assert:
607 that:
608 - result.changed
609
610 - name: remove resource policy from secret (idempotency)
611 secretsmanager_secret:
612 name: "{{ secret_name }}"
613 description: 'this is a change to this secret'
614 state: present
615 secret_type: 'string'
616 secret: "{{ super_secret_string }}"
617 register: result
618
619 - name: assert no change happened
620 assert:
621 that:
622 - not result.changed
623
624 # ============================================================
625 # Update secret using JSON string
626 # ============================================================
627
628 - name: Update secret with JSON (CHECK_MODE)
629 secretsmanager_secret:
630 name: "{{ secret_name }}"
631 description: 'this is a change to this secret'
632 state: present
633 secret_type: 'string'
634 json_secret:
635 my_key: '{{ super_secret_string }}'
636 register: result
637 check_mode: True
638
639 - name: assert key would be changed
640 assert:
641 that:
642 - result.changed
643
644 - name: Update secret with JSON
645 secretsmanager_secret:
646 name: "{{ secret_name }}"
647 state: present
648 description: 'this is a change to this secret'
649 secret_type: 'string'
650 json_secret:
651 my_key: '{{ super_secret_string }}'
652 register: result
653
654 - name: assert key is changed
655 assert:
656 that:
657 - result.changed
658
659 - name: Update secret with JSON - idempotency (CHECK_MODE)
660 secretsmanager_secret:
661 name: "{{ secret_name }}"
662 description: 'this is a change to this secret'
663 state: present
664 secret_type: 'string'
665 json_secret:
666 my_key: '{{ super_secret_string }}'
667 register: result
668 check_mode: True
669
670 - name: assert key is not changed
671 assert:
672 that:
673 - result is not changed
674
675 - name: Update secret with JSON - idempotency
676 secretsmanager_secret:
677 name: "{{ secret_name }}"
678 description: 'this is a change to this secret'
679 state: present
680 secret_type: 'string'
681 json_secret:
682 my_key: '{{ super_secret_string }}'
683 register: result
684 check_mode: True
685
686 - name: assert key is not changed
687 assert:
688 that:
689 - result is not changed
690
691 # ============================================================
692 # Overwrite testing
693 # ============================================================
694
695 - name: Create secret with overwrite = False (Check mode)
696 secretsmanager_secret:
697 name: "{{ secret_name }}-2"
698 state: present
699 secret_type: 'string'
700 secret: "{{ super_secret_string }}"
701 overwrite: False
702 register: result
703 check_mode: True
704
705 - name: assert key is changed
706 assert:
707 that:
708 - result is changed
709
710 - name: Create secret with overwrite = False
711 secretsmanager_secret:
712 name: "{{ secret_name }}-2"
713 state: present
714 secret_type: 'string'
715 secret: "{{ super_secret_string }}"
716 overwrite: False
717 register: result
718
719 - name: assert key is changed
720 assert:
721 that:
722 - result is changed
723
724 - name: Update secret with overwrite = False (Check mode)
725 secretsmanager_secret:
726 name: "{{ secret_name }}-2"
727 state: present
728 secret_type: 'string'
729 secret: "{{ super_secret_string }}-2"
730 overwrite: False
731 register: result
732 check_mode: True
733
734 - name: assert key is not changed
735 assert:
736 that:
737 - result is not changed
738
739 - name: Create secret with overwrite = False
740 secretsmanager_secret:
741 name: "{{ secret_name }}-2"
742 state: present
743 secret_type: 'string'
744 secret: "{{ super_secret_string }}-2"
745 overwrite: False
746 register: result
747
748 - name: assert key is not changed
749 assert:
750 that:
751 - result is not changed
752
753 # ============================================================
754 # Removal testing
755 # ============================================================
756
757 - name: remove secret
758 secretsmanager_secret:
759 name: "{{ secret_name }}"
760 state: absent
761 recovery_window: 7
762 register: result
763
764 - name: assert key is deleted
765 assert:
766 that:
767 - result.changed
768
769 - name: remove secret (idempotency)
770 secretsmanager_secret:
771 name: "{{ secret_name }}"
772 state: absent
773 recovery_window: 7
774 register: result
775
776 - name: assert no change happened
777 assert:
778 that:
779 - not result.changed
780
781 - name: immediate secret removal
782 secretsmanager_secret:
783 name: "{{ secret_name }}"
784 state: absent
785 recovery_window: 0
786 register: result
787
788 - name: assert key is deleted
789 assert:
790 that:
791 - result.changed
792
793 # AWS Doesn't expose when the secret will be removed, all we can do is
794 # check that we didn't throw an error
795 - name: immediate secret removal
796 secretsmanager_secret:
797 name: "{{ secret_name }}"
798 state: absent
799 recovery_window: 0
800 register: result
801
802 - name: assert no change happened
803 assert:
804 that:
805 - not result.failed
806
807 always:
808 - name: remove secret
809 secretsmanager_secret:
810 name: "{{ secret_name }}"
811 state: absent
812 recovery_window: 0
813 ignore_errors: yes
814
815 - name: remove secret 2
816 secretsmanager_secret:
817 name: "{{ secret_name }}-2"
818 state: absent
819 recovery_window: 0
820 ignore_errors: yes