-
Notifications
You must be signed in to change notification settings - Fork 1
/
applicationsecurity-events-aws-sns-cloudwatchlogs-cloudformation.json
336 lines (336 loc) · 15.2 KB
/
applicationsecurity-events-aws-sns-cloudwatchlogs-cloudformation.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "CloudFormation template for Trend Micro Cloud One Application Security Events - Amazon SNS Integration with Amazon CloudWatch Logs. For cost and billing purposes, all resources are tagged with Project=TrendMicro tags. Use AWS Resource Groups' tag filter to identify created resources.",
"Metadata": {
"AWS::CloudFormation::Interface": {
"ParameterGroups": [
{
"Label": {
"default": "[ Required section ]"
},
"Parameters": [
"C1ASCloudWatchLogRetentionPeriod",
"C1ASIntegrationExternalId"
]
}
],
"ParameterLabels": {
"C1ASCloudWatchLogRetentionPeriod": {
"default": "C1ASCloudWatchLogRetentionPeriod"
},
"C1ASIntegrationExternalId": {
"default": "C1ASIntegrationExternalId"
}
}
}
},
"Parameters": {
"C1ASCloudWatchLogRetentionPeriod":{
"Type": "Number",
"Description": "Choose a number of days to retain Application Security Events in CloudWatch Logs. Choose 9999 to Never Expire",
"AllowedValues": [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653, 9999]
},
"C1ASIntegrationExternalId": {
"Type": "String",
"Description": "You will need to fetch the External ID from your Cloud One Application Security portal. Direct link - https://cloudone.trendmicro.com/application#/integrations. The documentation for the IAM Role created as part of the Cross-Account IAM Role with External AWS Account and External ID is provided here under 'Create a Role' at https://cloudone.trendmicro.com/docs/application-security/sns-alerts/. NOTE: The IAM Role created from this CloudFormation template will be available in the Outputs section of this CloudFormation stack for review.",
"ConstraintDescription": "must be a valid External ID from Cloud One Application Security."
}
},
"Conditions": {
"IsCWLogsRetentionSetToNeverExpire": {
"Fn::Equals" : [
{
"Ref" : "C1ASCloudWatchLogRetentionPeriod"
},
"9999"
]
}
},
"Resources": {
"C1ASSnsTopic": {
"Type": "AWS::SNS::Topic",
"Properties": {
"DisplayName": "CloudOneApplicationSecuritySnsCWLogsTopic",
"TopicName": "CloudOneApplicationSecuritySnsCWLogsTopic",
"KmsMasterKeyId": {
"Fn::Join" : [
"", [
"arn:aws:kms:",
{ "Ref" : "AWS::Region" },
":",
{ "Ref" : "AWS::AccountId" },
":alias/aws/sns"
]
]
},
"Tags": [
{
"Key": "Owner",
"Value": "TrendMicro"
},
{
"Key": "Product",
"Value": "CloudOneApplicationSecurity"
},
{
"Key": "Name",
"Value": "CloudOneApplicationSecuritySnsCWLogsTopic"
}
]
}
},
"C1ASAPISnsPolicy": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "CloudOneApplicationSecurityEvent-Sns-Policy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "*"
}
]
},
"Roles": [
{
"Ref": "C1ASAPISnsRole"
}
]
}
},
"C1ASAPISnsRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": "CloudOneApplicationSecurityEvent-Sns-Role",
"Description": "Cross-account IAM role with External ID for Cloud One Application Security events integration with Amazon SNS",
"Path": "/",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::800880067056:root"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": { "Ref": "C1ASIntegrationExternalId"}
}
}
}
]
},
"Tags": [
{
"Key": "Owner",
"Value": "TrendMicro"
},
{
"Key": "Product",
"Value": "CloudOneApplicationSecurity"
},
{
"Key": "Name",
"Value": "CloudOneApplicationSecurityEvent-Sns-Role"
}
]
}
},
"C1ASEventsCWLogsGroup": {
"Type" : "AWS::Logs::LogGroup",
"Properties" : {
"LogGroupName" : "CloudOneApplicationSecurityEventsLogGroup",
"RetentionInDays" : { "Fn::If": [ "IsCWLogsRetentionSetToNeverExpire", { "Ref": "AWS::NoValue" }, { "Ref": "C1ASCloudWatchLogRetentionPeriod" }]}
}
},
"C1ASLambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Description": "Application Security Lambda IAM Role",
"Path": "/",
"RoleName": "CloudOneApplicationSecurityLambdaRole",
"Tags": [
{
"Key": "Owner",
"Value": "TrendMicro"
},
{
"Key": "Product",
"Value": "CloudOneApplicationSecurity"
},
{
"Key": "Name",
"Value": "CloudOneApplicationSecurityLambdaRole"
}
]
}
},
"C1ASCWLogsLambdaPolicy": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyName": "CloudOneApplicationSecurityEvent-CWLogs-Policy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "logs:CreateLogGroup",
"Resource": {
"Fn::Join" : [
"", [
"arn:aws:logs:",
{ "Ref" : "AWS::Region" },
":",
{ "Ref" : "AWS::AccountId" },
":*"
]
]
}
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
{
"Fn::GetAtt": [
"C1ASEventsCWLogsGroup",
"Arn"
]
}
]
},
{
"Effect": "Allow",
"Action": [
"logs:DescribeLogGroups"
],
"Resource": "*"
}
]
},
"Roles": [
{
"Ref": "C1ASLambdaRole"
}
]
}
},
"C1ASLambdaSnsCWLogsWriter": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Description": "Trend Micro Cloud One Application Security lambda function to read Application Security SIEM Events from the designated SNS topic and write to the designated CloudWatch Logs Group. Refer tags for the SNS Topic and CloudWatch Logs Group information.",
"FunctionName": "CloudOneApplicationSecurityLambdaSnsCWLogsWriter",
"Handler": "index.lambda_handler",
"Timeout": 5,
"Role": {
"Fn::GetAtt": [
"C1ASLambdaRole",
"Arn"
]
},
"Runtime": "python3.8",
"InlineCode": "import os\nimport boto3\nimport string\nimport random\nimport time\n\ncwlogs = boto3.client('logs')\ndef createLogGroup(logGroupName):\n\tcreateLogGroupResponse = cwlogs.create_log_group(\n\t\tlogGroupName=logGroupName,\n\t\ttags={\n\t\t\t\"Owner\": \"TrendMicro\",\n\t\t\t\"Product\": \"CloudOneApplicationSecurity\",\n\t\t\t\"Name\": logGroupName\n\t\t}\n\t)\n\ndef ifLogGroupExists(logGroupName):\n\tlistLogGroupsResponse = cwlogs.describe_log_groups()\n\tfor logGroup in listLogGroupsResponse[\"logGroups\"]:\n\t\tif logGroupName in logGroup[\"logGroupName\"]:\n\t\t\treturn True\n\treturn False\n\ndef createLogStream(logGroupName, logStreamName):\n\tcreateLogStreamResponse = cwlogs.create_log_stream(\n\t\tlogGroupName=logGroupName,\n\t\tlogStreamName=logStreamName\n\t)\n\tif createLogStreamResponse[\"ResponseMetadata\"][\"HTTPStatusCode\"] == 200:\n\t\treturn logStreamName\n\treturn \"\"\n\ndef putLogEvents(logGroupName, logStreamName, logEvents):\n\n\tputLogEventsResponse = cwlogs.put_log_events(\n\t\tlogGroupName=logGroupName,\n\t\tlogStreamName=logStreamName,\n\t\tlogEvents=logEvents\n\t)\n\tif \"rejectedLogEventsInfo\" not in putLogEventsResponse:\n\t\treturn True\n\treturn False\n\ndef lambda_handler(event, context):\n\n\tlogGroupName = str(os.environ.get(\"CloudWatchLogGroupName\"))\n\tlogStreamNamePrefix = str(os.environ.get(\"CloudWatchLogStreamNamePrefix\"))\n\n\tnonce = ''.join(random.choices(string.ascii_letters + string.digits, k=8)).upper()\n\n\tlogEvents = []\n\n\tepoch_time = int(round(time.time() * 1000))\n\n\ttempDict = {}\n\ttempDict.update({\"timestamp\": epoch_time, \"message\": event[\"Records\"][0][\"Sns\"][\"Message\"]})\n\n\tlogEvents.append(tempDict)\n\n\tif len(logEvents) > 0:\n\t\tif ifLogGroupExists(logGroupName):\n\t\t\tlogStreamName = createLogStream(logGroupName, logStreamNamePrefix + \"-\" + nonce)\n\t\t\tresult = putLogEvents(logGroupName, logStreamName, logEvents)\n\t\telse:\n\t\t\tcreateLogGroup(logGroupName)\n\t\t\tlogStreamName = createLogStream(logGroupName, logStreamNamePrefix + \"-\" + nonce)\n\t\t\tresult = putLogEvents(logGroupName, logStreamName, logEvents)\n",
"Environment": {
"Variables": {
"CloudWatchLogGroupName": "CloudOneApplicationSecurityEventsLogGroup",
"CloudWatchLogStreamNamePrefix": "CloudOneApplicationSecurityEventsLogStream"
}
},
"Events": {
"SnsTriggerEvent": {
"Type": "SNS",
"Properties": {
"Topic": {
"Ref": "C1ASSnsTopic"
}
}
}
},
"Tags": {
"Owner": "TrendMicro",
"Product": "CloudOneApplicationSecurity",
"Name": "CloudOneApplicationSecurityLambdaSnsCWLogsWriter",
"SnsTopicArn": {
"Ref": "C1ASSnsTopic"
},
"CloudWatchLogGroup": {
"Ref": "C1ASEventsCWLogsGroup"
}
}
}
}
},
"Outputs": {
"C1ASEventsCWLogsGroupArn": {
"Value": {
"Fn::GetAtt": [
"C1ASEventsCWLogsGroup",
"Arn"
]
},
"Description": "Bucket Name of the newly created S3 Bucket for Trend Micro Cloud One Application Security"
},
"C1ASLambdaSnsCWLogsWriter": {
"Description": "Lambda Function ARN for Trend Micro Cloud One Application Security",
"Value": {
"Fn::GetAtt": [
"C1ASLambdaSnsCWLogsWriter",
"Arn"
]
}
},
"C1ASAPISnsRole": {
"Description": "IAM Role Name of the Cross-Account Role between Trend Micro Cloud One Application Security and your AWS Account for SNS Event Integration.",
"Value": {
"Ref": "C1ASAPISnsRole"
}
},
"AWSAccountID": {
"Description": "AWS Account ID",
"Value": {
"Ref": "AWS::AccountId"
}
},
"AWSRegion": {
"Description": "AWS Region",
"Value": {
"Ref": "AWS::Region"
}
},
"C1ASSnsTopic": {
"Description": "SNS Topic Name for Trend Micro Cloud One Application Security",
"Value": {
"Fn::GetAtt": [
"C1ASSnsTopic",
"TopicName"
]
}
}
}
}