Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support EncryptionAtRestWithPlatformAndCustomerKeys in disk encryption #14218

Merged
merged 4 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions internal/services/compute/disk_encryption_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package compute

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-07-01/compute"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
)

// retrieveDiskEncryptionSetEncryptionType returns encryption type of the disk encryption set
func retrieveDiskEncryptionSetEncryptionType(ctx context.Context, client *compute.DiskEncryptionSetsClient, diskEncryptionSetId string) (*compute.EncryptionType, error) {
diskEncryptionSet, err := parse.DiskEncryptionSetID(diskEncryptionSetId)
if err != nil {
return nil, err
}

resp, err := client.Get(ctx, diskEncryptionSet.ResourceGroup, diskEncryptionSet.Name)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", *diskEncryptionSet, err)
}

var encryptionType *compute.EncryptionType
if props := resp.EncryptionSetProperties; props != nil && string(props.EncryptionType) != "" {
v := compute.EncryptionType(props.EncryptionType)
encryptionType = &v
}

if encryptionType == nil {
return nil, fmt.Errorf("retrieving %s: EncryptionType was nil", *diskEncryptionSet)
}

return encryptionType, nil
}
19 changes: 19 additions & 0 deletions internal/services/compute/disk_encryption_set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ func resourceDiskEncryptionSet() *pluginsdk.Resource {
Optional: true,
},

"encryption_type": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(compute.DiskEncryptionSetTypeEncryptionAtRestWithCustomerKey),
ValidateFunc: validation.StringInSlice([]string{
string(compute.DiskEncryptionSetTypeEncryptionAtRestWithCustomerKey),
string(compute.DiskEncryptionSetTypeEncryptionAtRestWithPlatformAndCustomerKeys),
}, false),
},

"identity": {
Type: pluginsdk.TypeList,
// whilst the API Documentation shows optional - attempting to send nothing returns:
Expand Down Expand Up @@ -132,6 +143,7 @@ func resourceDiskEncryptionSetCreate(d *pluginsdk.ResourceData, meta interface{}

location := azure.NormalizeLocation(d.Get("location").(string))
rotationToLatestKeyVersionEnabled := d.Get("auto_key_rotation_enabled").(bool)
encryptionType := d.Get("encryption_type").(string)
identityRaw := d.Get("identity").([]interface{})
t := d.Get("tags").(map[string]interface{})

Expand All @@ -145,6 +157,7 @@ func resourceDiskEncryptionSetCreate(d *pluginsdk.ResourceData, meta interface{}
},
},
RotationToLatestKeyVersionEnabled: utils.Bool(rotationToLatestKeyVersionEnabled),
EncryptionType: compute.DiskEncryptionSetType(encryptionType),
},
Identity: expandDiskEncryptionSetIdentity(identityRaw),
Tags: tags.Expand(t),
Expand Down Expand Up @@ -203,6 +216,12 @@ func resourceDiskEncryptionSetRead(d *pluginsdk.ResourceData, meta interface{})
}
d.Set("key_vault_key_id", keyVaultKeyId)
d.Set("auto_key_rotation_enabled", props.RotationToLatestKeyVersionEnabled)

encryptionType := string(compute.DiskEncryptionSetTypeEncryptionAtRestWithCustomerKey)
if props.EncryptionType != "" {
encryptionType = string(props.EncryptionType)
}
d.Set("encryption_type", encryptionType)
}

if err := d.Set("identity", flattenDiskEncryptionSetIdentity(resp.Identity)); err != nil {
Expand Down
34 changes: 34 additions & 0 deletions internal/services/compute/disk_encryption_set_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccDiskEncryptionSet_basic(t *testing.T) {
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("encryption_type").HasValue("EncryptionAtRestWithCustomerKey"),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -104,6 +105,21 @@ func TestAccDiskEncryptionSet_keyRotate(t *testing.T) {
})
}

func TestAccDiskEncryptionSet_withEncryptionType(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_disk_encryption_set", "test")
r := DiskEncryptionSetResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withPlatformAndCustomerKeys(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (DiskEncryptionSetResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.DiskEncryptionSetID(state.ID)
if err != nil {
Expand Down Expand Up @@ -292,3 +308,21 @@ resource "azurerm_disk_encryption_set" "test" {
}
`, r.dependencies(data), data.RandomInteger)
}

func (r DiskEncryptionSetResource) withPlatformAndCustomerKeys(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_disk_encryption_set" "test" {
name = "acctestDES-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
key_vault_key_id = azurerm_key_vault_key.test.id
encryption_type = "EncryptionAtRestWithPlatformAndCustomerKeys"

identity {
type = "SystemAssigned"
}
}
`, r.dependencies(data), data.RandomInteger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1089,12 +1089,17 @@ func resourceLinuxVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interface
diskName := d.Get("os_disk.0.name").(string)
log.Printf("[DEBUG] Updating encryption settings of OS Disk %q for Linux Virtual Machine %q (Resource Group %q) to %q..", diskName, id.Name, id.ResourceGroup, diskEncryptionSetId)

encryptionType, err := retrieveDiskEncryptionSetEncryptionType(ctx, meta.(*clients.Client).Compute.DiskEncryptionSetsClient, diskEncryptionSetId)
if err != nil {
return err
}

disksClient := meta.(*clients.Client).Compute.DisksClient

update := compute.DiskUpdate{
DiskUpdateProperties: &compute.DiskUpdateProperties{
Encryption: &compute.Encryption{
Type: compute.EncryptionTypeEncryptionAtRestWithCustomerKey,
Type: *encryptionType,
DiskEncryptionSetID: utils.String(diskEncryptionSetId),
},
},
Expand Down
16 changes: 13 additions & 3 deletions internal/services/compute/managed_disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func resourceManagedDisk() *pluginsdk.Resource {
// TODO: make this case-sensitive once this bug in the Azure API has been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/8132
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: azure.ValidateResourceID,
ValidateFunc: validate.DiskEncryptionSetID,
},

"encryption_settings": encryptionSettingsSchema(),
Expand Down Expand Up @@ -344,8 +344,13 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro
}

if diskEncryptionSetId := d.Get("disk_encryption_set_id").(string); diskEncryptionSetId != "" {
encryptionType, err := retrieveDiskEncryptionSetEncryptionType(ctx, meta.(*clients.Client).Compute.DiskEncryptionSetsClient, diskEncryptionSetId)
if err != nil {
return err
}

props.Encryption = &compute.Encryption{
Type: compute.EncryptionTypeEncryptionAtRestWithCustomerKey,
Type: *encryptionType,
DiskEncryptionSetID: utils.String(diskEncryptionSetId),
}
}
Expand Down Expand Up @@ -537,8 +542,13 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
if d.HasChange("disk_encryption_set_id") {
shouldShutDown = true
if diskEncryptionSetId := d.Get("disk_encryption_set_id").(string); diskEncryptionSetId != "" {
encryptionType, err := retrieveDiskEncryptionSetEncryptionType(ctx, meta.(*clients.Client).Compute.DiskEncryptionSetsClient, diskEncryptionSetId)
if err != nil {
return err
}

diskUpdate.Encryption = &compute.Encryption{
Type: compute.EncryptionTypeEncryptionAtRestWithCustomerKey,
Type: *encryptionType,
DiskEncryptionSetID: utils.String(diskEncryptionSetId),
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,17 @@ func resourceWindowsVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interfa
diskName := d.Get("os_disk.0.name").(string)
log.Printf("[DEBUG] Updating encryption settings of OS Disk %q for Windows Virtual Machine %q (Resource Group %q) to %q..", diskName, id.Name, id.ResourceGroup, diskEncryptionSetId)

encryptionType, err := retrieveDiskEncryptionSetEncryptionType(ctx, meta.(*clients.Client).Compute.DiskEncryptionSetsClient, diskEncryptionSetId)
if err != nil {
return err
}

disksClient := meta.(*clients.Client).Compute.DisksClient

update := compute.DiskUpdate{
DiskUpdateProperties: &compute.DiskUpdateProperties{
Encryption: &compute.Encryption{
Type: compute.EncryptionTypeEncryptionAtRestWithCustomerKey,
Type: *encryptionType,
DiskEncryptionSetID: utils.String(diskEncryptionSetId),
},
},
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/disk_encryption_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ The following arguments are supported:

* `auto_key_rotation_enabled` - (Optional) Boolean flag to specify whether Azure Disk Encryption Set automatically rotates encryption Key to latest version. Defaults to `false`.

* `encryption_type` - (Optional) The type of key used to encrypt the data of the disk. Possible values are `EncryptionAtRestWithCustomerKey` and `EncryptionAtRestWithPlatformAndCustomerKeys`. Defaults to `EncryptionAtRestWithCustomerKey`.

* `identity` - (Required) An `identity` block as defined below.

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