-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_key_vault.go
317 lines (266 loc) · 9.4 KB
/
resource_arm_key_vault.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package azurerm
import (
"fmt"
"log"
"net/http"
"regexp"
"time"
"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2016-10-01/keyvault"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
uuid "github.com/satori/go.uuid"
azschema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
// As can be seen in the API definition, the Sku Family only supports the value
// `A` and is a required field
// https://github.com/Azure/azure-rest-api-specs/blob/master/arm-keyvault/2015-06-01/swagger/keyvault.json#L239
var armKeyVaultSkuFamily = "A"
var keyVaultResourceName = "azurerm_key_vault"
func resourceArmKeyVault() *schema.Resource {
return &schema.Resource{
Create: resourceArmKeyVaultCreateUpdate,
Read: resourceArmKeyVaultRead,
Update: resourceArmKeyVaultCreateUpdate,
Delete: resourceArmKeyVaultDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
MigrateState: resourceAzureRMKeyVaultMigrateState,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateKeyVaultName,
},
"location": locationSchema(),
"resource_group_name": resourceGroupNameSchema(),
"sku": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(keyvault.Standard),
string(keyvault.Premium),
}, false),
},
},
},
},
"vault_uri": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateUUID,
},
"access_policy": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 16,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tenant_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateUUID,
},
"object_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateUUID,
},
"application_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateUUID,
},
"certificate_permissions": azschema.KeyVaultCertificatePermissionsSchema(),
"key_permissions": azschema.KeyVaultKeyPermissionsSchema(),
"secret_permissions": azschema.KeyVaultSecretPermissionsSchema(),
},
},
},
"enabled_for_deployment": {
Type: schema.TypeBool,
Optional: true,
},
"enabled_for_disk_encryption": {
Type: schema.TypeBool,
Optional: true,
},
"enabled_for_template_deployment": {
Type: schema.TypeBool,
Optional: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmKeyVaultCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).keyVaultClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for Azure ARM KeyVault creation.")
name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resGroup := d.Get("resource_group_name").(string)
tenantUUID := uuid.FromStringOrNil(d.Get("tenant_id").(string))
enabledForDeployment := d.Get("enabled_for_deployment").(bool)
enabledForDiskEncryption := d.Get("enabled_for_disk_encryption").(bool)
enabledForTemplateDeployment := d.Get("enabled_for_template_deployment").(bool)
tags := d.Get("tags").(map[string]interface{})
policies := d.Get("access_policy").([]interface{})
accessPolicies, err := azschema.ExpandKeyVaultAccessPolicies(policies)
if err != nil {
return fmt.Errorf("Error expanding `access_policy`: %+v", policies)
}
parameters := keyvault.VaultCreateOrUpdateParameters{
Location: &location,
Properties: &keyvault.VaultProperties{
TenantID: &tenantUUID,
Sku: expandKeyVaultSku(d),
AccessPolicies: accessPolicies,
EnabledForDeployment: &enabledForDeployment,
EnabledForDiskEncryption: &enabledForDiskEncryption,
EnabledForTemplateDeployment: &enabledForTemplateDeployment,
},
Tags: expandTags(tags),
}
// Locking this resource so we don't make modifications to it at the same time if there is a
// key vault access policy trying to update it as well
azureRMLockByName(name, keyVaultResourceName)
defer azureRMUnlockByName(name, keyVaultResourceName)
_, err = client.CreateOrUpdate(ctx, resGroup, name, parameters)
if err != nil {
return fmt.Errorf("Error updating Key Vault %q (Resource Group %q): %+v", name, resGroup, err)
}
read, err := client.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Key Vault %q (Resource Group %q): %+v", name, resGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read KeyVault %s (resource Group %q) ID", name, resGroup)
}
d.SetId(*read.ID)
if d.IsNewResource() {
if props := read.Properties; props != nil {
if vault := props.VaultURI; vault != nil {
log.Printf("[DEBUG] Waiting for Key Vault %q (Resource Group %q) to become available", name, resGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"available"},
Refresh: keyVaultRefreshFunc(*vault),
Timeout: 30 * time.Minute,
Delay: 30 * time.Second,
PollInterval: 10 * time.Second,
ContinuousTargetOccurence: 10,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Key Vault %q (Resource Group %q) to become available: %s", name, resGroup, err)
}
}
}
}
return resourceArmKeyVaultRead(d, meta)
}
func resourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).keyVaultClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["vaults"]
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on KeyVault %q (Resource Group %q): %+v", name, resGroup, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if props := resp.Properties; props != nil {
d.Set("tenant_id", props.TenantID.String())
d.Set("enabled_for_deployment", props.EnabledForDeployment)
d.Set("enabled_for_disk_encryption", props.EnabledForDiskEncryption)
d.Set("enabled_for_template_deployment", props.EnabledForTemplateDeployment)
if err := d.Set("sku", flattenKeyVaultSku(props.Sku)); err != nil {
return fmt.Errorf("Error flattening `sku` for KeyVault %q: %+v", resp.Name, err)
}
flattenedPolicies := azschema.FlattenKeyVaultAccessPolicies(props.AccessPolicies)
if err := d.Set("access_policy", flattenedPolicies); err != nil {
return fmt.Errorf("Error flattening `access_policy` for KeyVault %q: %+v", resp.Name, err)
}
d.Set("vault_uri", props.VaultURI)
}
flattenAndSetTags(d, resp.Tags)
return nil
}
func resourceArmKeyVaultDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).keyVaultClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["vaults"]
azureRMLockByName(name, keyVaultResourceName)
defer azureRMUnlockByName(name, keyVaultResourceName)
_, err = client.Delete(ctx, resGroup, name)
return err
}
func expandKeyVaultSku(d *schema.ResourceData) *keyvault.Sku {
skuSets := d.Get("sku").([]interface{})
sku := skuSets[0].(map[string]interface{})
return &keyvault.Sku{
Family: &armKeyVaultSkuFamily,
Name: keyvault.SkuName(sku["name"].(string)),
}
}
func flattenKeyVaultSku(sku *keyvault.Sku) []interface{} {
result := map[string]interface{}{
"name": string(sku.Name),
}
return []interface{}{result}
}
func validateKeyVaultName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if matched := regexp.MustCompile(`^[a-zA-Z0-9-]{3,24}$`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes and must be between 3-24 chars", k))
}
return
}
func keyVaultRefreshFunc(vaultUri string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking to see if KeyVault %q is available..", vaultUri)
var PTransport = &http.Transport{Proxy: http.ProxyFromEnvironment}
client := &http.Client{
Transport: PTransport,
}
conn, err := client.Get(vaultUri)
if err != nil {
log.Printf("[DEBUG] Didn't find KeyVault at %q", vaultUri)
return nil, "pending", fmt.Errorf("Error connecting to %q: %s", vaultUri, err)
}
defer conn.Body.Close()
log.Printf("[DEBUG] Found KeyVault at %q", vaultUri)
return "available", "available", nil
}
}