forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kusto_cluster_customer_managed_key_resource.go
282 lines (240 loc) · 10.1 KB
/
kusto_cluster_customer_managed_key_resource.go
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
package kusto
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/kusto/mgmt/2021-01-01/kusto"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/locks"
keyVaultParse "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/keyvault/parse"
keyVaultValidate "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/keyvault/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/kusto/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/kusto/validate"
msiValidate "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/msi/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceKustoClusterCustomerManagedKey() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceKustoClusterCustomerManagedKeyCreateUpdate,
Read: resourceKustoClusterCustomerManagedKeyRead,
Update: resourceKustoClusterCustomerManagedKeyCreateUpdate,
Delete: resourceKustoClusterCustomerManagedKeyDelete,
// TODO: this needs a custom ID validating importer
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"cluster_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ClusterID,
},
"key_vault_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: keyVaultValidate.VaultID,
},
"key_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"key_version": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"user_identity": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: msiValidate.UserAssignedIdentityID,
},
},
}
}
func resourceKustoClusterCustomerManagedKeyCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
clusterClient := meta.(*clients.Client).Kusto.ClustersClient
keyVaultsClient := meta.(*clients.Client).KeyVault
vaultsClient := keyVaultsClient.VaultsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
clusterIDRaw := d.Get("cluster_id").(string)
clusterID, err := parse.ClusterID(clusterIDRaw)
if err != nil {
return err
}
locks.ByName(clusterID.Name, "azurerm_kusto_cluster")
defer locks.UnlockByName(clusterID.Name, "azurerm_kusto_cluster")
cluster, err := clusterClient.Get(ctx, clusterID.ResourceGroup, clusterID.Name)
if err != nil {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
if cluster.ClusterProperties == nil {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): `ClusterProperties` was nil", clusterID.Name, clusterID.ResourceGroup)
}
// since we're mutating the kusto cluster here, we can use that as the ID
resourceID := clusterIDRaw
if d.IsNewResource() {
// whilst this looks superflurious given encryption is enabled by default, due to the way
// the Azure API works this technically can be nil
if cluster.ClusterProperties.KeyVaultProperties != nil {
return tf.ImportAsExistsError("azurerm_kusto_cluster_customer_managed_key", resourceID)
}
}
keyVaultIDRaw := d.Get("key_vault_id").(string)
keyVaultID, err := keyVaultParse.VaultID(keyVaultIDRaw)
if err != nil {
return err
}
keyVault, err := vaultsClient.Get(ctx, keyVaultID.ResourceGroup, keyVaultID.Name)
if err != nil {
return fmt.Errorf("retrieving Key Vault %q (Resource Group %q): %+v", keyVaultID.Name, keyVaultID.ResourceGroup, err)
}
softDeleteEnabled := false
purgeProtectionEnabled := false
if props := keyVault.Properties; props != nil {
if esd := props.EnableSoftDelete; esd != nil {
softDeleteEnabled = *esd
}
if epp := props.EnablePurgeProtection; epp != nil {
purgeProtectionEnabled = *epp
}
}
if !softDeleteEnabled || !purgeProtectionEnabled {
return fmt.Errorf("Key Vault %q (Resource Group %q) must be configured for both Purge Protection and Soft Delete", keyVaultID.Name, keyVaultID.ResourceGroup)
}
keyVaultBaseURL, err := keyVaultsClient.BaseUriForKeyVault(ctx, *keyVaultID)
if err != nil {
return fmt.Errorf("looking up Key Vault URI from Key Vault %q (Resource Group %q): %+v", keyVaultID.Name, keyVaultID.ResourceGroup, err)
}
keyName := d.Get("key_name").(string)
keyVersion := d.Get("key_version").(string)
props := kusto.ClusterUpdate{
ClusterProperties: &kusto.ClusterProperties{
KeyVaultProperties: &kusto.KeyVaultProperties{
KeyName: utils.String(keyName),
KeyVersion: utils.String(keyVersion),
KeyVaultURI: utils.String(*keyVaultBaseURL),
},
},
}
if v, ok := d.GetOk("user_identity"); ok {
props.ClusterProperties.KeyVaultProperties.UserIdentity = utils.String(v.(string))
}
future, err := clusterClient.Update(ctx, clusterID.ResourceGroup, clusterID.Name, props, "")
if err != nil {
return fmt.Errorf("updating Customer Managed Key for Kusto Cluster %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
if err = future.WaitForCompletionRef(ctx, clusterClient.Client); err != nil {
return fmt.Errorf("waiting for completion of Kusto Cluster Update %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
d.SetId(resourceID)
return resourceKustoClusterCustomerManagedKeyRead(d, meta)
}
func resourceKustoClusterCustomerManagedKeyRead(d *pluginsdk.ResourceData, meta interface{}) error {
clusterClient := meta.(*clients.Client).Kusto.ClustersClient
keyVaultsClient := meta.(*clients.Client).KeyVault
resourcesClient := meta.(*clients.Client).Resource
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
clusterID, err := parse.ClusterID(d.Id())
if err != nil {
return err
}
cluster, err := clusterClient.Get(ctx, clusterID.ResourceGroup, clusterID.Name)
if err != nil {
if utils.ResponseWasNotFound(cluster.Response) {
log.Printf("[DEBUG] Kusto Cluster %q could not be found in Resource Group %q - removing from state!", clusterID.Name, clusterID.ResourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
if cluster.ClusterProperties == nil {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): `ClusterProperties` was nil", clusterID.Name, clusterID.ResourceGroup)
}
if cluster.ClusterProperties.KeyVaultProperties == nil {
log.Printf("[DEBUG] Customer Managed Key was not defined for Kusto Cluster %q (Resource Group %q) - removing from state!", clusterID.Name, clusterID.ResourceGroup)
d.SetId("")
return nil
}
props := cluster.ClusterProperties.KeyVaultProperties
keyName := ""
keyVaultURI := ""
keyVersion := ""
userIdentity := ""
if props != nil {
if props.KeyName != nil {
keyName = *props.KeyName
}
if props.KeyVaultURI != nil {
keyVaultURI = *props.KeyVaultURI
}
if props.KeyVersion != nil {
keyVersion = *props.KeyVersion
}
if props.UserIdentity != nil {
userIdentity = *props.UserIdentity
}
}
if keyVaultURI == "" {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): `properties.keyVaultProperties.keyVaultUri` was nil", clusterID.Name, clusterID.ResourceGroup)
}
// now we have the key vault uri we can look up the ID
keyVaultID, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, resourcesClient, keyVaultURI)
if err != nil {
return fmt.Errorf("retrieving Key Vault ID from the Base URI %q: %+v", keyVaultURI, err)
}
d.Set("cluster_id", d.Id())
d.Set("key_vault_id", keyVaultID)
d.Set("key_name", keyName)
d.Set("key_version", keyVersion)
d.Set("user_identity", userIdentity)
return nil
}
func resourceKustoClusterCustomerManagedKeyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kusto.ClustersClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
clusterID, err := parse.ClusterID(d.Id())
if err != nil {
return err
}
locks.ByName(clusterID.Name, "azurerm_kusto_cluster")
defer locks.UnlockByName(clusterID.Name, "azurerm_kusto_cluster")
// confirm it still exists prior to trying to update it, else we'll get an error
cluster, err := client.Get(ctx, clusterID.ResourceGroup, clusterID.Name)
if err != nil {
if utils.ResponseWasNotFound(cluster.Response) {
return nil
}
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
// Since this isn't a real object, just modifying an existing object
// "Delete" doesn't really make sense it should really be a "Revert to Default"
// So instead of the Delete func actually deleting the Kusto Cluster I am
// making it reset the Kusto cluster to its default state
props := kusto.ClusterUpdate{
ClusterProperties: &kusto.ClusterProperties{
KeyVaultProperties: &kusto.KeyVaultProperties{},
},
}
future, err := client.Update(ctx, clusterID.ResourceGroup, clusterID.Name, props, "")
if err != nil {
return fmt.Errorf("removing Customer Managed Key for Kusto Cluster %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for completion of Kusto Cluster Update %q (Resource Group %q): %+v", clusterID.Name, clusterID.ResourceGroup, err)
}
return nil
}