Skip to content

Commit 82128bb

Browse files
authored
fix(wafv2): don't include Region dimension for CLOUDFRONT ACLs (#683)
The behaviour "conflicts" with the later added XAXR support. We more explicitely check for the relevant ACL scope now rather than just the prescence of now ubiquitous "region" prop. Fixes #681 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_ Co-authored-by: arkon <arkon@users.noreply.github.com>
1 parent 51cb629 commit 82128bb

File tree

3 files changed

+308
-17
lines changed

3 files changed

+308
-17
lines changed

lib/monitoring/aws-wafv2/WafV2MetricFactory.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from "../../common";
99

1010
const MetricNamespace = "AWS/WAFV2";
11-
const AllRulesDimensionValue = "ALL";
1211

1312
export interface WafV2MetricFactoryProps extends BaseMetricFactoryProps {
1413
/**
@@ -31,9 +30,9 @@ export class WafV2MetricFactory extends BaseMetricFactory<WafV2MetricFactoryProp
3130
}
3231

3332
this.dimensions = {
34-
Rule: AllRulesDimensionValue,
33+
Rule: "ALL",
3534
WebACL: props.acl.name,
36-
...(props.region && { Region: props.region }),
35+
...(props.acl.scope === "REGIONAL" && { Region: props.region }),
3736
};
3837
}
3938

test/monitoring/aws-wafv2/WafV2Monitoring.test.ts

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Stack } from "aws-cdk-lib";
22
import { Template } from "aws-cdk-lib/assertions";
3+
import { Dimension } from "aws-cdk-lib/aws-cloudwatch";
34
import { CfnWebACL } from "aws-cdk-lib/aws-wafv2";
45

5-
import { WafV2Monitoring } from "../../../lib";
6+
import { AlarmWithAnnotation, WafV2Monitoring } from "../../../lib";
67
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
78
import { TestMonitoringScope } from "../TestMonitoringScope";
89

9-
test("snapshot test: no alarms", () => {
10+
test("snapshot test: alarms with REGIONAL ACL", () => {
1011
const stack = new Stack();
1112
const acl = new CfnWebACL(stack, "DummyAcl", {
1213
name: "DummyAclName",
@@ -21,9 +22,31 @@ test("snapshot test: no alarms", () => {
2122

2223
const scope = new TestMonitoringScope(stack, "Scope");
2324

24-
const monitoring = new WafV2Monitoring(scope, { acl, region: "us-east-1" });
25+
let numAlarmsCreated = 0;
26+
27+
const monitoring = new WafV2Monitoring(scope, {
28+
acl,
29+
region: "us-east-1",
30+
addBlockedRequestsCountAlarm: {
31+
Warning: {
32+
maxErrorCount: 5,
33+
},
34+
},
35+
useCreatedAlarms: {
36+
consume(alarms) {
37+
expect(getDimensions(alarms[0])).toEqual([
38+
{ name: "Region", value: "us-east-1" },
39+
{ name: "Rule", value: "ALL" },
40+
{ name: "WebACL", value: "DummyAclName" },
41+
]);
42+
43+
numAlarmsCreated = alarms.length;
44+
},
45+
},
46+
});
2547

2648
addMonitoringDashboardsToStack(stack, monitoring);
49+
expect(numAlarmsCreated).toStrictEqual(1);
2750
expect(Template.fromStack(stack)).toMatchSnapshot();
2851
});
2952

@@ -47,7 +70,44 @@ test("with REGIONAL ACL but no region prop, throws error", () => {
4770
);
4871
});
4972

50-
test("snapshot test: all alarms", () => {
73+
test("with CLOUDFRONT ACL and region prop, does not include as dimension", () => {
74+
const stack = new Stack();
75+
const acl = new CfnWebACL(stack, "DummyAcl", {
76+
name: "DummyAclName",
77+
defaultAction: { allow: {} },
78+
scope: "CLOUDFRONT",
79+
visibilityConfig: {
80+
sampledRequestsEnabled: true,
81+
cloudWatchMetricsEnabled: true,
82+
metricName: "DummyMetricName",
83+
},
84+
});
85+
86+
const scope = new TestMonitoringScope(stack, "Scope");
87+
88+
const monitoring = new WafV2Monitoring(scope, {
89+
acl,
90+
region: "us-west-2",
91+
addBlockedRequestsCountAlarm: {
92+
Warning: {
93+
maxErrorCount: 5,
94+
},
95+
},
96+
useCreatedAlarms: {
97+
consume(alarms) {
98+
expect(getDimensions(alarms[0])).toEqual([
99+
{ name: "Rule", value: "ALL" },
100+
{ name: "WebACL", value: "DummyAclName" },
101+
]);
102+
},
103+
},
104+
});
105+
106+
addMonitoringDashboardsToStack(stack, monitoring);
107+
expect(Template.fromStack(stack)).toMatchSnapshot();
108+
});
109+
110+
test("snapshot test: all alarms ", () => {
51111
const stack = new Stack();
52112
const acl = new CfnWebACL(stack, "DummyAcl", {
53113
name: "DummyAclName",
@@ -87,3 +147,7 @@ test("snapshot test: all alarms", () => {
87147
expect(numAlarmsCreated).toStrictEqual(2);
88148
expect(Template.fromStack(stack)).toMatchSnapshot();
89149
});
150+
151+
function getDimensions(alarm: AlarmWithAnnotation): Dimension[] | undefined {
152+
return alarm.alarmDefinition.metric.toMetricConfig().metricStat?.dimensions;
153+
}

0 commit comments

Comments
 (0)