-
Notifications
You must be signed in to change notification settings - Fork 1
/
ci_template.yml
1232 lines (1200 loc) · 43.4 KB
/
ci_template.yml
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
---
AWSTemplateFormatVersion: "2010-09-09"
Description: Nitro Enclaves Vault - CI
Parameters:
pVpcId:
Type: "AWS::EC2::VPC::Id"
Description: VPC ID
pVpcCidr:
Type: String
Description: VPC CIDR
pApiSubnetIds:
Type: "List<AWS::EC2::Subnet::Id>"
Description: Subnet IDs for Lambda function
pInstanceSubnetIds:
Type: "List<AWS::EC2::Subnet::Id>"
Description: Subnet IDs for EC2 instances
pEncryptionKeyArn:
Type: String
Description: Encryption Key ARN
pHostedZoneId:
Type: "AWS::Route53::HostedZone::Id"
Description: Hosted Zone ID
pDomainName:
Type: String
Description: Domain name for vault API
pRepositoryId:
Type: String
Description: GitHub Organization and Project to check out
pBranchName:
Type: String
Description: Git branch to check out
Default: main
pKmsCloudFormationStackName:
Type: String
Description: KMS CloudFormation Stack Name
pUseCodeBuildFleet:
Type: String
Description: "Whether to use CodeBuild Fleet or not"
AllowedValues:
- "true"
- "false"
Default: "false"
Metadata:
"AWS::CloudFormation::Interface":
ParameterGroups:
- Label:
default: VPC Configuration
Parameters:
- pVpcId
- pVpcCidr
- pApiSubnetIds
- pInstanceSubnetIds
- Label:
default: Security Configuration
Parameters:
- pEncryptionKeyArn
- Label:
default: Certificate Configuration
Parameters:
- pHostedZoneId
- pDomainName
- Label:
default: Build Configuration
Parameters:
- pRepositoryId
- pBranchName
- pUseCodeBuildFleet
- pKmsCloudFormationStackName
Conditions:
cUseCodeBuildFleet: !Equals [!Ref pUseCodeBuildFleet, "true"]
Resources:
rSigningProfile:
Type: "AWS::Signer::SigningProfile"
Properties:
PlatformId: AWSLambda-SHA384-ECDSA
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rSigningProfile
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
rSigningProfilePermission:
Type: "AWS::Signer::ProfilePermission"
Properties:
Action: "signer:StartSigningJob"
Principal: !GetAtt rCodeBuildRole.Arn
ProfileName: !GetAtt rSigningProfile.ProfileName
StatementId: AllowCodeBuild
rCodeSigningConfig:
Type: "AWS::Lambda::CodeSigningConfig"
Properties:
Description: !Sub "Code signing for ${AWS::StackName}"
AllowedPublishers:
SigningProfileVersionArns:
- !GetAtt rSigningProfile.ProfileVersionArn
CodeSigningPolicies:
UntrustedArtifactOnDeployment: Enforce
rSigningSecret:
Type: "AWS::SecretsManager::Secret"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
cfn_nag:
rules_to_suppress:
- id: W77
reason: "Ignoring KMS"
Properties:
Description: !Sub "${AWS::StackName} Enclave Signing Secret"
rSigningSecretPolicy:
Type: "AWS::SecretsManager::ResourcePolicy"
Properties:
BlockPublicPolicy: true
ResourcePolicy:
Version: "2012-10-17"
Statement:
- Sid: EnforceIdentityPerimeter
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEqualsIfExists:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
BoolIfExists:
"aws:PrincipalIsAWSService": "false"
- Sid: EnforceConfusedDeputyProtection
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEqualsIfExists:
"aws:SourceAccount": !Ref "AWS::AccountId"
"Null":
"aws:SourceAccount": "false"
Bool:
"aws:PrincipalIsAWSService": "true"
- Sid: EnforceNetworkPerimeter
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEqualsIfExists:
"aws:SourceVpc": !Ref pVpcId
"aws:PrincipalTag/dp:exclude:network": "true"
BoolIfExists:
"aws:PrincipalIsAWSService": "false"
"aws:ViaAWSService": "false"
ArnNotLikeIfExists:
"aws:PrincipalArn": !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/*"
SecretId: !Ref rSigningSecret
rMeasurementSecret:
Type: "AWS::SecretsManager::Secret"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
cfn_nag:
rules_to_suppress:
- id: W77
reason: "Ignoring KMS"
Properties:
Description: !Sub "${AWS::StackName} Enclave Measurement Secret"
rMeasurementSecretPolicy:
Type: "AWS::SecretsManager::ResourcePolicy"
Properties:
BlockPublicPolicy: true
ResourcePolicy:
Version: "2012-10-17"
Statement:
- Sid: EnforceIdentityPerimeter
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEqualsIfExists:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
BoolIfExists:
"aws:PrincipalIsAWSService": "false"
- Sid: EnforceConfusedDeputyProtection
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEqualsIfExists:
"aws:SourceAccount": !Ref "AWS::AccountId"
"Null":
"aws:SourceAccount": "false"
Bool:
"aws:PrincipalIsAWSService": "true"
- Sid: EnforceNetworkPerimeter
Effect: Deny
Principal: "*"
Action: "*"
Resource: "*"
Condition:
StringNotEqualsIfExists:
"aws:SourceVpc": !Ref pVpcId
"aws:PrincipalTag/dp:exclude:network": "true"
BoolIfExists:
"aws:PrincipalIsAWSService": "false"
"aws:ViaAWSService": "false"
ArnNotLikeIfExists:
"aws:PrincipalArn": !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/*"
SecretId: !Ref rMeasurementSecret
rArtifactBucket:
Type: "AWS::S3::Bucket"
UpdateReplacePolicy: Retain
DeletionPolicy: RetainExceptOnCreate
Metadata:
cfn_nag:
rules_to_suppress:
- id: W35
reason: "Ignoring access logging"
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- BucketKeyEnabled: true
ServerSideEncryptionByDefault:
KMSMasterKeyID: "alias/aws/s3"
SSEAlgorithm: "aws:kms"
LifecycleConfiguration:
Rules:
- ExpirationInDays: 30
Id: ExpireAfter30Days
Status: Enabled
- AbortIncompleteMultipartUpload:
DaysAfterInitiation: 1
Id: ExpireNonCurrentAndIncompleteMultipartUpload
NoncurrentVersionExpiration:
NoncurrentDays: 1
Status: Enabled
ObjectLockEnabled: false
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerEnforced
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
rArtifactBucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref rArtifactBucket
PolicyDocument:
Statement:
- Sid: DenyUnEncryptedObjectUploads
Effect: Deny
Principal: "*"
Action: "s3:PutObject"
Resource: !Sub "${rArtifactBucket.Arn}/*"
Condition:
StringNotEquals:
"s3:x-amz-server-side-encryption": "aws:kms"
BoolIfExists:
"aws:ViaAWSService": "false" # required for Lambda code signing
- Sid: DenyInsecureConnections
Effect: Deny
Principal: "*"
Action: "*"
Resource:
- !Sub "${rArtifactBucket.Arn}/*"
- !GetAtt rArtifactBucket.Arn
Condition:
Bool:
"aws:SecureTransport": false
- Sid: EnforceIdentityPerimeter
Effect: Deny
Principal: "*"
Action: "*"
Resource:
- !Sub "${rArtifactBucket.Arn}/*"
- !GetAtt rArtifactBucket.Arn
Condition:
StringNotEqualsIfExists:
"aws:PrincipalAccount": !Ref "AWS::AccountId"
BoolIfExists:
"aws:PrincipalIsAWSService": "false"
- Sid: EnforceConfusedDeputyProtection
Effect: Deny
Principal: "*"
Action: "*"
Resource:
- !Sub "${rArtifactBucket.Arn}/*"
- !GetAtt rArtifactBucket.Arn
Condition:
StringNotEqualsIfExists:
"aws:SourceAccount": !Ref "AWS::AccountId"
"Null":
"aws:SourceAccount": "false"
Bool:
"aws:PrincipalIsAWSService": "true"
- Sid: EnforceNetworkPerimeter
Effect: Deny
Principal: "*"
Action: "*"
Resource:
- !Sub "${rArtifactBucket.Arn}/*"
- !GetAtt rArtifactBucket.Arn
Condition:
StringNotEqualsIfExists:
"aws:SourceVpc": !Ref pVpcId
"aws:PrincipalTag/dp:exclude:network": "true"
BoolIfExists:
"aws:PrincipalIsAWSService": "false"
"aws:ViaAWSService": "false"
ArnNotLikeIfExists:
"aws:PrincipalArn": !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/*"
rCodeConnection:
Type: "AWS::CodeConnections::Connection"
Properties:
ConnectionName: !Sub "${AWS::StackName}-${AWS::Region}"
ProviderType: GitHub
rCodeBuildFleet:
Type: "AWS::CodeBuild::Fleet"
Condition: cUseCodeBuildFleet
Properties:
BaseCapacity: 1
ComputeType: BUILD_GENERAL1_SMALL
EnvironmentType: LINUX_CONTAINER
rPermissionsBoundary:
Type: "AWS::IAM::ManagedPolicy"
Metadata:
cfn_nag:
rules_to_suppress:
- id: F5
reason: All actions allowed in boundary policy
- id: W13
reason: All resources allowed in boundary policy
- id: W28
reason: Boundary needs defined name to self reference
Properties:
Description: !Sub "DO NOT DELETE - Used by CloudFormation. Created by CloudFormation ${AWS::StackId}"
ManagedPolicyName: !Sub "${AWS::StackName}-boundary-${AWS::Region}"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: EnforceActionsHaveBoundary
Effect: Allow
Action:
- "iam:AttachRolePolicy"
- "iam:CreateRole"
- "iam:DeleteRolePolicy"
- "iam:DetachRolePolicy"
- "iam:PutRolePermissionsBoundary"
- "iam:PutRolePolicy"
Resource: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/vault/*"
Condition:
ArnEquals:
"iam:PermissionsBoundary": !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${AWS::StackName}-boundary-${AWS::Region}" # references self
- Sid: DenyChangesToBoundaryPolicy
Effect: Deny
Action:
- "iam:DeletePolicy"
- "iam:CreatePolicyVersion"
- "iam:CreatePolicy"
- "iam:DeletePolicyVersion"
- "iam:SetDefaultPolicyVersion"
Resource: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${AWS::StackName}-boundary-${AWS::Region}" # references self
- Sid: AllowModifyIamRoles
Effect: Allow
Action:
- "iam:DeleteRole"
- "iam:GetRole"
- "iam:GetRolePolicy"
- "iam:PassRole"
- "iam:TagRole"
- "iam:UntagRole"
- "iam:UpdateAssumeRolePolicy"
- "iam:UpdateRole"
- "iam:UpdateRoleDescription"
Resource: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/vault/*"
- Effect: Allow
Action: "cloudformation:CreateChangeSet"
Resource: !Sub "arn:${AWS::Partition}:cloudformation:*:aws:transform/Serverless-2016-10-31"
- Effect: Allow
Action: "ssm:GetParameters"
Resource: !Sub "arn:${AWS::Partition}:ssm:*::parameter/aws/*"
- Effect: Allow
Action:
- "route53:ChangeResourceRecordSets"
- "route53:GetHostedZone"
- "route53:ListResourceRecordSets"
Resource: !Sub "arn:${AWS::Partition}:route53:::hostedzone/${pHostedZoneId}"
- Effect: Allow
Action:
- "kms:GenerateDataKeyPairWithoutPlaintext"
- "kms:PutKeyPolicy"
- "kms:ScheduleKeyDeletion"
Resource: !Ref pEncryptionKeyArn
- Effect: Allow
Action: "lambda:CreateFunction"
Resource: "*"
Condition:
ArnEquals:
"lambda:CodeSigningConfigArn": !GetAtt rCodeSigningConfig.CodeSigningConfigArn
- Sid: OverlyPermissiveActions
Effect: Allow
Action:
- "acm:*"
- "apigateway:*"
- "application-autoscaling:*"
- "autoscaling:*"
- "cloudformation:DescribeStackResource"
- "cloudformation:SignalResource"
- "cloudwatch:DeleteAlarms"
- "cloudwatch:DescribeAlarms"
- "cloudwatch:PutMetricAlarm"
- "cloudwatch:PutMetricData"
- "cloudwatch:TagResource"
- "cloudwatch:UntagResource"
- "dynamodb:*"
- "ec2:AssignPrivateIpAddresses"
- "ec2:AssociateEnclaveCertificateIamRole"
- "ec2:AuthorizeSecurityGroupEgress"
- "ec2:AuthorizeSecurityGroupIngress"
- "ec2:CreateLaunchTemplate"
- "ec2:CreateLaunchTemplateVersion"
- "ec2:CreateNetworkInterface"
- "ec2:CreateSecurityGroup"
- "ec2:CreateTags"
- "ec2:DeleteNetworkInterface"
- "ec2:DeleteLaunchTemplate"
- "ec2:DeleteSecurityGroup"
- "ec2:DeleteTags"
- "ec2:Describe*"
- "ec2:DescribeNetworkInterfaces"
- "ec2:DetachNetworkInterface"
- "ec2:DisassociateEnclaveCertificateIamRole"
- "ec2:Get*"
- "ec2:RevokeSecurityGroupEgress"
- "ec2:RevokeSecurityGroupIngress"
- "ec2:RunInstances"
- "ec2:UnassignPrivateIpAddresses"
- "ec2messages:*"
- "elasticloadbalancing:*"
- "iam:CreateInstanceProfile"
- "iam:AddRoleToInstanceProfile"
- "iam:DeleteInstanceProfile"
- "iam:GetInstanceProfile"
- "iam:RemoveRoleFromInstanceProfile"
- "kms:Decrypt"
- "kms:DeleteAlias"
- "kms:DescribeKey"
- "lambda:*"
- "logs:*"
- "s3:GetObject"
- "scheduler:CreateSchedule"
- "scheduler:DeleteSchedule"
- "scheduler:GetSchedule"
- "scheduler:TagResource"
- "scheduler:UntagResource"
- "sns:CreateTopic"
- "sns:DeleteTopic"
- "sns:GetTopicAttributes"
- "sns:SetTopicAttributes"
- "sns:TagResource"
- "sns:UntagResource"
- "ssm:*"
- "ssmmessages:*"
- "route53:GetChange"
- "xray:PutTraceSegments"
- "xray:PutTelemetryRecords"
- "xray:GetSamplingRules"
- "xray:GetSamplingTargets"
- "xray:GetSamplingStatisticSummaries"
Resource: "*"
rInstanceRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: !Sub "ec2.${AWS::URLSuffix}"
Action: "sts:AssumeRole"
Condition:
StringEquals:
"aws:SourceAccount": !Ref "AWS::AccountId"
Description: !Sub "DO NOT DELETE - Used by EC2. Created by CloudFormation ${AWS::StackId}"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AmazonSSMManagedInstanceCore"
- !Sub "arn:${AWS::Partition}:iam::aws:policy/CloudWatchAgentServerPolicy"
Path: "/vault/"
PermissionsBoundary: !Ref rPermissionsBoundary
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rInstanceRole
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
rCloudFormationRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "cloudformation.amazonaws.com"
Action: "sts:AssumeRole"
Description: !Sub "DO NOT DELETE - Used by CloudFormation. Created by CloudFormation ${AWS::StackId}"
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AdministratorAccess" # limited by boundary
PermissionsBoundary: !Ref rPermissionsBoundary
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rCloudFormationRole
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
- Key: "dp:exclude:network"
Value: "true"
rCodePipelineRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: !Sub "codepipeline.${AWS::URLSuffix}"
Action: "sts:AssumeRole"
Description: !Sub "DO NOT DELETE - Used by CodePipeline. Created by CloudFormation ${AWS::StackId}"
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rCodePipelineRole
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
- Key: "dp:exclude:network"
Value: "true"
rCodePipelinePolicy:
Type: "AWS::IAM::Policy"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W12
reason: "Ignoring star on ecr:GetAuthorizationToken"
- id: W76
reason: "Ignoring complex policy"
Properties:
PolicyName: CodePipelinePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
- "s3:GetObjectVersion"
- "s3:PutObject"
- "s3:PutObjectAcl"
Resource: !Sub "${rArtifactBucket.Arn}/*"
- Effect: Allow
Action:
- "codebuild:BatchGetBuilds"
- "codebuild:StartBuild"
Resource:
- !GetAtt rVaultCodeBuildProject.Arn
- !GetAtt rApiCodeBuildProject.Arn
- !GetAtt rCanaryCodeBuildProject.Arn
- Effect: Allow
Action:
- "states:DescribeStateMachine"
- "states:StartExecution"
Resource: !Ref rKeyPolicyStateMachine
- Effect: Allow
Action: "states:DescribeExecution"
Resource: !Sub "arn:${AWS::Partition}:states:${AWS::Region}:${AWS::AccountId}:execution:${rKeyPolicyStateMachine.Name}:*"
- Effect: Allow
Action: "iam:PassRole"
Resource: !GetAtt rCloudFormationRole.Arn
Condition:
StringLike:
"iam:PassedToService": !Sub "cloudformation.${AWS::URLSuffix}"
- Effect: Allow
Action: "cloudformation:ValidateTemplate"
Resource: "*"
- Effect: Allow
Action:
- "cloudformation:ContinueUpdateRollback"
- "cloudformation:CreateChangeSet"
- "cloudformation:CreateStack"
- "cloudformation:DeleteStack"
- "cloudformation:RollbackStack"
- "cloudformation:UpdateStack"
Resource:
- !Sub "arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}*"
- !Sub "arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${pKmsCloudFormationStackName}*"
Condition:
ArnEquals:
"cloudformation:RoleArn": !GetAtt rCloudFormationRole.Arn
- Effect: Allow
Action:
- "cloudformation:CancelUpdateStack"
- "cloudformation:DeleteChangeSet"
- "cloudformation:DescribeStacks"
- "cloudformation:DescribeStackEvents"
- "cloudformation:DetectStackDrift"
- "cloudformation:DetectStackResourceDrift"
- "cloudformation:ExecuteChangeSet"
- "cloudformation:SetStackPolicy"
- "cloudformation:TagResource"
- "cloudformation:UntagResource"
Resource:
- !Sub "arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}*"
- !Sub "arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${pKmsCloudFormationStackName}*"
- Effect: Allow
Action: "codestar-connections:UseConnection"
Resource: !Ref rCodeConnection
Roles:
- !Ref rCodePipelineRole
rCodePipeline:
Type: "AWS::CodePipeline::Pipeline"
Properties:
ArtifactStore:
EncryptionKey:
Id: !Sub "arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:alias/aws/s3"
Type: KMS
Location: !Ref rArtifactBucket
Type: S3
PipelineType: V2
RestartExecutionOnUpdate: true
RoleArn: !GetAtt rCodePipelineRole.Arn
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: AWS
Version: "1"
Provider: CodeStarSourceConnection
Configuration:
BranchName: !Ref pBranchName
ConnectionArn: !Ref rCodeConnection
DetectChanges: true
FullRepositoryId: !Ref pRepositoryId
OutputArtifactFormat: CODE_ZIP
OutputArtifacts:
- Name: SourceOutput
RunOrder: 1
- Name: Build
Actions:
- Name: BuildVault
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
Configuration:
ProjectName: !Ref rVaultCodeBuildProject
InputArtifacts:
- Name: SourceOutput
OutputArtifacts:
- Name: VaultBuildOutput
RunOrder: 1
- Name: BuildApi
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
Configuration:
ProjectName: !Ref rApiCodeBuildProject
InputArtifacts:
- Name: SourceOutput
OutputArtifacts:
- Name: ApiBuildOutput
RunOrder: 1
- Name: BuildCanary
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
Configuration:
ProjectName: !Ref rCanaryCodeBuildProject
InputArtifacts:
- Name: SourceOutput
OutputArtifacts:
- Name: CanaryBuildOutput
RunOrder: 1
- Name: Deploy
Actions:
- Name: DeployVault
ActionTypeId:
Category: Deploy
Owner: AWS
Version: "1"
Provider: CloudFormation
Configuration:
ActionMode: REPLACE_ON_FAILURE
Capabilities: CAPABILITY_IAM
OutputFileName: VaultStackOutput.json
ParameterOverrides: !Sub
- |-
{
"pVpcId": "${pVpcId}",
"pVpcCidr": "${pVpcCidr}",
"pSubnetIds": "${pSubnetIds}",
"pHostedZoneId": "${pHostedZoneId}",
"pDomainName": "${pDomainName}",
"pInstanceRoleName": "${rInstanceRole}",
"pArtifactBucketName": {
"Fn::GetArtifactAtt": [
"VaultBuildOutput",
"BucketName"
]
},
"pArtifactObjectKey": {
"Fn::GetArtifactAtt": [
"VaultBuildOutput",
"ObjectKey"
]
}
}
- pSubnetIds: !Join [",", !Ref pInstanceSubnetIds]
RoleArn: !GetAtt rCloudFormationRole.Arn
StackName: !Sub "${AWS::StackName}-vault"
TemplateConfiguration: "VaultBuildOutput::vault_template_configuration.json"
TemplatePath: "VaultBuildOutput::vault_template.yml"
InputArtifacts:
- Name: VaultBuildOutput
OutputArtifacts:
- Name: VaultDeployOutput
RunOrder: 1
- Name: DeployApi
ActionTypeId:
Category: Deploy
Owner: AWS
Version: "1"
Provider: CloudFormation
Configuration:
ActionMode: CREATE_UPDATE
Capabilities: CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND
OutputFileName: ApiStackOutput.json
ParameterOverrides: !Sub
- |-
{
"pVpcId": "${pVpcId}",
"pSubnetIds": "${pSubnetIds}",
"pVpcCidr": "${pVpcCidr}",
"pEncryptionKeyArn": "${pEncryptionKeyArn}",
"pDomainName": "${pDomainName}",
"pPermissionsBoundaryArn": "${rPermissionsBoundary}",
"pCodeSigningConfigArn": "${rCodeSigningConfig.CodeSigningConfigArn}"
}
- pSubnetIds: !Join [",", !Ref pApiSubnetIds]
RoleArn: !GetAtt rCloudFormationRole.Arn
StackName: !Sub "${AWS::StackName}-api"
TemplateConfiguration: "ApiBuildOutput::template_configuration.json"
TemplatePath: "ApiBuildOutput::packaged.yml"
InputArtifacts:
- Name: ApiBuildOutput
OutputArtifacts:
- Name: ApiDeployOutput
RunOrder: 1
- Name: DeployCanary
ActionTypeId:
Category: Deploy
Owner: AWS
Version: "1"
Provider: CloudFormation
Configuration:
ActionMode: CREATE_UPDATE
Capabilities: CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND
OutputFileName: CanaryStackOutput.json
ParameterOverrides: !Sub
- |-
{
"pVpcId": "${pVpcId}",
"pSubnetIds": "${pSubnetIds}",
"pVpcCidr": "${pVpcCidr}",
"pEncryptionKeyArn": "${pEncryptionKeyArn}",
"pPermissionsBoundaryArn": "${rPermissionsBoundary}",
"pCodeSigningConfigArn": "${rCodeSigningConfig.CodeSigningConfigArn}",
"pTableName": {
"Fn::GetParam": ["ApiDeployOutput", "ApiStackOutput.json", "oTableName"]
}
}
- pSubnetIds: !Join [",", !Ref pApiSubnetIds]
RoleArn: !GetAtt rCloudFormationRole.Arn
StackName: !Sub "${AWS::StackName}-canary"
TemplateConfiguration: "CanaryBuildOutput::template_configuration.json"
TemplatePath: "CanaryBuildOutput::packaged.yml"
InputArtifacts:
- Name: CanaryBuildOutput
- Name: ApiDeployOutput
OutputArtifacts:
- Name: CanaryDeployOutput
RunOrder: 2
OnFailure:
Result: ROLLBACK
- Name: UpdateKeyPolicy
Actions:
- Name: UpdateKeyPolicyRoles
ActionTypeId:
Category: Deploy
Owner: AWS
Version: "1"
Provider: CloudFormation
Configuration:
ActionMode: CREATE_UPDATE
OutputFileName: KmsStackOutput.json
ParameterOverrides: !Sub |-
{
"pVpcId": "${pVpcId}",
"pStepFunctionsRoleArn": "${rStepFunctionsRole.Arn}",
"pInstanceRoleArn": "${rInstanceRole.Arn}",
"pApiFunctionArn": {
"Fn::GetParam": ["ApiDeployOutput", "ApiStackOutput.json", "oApiFunctionArn"]
},
"pApiFunctionRoleArn": {
"Fn::GetParam": ["ApiDeployOutput", "ApiStackOutput.json", "oApiFunctionRoleArn"]
}
}
RoleArn: !GetAtt rCloudFormationRole.Arn
StackName: !Ref pKmsCloudFormationStackName
TemplateConfiguration: "SourceOutput::kms_template_configuration.json"
TemplatePath: "SourceOutput::kms_template.yml"
InputArtifacts:
- Name: SourceOutput
- Name: ApiDeployOutput
OutputArtifacts:
- Name: KmsDeployOutput
RunOrder: 1
- Name: UpdateKeyPolicyAttestations
ActionTypeId:
Category: Invoke
Owner: AWS
Version: "1"
Provider: StepFunctions
Configuration:
StateMachineArn: !Ref rKeyPolicyStateMachine
OutputArtifacts:
- Name: KeyPolicyInvokeOutput
RunOrder: 2
OnFailure:
Result: ROLLBACK
rCodeBuildRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: !Sub "codebuild.${AWS::URLSuffix}"
Action: "sts:AssumeRole"
Condition:
StringEquals:
"aws:SourceAccount": !Ref "AWS::AccountId"
ArnLike:
"aws:SourceArn": !Sub "arn:${AWS::Partition}:codebuild:${AWS::Region}:${AWS::AccountId}:project/*"
Description: !Sub "DO NOT DELETE - Used by CodeBuild. Created by CloudFormation ${AWS::StackId}"
Policies:
- PolicyName: CodeBuildPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
- "s3:GetObjectTagging"
- "s3:GetObjectVersion"
- "s3:PutObject"
- "s3:PutObjectTagging"
- "s3:PutObjectVersionTagging"
Resource: !Sub "${rArtifactBucket.Arn}/*"
- Effect: Allow
Action: "s3:ListBucket" # required for Lambda code signing
Resource: !GetAtt rArtifactBucket.Arn
- Effect: Allow
Action: "secretsmanager:GetSecretValue"
Resource: !Ref rSigningSecret
- Effect: Allow
Action: "secretsmanager:PutSecretValue"
Resource: !Ref rMeasurementSecret
- Effect: Allow
Action: "signer:StartSigningJob"
Resource: !Ref rSigningProfile
- Effect: Allow
Action: "signer:DescribeSigningJob"
Resource: !Sub "arn:${AWS::Partition}:signer:${AWS::Region}:${AWS::AccountId}:/signing-jobs/*"
Tags:
- Key: "aws-cloudformation:stack-name"
Value: !Ref "AWS::StackName"
- Key: "aws-cloudformation:stack-id"
Value: !Ref "AWS::StackId"
- Key: "aws-cloudformation:logical-id"
Value: rCodeBuildRole
- Key: AppManagerCFNStackKey
Value: !Ref "AWS::StackName"
- Key: "dp:exclude:network"
Value: "true"
rCloudWatchLogsPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: CloudWatchLogs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- !GetAtt rVaultCodeBuildLogGroup.Arn
- !GetAtt rApiCodeBuildLogGroup.Arn
- !GetAtt rCanaryCodeBuildLogGroup.Arn
Roles:
- !Ref rCodeBuildRole
rVaultCodeBuildLogGroup:
Type: "AWS::Logs::LogGroup"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
cfn_nag:
rules_to_suppress:
- id: W84
reason: "Ignoring KMS key"
Properties:
LogGroupName: !Sub "/aws/codebuild/${rVaultCodeBuildProject}"
RetentionInDays: 3
rApiCodeBuildLogGroup:
Type: "AWS::Logs::LogGroup"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
cfn_nag:
rules_to_suppress:
- id: W84
reason: "Ignoring KMS key"
Properties:
LogGroupName: !Sub "/aws/codebuild/${rApiCodeBuildProject}"
RetentionInDays: 3
rCanaryCodeBuildLogGroup:
Type: "AWS::Logs::LogGroup"
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
cfn_nag:
rules_to_suppress:
- id: W84
reason: "Ignoring KMS key"
Properties:
LogGroupName: !Sub "/aws/codebuild/${rCanaryCodeBuildProject}"
RetentionInDays: 3
rVaultCodeBuildProject:
Type: "AWS::CodeBuild::Project"
Properties:
Artifacts: