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

azurerm_linux_virtual_machine azurerm_windows_virtual_machine: Add support for confidential vm #16905

Merged
merged 3 commits into from
May 25, 2022
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
65 changes: 51 additions & 14 deletions internal/services/compute/linux_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
if err != nil {
return fmt.Errorf("expanding `os_disk`: %+v", err)
}
securityEncryptionType := osDiskRaw[0].(map[string]interface{})["security_encryption_type"].(string)

secretsRaw := d.Get("secret").([]interface{})
secrets := expandLinuxSecrets(secretsRaw)
Expand Down Expand Up @@ -459,6 +460,12 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
}

if encryptionAtHostEnabled, ok := d.GetOk("encryption_at_host_enabled"); ok {
if encryptionAtHostEnabled.(bool) {
if compute.SecurityEncryptionTypesDiskWithVMGuestState == compute.SecurityEncryptionTypes(securityEncryptionType) {
return fmt.Errorf("`encryption_at_host_enabled` cannot be set to `true` when `os_disk.0.security_encryption_type` is set to `DiskWithVMGuestState`")
}
}

params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{
EncryptionAtHost: utils.Bool(encryptionAtHostEnabled.(bool)),
}
Expand All @@ -478,32 +485,54 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
}
}

if secureBootEnabled, ok := d.GetOk("secure_boot_enabled"); ok && secureBootEnabled.(bool) {
secureBootEnabled := d.Get("secure_boot_enabled").(bool)
vtpmEnabled := d.Get("vtpm_enabled").(bool)
if securityEncryptionType != "" {
if !secureBootEnabled {
return fmt.Errorf("`secure_boot_enabled` must be set to `true` when `os_disk.0.security_encryption_type` is specified")
}
if !vtpmEnabled {
return fmt.Errorf("`vtpm_enabled` must be set to `true` when `os_disk.0.security_encryption_type` is specified")
}

if params.VirtualMachineProperties.SecurityProfile == nil {
params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{}
}
params.VirtualMachineProperties.SecurityProfile.SecurityType = compute.SecurityTypesConfidentialVM

if params.VirtualMachineProperties.SecurityProfile.UefiSettings == nil {
params.VirtualMachineProperties.SecurityProfile.UefiSettings = &compute.UefiSettings{}
}
params.VirtualMachineProperties.SecurityProfile.SecurityType = compute.SecurityTypesTrustedLaunch
params.VirtualMachineProperties.SecurityProfile.UefiSettings.SecureBootEnabled = utils.Bool(secureBootEnabled.(bool))
params.VirtualMachineProperties.SecurityProfile.UefiSettings.SecureBootEnabled = utils.Bool(true)
params.VirtualMachineProperties.SecurityProfile.UefiSettings.VTpmEnabled = utils.Bool(true)
} else {
if secureBootEnabled {
if params.VirtualMachineProperties.SecurityProfile == nil {
params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{}
}
if params.VirtualMachineProperties.SecurityProfile.UefiSettings == nil {
params.VirtualMachineProperties.SecurityProfile.UefiSettings = &compute.UefiSettings{}
}
params.VirtualMachineProperties.SecurityProfile.SecurityType = compute.SecurityTypesTrustedLaunch
params.VirtualMachineProperties.SecurityProfile.UefiSettings.SecureBootEnabled = utils.Bool(secureBootEnabled)
}

if vtpmEnabled {
if params.VirtualMachineProperties.SecurityProfile == nil {
params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{}
}
if params.VirtualMachineProperties.SecurityProfile.UefiSettings == nil {
params.VirtualMachineProperties.SecurityProfile.UefiSettings = &compute.UefiSettings{}
}
params.VirtualMachineProperties.SecurityProfile.SecurityType = compute.SecurityTypesTrustedLaunch
params.VirtualMachineProperties.SecurityProfile.UefiSettings.VTpmEnabled = utils.Bool(vtpmEnabled)
}
}

if v, ok := d.GetOk("termination_notification"); ok {
params.VirtualMachineProperties.ScheduledEventsProfile = expandVirtualMachineScheduledEventsProfile(v.([]interface{}))
}

if vtpmEnabled, ok := d.GetOk("vtpm_enabled"); ok && vtpmEnabled.(bool) {
if params.VirtualMachineProperties.SecurityProfile == nil {
params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{}
}
if params.VirtualMachineProperties.SecurityProfile.UefiSettings == nil {
params.VirtualMachineProperties.SecurityProfile.UefiSettings = &compute.UefiSettings{}
}
params.VirtualMachineProperties.SecurityProfile.SecurityType = compute.SecurityTypesTrustedLaunch
params.VirtualMachineProperties.SecurityProfile.UefiSettings.VTpmEnabled = utils.Bool(vtpmEnabled.(bool))
}

if !provisionVMAgent && allowExtensionOperations {
return fmt.Errorf("`allow_extension_operations` cannot be set to `true` when `provision_vm_agent` is set to `false`")
}
Expand Down Expand Up @@ -1122,6 +1151,14 @@ func resourceLinuxVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interface
}

if d.HasChange("encryption_at_host_enabled") {
if d.Get("encryption_at_host_enabled").(bool) {
osDiskRaw := d.Get("os_disk").([]interface{})
securityEncryptionType := osDiskRaw[0].(map[string]interface{})["security_encryption_type"].(string)
if compute.SecurityEncryptionTypesDiskWithVMGuestState == compute.SecurityEncryptionTypes(securityEncryptionType) {
return fmt.Errorf("`encryption_at_host_enabled` cannot be set to `true` when `os_disk.0.security_encryption_type` is set to `DiskWithVMGuestState`")
}
}

shouldUpdate = true
shouldDeallocate = true // API returns the following error if not deallocate: 'securityProfile.encryptionAtHost' can be updated only when VM is in deallocated state

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,36 @@ func TestAccLinuxVirtualMachine_diskOSWriteAcceleratorEnabled(t *testing.T) {
})
}

func TestAccLinuxVirtualMachine_diskOSConfidentialVmWithGuestStateOnly(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}

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

func TestAccLinuxVirtualMachine_diskOSConfidentialVmWithDiskAndVMGuestStateCMK(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}

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

func (r LinuxVirtualMachineResource) diskOSBasic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down Expand Up @@ -815,3 +845,174 @@ resource "azurerm_linux_virtual_machine" "test" {
}
`, r.template(data), data.RandomInteger, enabled)
}

func (r LinuxVirtualMachineResource) diskOSConfidentialVmWithGuestStateOnly(data acceptance.TestData) string {
// Confidential VM has limited region support
data.Locations.Primary = "northeurope"
return fmt.Sprintf(`
%s

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_DC2as_v5"
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.test.id,
]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
security_encryption_type = "VMGuestStateOnly"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-confidential-vm-focal"
sku = "20_04-lts-cvm"
version = "latest"
}

vtpm_enabled = true
secure_boot_enabled = true
}
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) diskOSConfidentialVmWithDiskAndVMGuestStateCMK(data acceptance.TestData) string {
// Confidential VM has limited region support
data.Locations.Primary = "northeurope"
return fmt.Sprintf(`
provider "azurerm" {
features {
key_vault {
recover_soft_deleted_key_vaults = false
purge_soft_delete_on_destroy = false
purge_soft_deleted_keys_on_destroy = false
}
}
}

%[1]s

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_DC2as_v5"
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.test.id,
]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
security_encryption_type = "DiskWithVMGuestState"
secure_vm_disk_encryption_set_id = azurerm_disk_encryption_set.test.id
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-confidential-vm-focal"
sku = "20_04-lts-cvm"
version = "latest"
}

vtpm_enabled = true
secure_boot_enabled = true

depends_on = [
azurerm_key_vault_access_policy.disk-encryption,
]
}

data "azurerm_client_config" "current" {}

resource "azurerm_key_vault" "test" {
name = "acctestkv%[3]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "premium"
tenant_id = data.azurerm_client_config.current.tenant_id
enabled_for_disk_encryption = true
soft_delete_retention_days = 7
purge_protection_enabled = true
}

resource "azurerm_key_vault_access_policy" "service-principal" {
key_vault_id = azurerm_key_vault.test.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id

key_permissions = [
"Create",
"Delete",
"Get",
"Purge",
"Update",
]

secret_permissions = [
"Get",
"Delete",
"Set",
]
}

resource "azurerm_key_vault_key" "test" {
name = "examplekey"
key_vault_id = azurerm_key_vault.test.id
key_type = "RSA-HSM"
key_size = 2048

key_opts = [
"decrypt",
"encrypt",
"sign",
"unwrapKey",
"verify",
"wrapKey",
]

depends_on = [azurerm_key_vault_access_policy.service-principal]
}

resource "azurerm_disk_encryption_set" "test" {
name = "acctestdes-%[2]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 = "ConfidentialVmEncryptedWithCustomerKey"

identity {
type = "SystemAssigned"
}
}

resource "azurerm_key_vault_access_policy" "disk-encryption" {
key_vault_id = azurerm_key_vault.test.id

key_permissions = [
"Get",
"WrapKey",
"UnwrapKey",
]

tenant_id = azurerm_disk_encryption_set.test.identity.0.tenant_id
object_id = azurerm_disk_encryption_set.test.identity.0.principal_id
}
`, r.template(data), data.RandomInteger, data.RandomString)
}
Loading