Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/config/tasks/main.yaml

raw
1 ---
2 - name: 'aws_config integration tests'
3 collections:
4 - amazon.aws
5 module_defaults:
6 group/aws:
7 access_key: '{{ aws_access_key }}'
8 secret_key: '{{ aws_secret_key }}'
9 session_token: '{{ security_token | default(omit) }}'
10 region: '{{ aws_region }}'
11 block:
12
13 # ============================================================
14 # Prerequisites
15 # ============================================================
16 - name: get ARN of calling user
17 aws_caller_info:
18 register: aws_caller_info
19
20 - name: Store Account ID for later use
21 set_fact:
22 aws_account_id: "{{ aws_caller_info.account }}"
23
24 - name: ensure IAM role exists
25 iam_role:
26 name: '{{ config_role_name }}'
27 assume_role_policy_document: "{{ lookup('file','config-trust-policy.json') }}"
28 state: present
29 create_instance_profile: no
30 managed_policy:
31 - arn:aws:iam::aws:policy/service-role/AWS_ConfigRole
32 register: config_iam_role
33
34 - name: ensure SNS topic exists
35 sns_topic:
36 name: '{{ config_sns_name }}'
37 state: present
38 subscriptions:
39 - endpoint: "[email protected]"
40 protocol: "email"
41 register: config_sns_topic
42
43 - name: ensure S3 bucket exists
44 s3_bucket:
45 name: "{{ config_s3_bucket }}"
46
47 - name: ensure KMS key exists
48 kms_key:
49 alias: "{{ config_kms_key }}"
50 policy: "{{ lookup('template', 'config-kms-policy.json.j2') }}"
51 register: kms_key
52
53 - name: ensure S3 access for IAM role
54 iam_policy:
55 iam_type: role
56 iam_name: '{{ config_role_name }}'
57 policy_name: AwsConfigRecorderTestRoleS3Policy
58 state: present
59 policy_json: "{{ lookup( 'template', 'config-s3-policy.json.j2') }}"
60
61 # ============================================================
62 # Module requirement testing
63 # ============================================================
64 - name: test rule with no source parameter
65 config_rule:
66 name: random_name
67 state: present
68 register: output
69 ignore_errors: true
70
71 - name: assert failure when called with no source parameter
72 assert:
73 that:
74 - output.failed
75 - 'output.msg.startswith("missing required arguments:")'
76
77 - name: test resource_type delivery_channel with no s3_bucket parameter
78 config_delivery_channel:
79 name: random_name
80 state: present
81 register: output
82 ignore_errors: true
83
84 - name: assert failure when called with no s3_bucket parameter
85 assert:
86 that:
87 - output.failed
88 - 'output.msg.startswith("missing required arguments:")'
89
90 - name: test resource_type configuration_recorder with no role_arn parameter
91 config_recorder:
92 name: random_name
93 state: present
94 register: output
95 ignore_errors: true
96
97 - name: assert failure when called with no role_arn parameter
98 assert:
99 that:
100 - output.failed
101 - 'output.msg.startswith("state is present but all of the following are missing")'
102
103 - name: test resource_type configuration_recorder with no recording_group parameter
104 config_recorder:
105 name: random_name
106 state: present
107 role_arn: 'arn:aws:iam::123456789012:role/AwsConfigRecorder'
108 register: output
109 ignore_errors: true
110
111 - name: assert failure when called with no recording_group parameter
112 assert:
113 that:
114 - output.failed
115 - 'output.msg.startswith("state is present but all of the following are missing")'
116
117 - name: test resource_type aggregation_authorization with no authorized_account_id parameter
118 config_aggregation_authorization:
119 state: present
120 register: output
121 ignore_errors: true
122
123 - name: assert failure when called with no authorized_account_id parameter
124 assert:
125 that:
126 - output.failed
127 - 'output.msg.startswith("missing required arguments:")'
128
129 - name: test resource_type aggregation_authorization with no authorized_aws_region parameter
130 config_aggregation_authorization:
131 state: present
132 authorized_account_id: '123456789012'
133 register: output
134 ignore_errors: true
135
136 - name: assert failure when called with no authorized_aws_region parameter
137 assert:
138 that:
139 - output.failed
140 - 'output.msg.startswith("missing required arguments:")'
141
142 - name: test resource_type configuration_aggregator with no account_sources parameter
143 config_aggregator:
144 name: random_name
145 state: present
146 register: output
147 ignore_errors: true
148
149 - name: assert failure when called with no account_sources parameter
150 assert:
151 that:
152 - output.failed
153 - 'output.msg.startswith("missing required arguments: account_sources")'
154
155 - name: test resource_type configuration_aggregator with no organization_source parameter
156 config_aggregator:
157 name: random_name
158 state: present
159 account_sources: []
160 register: output
161 ignore_errors: true
162
163 - name: assert failure when called with no organization_source parameter
164 assert:
165 that:
166 - output.failed
167 - 'output.msg.startswith("missing required arguments: organization_source")'
168
169 # ============================================================
170 # Creation testing
171 # ============================================================
172 - name: Create Configuration Recorder for AWS Config
173 config_recorder:
174 name: '{{ resource_prefix }}-recorder'
175 state: present
176 role_arn: "{{ config_iam_role.iam_role.arn }}"
177 recording_group:
178 all_supported: true
179 include_global_types: true
180 register: output
181
182 - assert:
183 that:
184 - output.changed
185
186 - name: Create Delivery Channel for AWS Config
187 config_delivery_channel:
188 name: '{{ resource_prefix }}-channel'
189 state: present
190 s3_bucket: "{{ config_s3_bucket }}"
191 s3_prefix: "foo/bar"
192 sns_topic_arn: "{{ config_sns_topic.sns_arn }}"
193 delivery_frequency: 'Twelve_Hours'
194 register: output
195
196 - assert:
197 that:
198 - output.changed
199
200 - name: Create Delivery Channel for AWS Config with a KMS key
201 config_delivery_channel:
202 name: '{{ resource_prefix }}-channel'
203 state: present
204 s3_bucket: "{{ config_s3_bucket }}"
205 s3_prefix: "foo/bar"
206 kms_key_arn: "{{ kms_key.key_arn }}"
207 sns_topic_arn: "{{ config_sns_topic.sns_arn }}"
208 delivery_frequency: 'Twelve_Hours'
209 register: output
210
211 - assert:
212 that:
213 - output.changed
214
215 - name: Create Config Rule for AWS Config
216 config_rule:
217 name: '{{ resource_prefix }}-rule'
218 state: present
219 description: 'This AWS Config rule checks for public write access on S3 buckets'
220 scope:
221 compliance_types:
222 - 'AWS::S3::Bucket'
223 source:
224 owner: AWS
225 identifier: 'S3_BUCKET_PUBLIC_WRITE_PROHIBITED'
226 register: output
227
228 - assert:
229 that:
230 - output.changed
231
232 - name: Create aws_config_aggregator
233 config_aggregator:
234 name: random_name
235 state: present
236 account_sources: []
237 organization_source:
238 all_aws_regions: true
239 role_arn: "{{ config_iam_role.iam_role.arn }}"
240 register: output
241
242 - name: assert success
243 assert:
244 that:
245 - output is changed
246
247 - name: Create aws_config_aggregator - idempotency
248 config_aggregator:
249 name: random_name
250 state: present
251 account_sources: []
252 organization_source:
253 all_aws_regions: true
254 role_arn: "{{ config_iam_role.iam_role.arn }}"
255 register: output
256
257 - name: assert not changed
258 assert:
259 that:
260 - output is not changed
261
262 # ============================================================
263 # Update testing
264 # ============================================================
265 - name: Update Configuration Recorder
266 config_recorder:
267 name: '{{ resource_prefix }}-recorder'
268 state: present
269 role_arn: "{{ config_iam_role.iam_role.arn }}"
270 recording_group:
271 all_supported: false
272 include_global_types: false
273 resource_types:
274 - 'AWS::S3::Bucket'
275 register: output
276
277 - assert:
278 that:
279 - output.changed
280
281 - name: Update Delivery Channel
282 config_delivery_channel:
283 name: '{{ resource_prefix }}-channel'
284 state: present
285 s3_bucket: "{{ config_s3_bucket }}"
286 sns_topic_arn: "{{ config_sns_topic.sns_arn }}"
287 delivery_frequency: 'TwentyFour_Hours'
288 register: output
289
290 - assert:
291 that:
292 - output.changed
293
294 - name: Update Delivery Channel with KMS key
295 config_delivery_channel:
296 name: '{{ resource_prefix }}-channel'
297 state: present
298 s3_bucket: "{{ config_s3_bucket }}"
299 sns_topic_arn: "{{ config_sns_topic.sns_arn }}"
300 kms_key_arn: "{{ kms_key.key_arn }}"
301 delivery_frequency: 'TwentyFour_Hours'
302 register: output
303
304 - assert:
305 that:
306 - output.changed
307
308 - name: Update Config Rule
309 config_rule:
310 name: '{{ resource_prefix }}-rule'
311 state: present
312 description: 'This AWS Config rule checks for public write access on S3 buckets'
313 scope:
314 compliance_types:
315 - 'AWS::S3::Bucket'
316 source:
317 owner: AWS
318 identifier: 'S3_BUCKET_PUBLIC_READ_PROHIBITED'
319 register: output
320
321 - assert:
322 that:
323 - output.changed
324
325 - name: Update Config Rule - idempotency
326 config_rule:
327 name: '{{ resource_prefix }}-rule'
328 state: present
329 description: 'This AWS Config rule checks for public write access on S3 buckets'
330 scope:
331 compliance_types:
332 - 'AWS::S3::Bucket'
333 source:
334 owner: AWS
335 identifier: 'S3_BUCKET_PUBLIC_READ_PROHIBITED'
336 register: output
337
338 - assert:
339 that:
340 - output is not changed
341
342 - name: Update aws_config_aggregator
343 config_aggregator:
344 name: random_name
345 state: present
346 account_sources: []
347 organization_source:
348 all_aws_regions: false
349 aws_regions:
350 - '{{ aws_region }}'
351 role_arn: "{{ config_iam_role.iam_role.arn }}"
352 register: output
353
354 - name: assert success
355 assert:
356 that:
357 - output is changed
358
359 - name: Update aws_config_aggregator - idempotency
360 config_aggregator:
361 name: random_name
362 state: present
363 account_sources: []
364 organization_source:
365 all_aws_regions: false
366 aws_regions:
367 - '{{ aws_region }}'
368 role_arn: "{{ config_iam_role.iam_role.arn }}"
369 register: output
370
371 - name: assert success
372 assert:
373 that:
374 - output is not changed
375
376 # ============================================================
377 # Read testing
378 # ============================================================
379 - name: Don't update Configuration Recorder
380 config_recorder:
381 name: '{{ resource_prefix }}-recorder'
382 state: present
383 role_arn: "{{ config_iam_role.iam_role.arn }}"
384 recording_group:
385 all_supported: false
386 include_global_types: false
387 resource_types:
388 - 'AWS::S3::Bucket'
389 register: output
390
391 - assert:
392 that:
393 - not output.changed
394
395 - name: Don't update Delivery Channel
396 config_delivery_channel:
397 name: '{{ resource_prefix }}-channel'
398 state: present
399 s3_bucket: "{{ config_s3_bucket }}"
400 sns_topic_arn: "{{ config_sns_topic.sns_arn }}"
401 delivery_frequency: 'TwentyFour_Hours'
402 register: output
403
404 - assert:
405 that:
406 - not output.changed
407
408 - name: Don't update Config Rule
409 config_rule:
410 name: '{{ resource_prefix }}-rule'
411 state: present
412 description: 'This AWS Config rule checks for public write access on S3 buckets'
413 scope:
414 compliance_types:
415 - 'AWS::S3::Bucket'
416 source:
417 owner: AWS
418 identifier: 'S3_BUCKET_PUBLIC_READ_PROHIBITED'
419 register: output
420
421 - assert:
422 that:
423 - not output.changed
424
425 always:
426
427 - name: delete aws_config_aggregator
428 config_aggregator:
429 name: random_name
430 state: absent
431 register: output
432 ignore_errors: true
433
434 # ============================================================
435 # Destroy testing
436 # ============================================================
437 - name: Destroy Configuration Recorder
438 config_recorder:
439 name: '{{ resource_prefix }}-recorder'
440 state: absent
441 register: output
442 ignore_errors: true
443
444 # - assert:
445 # that:
446 # - output.changed
447
448 - name: Destroy Delivery Channel
449 config_delivery_channel:
450 name: '{{ resource_prefix }}-channel'
451 state: absent
452 s3_bucket: "{{ config_s3_bucket }}"
453 sns_topic_arn: "{{ config_sns_topic.sns_arn }}"
454 delivery_frequency: 'TwentyFour_Hours'
455 register: output
456 ignore_errors: true
457
458 # - assert:
459 # that:
460 # - output.changed
461
462 - name: Destroy Config Rule
463 config_rule:
464 name: '{{ resource_prefix }}-rule'
465 state: absent
466 description: 'This AWS Config rule checks for public write access on S3 buckets'
467 scope:
468 compliance_types:
469 - 'AWS::S3::Bucket'
470 source:
471 owner: AWS
472 identifier: 'S3_BUCKET_PUBLIC_READ_PROHIBITED'
473 register: output
474 ignore_errors: true
475
476 # - assert:
477 # that:
478 # - output.changed
479
480 # ============================================================
481 # Clean up prerequisites
482 # ============================================================
483 - name: remove S3 access from IAM role
484 iam_policy:
485 iam_type: role
486 iam_name: '{{ config_role_name }}'
487 policy_name: AwsConfigRecorderTestRoleS3Policy
488 state: absent
489 policy_json: "{{ lookup( 'template', 'config-s3-policy.json.j2') }}"
490 ignore_errors: true
491
492 - name: remove IAM role
493 iam_role:
494 name: '{{ config_role_name }}'
495 state: absent
496 ignore_errors: true
497
498 - name: remove SNS topic
499 sns_topic:
500 name: '{{ config_sns_name }}'
501 state: absent
502 ignore_errors: true
503
504 - name: remove S3 bucket
505 s3_bucket:
506 name: "{{ config_s3_bucket }}"
507 state: absent
508 force: true
509 ignore_errors: true
510
511 - name: remove KMS key
512 kms_key:
513 alias: "{{ config_kms_key }}"
514 state: absent
515 ignore_errors: true