-
Notifications
You must be signed in to change notification settings - Fork 10
/
cdn.tf
529 lines (421 loc) · 19.5 KB
/
cdn.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
resource "azurerm_cdn_frontdoor_profile" "cdn" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}cdn"
resource_group_name = local.resource_group.name
sku_name = local.cdn_frontdoor_sku
response_timeout_seconds = local.cdn_frontdoor_response_timeout
tags = local.tags
}
resource "azurerm_cdn_frontdoor_origin_group" "group" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}origingroup"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
load_balancing {}
dynamic "health_probe" {
for_each = local.enable_cdn_frontdoor_health_probe ? [0] : []
content {
protocol = local.cdn_frontdoor_health_probe_protocol
interval_in_seconds = local.cdn_frontdoor_health_probe_interval
request_type = local.cdn_frontdoor_health_probe_request_type
path = local.cdn_frontdoor_health_probe_path
}
}
}
resource "azurerm_cdn_frontdoor_origin_group" "custom_container_apps" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled
} : {}
name = "${local.resource_prefix}origingroup${replace(each.key, "-", "")}"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
load_balancing {}
dynamic "health_probe" {
for_each = each.value.ingress.enable_cdn_frontdoor_health_probe ? [0] : []
content {
protocol = each.value.ingress.cdn_frontdoor_health_probe_protocol
interval_in_seconds = each.value.ingress.cdn_frontdoor_health_probe_interval
request_type = each.value.ingress.cdn_frontdoor_health_probe_request_type
path = each.value.ingress.cdn_frontdoor_health_probe_path
}
}
}
resource "azurerm_cdn_frontdoor_origin" "origin" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}origin"
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.group[0].id
enabled = true
certificate_name_check_enabled = true
host_name = local.cdn_frontdoor_origin_fqdn_override
origin_host_header = local.cdn_frontdoor_origin_host_header_override
http_port = local.cdn_frontdoor_origin_http_port
https_port = local.cdn_frontdoor_origin_https_port
}
resource "azurerm_cdn_frontdoor_origin" "custom_container_apps" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled
} : {}
name = "${local.resource_prefix}origin${replace(each.key, "-", "")}"
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.custom_container_apps[each.key].id
enabled = true
certificate_name_check_enabled = true
host_name = each.value.ingress.cdn_frontdoor_origin_fqdn_override != "" ? each.value.ingress.cdn_frontdoor_origin_fqdn_override : azurerm_container_app.custom_container_apps[each.key].ingress[0].fqdn
origin_host_header = each.value.ingress.cdn_frontdoor_origin_host_header_override != "" ? each.value.ingress.cdn_frontdoor_origin_host_header_override : azurerm_container_app.custom_container_apps[each.key].ingress[0].fqdn
http_port = local.cdn_frontdoor_origin_http_port
https_port = local.cdn_frontdoor_origin_https_port
depends_on = [azurerm_container_app.custom_container_apps]
}
resource "azurerm_cdn_frontdoor_endpoint" "endpoint" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}cdnendpoint"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
tags = local.tags
}
resource "azurerm_cdn_frontdoor_endpoint" "custom_container_apps" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled
} : {}
name = "${local.resource_prefix}cdnendpoint-${replace(each.key, "-", "")}"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
tags = local.tags
}
resource "azurerm_cdn_frontdoor_custom_domain" "custom_domain" {
for_each = local.enable_cdn_frontdoor ? toset(local.cdn_frontdoor_custom_domains) : []
name = "${local.resource_prefix}custom-domain${index(local.cdn_frontdoor_custom_domains, each.value)}"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
dns_zone_id = local.enable_dns_zone && endswith(each.value, local.dns_zone_domain_name) ? azurerm_dns_zone.default[0].id : null
host_name = each.value
tls {
certificate_type = "ManagedCertificate"
minimum_tls_version = "TLS12"
}
}
resource "azurerm_cdn_frontdoor_custom_domain" "custom_container_apps" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled && container.ingress.cdn_frontdoor_custom_domain != ""
} : {}
name = "${local.resource_prefix}custom-domain-${each.key}"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
dns_zone_id = local.enable_dns_zone && endswith(each.value.ingress.cdn_frontdoor_custom_domain, local.dns_zone_domain_name) ? azurerm_dns_zone.default[0].id : null
host_name = each.value.ingress.cdn_frontdoor_custom_domain
tls {
certificate_type = "ManagedCertificate"
minimum_tls_version = "TLS12"
}
}
resource "azurerm_cdn_frontdoor_route" "route" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}route"
cdn_frontdoor_endpoint_id = azurerm_cdn_frontdoor_endpoint.endpoint[0].id
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.group[0].id
cdn_frontdoor_origin_ids = [azurerm_cdn_frontdoor_origin.origin[0].id]
cdn_frontdoor_rule_set_ids = local.ruleset_ids
enabled = true
forwarding_protocol = local.cdn_frontdoor_forwarding_protocol
https_redirect_enabled = true
patterns_to_match = ["/*"]
supported_protocols = ["Http", "Https"]
cdn_frontdoor_custom_domain_ids = [
for custom_domain in azurerm_cdn_frontdoor_custom_domain.custom_domain : custom_domain.id
]
link_to_default_domain = true
}
resource "azurerm_cdn_frontdoor_route" "custom_container_apps" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled
} : {}
name = "${local.resource_prefix}route-${replace(each.key, "-", "")}"
cdn_frontdoor_endpoint_id = azurerm_cdn_frontdoor_endpoint.custom_container_apps[each.key].id
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.custom_container_apps[each.key].id
cdn_frontdoor_origin_ids = [azurerm_cdn_frontdoor_origin.custom_container_apps[each.key].id]
cdn_frontdoor_rule_set_ids = local.ruleset_ids
enabled = true
forwarding_protocol = each.value.ingress.cdn_frontdoor_forwarding_protocol_override != "" ? each.value.ingress.cdn_frontdoor_forwarding_protocol_override : local.cdn_frontdoor_forwarding_protocol
https_redirect_enabled = true
patterns_to_match = ["/*"]
supported_protocols = ["Http", "Https"]
cdn_frontdoor_custom_domain_ids = each.value.ingress.cdn_frontdoor_custom_domain != "" ? [azurerm_cdn_frontdoor_custom_domain.custom_container_apps[each.key].id] : []
}
resource "azurerm_cdn_frontdoor_custom_domain_association" "custom_domain_association" {
for_each = local.enable_cdn_frontdoor ? [] : toset(local.cdn_frontdoor_custom_domains)
cdn_frontdoor_custom_domain_id = azurerm_cdn_frontdoor_custom_domain.custom_domain[each.value].id
cdn_frontdoor_route_ids = [azurerm_cdn_frontdoor_route.route[0].id]
}
resource "azurerm_cdn_frontdoor_rule_set" "redirects" {
count = local.enable_cdn_frontdoor && length(local.cdn_frontdoor_host_redirects) > 0 ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}redirects"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "redirect" {
for_each = local.enable_cdn_frontdoor ? { for index, host_redirect in local.cdn_frontdoor_host_redirects : index => { "from" : host_redirect.from, "to" : host_redirect.to } } : {}
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = "redirect${each.key}"
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.redirects[0].id
order = each.key
behavior_on_match = "Continue"
actions {
url_redirect_action {
redirect_type = "Moved"
redirect_protocol = "Https"
destination_hostname = each.value.to
}
}
conditions {
host_name_condition {
operator = "Equal"
negate_condition = false
match_values = [each.value.from]
transforms = ["Lowercase", "Trim"]
}
}
}
resource "azurerm_cdn_frontdoor_rule_set" "vdp" {
count = local.enable_cdn_frontdoor && local.enable_cdn_frontdoor_vdp_redirects ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}vdp"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "vdp_security_txt" {
count = local.enable_cdn_frontdoor && local.enable_cdn_frontdoor_vdp_redirects ? 1 : 0
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = "securitytxtredirect"
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.vdp[0].id
order = 1
behavior_on_match = "Continue"
actions {
url_redirect_action {
redirect_type = "PermanentRedirect"
redirect_protocol = "Https"
destination_hostname = local.cdn_frontdoor_vdp_destination_hostname
destination_path = "/.well-known/security.txt"
}
}
conditions {
url_filename_condition {
operator = "Equal"
match_values = ["security.txt", "/.well-known/security.txt"]
transforms = ["Lowercase", "RemoveNulls", "Trim"]
}
}
}
resource "azurerm_cdn_frontdoor_rule" "vdp_thanks_txt" {
count = local.enable_cdn_frontdoor && local.enable_cdn_frontdoor_vdp_redirects ? 1 : 0
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = "thankstxtredirect"
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.vdp[0].id
order = 2
behavior_on_match = "Continue"
actions {
url_redirect_action {
redirect_type = "PermanentRedirect"
redirect_protocol = "Https"
destination_hostname = local.cdn_frontdoor_vdp_destination_hostname
destination_path = "/thanks.txt"
}
}
conditions {
url_filename_condition {
operator = "Equal"
match_values = ["thanks.txt", "/.well-known/thanks.txt"]
transforms = ["Lowercase", "RemoveNulls", "Trim"]
}
}
}
resource "azurerm_cdn_frontdoor_firewall_policy" "waf" {
count = local.cdn_frontdoor_enable_waf ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}waf"
resource_group_name = local.resource_group.name
sku_name = azurerm_cdn_frontdoor_profile.cdn[0].sku_name
enabled = true
mode = local.cdn_frontdoor_waf_mode
custom_block_response_status_code = 403
custom_block_response_body = filebase64("${path.module}/html/waf-response.html")
dynamic "custom_rule" {
for_each = local.cdn_frontdoor_enable_rate_limiting ? [0] : []
content {
name = "RateLimiting"
enabled = true
priority = 1000
rate_limit_duration_in_minutes = local.cdn_frontdoor_rate_limiting_duration_in_minutes
rate_limit_threshold = local.cdn_frontdoor_rate_limiting_threshold
type = "RateLimitRule"
action = "Block"
dynamic "match_condition" {
for_each = length(local.cdn_frontdoor_rate_limiting_bypass_ip_list) > 0 ? [0] : []
content {
match_variable = "RemoteAddr"
operator = "IPMatch"
negation_condition = true
match_values = local.cdn_frontdoor_rate_limiting_bypass_ip_list
}
}
match_condition {
match_variable = "RequestUri"
operator = "Any"
negation_condition = false
match_values = []
}
}
}
dynamic "custom_rule" {
for_each = local.cdn_frontdoor_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_frontdoor_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 = rule.value["enabled"]
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.cdn_frontdoor_enable_waf ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}wafsecuritypolicy"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
security_policies {
firewall {
cdn_frontdoor_firewall_policy_id = azurerm_cdn_frontdoor_firewall_policy.waf[0].id
association {
patterns_to_match = ["/*"]
domain {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_endpoint.endpoint[0].id
}
dynamic "domain" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled
} : {}
content {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_endpoint.custom_container_apps[domain.key].id
}
}
dynamic "domain" {
for_each = toset(local.cdn_frontdoor_custom_domains)
content {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_custom_domain.custom_domain[domain.value].id
}
}
dynamic "domain" {
for_each = local.enable_cdn_frontdoor ? { for name, container in local.custom_container_apps : name => container
if container.ingress.external_enabled && container.ingress.cdn_frontdoor_custom_domain != ""
} : {}
content {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_custom_domain.custom_container_apps[domain.key].id
}
}
}
}
}
}
resource "azurerm_cdn_frontdoor_rule_set" "add_response_headers" {
count = local.enable_cdn_frontdoor && length(local.cdn_frontdoor_host_add_response_headers) > 0 ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}addresponseheaders"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "add_response_headers" {
for_each = local.enable_cdn_frontdoor ? { for index, response_header in local.cdn_frontdoor_host_add_response_headers : index => { "name" : response_header.name, "value" : response_header.value } } : {}
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = replace("addresponseheaders${each.key}", "-", "")
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.add_response_headers[0].id
order = each.key
behavior_on_match = "Continue"
actions {
response_header_action {
header_action = "Overwrite"
header_name = each.value.name
value = each.value.value
}
}
}
resource "azurerm_cdn_frontdoor_rule_set" "remove_response_headers" {
count = local.enable_cdn_frontdoor && length(local.cdn_frontdoor_remove_response_headers) > 0 ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}removeresponseheaders"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "remove_response_header" {
for_each = local.enable_cdn_frontdoor ? toset(local.cdn_frontdoor_remove_response_headers) : []
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = replace("removeresponseheader${each.value}", "-", "")
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.remove_response_headers[0].id
order = index(local.cdn_frontdoor_remove_response_headers, each.value)
behavior_on_match = "Continue"
actions {
response_header_action {
header_action = "Delete"
header_name = each.value
}
}
}
resource "azurerm_monitor_diagnostic_setting" "cdn" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}cdn"
target_resource_id = azurerm_cdn_frontdoor_profile.cdn[0].id
log_analytics_workspace_id = azurerm_log_analytics_workspace.container_app.id
dynamic "enabled_log" {
for_each = local.cdn_frontdoor_enable_waf_logs ? [1] : []
content {
category = "FrontdoorWebApplicationFirewallLog"
}
}
dynamic "enabled_log" {
for_each = local.cdn_frontdoor_enable_access_logs ? [1] : []
content {
category = "FrontdoorAccessLog"
}
}
dynamic "enabled_log" {
for_each = local.cdn_frontdoor_enable_health_probe_logs ? [1] : []
content {
category = "FrontdoorHealthProbeLog"
}
}
# The below metrics are kept in to avoid a diff in the Terraform Plan output
metric {
category = "AllMetrics"
enabled = false
}
}