-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
154 lines (132 loc) · 5.19 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
provider "azurerm" {
version = "=2.12.0"
features {}
}
resource "random_string" "unique" {
length = 6
special = false
upper = false
}
resource "azurerm_resource_group" "rg" {
name = "rg-${random_string.unique.result}"
location = var.rg_location
}
resource "azurerm_kubernetes_cluster" "aks" {
name = "aks-${random_string.unique.result}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
kubernetes_version = var.aks_version
dns_prefix = "aks"
default_node_pool {
name = "default"
node_count = var.aks_node_count
vm_size = var.aks_vm_size
}
identity {
type = "SystemAssigned"
}
}
resource "azurerm_user_assigned_identity" "mi" {
name = "mi-${random_string.unique.result}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
provider "helm" {
kubernetes {
load_config_file = false
host = azurerm_kubernetes_cluster.aks.kube_config.0.host
username = azurerm_kubernetes_cluster.aks.kube_config.0.username
password = azurerm_kubernetes_cluster.aks.kube_config.0.password
client_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
client_key = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
}
}
resource "helm_release" "aad_pod_id" {
name = "aad-pod-identity"
repository = "https://raw.githubusercontent.com/Azure/aad-pod-identity/master/charts"
chart = "aad-pod-identity"
version = "2.0.0"
namespace = var.aad_pod_id_ns
create_namespace = true
values = [
<<-EOF
azureIdentities:
- name: "${azurerm_user_assigned_identity.mi.name}"
resourceID: "${azurerm_user_assigned_identity.mi.id}"
clientID: "${azurerm_user_assigned_identity.mi.client_id}"
binding:
name: "${azurerm_user_assigned_identity.mi.name}-identity-binding"
selector: "${var.aad_pod_id_binding_selector}"
EOF
]
}
data "azurerm_resource_group" "aks_node_rg" {
name = azurerm_kubernetes_cluster.aks.node_resource_group
}
# Following roles are required by AAD Pod Identity and must be assigned to the Kubelet Identity
# https://github.com/Azure/aad-pod-identity/blob/master/docs/readmes/README.msi.md#pre-requisites---role-assignments
resource "azurerm_role_assignment" "vm_contributor" {
scope = data.azurerm_resource_group.aks_node_rg.id
role_definition_name = "Virtual Machine Contributor"
principal_id = azurerm_kubernetes_cluster.aks.kubelet_identity.0.object_id
}
resource "azurerm_role_assignment" "all_mi_operator" {
scope = data.azurerm_resource_group.aks_node_rg.id
role_definition_name = "Managed Identity Operator"
principal_id = azurerm_kubernetes_cluster.aks.kubelet_identity.0.object_id
}
resource "azurerm_role_assignment" "mi_operator" {
scope = azurerm_user_assigned_identity.mi.id
role_definition_name = "Managed Identity Operator"
principal_id = azurerm_kubernetes_cluster.aks.kubelet_identity.0.object_id
}
resource "helm_release" "kv_csi" {
name = "csi-secrets-provider-azure"
repository = "https://raw.githubusercontent.com/Azure/secrets-store-csi-driver-provider-azure/master/charts"
chart = "csi-secrets-store-provider-azure"
version = "0.0.7"
namespace = var.kv_csi_ns
create_namespace = true
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "kv" {
name = "kv-${random_string.unique.result}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
network_acls {
default_action = "Allow"
bypass = "AzureServices"
}
}
# Set permissions for currently logged-in Terraform SP to be able to read/modify secrets
resource "azurerm_key_vault_access_policy" "kv" {
key_vault_id = azurerm_key_vault.kv.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
secret_permissions = [
"Get",
"Set",
"Delete"
]
}
resource "azurerm_role_assignment" "kv" {
scope = azurerm_key_vault.kv.id
role_definition_name = "Reader"
principal_id = azurerm_user_assigned_identity.mi.principal_id
}
resource "azurerm_key_vault_access_policy" "mi" {
key_vault_id = azurerm_key_vault.kv.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = azurerm_user_assigned_identity.mi.principal_id
secret_permissions = ["Get"]
}
resource "azurerm_key_vault_secret" "demo" {
name = "demo-secret"
value = "demo-value"
key_vault_id = azurerm_key_vault.kv.id
# Must wait for Terraform SP policy to kick in before creating secrets
depends_on = [azurerm_key_vault_access_policy.kv]
}