-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
1087 lines (1013 loc) · 39.4 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This Terraform file defines the configuration for creating an IAM roles that allows CloudGuard access to the AWS account.
# The `data "aws_partition" "current"` block retrieves information about the current AWS partition.
data "aws_partition" "current" {}
# The `data "aws_caller_identity" "current"` block retrieves information about the current AWS caller identity.
data "aws_caller_identity" "current" {}
# The `data "aws_region" "current"` block retrieves information about the current AWS region.
data "aws_region" "current" {
lifecycle {
postcondition {
condition = self.name == local.region
error_message = "Error: AWP must be deployed in the same region as the CloudGuard Data Center: ${local.region} (Not in ${self.name})"
}
}
}
data "aws_organizations_organization" "org" {}
data "dome9_cloudaccount_aws" "cloud_account" {
id = var.awp_cloud_account_id
}
data "dome9_cloudaccount_aws" "centralized_cloud_account" {
count = var.awp_centralized_cloud_account_id != null ? 1 : 0
id = var.awp_centralized_cloud_account_id
}
# The data source retrieves the onboarding data of an AWS account in Dome9 AWP.
data "dome9_awp_aws_onboarding_data" "dome9_awp_aws_onboarding_data_source" {
cloud_account_id = data.dome9_cloudaccount_aws.cloud_account.id
}
# Define local values used in multiple places in the configuration.
locals {
awp_module_version = "10"
scan_mode = var.awp_scan_mode
stage = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.stage
region = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.region
cloud_guard_backend_account_id = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.cloud_guard_backend_account_id
agentless_bucket_name = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.agentless_bucket_name
remote_functions_prefix_key = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.remote_functions_prefix_key
remote_snapshots_utils_function_name = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.remote_snapshots_utils_function_name
remote_snapshots_utils_function_run_time = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.remote_snapshots_utils_function_run_time
remote_snapshots_utils_function_time_out = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.remote_snapshots_utils_function_time_out
awp_client_side_security_group_name = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.awp_client_side_security_group_name
cross_account_role_external_id = var.awp_cross_account_role_external_id != null ? var.awp_cross_account_role_external_id : data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.cross_account_role_external_id
remote_snapshots_utils_function_s3_pre_signed_url = data.dome9_awp_aws_onboarding_data.dome9_awp_aws_onboarding_data_source.remote_snapshots_utils_function_s3_pre_signed_url
centralized_external_account_id = var.awp_centralized_cloud_account_id != null ? data.dome9_cloudaccount_aws.centralized_cloud_account[0].external_account_number : ""
aws_organization_id = data.aws_organizations_organization.org.id
is_saas_scan_mode = local.scan_mode == "saas"
is_in_account_scan_mode = local.scan_mode == "inAccount"
is_in_account_hub_scan_mode_condition = local.scan_mode == "inAccountHub"
is_in_account_sub_scan_mode_condition = local.scan_mode == "inAccountSub"
is_scanner_mode_condition = local.is_in_account_scan_mode || local.is_in_account_hub_scan_mode_condition
is_scanned_mode_condition = !local.is_in_account_hub_scan_mode_condition
is_proxy_lambda_required_condition = !local.is_in_account_sub_scan_mode_condition
is_hosting_key_condition = local.is_in_account_hub_scan_mode_condition || local.is_saas_scan_mode
is_reencrypt_required_condition = local.is_saas_scan_mode || local.is_in_account_sub_scan_mode_condition
common_tags = merge({
Owner = "CG.AWP"
CG_AWP_OWNER = "CG.AWP"
Terraform = "true"
"CG.AL.TF.MODULE_VERSION" = local.awp_module_version
}, var.awp_additional_tags != null ? var.awp_additional_tags : {})
}
# This policy provides the cross-account-role with the ability to read AWP scanner resources
resource "aws_iam_policy" "CloudGuardAWPScannersReaderPolicy" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPScannersReaderPolicy"
tags = local.common_tags
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:DescribeInstances",
"ec2:DescribeVolumes",
"ec2:DescribeRegions",
"ec2:DescribeVpcs",
"ec2:DescribeSubnets",
"ec2:DescribeRouteTables",
"ec2:DescribeSecurityGroups",
]
Resource = "*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPScannersReaderPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPScannersReaderPolicyAttachment" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPScannersReaderPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPScannersReaderPolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPCrossAccountRole.name]
}
# This policy provides the cross-account-role the permissions to read client machines resources
resource "aws_iam_policy" "CloudGuardAWPReaderPolicy" {
count = local.is_scanned_mode_condition ? 1 : 0
name = "CloudGuardAWPReaderPolicy"
tags = local.common_tags
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:DescribeInstances",
"ec2:DescribeSnapshots",
"ec2:DescribeRegions",
"ec2:DescribeVolumes"
]
Resource = "*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPReaderPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPReaderPolicyAttachment" {
count = local.is_scanned_mode_condition ? 1 : 0
name = "CloudGuardAWPReaderPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPReaderPolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPCrossAccountRole.name]
}
# This policy provides the proxy lambda the permissions to create and teardown VPC setup
resource "aws_iam_policy" "CloudGuardAWPVpcManagementPolicy" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPVpcManagementPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:CreateSubnet",
"ec2:CreateVpcEndpoint",
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:CreateSubnet",
"ec2:CreateVpcEndpoint",
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/CG_AWP_OWNER" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:CreateVpc",
"ec2:CreateSubnet",
"ec2:CreateSecurityGroup",
"ec2:CreateVpcEndpoint",
]
Resource = "*"
Condition = {
StringEquals = {
"aws:RequestTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:CreateVpc",
"ec2:CreateSubnet",
"ec2:CreateSecurityGroup",
"ec2:CreateVpcEndpoint",
]
Resource = "*"
Condition = {
StringEquals = {
"aws:RequestTag/CG_AWP_OWNER" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:DescribeVpcs",
"ec2:DescribeSubnets",
"ec2:DescribeRouteTables",
"ec2:DescribeNetworkAcls",
"ec2:DescribeSecurityGroups",
"ec2:DescribeInternetGateways",
"ec2:DescribeSecurityGroupRules",
"ec2:DescribeVpcEndpoints"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ec2:ModifySubnetAttribute",
"ec2:AssociateRouteTable",
"ec2:DeleteVpc",
"ec2:DeleteSubnet",
"ec2:DeleteVolume",
"ec2:DeleteInternetGateway",
"ec2:RevokeSecurityGroupEgress",
"ec2:RevokeSecurityGroupIngress",
"ec2:AuthorizeSecurityGroupEgress",
"ec2:DeleteSecurityGroup",
"ec2:DeleteVpcEndpoints",
"ec2:CreateNetworkAclEntry"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:ModifySubnetAttribute",
"ec2:AssociateRouteTable",
"ec2:DeleteVpc",
"ec2:DeleteSubnet",
"ec2:DeleteVolume",
"ec2:DeleteInternetGateway",
"ec2:RevokeSecurityGroupEgress",
"ec2:RevokeSecurityGroupIngress",
"ec2:AuthorizeSecurityGroupEgress",
"ec2:DeleteSecurityGroup",
"ec2:DeleteVpcEndpoints",
"ec2:CreateNetworkAclEntry"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/CG_AWP_OWNER" = "CG.AWP"
}
}
}
]
})
}
# Policy attachment for 'CloudGuardAWPReaderPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPVpcManagementPolicyAttachment" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPVpcManagementPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPVpcManagementPolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name]
}
# This policy provides the cross-account-role permissions to manage the proxy lambda
resource "aws_iam_policy" "CloudGuardAWPProxyLambdaManagementPolicy" {
count = local.is_proxy_lambda_required_condition ? 1 : 0
name = "CloudGuardAWPProxyLambdaManagementPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"lambda:InvokeFunction",
"lambda:GetFunction",
"lambda:GetLayerVersion",
"lambda:TagResource",
"lambda:ListTags",
"lambda:UntagResource",
"lambda:UpdateFunctionCode",
"lambda:UpdateFunctionConfiguration",
"lambda:GetFunctionConfiguration"
]
Resource = aws_lambda_function.CloudGuardAWPSnapshotsUtilsFunction[count.index].arn
},
{
Effect = "Allow"
Action = "s3:GetObject"
Resource = "arn:${data.aws_partition.current.partition}:s3:::${local.agentless_bucket_name}/${local.remote_functions_prefix_key}*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPProxyLambdaManagementPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPProxyLambdaManagementPolicyAttachment" {
count = local.is_proxy_lambda_required_condition ? 1 : 0
name = "CloudGuardAWPProxyLambdaManagementPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPProxyLambdaManagementPolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPCrossAccountRole.name]
}
# This policy provides the cross-account-role permissions manage securit-group in case using client's default VPC
resource "aws_iam_policy" "CloudGuardAWPSecurityGroupManagementPolicy" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPSecurityGroupManagementPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:CreateSecurityGroup"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:RequestTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:CreateSecurityGroup"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:RequestTag/CG_AWP_OWNER" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:DescribeManagedPrefixLists",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSecurityGroupRules",
"ec2:RevokeSecurityGroupEgress",
"ec2:AuthorizeSecurityGroupEgress",
"ec2:CreateTags"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ec2:DeleteSecurityGroup"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:DeleteSecurityGroup"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/CG_AWP_OWNER" = "CG.AWP"
}
}
}
]
})
}
# Policy attachment for 'CloudGuardAWPSecurityGroupManagementPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPSecurityGroupManagementPolicyAttachment" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPSecurityGroupManagementPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPSecurityGroupManagementPolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPCrossAccountRole.name]
}
# This policy provides the AWP role the permissions to handle snapshots creation for the scanned machines
resource "aws_iam_policy" "CloudGuardAWPSnapshotsPolicy" {
count = local.is_scanned_mode_condition ? 1 : 0
name = "CloudGuardAWPSnapshotsPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:CreateSnapshot"
]
Resource = "arn:${data.aws_partition.current.partition}:ec2:*:${data.aws_caller_identity.current.account_id}:volume/*"
},
{
Effect = "Allow"
Action = [
"ec2:CopySnapshot",
"ec2:CreateSnapshot"
]
Resource = "arn:${data.aws_partition.current.partition}:ec2:*::snapshot/*"
Condition = {
StringEquals = {
"aws:RequestTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:CopySnapshot",
"ec2:CreateSnapshot"
]
Resource = "arn:${data.aws_partition.current.partition}:ec2:*::snapshot/*"
Condition = {
StringEquals = {
"aws:RequestTag/CG_AWP_OWNER" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:CreateTags"
]
Resource = "arn:${data.aws_partition.current.partition}:ec2:*::snapshot/*"
Condition = {
StringEquals = {
"ec2:CreateAction" = ["CreateSnapshot", "CopySnapshot"]
}
}
},
{
Effect = "Allow"
Action = [
"ec2:DescribeRegions",
"ec2:DescribeSnapshots"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ec2:DeleteSnapshot",
"ec2:ModifySnapshotAttribute"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:DeleteSnapshot",
"ec2:ModifySnapshotAttribute"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/CG_AWP_OWNER" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:DeleteSnapshot",
"ec2:ModifySnapshotAttribute"
]
Resource = "*"
Condition = {
StringLike = {
"aws:ResourceTag/CG.AL.InstanceID" = "i-*"
}
}
}
]
})
}
# Policy attachment for 'CloudGuardAWPSnapshotsPolicy' not inAccountSub case
resource "aws_iam_policy_attachment" "CloudGuardAWPSnapshotsPolicyAttachment" {
count = local.is_scanned_mode_condition ? 1 : 0
name = "CloudGuardAWPSnapshotsPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPSnapshotsPolicy[count.index].arn
roles = local.is_in_account_sub_scan_mode_condition ? [aws_iam_role.CloudGuardAWPOperatorRole[0].name] : [aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name]
}
# This policy provides the AWP proxy lambda with the permissions to manage AWP scanners
resource "aws_iam_policy" "CloudGuardAWPScannersPolicy" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPScannersPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ec2:RunInstances"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ec2:DescribeInstances",
"ec2:DescribeVolumes",
"ec2:DescribeRegions"
]
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ec2:TerminateInstances",
"ec2:DeleteVolume"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/Owner" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"ec2:TerminateInstances",
"ec2:DeleteVolume"
]
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/CG_AWP_OWNER" = "CG.AWP"
}
}
},
{
Effect = "Allow"
Action = [
"iam:CreateServiceLinkedRole"
]
Resource = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/spot.amazonaws.com/AWSServiceRoleForEC2Spot"
}
]
})
}
# Policy attachment for 'CloudGuardAWPScannersPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPScannersPolicyAttachment" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPScannersPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPScannersPolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name]
}
# This policy provides the AWP proxy lambda with the permissions to use the AWP key in case scanner attached with a re-encrypted snapshot
resource "aws_iam_policy" "CloudGuardAWPKeyUsagePolicy" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPKeyUsagePolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"kms:CreateGrant",
"kms:GenerateDataKey*",
"kms:ReEncrypt*"
]
Resource = local.is_in_account_hub_scan_mode_condition ? aws_kms_key.CloudGuardAWPKey[count.index].arn : "*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPKeyUsagePolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPKeyUsagePolicyAttachment" {
count = local.is_scanner_mode_condition ? 1 : 0
name = "CloudGuardAWPKeyUsagePolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPKeyUsagePolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name]
}
# This policy provides the cross-account-role with the permissions to replicate the AWP key to another region
resource "aws_iam_policy" "CloudGuardAWPKeyReplicationPolicy" {
count = local.is_hosting_key_condition ? 1 : 0
name = "CloudGuardAWPKeyReplicationPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"kms:DescribeKey",
"kms:ReplicateKey",
"kms:PutKeyPolicy",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion",
"kms:TagResource",
"kms:CreateAlias",
"kms:DeleteAlias",
"kms:GetKeyPolicy"
]
Resource = "arn:${data.aws_partition.current.partition}:kms:*:${data.aws_caller_identity.current.account_id}:key/${aws_kms_key.CloudGuardAWPKey[0].id}"
},
{
Effect = "Allow"
Action = [
"kms:CreateAlias",
"kms:DeleteAlias"
]
Resource = "arn:${data.aws_partition.current.partition}:kms:*:${data.aws_caller_identity.current.account_id}:alias/CloudGuardAWPKey"
},
{
Effect = "Allow"
Action = [
"kms:CreateKey"
]
Resource = "*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPKeyReplicationPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPKeyReplicationPolicyAttachment" {
count = local.is_hosting_key_condition ? 1 : 0
name = "CloudGuardAWPKeyReplicationPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPKeyReplicationPolicy[count.index].arn
roles = [
aws_iam_role.CloudGuardAWPCrossAccountRole.name,
aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name
]
}
# This policy provides the AWP role the permissions to handle re-encrypt encrypted snapshots to the AWP key
resource "aws_iam_policy" "CloudGuardAWPReEncryptionPolicy" {
count = local.is_reencrypt_required_condition ? 1 : 0
name = "CloudGuardAWPReEncryptionPolicy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"kms:CreateGrant",
"kms:ReEncryptTo",
"kms:RevokeGrant",
"kms:DescribeKey"
]
Resource = local.is_saas_scan_mode ? aws_kms_key.CloudGuardAWPKey[count.index].arn : "arn:${data.aws_partition.current.partition}:kms:*:${local.centralized_external_account_id}:alias/CloudGuardAWPKey"
},
{
Effect = "Allow"
Action = [
"kms:DescribeKey",
"kms:CreateGrant",
"kms:ReEncryptFrom",
"kms:GenerateDataKey*"
]
Resource = "*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPReEncryptionPolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPReEncryptionPolicyAttachment" {
count = local.is_reencrypt_required_condition ? 1 : 0
name = "CloudGuardAWPReEncryptionPolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPReEncryptionPolicy[count.index].arn
roles = local.is_saas_scan_mode ? [aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name] : [aws_iam_role.CloudGuardAWPOperatorRole[0].name]
}
# The `resource "aws_iam_role" "CloudGuardAWPCrossAccountRole"` block defines an IAM role that is used to allow CloudGuard AWP to access the AWS account.
resource "aws_iam_role" "CloudGuardAWPCrossAccountRole" {
name = var.awp_cross_account_role_name
description = "Role"
# The `assume_role_policy` attribute specifies the IAM policy that determines who can assume the role.
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = {
AWS = local.cloud_guard_backend_account_id # The AWS account id of the CloudGuard backend account.
}
Action = "sts:AssumeRole"
Condition = {
StringEquals = {
"sts:ExternalId" = local.cross_account_role_external_id # The external id of the cross account role.
}
}
}]
})
tags = local.common_tags
# The `depends_on` attribute specifies that this IAM role depends on the `aws_lambda_function.CloudGuardAWPSnapshotsUtilsFunction` resource.
depends_on = [aws_lambda_function.CloudGuardAWPSnapshotsUtilsFunction]
}
# The CloudGuardAWPCrossAccountRolePolicy resource defines an IAM policy that is used to define the permissions for the CloudGuardAWPCrossAccountRole.
resource "aws_iam_policy" "CloudGuardAWPCrossAccountRolePolicy" {
count = 1
name = "CloudGuardAWPCrossAccountRolePolicy"
description = "Policy for CloudGuardAWPCrossAccountRole"
tags = local.common_tags
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"cloudformation:DescribeStacks"
]
Resource = "arn:${data.aws_partition.current.partition}:cloudformation:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stack/*"
},
{
Effect = "Allow"
Action = local.is_in_account_sub_scan_mode_condition ? ["iam:GetRole"] : ["cloudformation:DescribeStacks"]
Resource = local.is_in_account_sub_scan_mode_condition ? aws_iam_role.CloudGuardAWPOperatorRole[0].arn : "arn:${data.aws_partition.current.partition}:cloudformation:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:stack/*"
},
{
Effect = "Allow",
Action = "iam:ListRoleTags",
Resource = aws_iam_role.CloudGuardAWPCrossAccountRole.arn
}
]
})
}
# Policy attachment for 'CloudGuardAWPCrossAccountRolePolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPCrossAccountRolePolicyAttachment" {
count = 1
name = "CloudGuardAWPCrossAccountRolePolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPCrossAccountRolePolicy[0].arn
roles = [aws_iam_role.CloudGuardAWPCrossAccountRole.name]
}
# The CloudGuardAWPSnapshotsUtilsLambdaExecutionRole resource defines an IAM role that is used to allow the CloudGuardAWPSnapshotsUtilsFunction to execute.
resource "aws_iam_role" "CloudGuardAWPSnapshotsUtilsLambdaExecutionRole" {
name = "CloudGuardAWPSnapshotsUtilsLambdaExecutionRole"
description = "CloudGuard AWP proxy lambda function execution role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
tags = local.common_tags
}
# Policy for CloudGuardAWPSnapshotsUtilsLambdaExecutionRole
resource "aws_iam_policy" "CloudGuardAWPSnapshotsUtilsLambdaExecutionRolePolicy" {
count = local.is_proxy_lambda_required_condition ? 1 : 0
name = "CloudGuardAWPSnapshotsUtilsLambdaExecutionRolePolicy"
description = "Policy for managing snapshots at client side and delete AWP KMS keys"
tags = local.common_tags
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = ["${aws_cloudwatch_log_group.CloudGuardAWPSnapshotsUtilsLogGroup[count.index].arn}:*"]
},
{
Effect = "Allow"
Action = [
"ec2:CreateTags"
]
Resource = "*"
},
{
Effect = "Allow"
Action = local.is_in_account_hub_scan_mode_condition ? ["sts:AssumeRole"] : ["ec2:CreateTags"]
Resource = local.is_in_account_hub_scan_mode_condition ? "arn:${data.aws_partition.current.partition}:iam::*:role/CloudGuardAWPOperatorRole" : "*"
}
]
})
}
# Policy attachment for 'CloudGuardAWPSnapshotsUtilsLambdaExecutionRolePolicy'
resource "aws_iam_policy_attachment" "CloudGuardAWPSnapshotsUtilsLambdaExecutionRolePolicyAttachment" {
count = local.is_proxy_lambda_required_condition ? 1 : 0
name = "CloudGuardAWPSnapshotsUtilsLambdaExecutionRolePolicyAttachment"
policy_arn = aws_iam_policy.CloudGuardAWPSnapshotsUtilsLambdaExecutionRolePolicy[count.index].arn
roles = [aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.name]
}
# The CloudGuardAWPOperatorRole
resource "aws_iam_role" "CloudGuardAWPOperatorRole" {
count = local.is_in_account_sub_scan_mode_condition ? 1 : 0
name = "CloudGuardAWPOperatorRole"
description = "Role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
AWS = local.centralized_external_account_id
}
Action = "sts:AssumeRole"
}
]
})
tags = local.common_tags
}
# CloudGuardAWPSnapshotsUtilsLogGroup : The CloudWatch log group that is used to store the logs of the CloudGuardAWPSnapshotsUtilsFunction.
resource "aws_cloudwatch_log_group" "CloudGuardAWPSnapshotsUtilsLogGroup" {
count = local.is_proxy_lambda_required_condition ? 1 : 0
name = "/aws/lambda/${aws_lambda_function.CloudGuardAWPSnapshotsUtilsFunction[count.index].function_name}"
retention_in_days = 30
depends_on = [
aws_lambda_function.CloudGuardAWPSnapshotsUtilsFunction
]
tags = local.common_tags
}
# The DownloadCloudGuardAWPSnapshotsUtilsFunctionZip resource use http data source to download the remote function file from S3 pre-signed URL.
data "http" "DownloadCloudGuardAWPSnapshotsUtilsFunctionZip" {
url = local.remote_snapshots_utils_function_s3_pre_signed_url
method = "GET"
request_headers = {
Accept = "application/zip"
Accept-Ranges = "bytes"
}
}
# The CloudGuardAWPSnapshotsUtilsFunctionZip resource defines a local file that is used to store the remote function file to be used in the lambda function.
resource "local_file" "CloudGuardAWPSnapshotsUtilsFunctionZip" {
filename = "${local.remote_snapshots_utils_function_name}.zip"
content_base64 = data.http.DownloadCloudGuardAWPSnapshotsUtilsFunctionZip.response_body_base64
}
# The CloudGuardAWPSnapshotsUtilsFunction resource defines a lambda function that is used to manage remote actions and resources.
resource "aws_lambda_function" "CloudGuardAWPSnapshotsUtilsFunction" {
count = local.is_proxy_lambda_required_condition ? 1 : 0
function_name = local.remote_snapshots_utils_function_name ## TBD allow adding a prefix
handler = "snapshots_utils.lambda_handler"
description = "CloudGuard AWP Proxy for managing remote actions and resources"
role = aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.arn
runtime = "python3.9"
memory_size = 256
timeout = local.remote_snapshots_utils_function_time_out
filename = local_file.CloudGuardAWPSnapshotsUtilsFunctionZip.filename
environment {
variables = {
CP_AWP_AWS_ACCOUNT = local.cloud_guard_backend_account_id
CP_AWP_CURRENT_ACCOUNT = data.aws_caller_identity.current.account_id
CP_AWP_SCANNER_ACCOUNT = local.is_saas_scan_mode ? local.cloud_guard_backend_account_id : data.aws_caller_identity.current.account_id
CP_AWP_MR_KMS_KEY_ID = local.is_hosting_key_condition ? aws_kms_key.CloudGuardAWPKey[0].arn : ""
CP_AWP_MR_KMS_KEY_ALIAS = local.is_hosting_key_condition ? "alias/CloudGuardAWPKey" : ""
CP_AWP_SCAN_MODE = local.scan_mode
CP_AWP_SECURITY_GROUP_NAME = local.awp_client_side_security_group_name
AWS_PARTITION = data.aws_partition.current.partition
}
}
tags = merge({
Terraform = "true"
"CG.AL.TF.MODULE_VERSION" = local.awp_module_version
}, local.common_tags)
}
resource "aws_kms_key" "CloudGuardAWPKey" {
count = local.is_hosting_key_condition ? 1 : 0
description = "CloudGuard AWP Multi-Region primary key for snapshots re-encryption"
enable_key_rotation = true
deletion_window_in_days = 7
# Conditionally set multi-region based on IsChinaPartition
multi_region = data.aws_partition.current.partition == "aws-cn" ? false : true
policy = jsonencode({
Version = "2012-10-17"
Id = "cloud-guard-awp-key"
Statement = [
{
Sid = "Enable IAM User Permissions"
Effect = "Allow"
Principal = {
AWS = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"
}
Action = "kms:*"
Resource = "*"
},
{
Sid = "Allow AWP BE Management"
Effect = "Allow"
Principal = {
AWS = "arn:${data.aws_partition.current.partition}:iam::${local.cloud_guard_backend_account_id}:root"
}
Action = [
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion",
"kms:PutKeyPolicy",
"kms:DescribeKey",
"kms:GetKeyPolicy"
]
Resource = "*"
},
{
Sid = "Allow usage of the key by scanner launched from AWP BE or Proxy lambda"
Effect = "Allow"
Principal = {
AWS = local.is_saas_scan_mode ? "arn:${data.aws_partition.current.partition}:iam::${local.cloud_guard_backend_account_id}:root" : aws_iam_role.CloudGuardAWPSnapshotsUtilsLambdaExecutionRole.arn
}
Action = [
"kms:ReEncryptFrom",
"kms:GenerateDataKey*",
]
Resource = "*"
},
{
Sid = local.is_saas_scan_mode ? "Allow re-encryption to this key by AWP BE" : "Allow re encryption to this key by all Sub accounts in the organization"
Effect = "Allow"
Principal = {
AWS = local.is_saas_scan_mode ? "arn:${data.aws_partition.current.partition}:iam::${local.cloud_guard_backend_account_id}:root" : "*"
}
Action = [
"kms:DescribeKey",
"kms:ReEncryptTo",
"kms:GenerateDataKey*"
]
Resource = "*"
Condition = local.is_saas_scan_mode ? {} : { StringEquals = { "aws:PrincipalOrgId" = local.aws_organization_id } }
},
{
Sid = local.is_saas_scan_mode ? "Allow attachment of persistent resources" : "Allow attachment of persistent resources for all sub accounts in the organization"
Effect = "Allow"
Principal = {
AWS = local.is_saas_scan_mode ? "arn:${data.aws_partition.current.partition}:iam::${local.cloud_guard_backend_account_id}:root" : "*"
}
Action = [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"
]
Resource = "*"
Condition = local.is_saas_scan_mode ? { Bool = { "kms:GrantIsForAWSResource" = true } } : { StringEquals = { "aws:PrincipalOrgId" = local.aws_organization_id } }
}
]
})
tags = local.common_tags
}
# The CloudGuardAWPKeyAlias resource defines a KMS key alias that is used to reference the KMS key
resource "aws_kms_alias" "CloudGuardAWPKeyAlias" {
count = local.is_hosting_key_condition ? 1 : 0
name = "alias/CloudGuardAWPKey"
target_key_id = aws_kms_key.CloudGuardAWPKey[count.index].arn
depends_on = [
aws_kms_key.CloudGuardAWPKey
]
}
# aws_lambda_invocation : The Lambda invocation that is used to cleanup dynamic resources before teardown.
resource "aws_lambda_invocation" "CloudGuardAWPSnapshotsUtilsCleanupFunctionInvocation_saas" {
count = local.is_saas_scan_mode ? 1 : 0
function_name = aws_lambda_function.CloudGuardAWPSnapshotsUtilsFunction[count.index].function_name
input = jsonencode({