-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
289 lines (220 loc) · 10.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
# Service principal used by Humanitec to provision resources
data "azurerm_resource_group" "main" {
name = var.resource_group_name
}
resource "azuread_application" "humanitec_provisioner" {
display_name = var.name
}
resource "azuread_service_principal" "humanitec_provisioner" {
client_id = azuread_application.humanitec_provisioner.client_id
}
resource "azuread_service_principal_password" "humanitec_provisioner" {
service_principal_id = azuread_service_principal.humanitec_provisioner.object_id
}
resource "azurerm_role_assignment" "resource_group" {
scope = data.azurerm_resource_group.main.id
role_definition_name = "Owner"
principal_id = azuread_service_principal.humanitec_provisioner.object_id
}
resource "humanitec_resource_account" "humanitec_provisioner" {
id = var.name
name = var.name
type = "azure"
credentials = jsonencode({
"appId" : azuread_service_principal.humanitec_provisioner.client_id,
"displayName" : azuread_application.humanitec_provisioner.display_name,
"password" : azuread_service_principal_password.humanitec_provisioner.value,
"tenant" : azuread_service_principal.humanitec_provisioner.application_tenant_id
})
depends_on = [
# Otherwise the account looses permissions before the resources are deleted
azurerm_role_assignment.resource_group
]
}
# Example application and resource definition criteria
resource "humanitec_application" "example" {
id = var.name
name = var.name
}
locals {
# Define the shared azure-blob-account resource id and class
azure_blob_account_res_id = "main-blob-account"
azure_blob_account_class = "default"
# Classes used to build the resource definition graph
blob_storage_basic_class = "basic"
blob_storage_admin_policy_class = "blob-storage-basic-admin"
blob_storage_reader_policy_class = "blob-storage-basic-reader"
# Classes that developers can select from
blob_storage_admin_class = "basic-admin"
blob_storage_reader_class = "basic-read-ony"
account_resource = "azure-blob-account.${local.azure_blob_account_class}#${local.azure_blob_account_res_id}"
blob_storage_scope = "/subscriptions/${var.subscription_id}/resourceGroups/${var.resource_group_name}/providers/Microsoft.Storage/storageAccounts/$${resources['${local.account_resource}'].outputs.name}/blobServices/default/containers/$${resources['azure-blob.${local.blob_storage_basic_class}'].outputs.container}"
# Azure build in role ids: https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles
build_in_azure_storage_blob_data_owner_role_id = "/providers/Microsoft.Authorization/roleDefinitions/b7e6dc6d-f1e8-4753-8033-0f276bb0955b"
build_in_azure_storage_blob_data_reader_role_id = "/providers/Microsoft.Authorization/roleDefinitions/2a2b9908-6ea1-4ae2-8e65-a410df84e7d1"
}
# Shared azure-blob-account resource
module "azure_blob_account" {
source = "../../humanitec-resource-defs/azure-blob-account/basic"
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
resource_group_name = var.resource_group_name
prefix = var.prefix
account_tier = var.account_tier
account_replication_type = var.account_replication_type
}
resource "humanitec_resource_definition_criteria" "azure_blob_account" {
resource_definition_id = module.azure_blob_account.id
app_id = humanitec_application.example.id
res_id = local.azure_blob_account_res_id
class = local.azure_blob_account_class
force_delete = true
}
# Workload or shared blob-storage resources
module "blob_storage" {
source = "../../humanitec-resource-defs/azure-blob/basic"
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
prefix = var.prefix
container_access_type = var.container_access_type
account_resource = local.account_resource
}
resource "humanitec_resource_definition_criteria" "blob_storage" {
resource_definition_id = module.blob_storage.id
app_id = humanitec_application.example.id
class = local.blob_storage_basic_class
force_delete = true
}
// Admin shared
// Exposed delegator resource definition
module "blob_storage_admin" {
source = "../../humanitec-resource-defs/azure-blob/delegator"
prefix = "${var.prefix}admin-"
policy_resource_class = local.blob_storage_admin_policy_class
blob_storage_resource_class = local.blob_storage_basic_class
}
resource "humanitec_resource_definition_criteria" "blob_storage_admin" {
resource_definition_id = module.blob_storage_admin.id
app_id = humanitec_application.example.id
class = local.blob_storage_admin_class
force_delete = true
}
module "role_definition_admin" {
source = "../../humanitec-resource-defs/azure-role-definition/echo"
prefix = "${var.prefix}admin-"
role_definition_id = local.build_in_azure_storage_blob_data_owner_role_id
role_definition_scope = local.blob_storage_scope
}
resource "humanitec_resource_definition_criteria" "role_definition_admin" {
resource_definition_id = module.role_definition_admin.id
app_id = humanitec_application.example.id
class = local.blob_storage_admin_policy_class
force_delete = true
}
// Reader shared
// Exposed delegator resource definition
module "blob_storage_reader" {
source = "../../humanitec-resource-defs/azure-blob/delegator"
prefix = "${var.prefix}reader-"
policy_resource_class = local.blob_storage_reader_policy_class
blob_storage_resource_class = local.blob_storage_basic_class
}
resource "humanitec_resource_definition_criteria" "blob_storage_reader" {
resource_definition_id = module.blob_storage_reader.id
app_id = humanitec_application.example.id
class = local.blob_storage_reader_class
force_delete = true
}
module "role_definition_reader" {
source = "../../humanitec-resource-defs/azure-role-definition/echo"
prefix = "${var.prefix}reader-"
role_definition_id = local.build_in_azure_storage_blob_data_reader_role_id
role_definition_scope = local.blob_storage_scope
}
resource "humanitec_resource_definition_criteria" "role_definition_reader" {
resource_definition_id = module.role_definition_reader.id
app_id = humanitec_application.example.id
class = local.blob_storage_reader_policy_class
force_delete = true
}
// Workload based
module "workload" {
source = "../../humanitec-resource-defs/workload/service-account"
prefix = var.prefix
}
resource "humanitec_resource_definition_criteria" "workload" {
resource_definition_id = module.workload.id
app_id = humanitec_application.example.id
force_delete = true
}
module "k8s_service_account" {
source = "../../humanitec-resource-defs/k8s/service-account"
prefix = var.prefix
}
resource "humanitec_resource_definition_criteria" "k8s_service_account" {
resource_definition_id = module.k8s_service_account.id
app_id = humanitec_application.example.id
force_delete = true
}
module "federated_identity" {
source = "../../humanitec-resource-defs/azure-federated-identity/basic"
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
prefix = var.prefix
resource_group_name = var.resource_group_name
audience = ["api://AzureADTokenExchange"]
issuer = var.aks_cluster_issuer_url
parent_id = "$${resources.azure-managed-identity.outputs.id}"
subject = "system:serviceaccount:$${resources.k8s-namespace.outputs.namespace}:$${resources.k8s-service-account.outputs.name}"
}
resource "humanitec_resource_definition_criteria" "federated_identity" {
resource_definition_id = module.federated_identity.id
app_id = humanitec_application.example.id
force_delete = true
}
module "managed_identity" {
source = "../../humanitec-resource-defs/azure-managed-identity/basic"
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
prefix = var.prefix
resource_group_name = var.resource_group_name
}
resource "humanitec_resource_definition_criteria" "managed_identity" {
resource_definition_id = module.managed_identity.id
app_id = humanitec_application.example.id
force_delete = true
}
module "role_assignment" {
source = "../../humanitec-resource-defs/azure-role-assignments/basic"
resource_packs_azure_url = var.resource_packs_azure_url
resource_packs_azure_rev = var.resource_packs_azure_rev
append_logs_to_error = true
terraform_state = local.terraform_state
driver_account = humanitec_resource_account.humanitec_provisioner.id
subscription_id = var.subscription_id
prefix = var.prefix
role_definition_ids = "$${resources.workload>azure-role-definition.outputs.id}"
scopes = "$${resources.workload>azure-role-definition.outputs.scope}"
principal_id = "$${resources.azure-managed-identity.outputs.principal_id}"
}
resource "humanitec_resource_definition_criteria" "role_assignment" {
resource_definition_id = module.role_assignment.id
app_id = humanitec_application.example.id
force_delete = true
}