-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
432 lines (381 loc) · 17.2 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
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
##-----------------------------------------------------------------------------
## Data block to query information
##-----------------------------------------------------------------------------
data "azurerm_client_config" "current_client_config" {}
##-----------------------------------------------------------------------------
## Locals declaration for determining the local variables
##-----------------------------------------------------------------------------
locals {
valid_rg_name = var.existing_private_dns_zone == null ? var.resource_group_name : var.existing_private_dns_zone_resource_group_name
private_dns_zone_name = var.enable_private_endpoint ? var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone[0].name : var.existing_private_dns_zone : null
}
##-----------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##-----------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/azure"
version = "1.0.0"
name = var.name
environment = var.environment
managedby = var.managedby
label_order = var.label_order
repository = var.repository
extra_tags = var.extra_tags
}
##-----------------------------------------------------------------------------
## Below resource will deploy keyvault in your azure environment.
##-----------------------------------------------------------------------------
resource "azurerm_key_vault" "key_vault" {
count = var.enabled ? 1 : 0
provider = azurerm.main_sub
name = format("%s-kv", module.labels.id)
location = var.location
resource_group_name = var.resource_group_name
enabled_for_disk_encryption = var.enabled_for_disk_encryption
tenant_id = data.azurerm_client_config.current_client_config.tenant_id
purge_protection_enabled = var.purge_protection_enabled
soft_delete_retention_days = var.soft_delete_retention_days
enable_rbac_authorization = var.enable_rbac_authorization
public_network_access_enabled = var.public_network_access_enabled
sku_name = var.sku_name
enabled_for_deployment = var.enabled_for_deployment
enabled_for_template_deployment = var.enabled_for_template_deployment
tags = module.labels.tags
dynamic "network_acls" {
for_each = var.network_acls == null ? [] : [var.network_acls]
iterator = acl
content {
bypass = acl.value.bypass
default_action = acl.value.default_action
ip_rules = acl.value.ip_rules
virtual_network_subnet_ids = acl.value.virtual_network_subnet_ids
}
}
dynamic "contact" {
for_each = var.certificate_contacts
content {
email = contact.value.email
name = contact.value.name
phone = contact.value.phone
}
}
lifecycle {
ignore_changes = [
tags,
]
}
}
##-----------------------------------------------------------------------------
## Below resource will provide user access on key vault based on policy based in azure environment.
## if rbac is enabled then below resource will create.
##-----------------------------------------------------------------------------
resource "azurerm_key_vault_access_policy" "readers_policy" {
provider = azurerm.main_sub
for_each = toset(var.enable_rbac_authorization && var.enabled && !var.managed_hardware_security_module_enabled ? [] : var.reader_objects_ids)
object_id = each.value
tenant_id = data.azurerm_client_config.current_client_config.tenant_id
key_vault_id = azurerm_key_vault.key_vault[0].id
key_permissions = [
"Get",
"List",
]
secret_permissions = [
"Get",
"List",
]
certificate_permissions = [
"Get",
"List",
]
}
resource "azurerm_key_vault_access_policy" "admin_policy" {
provider = azurerm.main_sub
for_each = toset(var.enable_rbac_authorization && var.enabled && !var.managed_hardware_security_module_enabled ? [] : var.admin_objects_ids)
object_id = each.value
tenant_id = data.azurerm_client_config.current_client_config.tenant_id
key_vault_id = azurerm_key_vault.key_vault[0].id
key_permissions = [
"Backup",
"Create",
"Decrypt",
"Delete",
"Encrypt",
"Get",
"Import",
"List",
"Purge",
"Recover",
"Restore",
"Sign",
"UnwrapKey",
"Update",
"Verify",
"WrapKey",
"Rotate",
"GetRotationPolicy",
"SetRotationPolicy",
"Release"
]
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",
]
}
##-----------------------------------------------------------------------------
## Below resource will provide user access on key vault based on role base access in azure environment.
## if rbac is enabled then below resource will create.
##-----------------------------------------------------------------------------
resource "azurerm_role_assignment" "rbac_keyvault_administrator" {
provider = azurerm.main_sub
for_each = toset(var.enable_rbac_authorization && var.enabled && !var.managed_hardware_security_module_enabled ? var.admin_objects_ids : [])
scope = azurerm_key_vault.key_vault[0].id
role_definition_name = "Key Vault Administrator"
principal_id = each.value
}
resource "azurerm_role_assignment" "rbac_keyvault_secrets_users" {
provider = azurerm.main_sub
for_each = toset(var.enable_rbac_authorization && var.enabled && !var.managed_hardware_security_module_enabled ? var.reader_objects_ids : [])
scope = azurerm_key_vault.key_vault[0].id
role_definition_name = "Key Vault Secrets User"
principal_id = each.value
}
resource "azurerm_role_assignment" "rbac_keyvault_reader" {
provider = azurerm.main_sub
for_each = toset(var.enable_rbac_authorization && var.enabled && !var.managed_hardware_security_module_enabled ? var.reader_objects_ids : [])
scope = azurerm_key_vault.key_vault[0].id
role_definition_name = "Key Vault Reader"
principal_id = each.value
}
##-----------------------------------------------------------------------------
##Below resource will deploy private endpoint for key vault.
##-----------------------------------------------------------------------------
resource "azurerm_private_endpoint" "pep" {
provider = azurerm.main_sub
count = var.enabled && var.enable_private_endpoint ? 1 : 0
name = format("%s-pe-kv", module.labels.id)
location = var.location
resource_group_name = var.resource_group_name
subnet_id = var.subnet_id
tags = module.labels.tags
private_service_connection {
name = format("%s-psc-kv", module.labels.id)
is_manual_connection = false
private_connection_resource_id = azurerm_key_vault.key_vault[0].id
subresource_names = ["vault"]
}
lifecycle {
ignore_changes = [
tags,
]
}
}
##-----------------------------------------------------------------------------
## Data block to retreive private ip of private endpoint.
##-----------------------------------------------------------------------------
data "azurerm_private_endpoint_connection" "private-ip" {
provider = azurerm.main_sub
count = var.enabled && var.enable_private_endpoint ? 1 : 0
name = azurerm_private_endpoint.pep[0].name
resource_group_name = var.resource_group_name
depends_on = [azurerm_key_vault.key_vault]
}
##-----------------------------------------------------------------------------
## Below resource will create private dns zone in your azure subscription.
## Will be created only when there is no existing private dns zone and private endpoint is enabled.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone" "dnszone" {
provider = azurerm.main_sub
count = var.enabled && var.existing_private_dns_zone == null && var.enable_private_endpoint ? 1 : 0
name = "privatelink.vaultcore.azure.net"
resource_group_name = var.resource_group_name
tags = module.labels.tags
}
##-----------------------------------------------------------------------------
## Below resource will create vnet link in private dns.
## Vnet link will be created when there is no existing private dns zone or existing private dns zone is in same subscription.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone_virtual_network_link" "vent-link" {
provider = azurerm.main_sub
count = var.enabled && var.enable_private_endpoint && var.diff_sub == false ? 1 : 0
name = var.existing_private_dns_zone == null ? format("%s-pdz-vnet-link-kv", module.labels.id) : format("%s-pdz-vnet-link-kv-1", module.labels.id)
resource_group_name = local.valid_rg_name
private_dns_zone_name = local.private_dns_zone_name
virtual_network_id = var.virtual_network_id
tags = module.labels.tags
}
##-----------------------------------------------------------------------------
## Below resource will create vnet link in existing private dns zone.
## Vnet link will be created when existing private dns zone is in different subscription.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone_virtual_network_link" "vent-link-1" {
provider = azurerm.dns_sub
count = var.enabled && var.enable_private_endpoint && var.diff_sub == true ? 1 : 0
name = var.existing_private_dns_zone == null ? format("%s-pdz-vnet-link-kv", module.labels.id) : format("%s-pdz-vnet-link-kv-1", module.labels.id)
resource_group_name = local.valid_rg_name
private_dns_zone_name = local.private_dns_zone_name
virtual_network_id = var.virtual_network_id
tags = module.labels.tags
}
##-----------------------------------------------------------------------------
## Below resource will create vnet link in existing private dns zone.
## Vnet link will be created when existing private dns zone is in different subscription.
## This resource is deployed when more than 1 vnet link is required and module can be called again to do so without deploying other key vault resources.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone_virtual_network_link" "vent-link-diff-subs" {
provider = azurerm.dns_sub
count = var.enabled && var.multi_sub_vnet_link && var.existing_private_dns_zone != null ? 1 : 0
name = format("%s-pdz-vnet-link-kv-1", module.labels.id)
resource_group_name = var.existing_private_dns_zone_resource_group_name
private_dns_zone_name = var.existing_private_dns_zone
virtual_network_id = var.virtual_network_id
tags = module.labels.tags
}
##-----------------------------------------------------------------------------
## Below resource will create vnet link in private dns zone.
## Below resource will be created when extra vnet link is required in dns zone in same subscription.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone_virtual_network_link" "addon_vent_link" {
provider = azurerm.main_sub
count = var.enabled && var.addon_vent_link ? 1 : 0
name = format("%s-pdz-vnet-link-kv-addon", module.labels.id)
resource_group_name = var.addon_resource_group_name
private_dns_zone_name = var.existing_private_dns_zone == null ? azurerm_private_dns_zone.dnszone[0].name : var.existing_private_dns_zone
virtual_network_id = var.addon_virtual_network_id
tags = module.labels.tags
}
##-----------------------------------------------------------------------------
## Below resource will create dns A record for private ip of private endpoint in private dns zone.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_a_record" "arecord" {
provider = azurerm.main_sub
count = var.enabled && var.enable_private_endpoint && var.diff_sub == false ? 1 : 0
name = azurerm_key_vault.key_vault[0].name
zone_name = local.private_dns_zone_name
resource_group_name = local.valid_rg_name
ttl = 3600
records = [data.azurerm_private_endpoint_connection.private-ip[0].private_service_connection[0].private_ip_address]
tags = module.labels.tags
lifecycle {
ignore_changes = [
tags,
]
}
}
##-----------------------------------------------------------------------------
## Below resource will create dns A record for private ip of private endpoint in private dns zone.
## This resource will be created when private dns is in different subscription.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_a_record" "arecord-1" {
provider = azurerm.dns_sub
count = var.enabled && var.enable_private_endpoint && var.diff_sub == true ? 1 : 0
name = azurerm_key_vault.key_vault[0].name
zone_name = local.private_dns_zone_name
resource_group_name = local.valid_rg_name
ttl = 3600
records = [data.azurerm_private_endpoint_connection.private-ip[0].private_service_connection[0].private_ip_address]
tags = module.labels.tags
lifecycle {
ignore_changes = [
tags,
]
}
}
##-----------------------------------------------------------------------------
## Below resources will create diagnostic setting for key vault and its components.
##-----------------------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "example" {
provider = azurerm.main_sub
count = var.enabled && var.diagnostic_setting_enable ? 1 : 0
name = format("%s-Key-vault-diagnostic-log", module.labels.id)
target_resource_id = azurerm_key_vault.key_vault[0].id
storage_account_id = var.storage_account_id
eventhub_name = var.eventhub_name
eventhub_authorization_rule_id = var.eventhub_authorization_rule_id
log_analytics_workspace_id = var.log_analytics_workspace_id
log_analytics_destination_type = var.log_analytics_destination_type
dynamic "metric" {
for_each = var.metric_enabled ? ["AllMetrics"] : []
content {
category = metric.value
enabled = true
}
}
dynamic "enabled_log" {
for_each = var.kv_logs.enabled ? var.kv_logs.category != null ? var.kv_logs.category : var.kv_logs.category_group : []
content {
category = var.kv_logs.category != null ? enabled_log.value : null
category_group = var.kv_logs.category == null ? enabled_log.value : null
}
}
lifecycle {
ignore_changes = [log_analytics_destination_type]
}
}
resource "azurerm_monitor_diagnostic_setting" "pe_kv_nic" {
provider = azurerm.main_sub
depends_on = [azurerm_private_endpoint.pep]
count = var.enabled && var.diagnostic_setting_enable && var.enable_private_endpoint ? 1 : 0
name = format("%s-pe-kv-nic-diagnostic-log", module.labels.id)
target_resource_id = azurerm_private_endpoint.pep[count.index].network_interface[0].id
storage_account_id = var.storage_account_id
eventhub_name = var.eventhub_name
eventhub_authorization_rule_id = var.eventhub_authorization_rule_id
log_analytics_workspace_id = var.log_analytics_workspace_id
log_analytics_destination_type = var.log_analytics_destination_type
dynamic "metric" {
for_each = var.metric_enabled ? ["AllMetrics"] : []
content {
category = metric.value
enabled = true
}
}
lifecycle {
ignore_changes = [log_analytics_destination_type]
}
}
resource "azurerm_key_vault_managed_hardware_security_module" "keyvault_hsm" {
provider = azurerm.main_sub
count = var.enabled && var.managed_hardware_security_module_enabled ? 1 : 0
name = format("%s-hsm-kv", module.labels.id)
location = var.location
resource_group_name = var.resource_group_name
sku_name = var.sku_name_hsm
tenant_id = data.azurerm_client_config.current_client_config.tenant_id
purge_protection_enabled = var.purge_protection_enabled
soft_delete_retention_days = var.soft_delete_retention_days
admin_object_ids = var.admin_objects_ids
public_network_access_enabled = var.public_network_access_enabled
dynamic "network_acls" {
for_each = var.network_acls == null ? [] : [var.network_acls]
iterator = acl
content {
bypass = acl.value.bypass
default_action = acl.value.default_action
}
}
tags = module.labels.tags
}