@@ -2,6 +2,12 @@ variables {
2
2
application = " test-app"
3
3
environment = " test-env"
4
4
database_name = " test-db"
5
+ tasks = [
6
+ {
7
+ from : " prod"
8
+ to : " dev"
9
+ }
10
+ ]
5
11
}
6
12
7
13
mock_provider "aws" {}
@@ -68,8 +74,12 @@ run "data_dump_unit_test" {
68
74
for el in data . aws_iam_policy_document . assume_ecs_task_role . statement [0 ]. principals :
69
75
true if el . type == " Service" && [
70
76
for identifier in el . identifiers : true if identifier == " ecs-tasks.amazonaws.com"
71
- ][0 ] == true
72
- ][0 ] == true
77
+ ][
78
+ 0
79
+ ] == true
80
+ ][
81
+ 0
82
+ ] == true
73
83
error_message = " Principal identifier should be: 'ecs-tasks.amazonaws.com'"
74
84
}
75
85
@@ -142,7 +152,7 @@ run "data_dump_unit_test" {
142
152
143
153
assert {
144
154
condition = contains (data. aws_iam_policy_document . data_dump . statement [1 ]. actions , " kms:Decrypt" )
145
- error_message = " Permission not found: kms:Encrypt "
155
+ error_message = " Permission not found: kms:Decrypt "
146
156
}
147
157
148
158
assert {
@@ -262,13 +272,37 @@ run "data_dump_unit_test" {
262
272
}
263
273
264
274
assert {
265
- condition = [for el in data . aws_iam_policy_document . data_dump_bucket_policy . statement [0 ]. condition : true if (el. variable == " aws:SecureTransport" && contains (el. values , " false" ))] == [true ]
275
+ condition = [
276
+ for el in data . aws_iam_policy_document . data_dump_bucket_policy . statement [0 ]. condition : true
277
+ if (el. variable == " aws:SecureTransport" && contains (el. values , " false" ))
278
+ ] == [true ]
266
279
error_message = " Should be denied if not aws:SecureTransport"
267
280
}
268
281
269
- # aws_s3_bucket_policy.data_dump_bucket_policy.policy cannot be tested with plan
282
+ assert {
283
+ condition = [for el in data . aws_iam_policy_document . data_dump_bucket_policy . statement [1 ]. principals : el . type ][0 ] == " AWS"
284
+ error_message = " Should be: AWS"
285
+ }
286
+
287
+ assert {
288
+ condition = flatten ([for el in data . aws_iam_policy_document . data_dump_bucket_policy . statement [1 ]. principals : el . identifiers ]) == [" arn:aws:iam::${ data . aws_caller_identity . current . account_id } :role/test-app-dev-test-db-load-task" ]
289
+ error_message = " Bucket policy principals incorrect"
290
+ }
291
+
292
+ assert {
293
+ condition = data. aws_iam_policy_document . data_dump_bucket_policy . statement [1 ]. actions == toset ([" s3:ListBucket" ,
294
+ " s3:GetObject" ,
295
+ " s3:GetObjectTagging" ,
296
+ " s3:GetObjectVersion" ,
297
+ " s3:GetObjectVersionTagging" ,
298
+ " s3:DeleteObject" ])
299
+ error_message = " Unexpected actions"
300
+ }
270
301
271
- # aws_kms_key.data_dump_kms_key policy cannot be tested with plan
302
+ assert {
303
+ condition = strcontains (aws_kms_key. data_dump_kms_key . policy , " arn:aws:iam::${ data . aws_caller_identity . current . account_id } :root" ) && ! strcontains (aws_kms_key. data_dump_kms_key . policy , " arn:aws:iam::000123456789:role/test-app-dev-test-db-load-task" )
304
+ error_message = " Unexpected KMS key policy principal"
305
+ }
272
306
273
307
assert {
274
308
condition = aws_kms_alias. data_dump_kms_alias . name == " alias/test-app-test-env-test-db-dump"
@@ -281,7 +315,10 @@ run "data_dump_unit_test" {
281
315
}
282
316
283
317
assert {
284
- condition = [for el in aws_s3_bucket_server_side_encryption_configuration . encryption-config . rule : el . apply_server_side_encryption_by_default [0 ]. sse_algorithm ] == [" aws:kms" ]
318
+ condition = [
319
+ for el in aws_s3_bucket_server_side_encryption_configuration . encryption-config . rule :
320
+ el . apply_server_side_encryption_by_default [0 ]. sse_algorithm
321
+ ] == [" aws:kms" ]
285
322
error_message = " Server side encryption algorithm should be: aws:kms"
286
323
}
287
324
@@ -295,3 +332,40 @@ run "data_dump_unit_test" {
295
332
error_message = " Public access block has expected conditions"
296
333
}
297
334
}
335
+
336
+ run "cross_account_data_dump_unit_test" {
337
+ command = plan
338
+
339
+ variables {
340
+ tasks = [
341
+ {
342
+ from : " prod"
343
+ from_account : " 123456789000"
344
+ to : " dev"
345
+ to_account : " 000123456789"
346
+ }
347
+ ]
348
+ }
349
+
350
+ assert {
351
+ condition = [for el in data . aws_iam_policy_document . data_dump_bucket_policy . statement [1 ]. principals : el . type ][0 ] == " AWS"
352
+ error_message = " Should be: AWS"
353
+ }
354
+ assert {
355
+ condition = flatten ([for el in data . aws_iam_policy_document . data_dump_bucket_policy . statement [1 ]. principals : el . identifiers ]) == [" arn:aws:iam::000123456789:role/test-app-dev-test-db-load-task" ]
356
+ error_message = " Bucket policy principals incorrect"
357
+ }
358
+ assert {
359
+ condition = data. aws_iam_policy_document . data_dump_bucket_policy . statement [1 ]. actions == toset ([" s3:ListBucket" ,
360
+ " s3:GetObject" ,
361
+ " s3:GetObjectTagging" ,
362
+ " s3:GetObjectVersion" ,
363
+ " s3:GetObjectVersionTagging" ,
364
+ " s3:DeleteObject" ])
365
+ error_message = " Unexpected actions"
366
+ }
367
+ assert {
368
+ condition = strcontains (aws_kms_key. data_dump_kms_key . policy , " arn:aws:iam::${ data . aws_caller_identity . current . account_id } :root" ) && strcontains (aws_kms_key. data_dump_kms_key . policy , " arn:aws:iam::000123456789:role/test-app-dev-test-db-load-task" )
369
+ error_message = " Unexpected KMS key policy principal"
370
+ }
371
+ }
0 commit comments