-
Notifications
You must be signed in to change notification settings - Fork 2
/
hashivault.tf
168 lines (139 loc) · 4.77 KB
/
hashivault.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
##############################################################################
# This configuration is for a single vault Node with storage account backend #
##############################################################################
#####################################
# Storage Account for Vault backend #
#####################################
variable "share-list" {
default = [
"file",
"logs",
"config",
]
}
resource "azurerm_storage_account" "vaultbackend" {
name = "hashivault${local.company}"
resource_group_name = azurerm_resource_group.rg-vault-eu.name
location = azurerm_resource_group.rg-vault-eu.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_share" "vaultbackend-share" {
for_each = toset(var.share-list)
name = each.value
storage_account_name = azurerm_storage_account.vaultbackend.name
quota = "50"
}
###########################
# Create self signed cert #
###########################
resource "tls_private_key" "vault-tls-key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "tls_self_signed_cert" "vault-tls-cert" {
key_algorithm = tls_private_key.vault-tls-key.algorithm
private_key_pem = tls_private_key.vault-tls-key.private_key_pem
validity_period_hours = 87600
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth",
]
dns_names = ["vault-${local.company}.${azurerm_resource_group.rg-vault-eu.location}.azurecontainer.io"]
subject {
common_name = "vault-${local.company}.${azurerm_resource_group.rg-vault-eu.location}.azurecontainer.io"
organization = "Capsule Corps."
}
}
resource "local_file" "vault-key" {
content = tls_private_key.vault-tls-key.private_key_pem
filename = "${path.module}/vault.key"
}
resource "local_file" "vault-cert" {
content = tls_self_signed_cert.vault-tls-cert.cert_pem
filename = "${path.module}/vault.crt"
}
############################
# Upload vault config file #
############################
variable "filelist" {
default = [
"vault.key",
"vault.crt",
"config.hcl",
]
}
resource "null_resource" "uploadvaultconfig" {
for_each = toset(var.filelist)
provisioner "local-exec" {
command = "az storage file upload --account-key '${azurerm_storage_account.vaultbackend.primary_access_key}' --account-name '${azurerm_storage_account.vaultbackend.name}' --share-name '${azurerm_storage_share.vaultbackend-share["config"].name}' --source ${each.value}" #config.hcl"
#interpreter = ["Bash", "-Command"]
}
depends_on = [
azurerm_storage_share.vaultbackend-share,
local_file.vault-key,
local_file.vault-cert,
]
}
################################
# Container Instance for Vault #
################################
resource "azurerm_container_group" "vault-aci" {
name = local.vault-name
location = azurerm_resource_group.rg-vault-eu.location
resource_group_name = azurerm_resource_group.rg-vault-eu.name
ip_address_type = "public"
dns_name_label = local.vault-name
os_type = "Linux"
identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.vault-identity.id,
]
}
container {
name = local.vault-name
image = "vault:1.9.2"#"vault:1.6.2"
cpu = "1"
memory = "2"
dynamic "volume" {
for_each = toset(var.share-list)
content {
name = volume.value
read_only = "false"
share_name = azurerm_storage_share.vaultbackend-share[volume.key].name
storage_account_name = azurerm_storage_account.vaultbackend.name
storage_account_key = azurerm_storage_account.vaultbackend.primary_access_key
mount_path = "/vault/${volume.value}"
}
}
ports {
port = "8200"
protocol = "TCP"
}
commands = [
"vault", "server", "-config=/vault/config/config.hcl"
]
environment_variables = {
"AZURE_TENANT_ID" = data.azurerm_client_config.current_config.tenant_id,
"VAULT_AZUREKEYVAULT_VAULT_NAME" = module.kv-vault-eu.Name,
"VAULT_AZUREKEYVAULT_KEY_NAME" = azurerm_key_vault_key.hashivault-key.name,
"VAULT_SKIP_VERIFY" = true,
}
}
depends_on = [
azurerm_storage_share.vaultbackend-share,
null_resource.uploadvaultconfig,
azurerm_key_vault_key.hashivault-key,
]
}
output "To-Configure-Vault-Address" {
value = "export VAULT_ADDR=https://${local.vault-name}.${azurerm_resource_group.rg-vault-eu.location}.azurecontainer.io:8200"
}
output "To-Ignore-SelfSigned-Certs" {
value = "export VAULT_SKIP_VERIFY=true"
}
output "To-Initialize-Vault" {
value = "vault operator init -recovery-shares=3 -recovery-threshold=2"
}