-
Notifications
You must be signed in to change notification settings - Fork 0
/
waf.tf
241 lines (193 loc) · 7 KB
/
waf.tf
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
resource "azurerm_cdn_frontdoor_firewall_policy" "waf" {
count = local.waf_application == "CDN" ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}waf"
resource_group_name = local.resource_group.name
sku_name = azurerm_cdn_frontdoor_profile.waf[0].sku_name
enabled = local.enable_waf
mode = local.waf_mode
custom_block_response_status_code = local.cdn_waf_custom_block_response_status_code != 0 ? local.cdn_waf_custom_block_response_status_code : null
custom_block_response_body = local.cdn_waf_custom_block_response_body != "" ? local.cdn_waf_custom_block_response_body : null
dynamic "custom_rule" {
for_each = local.cdn_waf_enable_rate_limiting ? [1] : []
content {
name = "RateLimiting"
enabled = true
priority = 1
rate_limit_duration_in_minutes = local.cdn_waf_rate_limiting_duration_in_minutes
rate_limit_threshold = local.cdn_waf_rate_limiting_threshold
type = "RateLimitRule"
action = local.cdn_waf_rate_limiting_action
dynamic "match_condition" {
for_each = length(local.cdn_waf_rate_limiting_bypass_ip_list) > 0 ? [1] : []
content {
match_variable = "RemoteAddr"
operator = "IPMatch"
negation_condition = true
match_values = local.cdn_waf_rate_limiting_bypass_ip_list
}
}
match_condition {
match_variable = "RequestUri"
operator = "RegEx"
negation_condition = false
match_values = ["/.*"]
}
}
}
dynamic "custom_rule" {
for_each = local.waf_custom_rules
content {
name = custom_rule.key
enabled = true
priority = custom_rule.value["priority"]
type = "MatchRule"
action = custom_rule.value["action"]
dynamic "match_condition" {
for_each = custom_rule.value["match_conditions"]
content {
match_variable = match_condition.value["match_variable"]
match_values = match_condition.value["match_values"]
operator = match_condition.value["operator"]
selector = match_condition.value["selector"]
}
}
}
}
dynamic "managed_rule" {
for_each = local.cdn_waf_managed_rulesets
content {
type = managed_rule.key
version = managed_rule.value["version"]
action = managed_rule.value["action"]
dynamic "exclusion" {
for_each = managed_rule.value["exclusions"]
content {
match_variable = exclusion.value["match_variable"]
operator = exclusion.value["operator"]
selector = exclusion.value["selector"]
}
}
dynamic "override" {
for_each = managed_rule.value["overrides"]
content {
rule_group_name = override.key
dynamic "rule" {
for_each = override.value
content {
rule_id = rule.key
enabled = true
action = rule.value["action"]
dynamic "exclusion" {
for_each = rule.value["exclusions"]
content {
match_variable = exclusion.value["match_variable"]
operator = exclusion.value["operator"]
selector = exclusion.value["selector"]
}
}
}
}
}
}
}
}
tags = local.tags
}
resource "azurerm_cdn_frontdoor_security_policy" "waf" {
count = local.waf_application == "CDN" ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}wafsecuritypolicy"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.waf[0].id
security_policies {
firewall {
cdn_frontdoor_firewall_policy_id = azurerm_cdn_frontdoor_firewall_policy.waf[0].id
association {
patterns_to_match = ["/*"]
dynamic "domain" {
for_each = local.waf_targets
content {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_endpoint.waf[domain.key].id
}
}
dynamic "domain" {
for_each = local.cdn_custom_domains
content {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_custom_domain.waf[domain.key].id
}
}
}
}
}
}
resource "azurerm_web_application_firewall_policy" "waf" {
count = local.waf_application == "AppGatewayV2" ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}waf"
resource_group_name = local.resource_group.name
location = local.resource_group.location
policy_settings {
enabled = local.enable_waf
mode = local.waf_mode
}
dynamic "custom_rules" {
for_each = local.waf_custom_rules
content {
name = custom_rules.key
priority = custom_rules.value["priority"]
rule_type = "MatchRule"
action = custom_rules.value["action"]
dynamic "match_conditions" {
for_each = custom_rules.value["match_conditions"]
content {
match_variables {
variable_name = match_conditions.value["match_variable"]
selector = match_conditions.value["selector"] == "" ? null : match_conditions.value["selector"]
}
match_values = match_conditions.value["match_values"]
operator = match_conditions.value["operator"]
}
}
}
}
managed_rules {
dynamic "exclusion" {
for_each = local.app_gateway_v2_waf_managed_rulesets_exclusions
content {
match_variable = exclusion.value.match_variable
selector = exclusion.value.selector
selector_match_operator = exclusion.value.selector_match_operator
dynamic "excluded_rule_set" {
for_each = exclusion.value.excluded_rule_set
content {
type = excluded_rule_set.key
version = excluded_rule_set.value.version
rule_group {
rule_group_name = excluded_rule_set.value.rule_group_name
excluded_rules = excluded_rule_set.value.excluded_rules
}
}
}
}
}
dynamic "managed_rule_set" {
for_each = local.app_gateway_v2_waf_managed_rulesets
content {
type = managed_rule_set.key
version = managed_rule_set.value.version
dynamic "rule_group_override" {
for_each = managed_rule_set.value.overrides
content {
rule_group_name = rule_group_override.key
dynamic "rule" {
for_each = rule_group_override.value.rules
content {
id = rule.key
enabled = rule.value.enabled
action = rule.value.action
}
}
}
}
}
}
}
tags = local.tags
}