-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
encryption_settings.go
150 lines (127 loc) · 3.83 KB
/
encryption_settings.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
package azurerm
import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func encryptionSettingsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
// Azure can change enabled from false to true, but not the other way around, so
// to keep idempotency, we'll conservatively set this to ForceNew=true
ForceNew: true,
},
"disk_encryption_key": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"secret_url": {
Type: schema.TypeString,
Required: true,
},
"source_vault_id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"key_encryption_key": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_url": {
Type: schema.TypeString,
Required: true,
},
"source_vault_id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
}
}
func expandManagedDiskEncryptionSettings(settings map[string]interface{}) *compute.EncryptionSettingsCollection {
enabled := settings["enabled"].(bool)
config := &compute.EncryptionSettingsCollection{
Enabled: utils.Bool(enabled),
}
var diskEncryptionKey *compute.KeyVaultAndSecretReference
var keyEncryptionKey *compute.KeyVaultAndKeyReference
if v := settings["disk_encryption_key"].([]interface{}); len(v) > 0 {
dek := v[0].(map[string]interface{})
secretURL := dek["secret_url"].(string)
sourceVaultId := dek["source_vault_id"].(string)
diskEncryptionKey = &compute.KeyVaultAndSecretReference{
SecretURL: utils.String(secretURL),
SourceVault: &compute.SourceVault{
ID: utils.String(sourceVaultId),
},
}
}
if v := settings["key_encryption_key"].([]interface{}); len(v) > 0 {
kek := v[0].(map[string]interface{})
secretURL := kek["key_url"].(string)
sourceVaultId := kek["source_vault_id"].(string)
keyEncryptionKey = &compute.KeyVaultAndKeyReference{
KeyURL: utils.String(secretURL),
SourceVault: &compute.SourceVault{
ID: utils.String(sourceVaultId),
},
}
}
// at this time we only support a single element
config.EncryptionSettings = &[]compute.EncryptionSettingsElement{
{
DiskEncryptionKey: diskEncryptionKey,
KeyEncryptionKey: keyEncryptionKey,
},
}
return config
}
func flattenManagedDiskEncryptionSettings(encryptionSettings *compute.EncryptionSettingsCollection) []interface{} {
if encryptionSettings == nil {
return []interface{}{}
}
value := map[string]interface{}{
"enabled": *encryptionSettings.Enabled,
}
if encryptionSettings.EncryptionSettings != nil && len(*encryptionSettings.EncryptionSettings) > 0 {
// at this time we only support a single element
settings := (*encryptionSettings.EncryptionSettings)[0]
if key := settings.DiskEncryptionKey; key != nil {
keys := make(map[string]interface{})
keys["secret_url"] = *key.SecretURL
if vault := key.SourceVault; vault != nil {
keys["source_vault_id"] = *vault.ID
}
value["disk_encryption_key"] = []interface{}{keys}
}
if key := settings.KeyEncryptionKey; key != nil {
keys := make(map[string]interface{})
keys["key_url"] = *key.KeyURL
if vault := key.SourceVault; vault != nil {
keys["source_vault_id"] = *vault.ID
}
value["key_encryption_key"] = []interface{}{keys}
}
}
return []interface{}{
value,
}
}