-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcfn-security-pipeline.yml
389 lines (389 loc) · 13 KB
/
cfn-security-pipeline.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
AWSTemplateFormatVersion: 2010-09-09
Description: Deploys a sample CodePipeline to scan and deploy CFN templates
Parameters:
InitialCommitBucket:
Type: String
Description: The name of the S3 bucket containing the package for the initial commit for the DevSecOps pipeline
InitialCommitKey:
Type: String
Description: Name of the package for the initial commit for the DevSecOps pipeline DO NOT include .zip
Default: cfn-devsecops
ExampleStackName:
Type: String
Description: Name of the stack to be deployed in the final stage. Placeholder value and will likely not scale in prod
Default: SecDevOpsStack
Resources:
DevSecOpsCICDCodeCommit:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryDescription: Contains the Dockerfile and other scripts for the DevSecOps container scanning CICD pipeline
RepositoryName: cfn-devsecops
Code:
S3:
Bucket: !Ref InitialCommitBucket
Key: !Sub '${InitialCommitKey}.zip'
CodeBuildServiceRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser
Policies:
- PolicyName: CodeBuildServiceRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- codecommit:GitPull
Resource: !GetAtt DevSecOpsCICDCodeCommit.Arn
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
# these permissions are to create the stack within the pipeline
- s3:CreateBucket
- codecommit:CreateRepository
Resource: '*'
- Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectVersion
- s3:PutObject
- s3:GetBucketAcl
- s3:GetBucketLocation
Resource:
- !Sub 'arn:aws:s3:::${DevSecOpsCICDCodePipelineArtifactBucket}'
- !Sub 'arn:aws:s3:::${DevSecOpsCICDCodePipelineArtifactBucket}/*'
- Effect: Allow
Action:
- kms:Decrypt
- ssm:GetParameter
- ssm:GetParameters
- cloudformation:DetectStackSetDrift
- cloudformation:ListExports
- cloudformation:DescribeStackDriftDetectionStatus
- cloudformation:DetectStackDrift
- cloudformation:ListStackSetOperations
- cloudformation:ListStackInstances
- cloudformation:ListTypes
- cloudformation:DescribeStackResource
- cloudformation:UpdateStackSet
- cloudformation:CreateChangeSet
- cloudformation:CreateStackInstances
- cloudformation:ListTypeRegistrations
- cloudformation:ListStackSetOperationResults
- cloudformation:DetectStackResourceDrift
- cloudformation:EstimateTemplateCost
- cloudformation:DescribeStackEvents
- cloudformation:DescribeStackSetOperation
- cloudformation:UpdateStack
- cloudformation:DescribeAccountLimits
- cloudformation:DescribeChangeSet
- cloudformation:CreateStackSet
- cloudformation:ExecuteChangeSet
- cloudformation:ListStackResources
- cloudformation:SetStackPolicy
- cloudformation:ListStacks
- cloudformation:DescribeType
- cloudformation:ListImports
- cloudformation:DescribeStackInstance
- cloudformation:DescribeStackResources
- cloudformation:DeleteStackSet
- cloudformation:DescribeTypeRegistration
- cloudformation:GetTemplateSummary
- cloudformation:DescribeStacks
- cloudformation:DescribeStackResourceDrifts
- cloudformation:GetStackPolicy
- cloudformation:DescribeStackSet
- cloudformation:ListStackSets
- cloudformation:CreateStack
- cloudformation:GetTemplate
- cloudformation:DeleteStack
- cloudformation:ValidateTemplate
- cloudformation:ListChangeSets
- cloudformation:ListTypeVersions
Resource: '*'
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal: { Service: codebuild.amazonaws.com }
Action:
- sts:AssumeRole
CodePipelineServiceRole:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyName: CodePipelineServiceRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- codecommit:CancelUploadArchive
- codecommit:GetBranch
- codecommit:GetCommit
- codecommit:GetUploadArchiveStatus
- codecommit:UploadArchive
Resource: !GetAtt DevSecOpsCICDCodeCommit.Arn
- Effect: Allow
Action:
- cloudwatch:*
Resource: '*'
- Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectVersion
- s3:PutObject
- s3:GetBucketAcl
- s3:GetBucketLocation
- s3:PutBucketPolicy
- s3:ListAllMyBuckets
- s3:ListBucket
Resource:
- !Sub 'arn:aws:s3:::${DevSecOpsCICDCodePipelineArtifactBucket}'
- !Sub 'arn:aws:s3:::${DevSecOpsCICDCodePipelineArtifactBucket}/*'
- Effect: Allow
Action:
- codebuild:BatchGetBuilds
- codebuild:StartBuild
Resource:
- !GetAtt LintingStage.Arn
- !GetAtt SecretScanStage.Arn
- !GetAtt CfRipperStage.Arn
- !GetAtt CfnNagStage.Arn
- !GetAtt CheckovDeploymentStage.Arn
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal: { Service: codepipeline.amazonaws.com }
Action:
- sts:AssumeRole
DevSecOpsCICDCodePipelineArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'devsecopscicd-artifacts-${AWS::AccountId}'
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
LintingStage:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Description: Uses python-cfn-lint to lint CFN Templates - Managed by CloudFormation
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:4.0
PrivilegedMode: True
Type: LINUX_CONTAINER
LogsConfig:
CloudWatchLogs:
Status: ENABLED
Name: CFN-Linter
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
BuildSpec: buildspecs/lint-buildspec.yaml
Type: CODEPIPELINE
SecretScanStage:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Description: Uses Yelp's Detect-Secrets to look for any secrets or sensitive material - Managed by CloudFormation
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:4.0
PrivilegedMode: True
Type: LINUX_CONTAINER
LogsConfig:
CloudWatchLogs:
Status: ENABLED
Name: CFN-Secrets
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
BuildSpec: buildspecs/secrets-buildspec.yml
Type: CODEPIPELINE
CfRipperStage:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Description: Uses CF-Ripper to perfrom static security analysis of CloudFormation templates - Managed by CloudFormation
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
Image: aws/codebuild/standard:4.0
PrivilegedMode: True
Type: LINUX_CONTAINER
LogsConfig:
CloudWatchLogs:
Status: ENABLED
Name: CF-Ripper
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
BuildSpec: buildspecs/cfripper-buildspec.yml
Type: CODEPIPELINE
CfnNagStage:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Description: Uses CFN-Nag to perfrom static security analysis of CloudFormation templates - Managed by CloudFormation
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
Image: aws/codebuild/standard:4.0
PrivilegedMode: True
Type: LINUX_CONTAINER
LogsConfig:
CloudWatchLogs:
Status: ENABLED
Name: CFN-Nag
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
BuildSpec: buildspecs/cfnnag-buildspec.yml
Type: CODEPIPELINE
CheckovDeploymentStage:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Description: Uses Checkov to perform static security analysis of CloudFormation templates before creating and deploying a stack - Managed by CloudFormation
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
Image: aws/codebuild/standard:4.0
PrivilegedMode: True
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: STACK_NAME
Type: PLAINTEXT
Value: !Ref ExampleStackName
LogsConfig:
CloudWatchLogs:
Status: ENABLED
Name: CheckovDeploymentStage
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
BuildSpec: buildspecs/checkov-createstack-buildspec.yml
Type: CODEPIPELINE
DevSecOpsCICDCodePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Location: !Ref DevSecOpsCICDCodePipelineArtifactBucket
Type: S3
Name: !Sub 'DevSecOpsCICD-scan-cicd-pipeline-${AWS::AccountId}'
RestartExecutionOnUpdate: True
RoleArn: !GetAtt CodePipelineServiceRole.Arn
Stages:
-
Name: Source
Actions:
-
Name: SourceAction
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: CodeCommit
Configuration:
RepositoryName: !GetAtt DevSecOpsCICDCodeCommit.Name
BranchName: master
OutputArtifacts:
-
Name: SourceOutput
RunOrder: 1
-
Name: CFN-Linter
Actions:
-
InputArtifacts:
-
Name: SourceOutput
Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref LintingStage
PrimarySource: SourceOutput
RunOrder: 2
-
Name: CFN-Secrets
Actions:
-
InputArtifacts:
-
Name: SourceOutput
Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref SecretScanStage
PrimarySource: SourceOutput
RunOrder: 3
-
Name: CF-Ripper
Actions:
-
InputArtifacts:
-
Name: SourceOutput
Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CfRipperStage
PrimarySource: SourceOutput
RunOrder: 4
-
Name: CFN-Nag
Actions:
-
InputArtifacts:
-
Name: SourceOutput
Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CfnNagStage
PrimarySource: SourceOutput
RunOrder: 5
-
Name: CheckovDeploymentStage
Actions:
-
InputArtifacts:
-
Name: SourceOutput
Name: BuildAction
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CheckovDeploymentStage
PrimarySource: SourceOutput
RunOrder: 6