Skip to content

Commit e99c51a

Browse files
fix : support overrideAction for WAF (#582)
1 parent 0832428 commit e99c51a

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

doc/WAF.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ waf:
119119
- US
120120
```
121121

122+
```yml
123+
waf:
124+
enabled: true
125+
defaultAction: Block
126+
rules:
127+
# using ManagedRuleGroup
128+
- name: "AWSManagedRulesCommonRuleSet"
129+
priority: 20
130+
overrideAction:
131+
None: {}
132+
statement:
133+
ManagedRuleGroupStatement:
134+
VendorName: "AWS"
135+
Name: "AWSManagedRulesCommonRuleSet"
136+
```
137+
122138
### Per API Key rules
123139

124140
In some cases, you might want to enable a rule for a given API key only. You can specify `wafRules` under the `appSync.apiKeys` attribute. The rules will apply only to that API key.

src/__tests__/__snapshots__/waf.test.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,27 @@ Object {
411411
}
412412
`;
413413

414+
exports[`Waf Custom rules should generate a custom rule with ManagedRuleGroup 1`] = `
415+
Object {
416+
"Name": "MyRule1",
417+
"OverrideAction": Object {
418+
"None": Object {},
419+
},
420+
"Priority": 200,
421+
"Statement": Object {
422+
"ManagedRuleGroupStatement": Object {
423+
"Name": "AWSManagedRulesCommonRuleSet",
424+
"VendorName": "AWS",
425+
},
426+
},
427+
"VisibilityConfig": Object {
428+
"CloudWatchMetricsEnabled": true,
429+
"MetricName": "MyRule1",
430+
"SampledRequestsEnabled": true,
431+
},
432+
}
433+
`;
434+
414435
exports[`Waf Disable introspection should generate a preset rule 1`] = `
415436
Object {
416437
"Action": Object {

src/__tests__/waf.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,27 @@ describe('Waf', () => {
168168
),
169169
).toMatchSnapshot();
170170
});
171+
172+
it('should generate a custom rule with ManagedRuleGroup', () => {
173+
expect(
174+
waf.buildWafRule(
175+
{
176+
name: 'MyRule1',
177+
priority: 200,
178+
overrideAction: {
179+
None: {},
180+
},
181+
statement: {
182+
ManagedRuleGroupStatement: {
183+
Name: 'AWSManagedRulesCommonRuleSet',
184+
VendorName: 'AWS',
185+
},
186+
},
187+
},
188+
'Base',
189+
),
190+
).toMatchSnapshot();
191+
});
171192
});
172193

173194
describe('ApiKey rules', () => {

src/resources/Waf.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
WafThrottleConfig,
1515
} from '../types/plugin';
1616
import { Api } from './Api';
17+
import { toCfnKeys } from '../utils';
1718

1819
export class Waf {
1920
constructor(private api: Api, private config: WafConfig) {}
@@ -106,10 +107,10 @@ export class Waf {
106107
}
107108

108109
const action: WafRuleAction = rule.action || 'Allow';
110+
const overrideAction = rule.overrideAction;
109111

110112
const result: CfnWafRule = {
111113
Name: rule.name,
112-
Action: { [action]: {} },
113114
Priority: rule.priority,
114115
Statement: rule.statement,
115116
VisibilityConfig: this.getWafVisibilityConfig(
@@ -118,6 +119,12 @@ export class Waf {
118119
),
119120
};
120121

122+
if (overrideAction) {
123+
result.OverrideAction = toCfnKeys(overrideAction);
124+
} else {
125+
result.Action = { [action]: {} };
126+
}
127+
121128
return result;
122129
}
123130

0 commit comments

Comments
 (0)