Skip to content

Commit c17e2b5

Browse files
author
Eugene Cheung
authored
feat(wafv2): better handle contextually required region prop (#535)
1. Throw an error at build time if we're missing the prop when it's needed. 2. Automatically get the region when needed when using `monitorScope`. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent cf26388 commit c17e2b5

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

lib/facade/MonitoringAspect.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IAspect } from "aws-cdk-lib";
1+
import { IAspect, Stack } from "aws-cdk-lib";
22
import * as apigw from "aws-cdk-lib/aws-apigateway";
33
import * as apigwv2 from "aws-cdk-lib/aws-apigatewayv2";
44
import * as appsync from "aws-cdk-lib/aws-appsync";
@@ -430,8 +430,14 @@ export class MonitoringAspect implements IAspect {
430430
this.props.webApplicationFirewallAclV2,
431431
);
432432
if (isEnabled && node instanceof wafv2.CfnWebACL) {
433+
const regionProps: Record<string, string> = {};
434+
if (node.scope === "REGIONAL") {
435+
regionProps.region = Stack.of(node).region;
436+
}
437+
433438
this.monitoringFacade.monitorWebApplicationFirewallAclV2({
434439
acl: node,
440+
...regionProps,
435441
...props,
436442
});
437443
}

lib/monitoring/aws-wafv2/WafV2MetricFactory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class WafV2MetricFactory extends BaseMetricFactory<WafV2MetricFactoryProp
2828
constructor(metricFactory: MetricFactory, props: WafV2MetricFactoryProps) {
2929
super(metricFactory, props);
3030

31+
if (props.acl.scope === "REGIONAL" && !props.region) {
32+
throw new Error(`region is required if CfnWebACL has "REGIONAL" scope`);
33+
}
34+
3135
this.dimensions = {
3236
Rule: AllRulesDimensionValue,
3337
WebACL: props.acl.name,

test/facade/__snapshots__/MonitoringAspect.test.ts.snap

Lines changed: 38 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ test("snapshot test: no alarms", () => {
2727
expect(Template.fromStack(stack)).toMatchSnapshot();
2828
});
2929

30+
test("with REGIONAL ACL but no region prop, throws error", () => {
31+
const stack = new Stack();
32+
const acl = new CfnWebACL(stack, "DummyAcl", {
33+
name: "DummyAclName",
34+
defaultAction: { allow: {} },
35+
scope: "REGIONAL",
36+
visibilityConfig: {
37+
sampledRequestsEnabled: true,
38+
cloudWatchMetricsEnabled: true,
39+
metricName: "DummyMetricName",
40+
},
41+
});
42+
43+
const scope = new TestMonitoringScope(stack, "Scope");
44+
45+
expect(() => new WafV2Monitoring(scope, { acl })).toThrow(
46+
`region is required if CfnWebACL has "REGIONAL" scope`,
47+
);
48+
});
49+
3050
test("snapshot test: all alarms", () => {
3151
const stack = new Stack();
3252
const acl = new CfnWebACL(stack, "DummyAcl", {

0 commit comments

Comments
 (0)