-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwaf.cfndsl.rb
More file actions
165 lines (129 loc) · 5.39 KB
/
waf.cfndsl.rb
File metadata and controls
165 lines (129 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
CloudFormation do
Description "#{component_name} - #{component_version}"
# SQL injection match conditions
control_sqlinjectionmatchset.each do |sqlinjectionmatchset|
sqlinjectionmatchtuple_list = []
sqlinjectionmatchset["sqlinjectionmatchtuples"].each do |sqlinjectionmatchtuple|
object = {}
object[:FieldToMatch] = {}
object[:FieldToMatch][:Type] = sqlinjectionmatchtuple["field_type"]
object[:TextTransformation] = sqlinjectionmatchtuple["texttransformation"]
object[:FieldToMatch][:Data] = sqlinjectionmatchtuple["field_data"] if sqlinjectionmatchtuple.has_key?("field_data")
sqlinjectionmatchtuple_list << object
end
Resource("#{sqlinjectionmatchset["name"]}") do
Type("AWS::WAF::SqlInjectionMatchSet")
Property("Name", FnJoin("-", [ Ref('EnvironmentName'), sqlinjectionmatchset["name"]]))
Property("SqlInjectionMatchTuples", sqlinjectionmatchtuple_list )
end
end if defined? control_sqlinjectionmatchset
# Cross-site scripting match conditions
control_wafrxssset.each do |wafrxssset|
xssmatchtuple_list = []
wafrxssset["xssmatchtuples"].each do |xssmatchtuple|
object = {}
object[:FieldToMatch] = {}
object[:FieldToMatch][:Type] = xssmatchtuple["field_type"]
object[:TextTransformation] = xssmatchtuple["texttransformation"]
object[:FieldToMatch][:Data] = xssmatchtuple["field_data"] if xssmatchtuple.has_key?("field_data")
xssmatchtuple_list << object
end
Resource("#{wafrxssset["name"]}") do
Type("AWS::WAF::XssMatchSet")
Property("Name", FnJoin("-", [Ref("EnvironmentName"), wafrxssset["name"]]))
Property("XssMatchTuples", xssmatchtuple_list )
end
end if defined? control_wafrxssset
# Size constraint conditions
control_sizeconstraintset.each do |sizeconstraintset|
sizeconstraint_list = []
sizeconstraintset["sizeconstraints"].each do |sizeconstraint|
object = {}
object[:FieldToMatch] = {}
object[:FieldToMatch][:Type] = sizeconstraint["field_type"]
object[:TextTransformation] = sizeconstraint["texttransformation"]
object[:FieldToMatch][:Data] = sizeconstraint["field_data"] if sizeconstraint.has_key?("field_data")
sizeconstraint_list << object
end
Resource("#{sizeconstraintset["name"]}") do
Type("AWS::WAF::SizeConstraintSet")
Property("Name", FnJoin("-", [Ref("EnvironmentName"), sizeconstraintset["name"]]))
Property("SizeConstraints", sizeconstraint_list )
end
end if defined? control_sizeconstraintset
control_bytematchset.each do |bytematchset|
bytematchtuple_list = []
bytematchset["bytematchtuples"].each do |bytematchtuple|
object = {}
object[:FieldToMatch] = {}
object[:TextTransformation] = bytematchtuple["texttransformation"]
object[:PositionalConstraint] = bytematchtuple["positionalconstraint"]
object[:TargetString] = bytematchtuple["targetstring"]
object[:FieldToMatch][:Type] = bytematchtuple["field_type"]
object[:FieldToMatch][:Data] = bytematchtuple["field_data"] if bytematchtuple.has_key?("field_data")
bytematchtuple_list << object
end
Resource("#{bytematchset["name"]}") do
Type("AWS::WAF::ByteMatchSet")
Property("Name", FnJoin("-", [Ref("EnvironmentName"), bytematchset["name"]]))
Property("ByteMatchTuples", bytematchtuple_list )
end
end if defined? control_bytematchset
control_ipset.each do |ipset|
ipsetdescriptor_list = []
ipset["ipsetdescriptors"].each do |ipsetdescriptor|
ipsetdescriptor_list << {
Type: ipsetdescriptor["type"] || "IPV4",
Value: ipsetdescriptor["value"]
}
end
Resource("#{ipset["name"]}") do
Type("AWS::WAF::IPSet")
Property("Name", FnJoin("-", [Ref("EnvironmentName"), ipset["name"]]))
Property("IPSetDescriptors", ipsetdescriptor_list )
end
end if defined? control_ipset
## Create the Rules
wafrules.each do |wafrule|
waf_predicates = []
wafrule["predicates"].each do |predicate|
if predicate['type']=="RegexMatch"
waf_predicates << {
DataId: FnGetAtt(predicate["conditionName"],'MatchID'),
Negated: predicate["negated"],
Type: predicate["type"]
}
else
waf_predicates << {
DataId: Ref( "#{predicate["conditionName"]}" ),
Negated: predicate["negated"],
Type: predicate["type"]
}
end
end
Resource(wafrule["ruleid"]) do
Type("AWS::WAF::Rule")
Property("MetricName", FnJoin("", [Ref("EnvironmentName"), wafrule["ruleid"] ]))
Property("Name", FnJoin("-", [Ref("EnvironmentName"), wafrule["ruleid"] ]))
Property("Predicates", waf_predicates )
end
end if defined? wafrules
if defined? wafacl
waf_rules = []
wafacl['rules'].each do |rule|
waf_rules << {
Action: { Type: rule["action"] },
Priority: rule["priority"],
RuleId: Ref(rule["ruleid"])
}
end
Resource("wafrOwaspACL") do
Type("AWS::WAF::WebACL")
Property("MetricName", FnJoin("", [ Ref("EnvironmentName"), wafacl['metricName'] ]))
Property("Name", FnJoin("-", [Ref("EnvironmentName"), wafacl['name'] ]))
Property("DefaultAction", { "Type" => "ALLOW" })
Property("Rules", waf_rules)
end
Output('WAFWebACL', Ref('wafrOwaspACL'))
end
end