-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(waf): add basic WAF monitoring (#89)
Related #76 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
- Loading branch information
Showing
13 changed files
with
884 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { DimensionHash } from "monocdk/aws-cloudwatch"; | ||
import { CfnWebACL } from "monocdk/aws-wafv2"; | ||
import { MetricFactory, MetricStatistic } from "../../common"; | ||
|
||
const MetricNamespace = "AWS/WAFV2"; | ||
const AllRulesDimensionValue = "ALL"; | ||
|
||
export interface WafV2MetricFactoryProps { | ||
readonly region?: string; | ||
readonly acl: CfnWebACL; | ||
} | ||
|
||
/** | ||
* https://docs.aws.amazon.com/waf/latest/developerguide/monitoring-cloudwatch.html | ||
*/ | ||
export class WafV2MetricFactory { | ||
protected readonly metricFactory: MetricFactory; | ||
protected readonly dimensions: DimensionHash; | ||
|
||
constructor(metricFactory: MetricFactory, props: WafV2MetricFactoryProps) { | ||
this.metricFactory = metricFactory; | ||
this.dimensions = { | ||
Rule: AllRulesDimensionValue, | ||
WebACL: props.acl.name, | ||
}; | ||
} | ||
|
||
metricAllowedRequests() { | ||
return this.metricFactory.createMetric( | ||
"AllowedRequests", | ||
MetricStatistic.SUM, | ||
"Allowed", | ||
this.dimensions, | ||
undefined, | ||
MetricNamespace | ||
); | ||
} | ||
|
||
metricBlockedRequests() { | ||
return this.metricFactory.createMetric( | ||
"BlockedRequests", | ||
MetricStatistic.SUM, | ||
"Blocked", | ||
this.dimensions, | ||
undefined, | ||
MetricNamespace | ||
); | ||
} | ||
|
||
metricBlockedRequestsRate() { | ||
return this.metricFactory.createMetricMath( | ||
"100 * (blocked / (allowed + blocked))", | ||
{ | ||
allowed: this.metricAllowedRequests(), | ||
blocked: this.metricBlockedRequests(), | ||
}, | ||
"Blocked (rate)" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { GraphWidget, IWidget } from "monocdk/aws-cloudwatch"; | ||
import { | ||
BaseMonitoringProps, | ||
CountAxisFromZero, | ||
DefaultGraphWidgetHeight, | ||
DefaultSummaryWidgetHeight, | ||
MetricWithAlarmSupport, | ||
Monitoring, | ||
MonitoringScope, | ||
RateAxisFromZero, | ||
ThirdWidth, | ||
} from "../../common"; | ||
import { | ||
MonitoringHeaderWidget, | ||
MonitoringNamingStrategy, | ||
} from "../../dashboard"; | ||
import { | ||
WafV2MetricFactory, | ||
WafV2MetricFactoryProps, | ||
} from "./WafV2MetricFactory"; | ||
|
||
export interface WafV2MonitoringOptions extends BaseMonitoringProps {} | ||
|
||
export interface WafV2MonitoringProps | ||
extends WafV2MetricFactoryProps, | ||
WafV2MonitoringOptions {} | ||
|
||
/** | ||
* Monitoring for AWS Web Application Firewall. | ||
* | ||
* @see https://docs.aws.amazon.com/waf/latest/developerguide/monitoring-cloudwatch.html | ||
*/ | ||
export class WafV2Monitoring extends Monitoring { | ||
protected readonly humanReadableName: string; | ||
|
||
protected readonly allowedRequestsMetric: MetricWithAlarmSupport; | ||
protected readonly blockedRequestsMetric: MetricWithAlarmSupport; | ||
protected readonly blockedRequestsRateMetric: MetricWithAlarmSupport; | ||
|
||
constructor(scope: MonitoringScope, props: WafV2MonitoringProps) { | ||
super(scope, props); | ||
|
||
const namingStrategy = new MonitoringNamingStrategy({ | ||
...props, | ||
namedConstruct: props.acl, | ||
}); | ||
this.humanReadableName = namingStrategy.resolveHumanReadableName(); | ||
|
||
const metricFactory = new WafV2MetricFactory( | ||
scope.createMetricFactory(), | ||
props | ||
); | ||
|
||
this.allowedRequestsMetric = metricFactory.metricAllowedRequests(); | ||
this.blockedRequestsMetric = metricFactory.metricBlockedRequests(); | ||
this.blockedRequestsRateMetric = metricFactory.metricBlockedRequestsRate(); | ||
} | ||
|
||
summaryWidgets(): IWidget[] { | ||
return [ | ||
this.createTitleWidget(), | ||
this.createAllowedRequestsWidget(ThirdWidth, DefaultSummaryWidgetHeight), | ||
this.createBlockedRequestsWidget(ThirdWidth, DefaultSummaryWidgetHeight), | ||
this.createBlockedRequestsRateWidget( | ||
ThirdWidth, | ||
DefaultSummaryWidgetHeight | ||
), | ||
]; | ||
} | ||
|
||
widgets(): IWidget[] { | ||
return [ | ||
this.createTitleWidget(), | ||
this.createAllowedRequestsWidget(ThirdWidth, DefaultGraphWidgetHeight), | ||
this.createBlockedRequestsWidget(ThirdWidth, DefaultSummaryWidgetHeight), | ||
this.createBlockedRequestsRateWidget( | ||
ThirdWidth, | ||
DefaultSummaryWidgetHeight | ||
), | ||
]; | ||
} | ||
|
||
protected createTitleWidget() { | ||
return new MonitoringHeaderWidget({ | ||
family: "Web Application Firewall", | ||
title: this.humanReadableName, | ||
}); | ||
} | ||
|
||
protected createAllowedRequestsWidget(width: number, height: number) { | ||
return new GraphWidget({ | ||
width, | ||
height, | ||
title: "Allowed Requests", | ||
left: [this.allowedRequestsMetric], | ||
leftYAxis: CountAxisFromZero, | ||
}); | ||
} | ||
|
||
protected createBlockedRequestsWidget(width: number, height: number) { | ||
return new GraphWidget({ | ||
width, | ||
height, | ||
title: "Blocked Requests", | ||
left: [this.blockedRequestsMetric], | ||
leftYAxis: CountAxisFromZero, | ||
}); | ||
} | ||
|
||
protected createBlockedRequestsRateWidget(width: number, height: number) { | ||
return new GraphWidget({ | ||
width, | ||
height, | ||
title: "Blocked Requests (rate)", | ||
left: [this.blockedRequestsRateMetric], | ||
leftYAxis: RateAxisFromZero, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./WafV2MetricFactory"; | ||
export * from "./WafV2Monitoring"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.