Skip to content

Commit e2a8ec2

Browse files
authored
Merge pull request #13747 from Tbohunek/desautorotate
`azurerm_disk_encryption_set` support for `enable_auto_key_rotation`
2 parents f244bad + ade541b commit e2a8ec2

6 files changed

+79
-61
lines changed

internal/services/compute/disk_encryption_set_data_source.go

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func dataSourceDiskEncryptionSet() *pluginsdk.Resource {
3232

3333
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
3434

35+
"auto_key_rotation_enabled": {
36+
Type: pluginsdk.TypeBool,
37+
Computed: true,
38+
},
39+
3540
"tags": tags.SchemaDataSource(),
3641
},
3742
}
@@ -61,5 +66,9 @@ func dataSourceDiskEncryptionSetRead(d *pluginsdk.ResourceData, meta interface{}
6166
d.Set("location", azure.NormalizeLocation(*location))
6267
}
6368

69+
if props := resp.EncryptionSetProperties; props != nil {
70+
d.Set("auto_key_rotation_enabled", props.RotationToLatestKeyVersionEnabled)
71+
}
72+
6473
return tags.FlattenAndSet(d, resp.Tags)
6574
}

internal/services/compute/disk_encryption_set_data_source_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ func TestAccDataSourceDiskEncryptionSet_basic(t *testing.T) {
1717
data.DataSourceTest(t, []acceptance.TestStep{
1818
{
1919
Config: r.basic(data),
20+
Check: acceptance.ComposeTestCheckFunc(
21+
check.That(data.ResourceName).Key("location").Exists(),
22+
check.That(data.ResourceName).Key("auto_key_rotation_enabled").HasValue("false"),
23+
),
2024
},
25+
})
26+
}
27+
28+
func TestAccDataSourceDiskEncryptionSet_update(t *testing.T) {
29+
data := acceptance.BuildTestData(t, "data.azurerm_disk_encryption_set", "test")
30+
r := DiskEncryptionSetDataSource{}
31+
data.DataSourceTest(t, []acceptance.TestStep{
2132
{
2233
Config: r.basic(data),
34+
},
35+
{
36+
Config: r.complete(data),
2337
Check: acceptance.ComposeTestCheckFunc(
2438
check.That(data.ResourceName).Key("location").Exists(),
39+
check.That(data.ResourceName).Key("auto_key_rotation_enabled").HasValue("true"),
2540
),
2641
},
2742
})
@@ -37,3 +52,14 @@ data "azurerm_disk_encryption_set" "test" {
3752
}
3853
`, DiskEncryptionSetResource{}.basic(data))
3954
}
55+
56+
func (DiskEncryptionSetDataSource) complete(data acceptance.TestData) string {
57+
return fmt.Sprintf(`
58+
%s
59+
60+
data "azurerm_disk_encryption_set" "test" {
61+
name = azurerm_disk_encryption_set.test.name
62+
resource_group_name = azurerm_disk_encryption_set.test.resource_group_name
63+
}
64+
`, DiskEncryptionSetResource{}.complete(data))
65+
}

internal/services/compute/disk_encryption_set_resource.go

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func resourceDiskEncryptionSet() *pluginsdk.Resource {
6060
ValidateFunc: keyVaultValidate.NestedItemId,
6161
},
6262

63+
"auto_key_rotation_enabled": {
64+
Type: pluginsdk.TypeBool,
65+
Optional: true,
66+
},
67+
6368
"identity": {
6469
Type: pluginsdk.TypeList,
6570
// whilst the API Documentation shows optional - attempting to send nothing returns:
@@ -126,6 +131,7 @@ func resourceDiskEncryptionSetCreate(d *pluginsdk.ResourceData, meta interface{}
126131
}
127132

128133
location := azure.NormalizeLocation(d.Get("location").(string))
134+
rotationToLatestKeyVersionEnabled := d.Get("auto_key_rotation_enabled").(bool)
129135
identityRaw := d.Get("identity").([]interface{})
130136
t := d.Get("tags").(map[string]interface{})
131137

@@ -138,6 +144,7 @@ func resourceDiskEncryptionSetCreate(d *pluginsdk.ResourceData, meta interface{}
138144
ID: utils.String(keyVaultDetails.keyVaultId),
139145
},
140146
},
147+
RotationToLatestKeyVersionEnabled: utils.Bool(rotationToLatestKeyVersionEnabled),
141148
},
142149
Identity: expandDiskEncryptionSetIdentity(identityRaw),
143150
Tags: tags.Expand(t),
@@ -195,6 +202,7 @@ func resourceDiskEncryptionSetRead(d *pluginsdk.ResourceData, meta interface{})
195202
keyVaultKeyId = *props.ActiveKey.KeyURL
196203
}
197204
d.Set("key_vault_key_id", keyVaultKeyId)
205+
d.Set("auto_key_rotation_enabled", props.RotationToLatestKeyVersionEnabled)
198206
}
199207

200208
if err := d.Set("identity", flattenDiskEncryptionSetIdentity(resp.Identity)); err != nil {
@@ -243,6 +251,14 @@ func resourceDiskEncryptionSetUpdate(d *pluginsdk.ResourceData, meta interface{}
243251
}
244252
}
245253

254+
if d.HasChange("auto_key_rotation_enabled") {
255+
if update.DiskEncryptionSetUpdateProperties == nil {
256+
update.DiskEncryptionSetUpdateProperties = &compute.DiskEncryptionSetUpdateProperties{}
257+
}
258+
259+
update.DiskEncryptionSetUpdateProperties.RotationToLatestKeyVersionEnabled = utils.Bool(d.Get("auto_key_rotation_enabled").(bool))
260+
}
261+
246262
future, err := client.Update(ctx, id.ResourceGroup, id.Name, update)
247263
if err != nil {
248264
return fmt.Errorf("updating Disk Encryption Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)

internal/services/compute/disk_encryption_set_resource_test.go

+24-61
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ func TestAccDiskEncryptionSet_keyRotate(t *testing.T) {
9494
check.That(data.ResourceName).ExistsInAzure(r),
9595
),
9696
},
97-
data.ImportStep(),
98-
// we have to first grant the permission for DiskEncryptionSet to access the KeyVault
99-
{
100-
Config: r.grantAccessToKeyVault(data),
101-
Check: acceptance.ComposeTestCheckFunc(
102-
check.That(data.ResourceName).ExistsInAzure(r),
103-
),
104-
},
105-
data.ImportStep(),
106-
// after the access is granted, we can rotate the key in DiskEncryptionSet
10797
{
10898
Config: r.keyRotate(data),
10999
Check: acceptance.ComposeTestCheckFunc(
@@ -194,6 +184,20 @@ resource "azurerm_key_vault_key" "test" {
194184
195185
depends_on = ["azurerm_key_vault_access_policy.service-principal"]
196186
}
187+
188+
resource "azurerm_key_vault_access_policy" "disk-encryption" {
189+
key_vault_id = azurerm_key_vault.test.id
190+
191+
key_permissions = [
192+
"Get",
193+
"WrapKey",
194+
"UnwrapKey",
195+
]
196+
197+
tenant_id = azurerm_disk_encryption_set.test.identity.0.tenant_id
198+
object_id = azurerm_disk_encryption_set.test.identity.0.principal_id
199+
}
200+
197201
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
198202
}
199203

@@ -236,10 +240,11 @@ func (r DiskEncryptionSetResource) complete(data acceptance.TestData) string {
236240
%s
237241
238242
resource "azurerm_disk_encryption_set" "test" {
239-
name = "acctestDES-%d"
240-
resource_group_name = azurerm_resource_group.test.name
241-
location = azurerm_resource_group.test.location
242-
key_vault_key_id = azurerm_key_vault_key.test.id
243+
name = "acctestDES-%d"
244+
resource_group_name = azurerm_resource_group.test.name
245+
location = azurerm_resource_group.test.location
246+
key_vault_key_id = azurerm_key_vault_key.test.id
247+
auto_key_rotation_enabled = true
243248
244249
identity {
245250
type = "SystemAssigned"
@@ -252,36 +257,6 @@ resource "azurerm_disk_encryption_set" "test" {
252257
`, r.dependencies(data), data.RandomInteger)
253258
}
254259

255-
func (r DiskEncryptionSetResource) grantAccessToKeyVault(data acceptance.TestData) string {
256-
return fmt.Sprintf(`
257-
%s
258-
259-
resource "azurerm_key_vault_access_policy" "disk-encryption" {
260-
key_vault_id = azurerm_key_vault.test.id
261-
262-
key_permissions = [
263-
"Get",
264-
"WrapKey",
265-
"UnwrapKey",
266-
]
267-
268-
tenant_id = azurerm_disk_encryption_set.test.identity.0.tenant_id
269-
object_id = azurerm_disk_encryption_set.test.identity.0.principal_id
270-
}
271-
272-
resource "azurerm_disk_encryption_set" "test" {
273-
name = "acctestDES-%d"
274-
resource_group_name = azurerm_resource_group.test.name
275-
location = azurerm_resource_group.test.location
276-
key_vault_key_id = azurerm_key_vault_key.test.id
277-
278-
identity {
279-
type = "SystemAssigned"
280-
}
281-
}
282-
`, r.dependencies(data), data.RandomInteger)
283-
}
284-
285260
func (r DiskEncryptionSetResource) keyRotate(data acceptance.TestData) string {
286261
return fmt.Sprintf(`
287262
%s
@@ -304,24 +279,12 @@ resource "azurerm_key_vault_key" "new" {
304279
depends_on = ["azurerm_key_vault_access_policy.service-principal"]
305280
}
306281
307-
resource "azurerm_key_vault_access_policy" "disk-encryption" {
308-
key_vault_id = azurerm_key_vault.test.id
309-
310-
key_permissions = [
311-
"Get",
312-
"WrapKey",
313-
"UnwrapKey",
314-
]
315-
316-
tenant_id = azurerm_disk_encryption_set.test.identity.0.tenant_id
317-
object_id = azurerm_disk_encryption_set.test.identity.0.principal_id
318-
}
319-
320282
resource "azurerm_disk_encryption_set" "test" {
321-
name = "acctestDES-%d"
322-
resource_group_name = azurerm_resource_group.test.name
323-
location = azurerm_resource_group.test.location
324-
key_vault_key_id = azurerm_key_vault_key.new.id
283+
name = "acctestDES-%d"
284+
resource_group_name = azurerm_resource_group.test.name
285+
location = azurerm_resource_group.test.location
286+
key_vault_key_id = azurerm_key_vault_key.new.id
287+
auto_key_rotation_enabled = true
325288
326289
identity {
327290
type = "SystemAssigned"

website/docs/d/disk_encryption_set.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The following attributes are exported:
2626

2727
* `location` - The location where the Disk Encryption Set exists.
2828

29+
* `auto_key_rotation_enabled` - Is the Azure Disk Encryption Set Key automatically rotated to latest version?
30+
2931
* `tags` - A mapping of tags assigned to the Disk Encryption Set.
3032

3133
## Timeouts

website/docs/r/disk_encryption_set.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ The following arguments are supported:
105105

106106
-> **NOTE** Access to the KeyVault must be granted for this Disk Encryption Set, if you want to further use this Disk Encryption Set in a Managed Disk or Virtual Machine, or Virtual Machine Scale Set. For instructions, please refer to the doc of [Server side encryption of Azure managed disks](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/disk-encryption).
107107

108+
* `auto_key_rotation_enabled` - (Optional) Boolean flag to specify whether Azure Disk Encryption Set automatically rotates encryption Key to latest version. Defaults to `false`.
109+
108110
* `identity` - (Required) A `identity` block defined below.
109111

110112
* `tags` - (Optional) A mapping of tags to assign to the Disk Encryption Set.

0 commit comments

Comments
 (0)