-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitoring-stack.ts
341 lines (319 loc) · 9.79 KB
/
monitoring-stack.ts
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
import * as cloudtrail from "@aws-cdk/aws-cloudtrail";
import * as events from "@aws-cdk/aws-events";
import * as cdk from "@aws-cdk/core";
import * as sns from "@aws-cdk/aws-sns";
import * as subscriptions from "@aws-cdk/aws-sns-subscriptions";
import { ServicePrincipals } from "cdk-constants";
import * as targets from "@aws-cdk/aws-events-targets";
import { createPythonLambda } from "./common/lambda";
import * as iam from "@aws-cdk/aws-iam";
import * as cloudwatch from "@aws-cdk/aws-cloudwatch";
import * as cw_actions from "@aws-cdk/aws-cloudwatch-actions";
import { Duration } from "@aws-cdk/core";
import * as wafv2 from "@aws-cdk/aws-wafv2";
import { Config } from "../bin/infra";
export class MonitoringStack extends cdk.Stack {
constructor(
scope: cdk.Construct,
id: string,
config: Config,
props?: cdk.StackProps
) {
super(scope, id, props);
/*
* Test user
*/
const user = new iam.User(this, "testUser");
user.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName("AdministratorAccess")
);
/*
* CloudTrail
*/
const trail = new cloudtrail.Trail(this, "cloudtrail", {
sendToCloudWatchLogs: true,
});
/*
* Cloudtrail disabled IR
*/
// SNS topic
const snsTopic = new sns.Topic(this, "CloudtrailAlert", {
displayName: "Cloudtrail Alert",
});
const emailSubscription = new subscriptions.EmailSubscription(config.email);
snsTopic.addSubscription(emailSubscription);
/*
* Events for detecting that cloudtrail was turned off
* Create rule to be triggered on Cloudtrail change
*/
const eventPattern: events.EventPattern = {
source: ["aws.cloudtrail"],
detail: {
eventSource: [ServicePrincipals.CLOUD_TRAIL],
eventName: [
"StopLogging",
"StartLogging",
"DeleteTrail",
"CreateTrail",
],
},
};
const rule = new events.Rule(this, "ruleFromCDKForStoppedLogging", {
eventPattern: eventPattern,
description: "If CloudTrail logging is stopped this event will fire",
});
// Create response lambda to block AWS user
const lambdaCloudtrailLogging = createPythonLambda(
this,
"util",
"cloudtrail_restartlog"
);
lambdaCloudtrailLogging.addEnvironment("TRAIL_ARN", trail.trailArn);
lambdaCloudtrailLogging.addEnvironment("SNS_ARN", snsTopic.topicArn);
snsTopic.grantPublish(lambdaCloudtrailLogging);
// allow lambda to restart Cloudtrail
lambdaCloudtrailLogging.addToRolePolicy(
new iam.PolicyStatement({
actions: ["cloudtrail:StartLogging"],
effect: iam.Effect.ALLOW,
resources: ["*"],
})
);
// allow lambda to attach deny all policy to user
lambdaCloudtrailLogging.addToRolePolicy(
new iam.PolicyStatement({
actions: ["iam:AttachUserPolicy"],
effect: iam.Effect.ALLOW,
resources: ["*"],
conditions: {
ArnEquals: { "iam:PolicyARN": "arn:aws:iam::aws:policy/AWSDenyAll" },
},
})
);
// Set rule to trigger lambda and sns notification
rule.addTarget(new targets.LambdaFunction(lambdaCloudtrailLogging));
rule.addTarget(new targets.SnsTopic(snsTopic));
/*
* Dynamodb reading rate limits - Cloudwatch alarm for excessive read rate
* CloudWatch rulesets here
*/
const stackName = this.stackName.replace("monitoring", "infrastructure"); // to correctly reference other stack
const snsTopicCw = new sns.Topic(this, "CloudwatchAlert", {
displayName: "Cloudwatch Alert",
});
snsTopicCw.addSubscription(emailSubscription);
// Doctor table
const ddbMetric = new cloudwatch.Metric({
metricName: "ConsumedReadCapacityUnits",
namespace: "AWS/DynamoDB",
statistic: "Sum",
dimensions: {
TableName: cdk.Fn.importValue(stackName + "-DoctorTable"),
},
});
const ddbExcessReadAlarmDoc = new cloudwatch.Alarm(
this,
"ddbExcessReadAlarmDoc",
{
metric: ddbMetric,
threshold: 1200,
period: Duration.seconds(60),
evaluationPeriods: 1,
datapointsToAlarm: 1,
}
);
// Patients table
const ddbMetricPat = new cloudwatch.Metric({
metricName: "ConsumedReadCapacityUnits",
namespace: "AWS/DynamoDB",
statistic: "Sum",
dimensions: {
TableName: cdk.Fn.importValue(stackName + "-PatientTable"),
},
});
const ddbExcessReadAlarmPat = new cloudwatch.Alarm(
this,
"ddbExcessReadAlarmPat",
{
metric: ddbMetricPat,
threshold: 1200,
period: Duration.seconds(60),
evaluationPeriods: 1,
datapointsToAlarm: 1,
}
);
// binding sns topic to cloudwatch alarm
ddbExcessReadAlarmDoc.addAlarmAction(new cw_actions.SnsAction(snsTopicCw));
ddbExcessReadAlarmPat.addAlarmAction(new cw_actions.SnsAction(snsTopicCw));
//Front-end IR and Infrastructure
/*
* WAF
* only allow 128 requests per 5 minutes, a pretty generous limit for a small practice
* assuming that a clinic is located at the same ip address
*/
const wafAPI = new wafv2.CfnWebACL(this, "waf-api", {
defaultAction: {
allow: {},
},
visibilityConfig: {
cloudWatchMetricsEnabled: true,
metricName: "waf-apigateway",
sampledRequestsEnabled: true,
},
scope: "REGIONAL",
rules: [
{
name: "AWS-AWSManagedRulesAmazonIpReputationList",
priority: 0,
statement: {
managedRuleGroupStatement: {
vendorName: "AWS",
name: "AWSManagedRulesAmazonIpReputationList",
},
},
overrideAction: {
none: {},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "waf-apigateway-ipreputation",
},
},
{
name: "AWS-AWSManagedRulesCommonRuleSet",
priority: 1,
statement: {
managedRuleGroupStatement: {
vendorName: "AWS",
name: "AWSManagedRulesCommonRuleSet",
},
},
overrideAction: {
none: {},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "waf-apigateway-commonattacks",
},
},
{
name: "ratelimit",
priority: 2,
statement: {
rateBasedStatement: {
limit: 128,
aggregateKeyType: "IP",
},
},
visibilityConfig: {
cloudWatchMetricsEnabled: true,
metricName: "waf-apigateway-ratelimit",
sampledRequestsEnabled: true,
},
action: {
block: {},
},
},
],
});
new wafv2.CfnWebACLAssociation(this, "waf-assoc-apigateway", {
resourceArn: `arn:aws:apigateway:${
this.region
}::/restapis/${cdk.Fn.importValue(
stackName + "-APIGateway"
)}/stages/prod`,
webAclArn: wafAPI.attrArn,
});
// grab cloudwatch metric and crate alarm
const wafMetric = new cloudwatch.Metric({
metricName: "BlockedRequests",
namespace: "AWS/WAFV2",
statistic: "Sum",
dimensions: {
Rule: "waf-apigateway-ratelimit",
WebACL: cdk.Fn.select(
0,
cdk.Fn.split("|", cdk.Fn.ref(wafAPI.logicalId))
),
Region: "ap-southeast-2",
},
});
const wafAlarm = new cloudwatch.Alarm(
this,
"waf-apigateway-ratelimit-breached",
{
metric: wafMetric,
threshold: 128,
period: Duration.seconds(60),
evaluationPeriods: 1,
datapointsToAlarm: 1,
}
);
const snsTopicWAF = new sns.Topic(this, "WAFAlert", {
displayName: "WAF Alert",
});
snsTopicWAF.addSubscription(emailSubscription);
wafAlarm.addAlarmAction(new cw_actions.SnsAction(snsTopicWAF));
/*
* Front-end IR and Infrastructure
* Detecting S3 bucket tampering
*/
trail.addS3EventSelector(
[
cdk.Fn.join("", [
"arn:aws:s3:::",
cdk.Fn.importValue("doqutore-frontend-S3Bucket"),
"/",
]),
],
{
readWriteType: cloudtrail.ReadWriteType.WRITE_ONLY,
}
);
// Create rule to fire event on modification of S3 bucket
const frontendPattern: events.EventPattern = {
source: ["aws.s3"],
detail: {
eventSource: [ServicePrincipals.S3],
eventName: ["DeleteObject", "PutObject"],
requestParameters: {
bucketName: [
cdk.Fn.importValue(config.prefix + "-frontend-S3Bucket"),
],
},
},
};
const frontendRule = new events.Rule(this, "s3Modified", {
eventPattern: frontendPattern,
description:
"If the frontend S3 bucket is modified then this event will fire",
});
// SNS topic for event notifications
const frontendSnsTopic = new sns.Topic(this, "FrontendAlert", {
displayName: "Frontend Alert",
});
frontendSnsTopic.addSubscription(emailSubscription);
// Create lambda to respond by redeploying
const lambdaFrontendCloudtrailLogging = createPythonLambda(
this,
"util",
"cloudtrail_retrigger_pipeline",
1
);
lambdaFrontendCloudtrailLogging.addEnvironment(
"SNS_ARN",
frontendSnsTopic.topicArn
);
lambdaFrontendCloudtrailLogging.addEnvironment(
"GITHUB_KEY",
config.githubKey
);
frontendSnsTopic.grantPublish(lambdaFrontendCloudtrailLogging);
// Set rule to trigger lambda
frontendRule.addTarget(
new targets.LambdaFunction(lambdaFrontendCloudtrailLogging)
);
}
}