Skip to content

Commit 3e3477e

Browse files
author
dmytro_velychko3
committed
feat: redactor
1 parent 2bd44d9 commit 3e3477e

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ locals {
33
suffix = length(var.suffix) == 0 ? "" : "-${var.suffix}"
44
}
55

6+
data "azurerm_key_vault_secret" "sp_client_id" {
7+
name = var.sp_client_id_secret_name
8+
key_vault_id = var.key_vault_id
9+
}
10+
11+
data "azurerm_key_vault_secret" "sp_key" {
12+
name = var.sp_key_secret_name
13+
key_vault_id = var.key_vault_id
14+
}
15+
16+
data "azurerm_key_vault_secret" "tenant_id" {
17+
name = var.tenant_id_secret_name
18+
key_vault_id = var.key_vault_id
19+
}
20+
621
resource "databricks_workspace_conf" "this" {
722
count = local.ip_rules == null ? 0 : 1
823

mount.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
resource "databricks_mount" "adls" {
2+
for_each = var.mountpoints
3+
4+
name = each.key
5+
uri = "abfss://${each.value["container_name"]}@${each.value["storage_account_name"]}.dfs.core.windows.net"
6+
extra_configs = {
7+
"fs.azure.account.auth.type" : "OAuth",
8+
"fs.azure.account.oauth.provider.type" : "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
9+
"fs.azure.account.oauth2.client.id" : data.azurerm_key_vault_secret.sp_client_id.value,
10+
"fs.azure.account.oauth2.client.secret" : databricks_secret.main[data.azurerm_key_vault_secret.sp_key.name].config_reference,
11+
"fs.azure.account.oauth2.client.endpoint" : "https://login.microsoftonline.com/${data.azurerm_key_vault_secret.tenant_id.value}/oauth2/token",
12+
"fs.azure.createRemoteFileSystemDuringInitialization" : "false",
13+
"spark.databricks.sqldw.jdbc.service.principal.client.id" : data.azurerm_key_vault_secret.sp_client_id.value,
14+
"spark.databricks.sqldw.jdbc.service.principal.client.secret" : databricks_secret.main[data.azurerm_key_vault_secret.sp_key.name].config_reference
15+
}
16+
}

permissions.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
locals {
2-
secrets_acl_objects_list = flatten([for param in var.secret_scope_object : [
2+
secrets_acl_objects_list = flatten([for param in local.secret_scope_object : [
3+
#secrets_acl_objects_list = flatten([for param in var.secret_scope_object : [
34
for permission in param.acl : {
45
scope = param.scope_name, principal = permission.principal, permission = permission.permission
56
}] if param.acl != null
@@ -23,6 +24,7 @@ resource "databricks_permissions" "default_cluster" {
2324
resource "databricks_permissions" "cluster_policy" {
2425
for_each = {
2526
for policy in var.cluster_policies_object : (policy.name) => policy
27+
#for policy in var.cluster_policies_object : (policy.name) => policy
2628
if policy.can_use != null
2729
}
2830

@@ -38,7 +40,7 @@ resource "databricks_permissions" "cluster_policy" {
3840
}
3941

4042
resource "databricks_permissions" "unity_cluster" {
41-
count = var.unity_cluster_config.permissions != null && var.unity_cluster_enabled ? 1 : 0
43+
count = length(var.unity_cluster_config.permissions) != 0 && var.unity_cluster_enabled ? 1 : 0
4244

4345
cluster_id = databricks_cluster.this[0].id
4446

secrets.tf

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
locals {
2+
sp_secrets = {
3+
(var.sp_client_id_secret_name) = { value = data.azurerm_key_vault_secret.sp_client_id.value }
4+
(var.sp_key_secret_name) = { value = data.azurerm_key_vault_secret.sp_key.value }
5+
}
6+
7+
secrets_objects_list = flatten([for param in var.secret_scope : [
8+
for secret in param.secrets : {
9+
scope_name = param.scope_name, key = secret.key, string_value = secret.string_value
10+
}] if param.secrets != null
11+
])
12+
13+
secret_scope_object = [for param in var.secret_scope : {
14+
scope_name = databricks_secret_scope.this[param.scope_name].name
15+
acl = param.acl
16+
} if param.acl != null]
17+
18+
#cluster_policies_object = [for policy in var.custom_cluster_policies : {
19+
# id = databricks_cluster_policy.this[policy.name].id
20+
# name = databricks_cluster_policy.this[policy.name].name
21+
# can_use = policy.can_use
22+
#} if policy.definition != null && var.sku == "premium"]
23+
}
24+
25+
# Secret Scope with SP secrets for mounting Azure Data Lake Storage
26+
resource "databricks_secret_scope" "main" {
27+
name = "main"
28+
initial_manage_principal = var.sku == "premium" ? null : "users"
29+
}
30+
31+
resource "databricks_secret" "main" {
32+
for_each = local.sp_secrets
33+
34+
key = each.key
35+
string_value = each.value["value"]
36+
scope = databricks_secret_scope.main.id
37+
}
38+
39+
# Custom additional Databricks Secret Scope
40+
resource "databricks_secret_scope" "this" {
41+
for_each = {
42+
for param in var.secret_scope : (param.scope_name) => param
43+
if param.scope_name != null
44+
}
45+
46+
name = each.key
47+
initial_manage_principal = var.sku == "premium" ? null : "users"
48+
}
49+
50+
resource "databricks_secret" "this" {
51+
for_each = { for entry in local.secrets_objects_list : "${entry.scope_name}.${entry.key}" => entry }
52+
53+
key = each.value.key
54+
string_value = each.value.string_value
55+
scope = databricks_secret_scope.this[each.value.scope_name].id
56+
}
57+
58+
# At the nearest future, Azure will allow acquiring AAD tokens by service principals,
59+
# thus providing an ability to create Azure backed Key Vault with Terraform
60+
# https://github.com/databricks/terraform-provider-databricks/pull/1965
61+
62+
## Azure Key Vault-backed Scope
63+
#resource "azurerm_key_vault_access_policy" "databricks" {
64+
# count = var.key_vault_secret_scope.key_vault_id != null ? 1 : 0
65+
66+
# key_vault_id = var.key_vault_secret_scope.key_vault_id
67+
# object_id = "9b38785a-6e08-4087-a0c4-20634343f21f" # Global 'AzureDatabricks' SP object id
68+
# tenant_id = data.azurerm_key_vault_secret.tenant_id.value
69+
#
70+
# secret_permissions = [
71+
# "Get",
72+
# "List",
73+
# ]
74+
#}
75+
#
76+
#resource "databricks_secret_scope" "external" {
77+
# count = var.key_vault_secret_scope.key_vault_id != null ? 1 : 0
78+
#
79+
# name = "external"
80+
# keyvault_metadata {
81+
# resource_id = var.key_vault_secret_scope.key_vault_id
82+
# dns_name = var.key_vault_secret_scope.dns_name
83+
# }
84+
# depends_on = [azurerm_key_vault_access_policy.databricks]
85+
#}

variables.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,65 @@ variable "unity_cluster_config" {
216216
description = "Specifies the databricks unity cluster configuration"
217217
default = {}
218218
}
219+
220+
######
221+
variable "sp_client_id_secret_name" {
222+
type = string
223+
description = "The name of Azure Key Vault secret that contains ClientID of Service Principal to access in Azure Key Vault"
224+
}
225+
226+
variable "sp_key_secret_name" {
227+
type = string
228+
description = "The name of Azure Key Vault secret that contains client secret of Service Principal to access in Azure Key Vault"
229+
}
230+
231+
# Secret Scope variables
232+
variable "secret_scope" {
233+
type = list(object({
234+
scope_name = string
235+
acl = optional(list(object({
236+
principal = string
237+
permission = string
238+
})))
239+
secrets = optional(list(object({
240+
key = string
241+
string_value = string
242+
})))
243+
}))
244+
description = <<-EOT
245+
Provides an ability to create custom Secret Scope, store secrets in it and assigning ACL for access management
246+
scope_name - name of Secret Scope to create;
247+
acl - list of objects, where 'principal' custom group name, this group is created in 'Premium' module; 'permission' is one of "READ", "WRITE", "MANAGE";
248+
secrets - list of objects, where object's 'key' param is created key name and 'string_value' is a value for it;
249+
EOT
250+
default = [{
251+
scope_name = null
252+
acl = null
253+
secrets = null
254+
}]
255+
}
256+
257+
variable "sku" {
258+
type = string
259+
description = "The sku to use for the Databricks Workspace: [standard|premium|trial]"
260+
default = "premium"
261+
}
262+
263+
variable "key_vault_id" {
264+
type = string
265+
description = "ID of the Key Vault instance where the Secret resides"
266+
}
267+
268+
variable "tenant_id_secret_name" {
269+
type = string
270+
description = "The name of Azure Key Vault secret that contains tenant ID secret of Service Principal to access in Azure Key Vault"
271+
}
272+
273+
variable "mountpoints" {
274+
type = map(object({
275+
storage_account_name = string
276+
container_name = string
277+
}))
278+
description = "Mountpoints for databricks"
279+
default = {}
280+
}

0 commit comments

Comments
 (0)