This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
115 lines (95 loc) · 4.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
#-------------------------------
# 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)
}
data "azurerm_client_config" "current" {}
#---------------------------------------------------------
# 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" {
#ts:skip=AC_AZURE_0389 RSG lock should be skipped for now.
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, )
}
#---------------------------------------------------------
# Key-vault Creation or selection
#---------------------------------------------------------
resource "random_string" "unique" {
length = 5
special = false
upper = false
}
#tfsec:ignore:AZU020
resource "azurerm_key_vault" "main" {
name = format("kv%s%s", lower(replace(var.name, "/[[:^alpha:]]/", "")), random_string.unique.result)
resource_group_name = local.resource_group_name
location = local.location
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = var.skuname
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
#tfsec:ignore:azure-keyvault-no-purge
purge_protection_enabled = var.purge_protection_enabled
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
}
}
tags = merge({ "ResourceName" = format("kv%s%s", lower(replace(var.name, "/[[:^alnum:]]/", "")), random_string.unique.result) }, var.tags, )
}
#---------------------------------------------------------
# Key-vault Access Policies
#---------------------------------------------------------
resource "azurerm_key_vault_access_policy" "main" {
count = length(var.access_policies)
key_vault_id = azurerm_key_vault.main.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = var.access_policies[count.index].object_id
secret_permissions = var.access_policies[count.index].secret_permissions
key_permissions = var.access_policies[count.index].key_permissions
certificate_permissions = var.access_policies[count.index].certificate_permissions
storage_permissions = var.access_policies[count.index].storage_permissions
}
resource "azurerm_log_analytics_workspace" "main" {
count = var.logging_enabled ? 1 : 0
name = var.log_analytics_workspace_name == null ? "${var.prefix}-workspace" : var.log_analytics_workspace_name
location = local.location
resource_group_name = local.resource_group_name
sku = var.log_analytics_workspace_sku
retention_in_days = var.log_retention_in_days
tags = var.tags
}
resource "azurerm_monitor_diagnostic_setting" "main" {
count = var.logging_enabled ? 1 : 0
name = format("kvmonitor%s%s", lower(replace(var.name, "/[[:^alnum:]]/", "")), random_string.unique.result)
target_resource_id = azurerm_key_vault.main.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.main[0].id
log {
category = "AuditEvent"
enabled = false
retention_policy {
enabled = false
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
}