Skip to content

Commit 3d12df7

Browse files
committed
support for regex pattern sets
1 parent 9e9225c commit 3d12df7

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ rules:
8484
Limit: 1000
8585
```
8686
87-
### IPSets
87+
### IP Sets
8888
8989
to create static ip white and black lists use the following config:
9090
@@ -115,4 +115,38 @@ rules:
115115
Arn:
116116
# reference the ipset name in your config
117117
Fn::GetAtt: ['Whitelist', 'Arn']
118+
```
119+
120+
### Regex Pattern Sets
121+
122+
create the RegexPatternSet with an optional description specifying a list of regexes
123+
124+
```yaml
125+
pattern_sets:
126+
MyPattern:
127+
desc: test pattern
128+
regexes:
129+
- '^[\w\-]+$'
130+
```
131+
132+
create a rule using the RegexPatternSet
133+
134+
```yaml
135+
rules:
136+
Regex:
137+
priority: 10
138+
action:
139+
Allow: {}
140+
statement:
141+
RegexPatternSetReferenceStatement:
142+
Arn:
143+
# reference the pattern set name in your config
144+
Fn::GetAtt: ['MyPattern', 'Arn']
145+
# set the field amd transform properities acording to
146+
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-wafv2-webacl-fieldtomatch.html
147+
FieldToMatch:
148+
AllQueryArguments: {}
149+
TextTransformations:
150+
- Priority: 1
151+
Type: NONE
118152
```

tests/iplists.test.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ test_metadata:
44
description: IP white and black lists
55

66
ipsets:
7-
Whitelist:
7+
WhitelistOne:
88
desc: ips to whitelist for my waf
99
addresses:
1010
- 127.0.0.1/32
11+
WhitelistTwo:
12+
desc: ips to whitelist for my waf
13+
addresses:
14+
- 169.168.0.1/32
1115

1216
rules:
1317
IPWhitelistRule:
@@ -19,4 +23,7 @@ rules:
1923
Statements:
2024
- IPSetReferenceStatement:
2125
Arn:
22-
Fn::GetAtt: ['Whitelist', 'Arn']
26+
Fn::GetAtt: ['WhitelistOne', 'Arn']
27+
- IPSetReferenceStatement:
28+
Arn:
29+
Fn::GetAtt: ['WhitelistTwo', 'Arn']

tests/pattern_sets.test.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
test_metadata:
2+
type: config
3+
name: pattern_sets
4+
description: regular expression pattern set rules
5+
6+
pattern_sets:
7+
MyPattern:
8+
desc: test pattern
9+
regexes:
10+
- '^[\w\-]+$'
11+
12+
rules:
13+
Regex:
14+
priority: 10
15+
action:
16+
Allow: {}
17+
statement:
18+
RegexPatternSetReferenceStatement:
19+
Arn:
20+
Fn::GetAtt: ['MyPattern', 'Arn']
21+
FieldToMatch:
22+
AllQueryArguments: {}
23+
TextTransformations:
24+
- Priority: 1
25+
Type: NONE

wafv2.cfndsl.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
extra_tags = external_parameters.fetch(:extra_tags, {})
77
extra_tags.each { |key,value| tags << { Key: FnSub(key), Value: FnSub(value) } }
88

9+
ipsets = external_parameters.fetch(:ipsets, [])
910
ipsets.each do |name, properties|
1011
WAFv2_IPSet(name) {
1112
Name FnSub("${EnvironmentName}-#{name}")
@@ -17,6 +18,17 @@
1718
}
1819
end
1920

21+
pattern_sets = external_parameters.fetch(:pattern_sets, [])
22+
pattern_sets.each do |name, properties|
23+
WAFv2_RegexPatternSet(name) {
24+
Description properties['desc'] if properties.has_key?('desc')
25+
Name FnSub("${EnvironmentName}-#{name}")
26+
RegularExpressionList properties['regexes']
27+
Scope Ref(:Scope)
28+
Tags tags
29+
}
30+
end
31+
2032
waf_rules = []
2133
rules = external_parameters.fetch(:rules, {})
2234
# Loop over each rule in the confic and either override the default or add a new rule

0 commit comments

Comments
 (0)