-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.tf
346 lines (303 loc) · 12.5 KB
/
main.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
#---------------------------------
# Local declarations
#---------------------------------
locals {
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
public_ip_map = { for pip in var.public_ip_names : pip => true }
fw_nat_rules = { for idx, rule in var.firewall_nat_rules : rule.name => {
idx : idx,
rule : rule,
}
}
fw_network_rules = { for idx, rule in var.firewall_network_rules : rule.name => {
idx : idx,
rule : rule,
}
}
fw_application_rules = { for idx, rule in var.firewall_application_rules : rule.name => {
idx : idx,
rule : rule,
}
}
}
#---------------------------------------------------------
# Resource Group Creation or selection - Default is "true"
#----------------------------------------------------------
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group == false ? 1 : 0
name = var.resource_group_name
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = lower(var.resource_group_name)
location = var.location
tags = merge({ "ResourceName" = format("%s", var.resource_group_name) }, var.tags, )
}
data "azurerm_storage_account" "storeacc" {
count = var.storage_account_name != null ? 1 : 0
name = var.storage_account_name
resource_group_name = local.resource_group_name
}
#---------------------------------------------------------
# Firewall Subnet Creation or selection
#----------------------------------------------------------
resource "azurerm_subnet" "fw-snet" {
name = "AzureFirewallSubnet"
resource_group_name = local.resource_group_name
virtual_network_name = var.virtual_network_name
address_prefixes = var.firewall_subnet_address_prefix #[cidrsubnet(element(var.vnet_address_space, 0), 10, 0)]
service_endpoints = var.firewall_service_endpoints
}
#---------------------------------------------------------
# Firewall Managemnet Subnet Creation
#----------------------------------------------------------
resource "azurerm_subnet" "fw-mgnt-snet" {
count = var.enable_forced_tunneling ? 1 : 0
name = "AzureFirewallManagementSubnet"
resource_group_name = local.resource_group_name
virtual_network_name = var.virtual_network_name
address_prefixes = var.firewall_management_subnet_address_prefix #[cidrsubnet(element(var.vnet_address_space, 0), 10, 0)]
}
#------------------------------------------
# Public IP resources for Azure Firewall
#------------------------------------------
resource "azurerm_public_ip_prefix" "fw-pref" {
name = lower("${var.firewall_config.name}-pip-prefix")
resource_group_name = local.resource_group_name
location = local.location
prefix_length = var.public_ip_prefix_length
tags = merge({ "ResourceName" = lower("${var.firewall_config.name}-pip-prefix") }, var.tags, )
}
resource "azurerm_public_ip" "fw-pip" {
for_each = local.public_ip_map
name = lower("pip-${var.firewall_config.name}-${each.key}")
location = local.location
resource_group_name = local.resource_group_name
allocation_method = "Static"
sku = "Standard"
public_ip_prefix_id = azurerm_public_ip_prefix.fw-pref.id
tags = merge({ "ResourceName" = lower("pip-${var.firewall_config.name}-${each.key}") }, var.tags, )
}
resource "azurerm_public_ip" "fw-mgnt-pip" {
count = var.enable_forced_tunneling ? 1 : 0
name = lower("pip-${var.firewall_config.name}-fw-mgnt")
location = local.location
resource_group_name = local.resource_group_name
allocation_method = "Static"
sku = "Standard"
tags = merge({ "ResourceName" = lower("pip-${var.firewall_config.name}-fw-mgnt") }, var.tags, )
}
#-----------------
# Azure Firewall
#-----------------
resource "azurerm_firewall" "fw" {
name = format("%s", var.firewall_config.name)
resource_group_name = local.resource_group_name
location = local.location
sku_name = var.firewall_config.sku_name
sku_tier = var.firewall_config.sku_tier
# firewall_policy_id = var.firewall_policy != null ? azurerm_firewall_policy.fw-policy.0.id : null
dns_servers = var.firewall_config.dns_servers
private_ip_ranges = var.firewall_config.private_ip_ranges
threat_intel_mode = lookup(var.firewall_config, "threat_intel_mode", "Alert")
zones = var.firewall_config.zones
tags = merge({ "ResourceName" = format("%s", var.firewall_config.name) }, var.tags, )
dynamic "ip_configuration" {
for_each = local.public_ip_map
iterator = ip
content {
name = ip.key
subnet_id = ip.key == var.public_ip_names[0] ? azurerm_subnet.fw-snet.id : null
public_ip_address_id = azurerm_public_ip.fw-pip[ip.key].id
}
}
dynamic "management_ip_configuration" {
for_each = var.enable_forced_tunneling ? [1] : []
content {
name = lower("${var.firewall_config.name}-forced-tunnel")
subnet_id = azurerm_subnet.fw-mgnt-snet.0.id
public_ip_address_id = azurerm_public_ip.fw-mgnt-pip.0.id
}
}
dynamic "virtual_hub" {
for_each = var.virtual_hub != null ? [var.virtual_hub] : []
content {
virtual_hub_id = virtual_hub.value.virtual_hub_id
public_ip_count = virtual_hub.value.public_ip_count
}
}
}
#----------------------------------------------
# Azure Firewall Network/Application/NAT Rules
#----------------------------------------------
resource "azurerm_firewall_application_rule_collection" "fw_app" {
for_each = local.fw_application_rules
name = lower(format("fw-app-rule-%s-${var.firewall_config.name}-${local.location}", each.key))
azure_firewall_name = azurerm_firewall.fw.name
resource_group_name = local.resource_group_name
priority = 100 * (each.value.idx + 1)
action = each.value.rule.action
rule {
name = each.key
description = each.value.rule.description
source_addresses = each.value.rule.source_addresses
source_ip_groups = each.value.rule.source_ip_groups
fqdn_tags = each.value.rule.fqdn_tags
target_fqdns = each.value.rule.target_fqdns
protocol {
type = each.value.rule.protocol.type
port = each.value.rule.protocol.port
}
}
}
resource "azurerm_firewall_network_rule_collection" "fw" {
for_each = local.fw_network_rules
name = lower(format("fw-net-rule-%s-${var.firewall_config.name}-${local.location}", each.key))
azure_firewall_name = azurerm_firewall.fw.name
resource_group_name = local.resource_group_name
priority = 100 * (each.value.idx + 1)
action = each.value.rule.action
rule {
name = each.key
description = each.value.rule.description
source_addresses = each.value.rule.source_addresses
destination_ports = each.value.rule.destination_ports
destination_addresses = [for dest in each.value.rule.destination_addresses : contains(var.public_ip_names, dest) ? azurerm_public_ip.fw-pip[dest].ip_address : dest]
destination_fqdns = each.value.rule.destination_fqdns
protocols = each.value.rule.protocols
}
}
resource "azurerm_firewall_nat_rule_collection" "fw" {
for_each = local.fw_nat_rules
name = lower(format("fw-nat-rule-%s-${var.firewall_config.name}-${local.location}", each.key))
azure_firewall_name = azurerm_firewall.fw.name
resource_group_name = local.resource_group_name
priority = 100 * (each.value.idx + 1)
action = each.value.rule.action
rule {
name = each.key
description = each.value.rule.description
source_addresses = each.value.rule.source_addresses
destination_ports = each.value.rule.destination_ports
destination_addresses = [for dest in each.value.rule.destination_addresses : contains(var.public_ip_names, dest) ? azurerm_public_ip.fw-pip[dest].ip_address : dest]
protocols = each.value.rule.protocols
translated_address = each.value.rule.translated_address
translated_port = each.value.rule.translated_port
}
}
/*
#---------------------------------------------------------------
# Azure Firewall Policy
#---------------------------------------------------------------
resource "azurerm_firewall_policy" "fw-policy" {
count = var.firewall_policy != null ? 1 : 0
name = lower(format("fw-policy-%s", var.firewall_config.name))
resource_group_name = local.resource_group_name
location = local.location
sku = var.firewall_policy.sku
base_policy_id = var.firewall_policy.base_policy_id
threat_intelligence_mode = lookup(var.firewall_policy, "threat_intelligence_mode", "Alert")
dynamic "dns" {
for_each = var.firewall_policy.dns != null ? [var.firewall_policy.dns] : []
content {
servers = dns.value.servers
proxy_enabled = dns.value.proxy_enabled
}
}
dynamic "threat_intelligence_allowlist" {
for_each = var.firewall_policy.threat_intelligence_allowlist != null ? [var.firewall_policy.threat_intelligence_allowlist] : []
content {
ip_addresses = threat_intelligence_allowlist.value.ip_addresses
fqdns = threat_intelligence_allowlist.value.fqdns
}
}
}
*/
#---------------------------------------------------------------
# azurerm monitoring diagnostics - Firewall and Public IP's
#---------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "fw-mgnt-pip-diag" {
count = var.log_analytics_workspace_id != null || var.storage_account_name != null && var.enable_forced_tunneling ? 1 : 0
name = lower("fw-${var.firewall_config.name}-mgnt-pip-diag")
target_resource_id = azurerm_public_ip.fw-mgnt-pip.0.id
storage_account_id = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.id : null
log_analytics_workspace_id = var.log_analytics_workspace_id
dynamic "log" {
for_each = var.fw_pip_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
days = 0
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
days = 0
}
}
lifecycle {
ignore_changes = [log, metric]
}
}
resource "azurerm_monitor_diagnostic_setting" "fw-pip-diag" {
for_each = { for pip in var.public_ip_names : pip => true if var.log_analytics_workspace_id != null || var.storage_account_name != null }
name = lower("fw-${var.firewall_config.name}-${each.key}-pip-diag")
target_resource_id = azurerm_public_ip.fw-pip[each.key].id
storage_account_id = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.id : null
log_analytics_workspace_id = var.log_analytics_workspace_id
dynamic "log" {
for_each = var.fw_pip_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
days = 0
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
days = 0
}
}
lifecycle {
ignore_changes = [log, metric]
}
}
resource "azurerm_monitor_diagnostic_setting" "fw-diag" {
count = var.log_analytics_workspace_id != null || var.storage_account_name != null ? 1 : 0
name = lower("${var.firewall_config.name}-diag")
target_resource_id = azurerm_firewall.fw.id
storage_account_id = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.id : null
log_analytics_workspace_id = var.log_analytics_workspace_id
dynamic "log" {
for_each = var.fw_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
days = 0
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
days = 0
}
}
lifecycle {
ignore_changes = [log, metric]
}
}