-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.tf
234 lines (201 loc) · 10.7 KB
/
security.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
resource "azurerm_network_security_group" "this" {
name = "${var.vnet_name}-nsg"
location = azurerm_virtual_network.this.location
resource_group_name = azurerm_virtual_network.this.resource_group_name
tags = merge(
try(var.tags),
tomap({
"Resource Type" = "Network Security Group"
})
)
}
resource "azurerm_network_security_rule" "default" {
for_each = local.security_rules
name = each.value.name
priority = each.value.priority
direction = each.value.direction
access = each.value.access
protocol = each.value.protocol
source_port_range = each.value.source_port_range
source_port_ranges = each.value.source_port_ranges
destination_port_range = each.value.destination_port_range
destination_port_ranges = each.value.destination_port_ranges
destination_application_security_group_ids = each.value.destination_application_security_group_ids
source_address_prefix = each.value.source_address_prefix
source_address_prefixes = each.value.source_address_prefixes
source_application_security_group_ids = each.value.source_application_security_group_ids
destination_address_prefix = each.value.destination_address_prefix
destination_address_prefixes = each.value.destination_address_prefixes
resource_group_name = azurerm_network_security_group.this.resource_group_name
network_security_group_name = azurerm_network_security_group.this.name
}
resource "azurerm_subnet_network_security_group_association" "this" {
for_each = {
for key, subnet in local.default_subnets : key => subnet if !subnet.no_nsg_association
}
subnet_id = azurerm_subnet.this[each.key].id
network_security_group_id = each.value.network_security_group_id != null ? each.value.network_security_group_id : azurerm_network_security_group.this.id
}
## Simple NSG, Default Azure
resource "azurerm_network_security_group" "simple" {
for_each = local.subnets_with_nsg_azure_default
name = lower("${var.vnet_name}-${each.key}-nsg")
location = azurerm_virtual_network.this.location
resource_group_name = azurerm_virtual_network.this.resource_group_name
tags = merge(
try(var.tags),
tomap({
"Resource Type" = "Network Security Group"
})
)
}
resource "azurerm_network_security_rule" "simple" {
for_each = {
for item, rule in local.nsg_with_default_security_rules : lower("${rule.subnet_key}_${rule.priority}_${rule.access}_${rule.direction}") => rule
}
access = each.value.access
direction = each.value.direction
name = each.value.name
network_security_group_name = azurerm_network_security_group.additional[each.value.subnet_key].name
priority = each.value.priority
protocol = each.value.protocol
resource_group_name = azurerm_network_security_group.this.resource_group_name
description = each.value.description
destination_address_prefix = each.value.destination_address_prefix
destination_address_prefixes = each.value.destination_address_prefixes
destination_application_security_group_ids = each.value.destination_application_security_group_ids
destination_port_range = each.value.destination_port_range
destination_port_ranges = each.value.destination_port_ranges
source_address_prefix = each.value.source_address_prefix
source_address_prefixes = each.value.source_address_prefixes
source_application_security_group_ids = each.value.source_application_security_group_ids
source_port_range = each.value.source_port_range
source_port_ranges = each.value.source_port_ranges
dynamic "timeouts" {
for_each = each.value.timeouts == null ? [] : [each.value.timeouts]
content {
create = timeouts.value.create
delete = timeouts.value.delete
read = timeouts.value.read
update = timeouts.value.update
}
}
# Do not remove this `depends_on` block. It is required to ensure the NSG is created before the rule.
depends_on = [azurerm_network_security_group.simple]
}
resource "azurerm_subnet_network_security_group_association" "simple" {
for_each = {
for key, subnet in local.subnets_with_nsg_azure_default : key => subnet
}
subnet_id = azurerm_subnet.this[each.key].id
network_security_group_id = azurerm_network_security_group.simple[each.key].id
}
### Additional NSGs and rules
resource "azurerm_network_security_group" "additional" {
for_each = local.subnets_with_nsg
name = lower("${var.vnet_name}-${each.key}-nsg")
location = azurerm_virtual_network.this.location
resource_group_name = azurerm_virtual_network.this.resource_group_name
tags = merge(
try(var.tags),
tomap({
"Resource Type" = "Network Security Group"
})
)
}
resource "azurerm_network_security_rule" "additional" {
for_each = {
for item, rule in local.nsg_with_rules : lower("${rule.subnet_key}_${rule.priority}_${rule.access}_${rule.direction}") => rule
}
access = each.value.access
direction = each.value.direction
name = each.value.name
network_security_group_name = azurerm_network_security_group.additional[each.value.subnet_key].name
priority = each.value.priority
protocol = each.value.protocol
resource_group_name = azurerm_network_security_group.this.resource_group_name
description = each.value.description
destination_address_prefix = each.value.destination_address_prefix
destination_address_prefixes = each.value.destination_address_prefixes
destination_application_security_group_ids = each.value.destination_application_security_group_ids
destination_port_range = each.value.destination_port_range
destination_port_ranges = each.value.destination_port_ranges
source_address_prefix = each.value.source_address_prefix
source_address_prefixes = each.value.source_address_prefixes
source_application_security_group_ids = each.value.source_application_security_group_ids
source_port_range = each.value.source_port_range
source_port_ranges = each.value.source_port_ranges
dynamic "timeouts" {
for_each = each.value.timeouts == null ? [] : [each.value.timeouts]
content {
create = timeouts.value.create
delete = timeouts.value.delete
read = timeouts.value.read
update = timeouts.value.update
}
}
# Do not remove this `depends_on` block. It is required to ensure the NSG is created before the rule.
depends_on = [azurerm_network_security_group.additional]
}
resource "azurerm_subnet_network_security_group_association" "additional" {
for_each = {
for key, subnet in local.subnets_with_nsg : key => subnet if !subnet.no_nsg_association
}
subnet_id = azurerm_subnet.this[each.key].id
network_security_group_id = azurerm_network_security_group.additional[each.key].id
}
## Azure Bastion NSG and rules
resource "azurerm_network_security_group" "azbastion" {
for_each = local.azure_bastion_subnet
name = lower("${var.vnet_name}-${each.key}-nsg")
location = azurerm_virtual_network.this.location
resource_group_name = azurerm_virtual_network.this.resource_group_name
tags = merge(
try(var.tags),
tomap({
"Resource Type" = "Network Security Group"
})
)
}
resource "azurerm_network_security_rule" "azbastion" {
for_each = {
for item, rule in local.azure_bastion_with_rules : lower("${rule.subnet_key}_${rule.priority}_${rule.access}_${rule.direction}") => rule
}
access = each.value.access
direction = each.value.direction
name = each.value.name
network_security_group_name = azurerm_network_security_group.azbastion[each.value.subnet_key].name
priority = each.value.priority
protocol = each.value.protocol
resource_group_name = azurerm_network_security_group.this.resource_group_name
description = each.value.description
destination_address_prefix = each.value.destination_address_prefix
destination_address_prefixes = each.value.destination_address_prefixes
destination_application_security_group_ids = each.value.destination_application_security_group_ids
destination_port_range = each.value.destination_port_range
destination_port_ranges = each.value.destination_port_ranges
source_address_prefix = each.value.source_address_prefix
source_address_prefixes = each.value.source_address_prefixes
source_application_security_group_ids = each.value.source_application_security_group_ids
source_port_range = each.value.source_port_range
source_port_ranges = each.value.source_port_ranges
dynamic "timeouts" {
for_each = each.value.timeouts == null ? [] : [each.value.timeouts]
content {
create = timeouts.value.create
delete = timeouts.value.delete
read = timeouts.value.read
update = timeouts.value.update
}
}
# Do not remove this `depends_on` block. It is required to ensure the NSG is created before the rule.
depends_on = [azurerm_network_security_group.azbastion]
}
resource "azurerm_subnet_network_security_group_association" "azbastion" {
for_each = {
for key, subnet in local.azure_bastion_subnet : key => subnet if !subnet.no_nsg_association
}
subnet_id = azurerm_subnet.this[each.key].id
network_security_group_id = azurerm_network_security_group.azbastion[each.key].id
depends_on = [azurerm_network_security_rule.azbastion]
}