-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.tf
338 lines (297 loc) · 13.9 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
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)
access_policies = [
for p in var.access_policies : merge({
azure_ad_group_names = []
object_ids = []
azure_ad_user_principal_names = []
certificate_permissions = []
key_permissions = []
secret_permissions = []
storage_permissions = []
azure_ad_service_principal_names = []
}, p)
]
azure_ad_group_names = distinct(flatten(local.access_policies[*].azure_ad_group_names))
azure_ad_user_principal_names = distinct(flatten(local.access_policies[*].azure_ad_user_principal_names))
azure_ad_service_principal_names = distinct(flatten(local.access_policies[*].azure_ad_service_principal_names))
group_object_ids = { for g in data.azuread_group.adgrp : lower(g.display_name) => g.id }
user_object_ids = { for u in data.azuread_user.adusr : lower(u.user_principal_name) => u.id }
spn_object_ids = { for s in data.azuread_service_principal.adspn : lower(s.display_name) => s.id }
flattened_access_policies = concat(
flatten([
for p in local.access_policies : flatten([
for i in p.object_ids : {
object_id = i
certificate_permissions = p.certificate_permissions
key_permissions = p.key_permissions
secret_permissions = p.secret_permissions
storage_permissions = p.storage_permissions
}
])
]),
flatten([
for p in local.access_policies : flatten([
for n in p.azure_ad_group_names : {
object_id = local.group_object_ids[lower(n)]
certificate_permissions = p.certificate_permissions
key_permissions = p.key_permissions
secret_permissions = p.secret_permissions
storage_permissions = p.storage_permissions
}
])
]),
flatten([
for p in local.access_policies : flatten([
for n in p.azure_ad_user_principal_names : {
object_id = local.user_object_ids[lower(n)]
certificate_permissions = p.certificate_permissions
key_permissions = p.key_permissions
secret_permissions = p.secret_permissions
storage_permissions = p.storage_permissions
}
])
]),
flatten([
for p in local.access_policies : flatten([
for n in p.azure_ad_service_principal_names : {
object_id = local.spn_object_ids[lower(n)]
certificate_permissions = p.certificate_permissions
key_permissions = p.key_permissions
secret_permissions = p.secret_permissions
storage_permissions = p.storage_permissions
}
])
])
)
grouped_access_policies = { for p in local.flattened_access_policies : p.object_id => p... }
combined_access_policies = [
for k, v in local.grouped_access_policies : {
object_id = k
certificate_permissions = distinct(flatten(v[*].certificate_permissions))
key_permissions = distinct(flatten(v[*].key_permissions))
secret_permissions = distinct(flatten(v[*].secret_permissions))
storage_permissions = distinct(flatten(v[*].storage_permissions))
}
]
service_principal_object_id = data.azurerm_client_config.current.object_id
self_permissions = {
object_id = local.service_principal_object_id
tenant_id = data.azurerm_client_config.current.tenant_id
key_permissions = ["create", "delete", "get", "backup", "decrypt", "encrypt", "import", "list", "purge", "recover", "restore", "sign", "update", "verify"]
secret_permissions = ["backup", "delete", "get", "list", "purge", "recover", "restore", "set"]
certificate_permissions = ["backup", "create", "delete", "deleteissuers", "get", "getissuers", "import", "list", "listissuers", "managecontacts", "manageissuers", "purge", "recover", "restore", "setissuers", "update"]
storage_permissions = ["backup", "delete", "deletesas", "get", "getsas", "list", "listsas", "purge", "recover", "regeneratekey", "restore", "set", "setsas", "update"]
}
}
data "azuread_group" "adgrp" {
count = length(local.azure_ad_group_names)
display_name = local.azure_ad_group_names[count.index]
}
data "azuread_user" "adusr" {
count = length(local.azure_ad_user_principal_names)
user_principal_name = local.azure_ad_user_principal_names[count.index]
}
data "azuread_service_principal" "adspn" {
count = length(local.azure_ad_service_principal_names)
display_name = local.azure_ad_service_principal_names[count.index]
}
#----------------------------------------------------------
# Resource Group Creation or selection - Default is "true"
#----------------------------------------------------------
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group ? 0 : 1
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_client_config" "current" {}
#-------------------------------------------------
# Keyvault Creation - Default is "true"
#-------------------------------------------------
resource "azurerm_key_vault" "main" {
name = lower("kv-${var.key_vault_name}")
location = local.location
resource_group_name = local.resource_group_name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = var.key_vault_sku_pricing_tier
enabled_for_deployment = var.enabled_for_deployment
enabled_for_disk_encryption = var.enabled_for_disk_encryption
enabled_for_template_deployment = var.enabled_for_template_deployment
soft_delete_retention_days = var.soft_delete_retention_days
enable_rbac_authorization = var.enable_rbac_authorization
purge_protection_enabled = var.enable_purge_protection
tags = merge({ "ResourceName" = lower("kv-${var.key_vault_name}") }, var.tags, )
dynamic "network_acls" {
for_each = var.network_acls != null ? [true] : []
content {
bypass = var.network_acls.bypass
default_action = var.network_acls.default_action
ip_rules = var.network_acls.ip_rules
virtual_network_subnet_ids = var.network_acls.virtual_network_subnet_ids
}
}
dynamic "access_policy" {
for_each = local.combined_access_policies
content {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = access_policy.value.object_id
certificate_permissions = access_policy.value.certificate_permissions
key_permissions = access_policy.value.key_permissions
secret_permissions = access_policy.value.secret_permissions
storage_permissions = access_policy.value.storage_permissions
}
}
dynamic "access_policy" {
for_each = local.service_principal_object_id != "" ? [local.self_permissions] : []
content {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = access_policy.value.object_id
certificate_permissions = access_policy.value.certificate_permissions
key_permissions = access_policy.value.key_permissions
secret_permissions = access_policy.value.secret_permissions
storage_permissions = access_policy.value.storage_permissions
}
}
dynamic "contact" {
for_each = var.certificate_contacts
content {
email = contact.value.email
name = contact.value.name
phone = contact.value.phone
}
}
lifecycle {
ignore_changes = [
tags,
]
}
}
#-----------------------------------------------------------------------------------
# Keyvault Secret - Random password Creation if value is empty - Default is "false"
#-----------------------------------------------------------------------------------
resource "random_password" "passwd" {
for_each = { for k, v in var.secrets : k => v if v == "" }
length = var.random_password_length
min_upper = 4
min_lower = 2
min_numeric = 4
min_special = 4
keepers = {
name = each.key
}
}
resource "azurerm_key_vault_secret" "keys" {
for_each = var.secrets
name = each.key
value = each.value != "" ? each.value : random_password.passwd[each.key].result
key_vault_id = azurerm_key_vault.main.id
lifecycle {
ignore_changes = [
tags,
value,
]
}
}
#---------------------------------------------------------
# Private Link for Keyvault - Default is "false"
#---------------------------------------------------------
data "azurerm_virtual_network" "vnet01" {
count = var.enable_private_endpoint && var.existing_vnet_id == null ? 1 : 0
name = var.virtual_network_name
resource_group_name = local.resource_group_name
}
resource "azurerm_subnet" "snet-ep" {
count = var.enable_private_endpoint && var.existing_subnet_id == null ? 1 : 0
name = "snet-endpoint-${local.location}"
resource_group_name = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.resource_group_name : element(split("/", var.existing_vnet_id), 4)
virtual_network_name = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.name : element(split("/", var.existing_vnet_id), 8)
address_prefixes = var.private_subnet_address_prefix
enforce_private_link_endpoint_network_policies = true
}
resource "azurerm_private_endpoint" "pep1" {
count = var.enable_private_endpoint ? 1 : 0
name = format("%s-private-endpoint", var.key_vault_name)
location = local.location
resource_group_name = local.resource_group_name
subnet_id = var.existing_subnet_id == null ? azurerm_subnet.snet-ep.0.id : var.existing_subnet_id
tags = merge({ "Name" = format("%s-private-endpoint", var.key_vault_name) }, var.tags, )
private_service_connection {
name = "keyvault-privatelink"
is_manual_connection = false
private_connection_resource_id = azurerm_key_vault.main.id
subresource_names = ["vault"]
}
lifecycle {
ignore_changes = [
tags,
]
}
}
data "azurerm_private_endpoint_connection" "private-ip1" {
count = var.enable_private_endpoint ? 1 : 0
name = azurerm_private_endpoint.pep1.0.name
resource_group_name = local.resource_group_name
depends_on = [azurerm_key_vault.main]
}
resource "azurerm_private_dns_zone" "dnszone1" {
count = var.existing_private_dns_zone == null && var.enable_private_endpoint ? 1 : 0
name = "privatelink.vaultcore.azure.net"
resource_group_name = local.resource_group_name
tags = merge({ "Name" = format("%s", "KeyVault-Private-DNS-Zone") }, var.tags, )
}
resource "azurerm_private_dns_zone_virtual_network_link" "vent-link1" {
count = var.enable_private_endpoint ? 1 : 0
name = "vnet-private-zone-link"
resource_group_name = local.resource_group_name
private_dns_zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
virtual_network_id = var.existing_vnet_id == null ? data.azurerm_virtual_network.vnet01.0.id : var.existing_vnet_id
registration_enabled = true
tags = merge({ "Name" = format("%s", "vnet-private-zone-link") }, var.tags, )
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "azurerm_private_dns_a_record" "arecord1" {
count = var.enable_private_endpoint ? 1 : 0
name = azurerm_key_vault.main.name
zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone1.0.name : var.existing_private_dns_zone
resource_group_name = local.resource_group_name
ttl = 300
records = [data.azurerm_private_endpoint_connection.private-ip1.0.private_service_connection.0.private_ip_address]
}
#---------------------------------------------------
# azurerm monitoring diagnostics - KeyVault
#---------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "diag" {
count = var.log_analytics_workspace_id != null ? 1 : 0
name = lower(format("%s-diag", azurerm_key_vault.main.name))
target_resource_id = azurerm_key_vault.main.id
log_analytics_workspace_id = var.log_analytics_workspace_id
storage_account_id = var.storage_account_id != null ? var.storage_account_id : null
dynamic "log" {
for_each = var.kv_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
days = 0
}
}
}
metric {
category = "AllMetrics"
enabled = true
retention_policy {
enabled = false
}
}
}