-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanti-virus-code-build.yaml
243 lines (242 loc) · 6.97 KB
/
anti-virus-code-build.yaml
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
Description: Automating continuous updates of virus signature definitions to be prepared for the latest malware threats (SO9547)
Parameters:
UploadBucketName:
Type: String
Description: The name of the Amazon S3 bucket where files are uploaded
Resources:
# repository - no dependency
clamavECRRepo:
Type: AWS::ECR::Repository
Properties:
RepositoryName:
Fn::Join:
- ""
- - "clamav-scan-file"
- Fn::Select:
- 2
- Fn::Split:
- "/"
- Ref: AWS::StackId
ImageScanningConfiguration:
ScanOnPush: true
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AVScanRepo
# codebuild role
codebuildIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Version: "2012-10-17"
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AVScanUpdateDefinitions/Role/Resource
# code build policy statement - assigned to the above created role
codebuildPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action:
- s3:GetObject
Effect: Allow
Resource:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":s3:::"
- Ref: UploadBucketName
- /scan-file.zip
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":s3:::"
- Ref: UploadBucketName
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :log-group:/aws/codebuild/
- Ref: codebuildProject
- :*
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :log-group:/aws/codebuild/
- Ref: codebuildProject
- Action:
- codebuild:BatchPutCodeCoverages
- codebuild:BatchPutTestCases
- codebuild:CreateReport
- codebuild:CreateReportGroup
- codebuild:UpdateReport
Effect: Allow
Resource:
Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":codebuild:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :report-group/
- Ref: codebuildProject
- -*
- Action: ecr:GetAuthorizationToken
Effect: Allow
Resource: "*"
- Action:
- ecr:BatchCheckLayerAvailability
- ecr:CompleteLayerUpload
- ecr:InitiateLayerUpload
- ecr:PutImage
- ecr:UploadLayerPart
Effect: Allow
Resource:
Fn::GetAtt:
- clamavECRRepo
- Arn
Version: "2012-10-17"
PolicyName: codebuildPolicy
Roles:
- Ref: codebuildIAMRole
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AVScanUpdateDefinitions/Role/DefaultPolicy/Resource
# codebuild project - depends on the role and policy above
codebuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: NO_ARTIFACTS
Environment:
ComputeType: BUILD_GENERAL1_SMALL
EnvironmentVariables:
- Name: REPO_URI
Type: PLAINTEXT
Value:
Fn::GetAtt:
- clamavECRRepo
- RepositoryUri
- Name: AWS_DEFAULT_REGION
Type: PLAINTEXT
Value:
Ref: AWS::Region
- Name: AWS_ACCOUNT_ID
Type: PLAINTEXT
Value:
Ref: AWS::AccountId
Image: aws/codebuild/standard:6.0
ImagePullCredentialsType: CODEBUILD
PrivilegedMode: true
Type: LINUX_CONTAINER
ServiceRole:
Fn::GetAtt:
- codebuildIAMRole
- Arn
Source:
Location:
Fn::Join:
- ""
- - Ref: UploadBucketName
- /scan-file.zip
Type: S3
Cache:
Type: NO_CACHE
EncryptionKey: alias/aws/s3
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AVScanUpdateDefinitions/Resource
#role for cron rule
cronEventIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: events.amazonaws.com
Version: "2012-10-17"
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AVScanUpdateDefinitions/EventsRole/Resource
#policy for cron rule role
cronEventPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action: codebuild:StartBuild
Effect: Allow
Resource:
Fn::GetAtt:
- codebuildProject
- Arn
Version: "2012-10-17"
PolicyName: cronEventPolicy
Roles:
- Ref: cronEventIAMRole
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AVScanUpdateDefinitions/EventsRole/DefaultPolicy/Resource
# Cron rule - only dependency on codebuild
cronEventRule:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: cron(0 5 * * ? *)
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- codebuildProject
- Arn
Id: Target0
RoleArn:
Fn::GetAtt:
- cronEventIAMRole
- Arn
Metadata:
aws:cdk:path: VirusScan/avcodebuild/AV-updates-rule/Resource
Outputs:
ecrRepoArn:
Value:
Fn::GetAtt:
- clamavECRRepo
- Arn
Export:
Name:
'Fn::Sub': '${AWS::StackName}-repoArn'
ecrRepoUri:
Value:
Fn::GetAtt:
- clamavECRRepo
- RepositoryUri
Export:
Name:
'Fn::Sub': '${AWS::StackName}-repoUri'
codeBuildResource:
Value:
Ref: codebuildProject
Export:
Name:
'Fn::Sub': '${AWS::StackName}-projectName'