Skip to content

Commit 6f704a9

Browse files
authored
Merge pull request #10208 from Jyotsna-Anand/jyanand-ade-updatekek
Fixed bug in Kek->NoKEK encryption settings update
2 parents 890ba67 + b7b7f67 commit 6f704a9

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineExtensionTests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,22 @@ function Test-AzureDiskEncryptionExtension
15941594
Assert-NotNull $OsVolumeEncryptionSettings.DiskEncryptionKey.SecretUrl;
15951595
Assert-NotNull $OsVolumeEncryptionSettings.DiskEncryptionKey.SourceVault;
15961596

1597+
#Update encryption settings on the VM from KEK to No KEK
1598+
Set-AzVMDiskEncryptionExtension -ResourceGroupName $rgname -VMName $vmName -AadClientID $aadClientID -AadClientSecret $aadClientSecret -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $keyVaultResourceId -Force;
1599+
#Get encryption status
1600+
$encryptionStatus = Get-AzVmDiskEncryptionStatus -ResourceGroupName $rgname -VMName $vmName;
1601+
#Verify encryption is enabled on OS volume and data volumes
1602+
$OsVolumeEncryptionSettings = $encryptionStatus.OsVolumeEncryptionSettings;
1603+
Assert-AreEqual $encryptionStatus.OsVolumeEncrypted $true;
1604+
Assert-AreEqual $encryptionStatus.DataVolumesEncrypted $true;
1605+
#verify diskencryption keyvault url & secret url are not null
1606+
Assert-NotNull $OsVolumeEncryptionSettings;
1607+
Assert-NotNull $OsVolumeEncryptionSettings.DiskEncryptionKey.SecretUrl;
1608+
Assert-NotNull $OsVolumeEncryptionSettings.DiskEncryptionKey.SourceVault;
1609+
#verify key encryption key keyvault url & secret url are null after the update
1610+
Assert-Null $OsVolumeEncryptionSettings.KeyEncryptionKey.SecretUrl;
1611+
Assert-Null $OsVolumeEncryptionSettings.KeyEncryptionKey.SourceVault;
1612+
15971613
#Remove the VM
15981614
Remove-AzVm -ResourceGroupName $rgname -Name $vmName -Force;
15991615

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Add Priority, EvictionPolicy, and MaxPrice parameters to New-AzVM and New-AzVmss cmdlets
2323
* Fix warning message and help document for Add-AzVMAdditionalUnattendContent and Add-AzVMSshPublicKey cmdlets
2424
* Fix -skipVmBackup exception for Linux VMs with managed disks for Set-AzVMDiskEncryptionExtension.
25+
* Fix bug in update encryption settings in Set-AzVMDiskEncryptionExtension, two pass scenario.
2526

2627
## Version 2.6.0
2728
* Add UploadSizeInBytes parameter tp New-AzDiskConfig

src/Compute/Compute/Extension/AzureDiskEncryption/SetAzureDiskEncryptionExtension.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
314314
ErrorCategory.InvalidResult,
315315
null));
316316
}
317+
//encryption settings object to clear out encryption settings before updating
318+
DiskEncryptionSettings resetEncryptionSettings = new DiskEncryptionSettings();
319+
resetEncryptionSettings.Enabled = false;
320+
resetEncryptionSettings.DiskEncryptionKey = null;
321+
resetEncryptionSettings.KeyEncryptionKey = null;
317322

318323
DiskEncryptionSettings encryptionSettings = new DiskEncryptionSettings();
319324
encryptionSettings.Enabled = true;
@@ -326,6 +331,7 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
326331
encryptionSettings.KeyEncryptionKey.SourceVault = new SubResource(this.KeyEncryptionKeyVaultId);
327332
encryptionSettings.KeyEncryptionKey.KeyUrl = this.KeyEncryptionKeyUrl;
328333
}
334+
329335
vmParameters.StorageProfile.OsDisk.EncryptionSettings = encryptionSettings;
330336
var parameters = new VirtualMachine
331337
{
@@ -352,14 +358,35 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
352358
}
353359
else
354360
{
355-
356-
// stop-update-start
361+
// stop-clear-update-start
357362
// stop vm
358363
this.ComputeClient.ComputeManagementClient.VirtualMachines
359364
.DeallocateWithHttpMessagesAsync(this.ResourceGroupName, this.VMName).GetAwaiter()
360365
.GetResult();
361366

362-
// update vm
367+
// first update vm call to clear encryption settings
368+
vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(
369+
this.ResourceGroupName, this.VMName));
370+
vmParameters.StorageProfile.OsDisk.EncryptionSettings = resetEncryptionSettings;
371+
parameters = new VirtualMachine
372+
{
373+
DiagnosticsProfile = vmParameters.DiagnosticsProfile,
374+
HardwareProfile = vmParameters.HardwareProfile,
375+
StorageProfile = vmParameters.StorageProfile,
376+
NetworkProfile = vmParameters.NetworkProfile,
377+
OsProfile = vmParameters.OsProfile,
378+
Plan = vmParameters.Plan,
379+
AvailabilitySet = vmParameters.AvailabilitySet,
380+
Location = vmParameters.Location,
381+
Tags = vmParameters.Tags
382+
};
383+
384+
updateResult = this.ComputeClient.ComputeManagementClient.VirtualMachines.CreateOrUpdateWithHttpMessagesAsync(
385+
this.ResourceGroupName,
386+
vmParameters.Name,
387+
parameters).GetAwaiter().GetResult();
388+
389+
// second update vm call to set new encryption settings
363390
vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(
364391
this.ResourceGroupName, this.VMName));
365392
vmParameters.StorageProfile.OsDisk.EncryptionSettings = encryptionSettings;
@@ -566,23 +593,23 @@ public override void ExecuteCmdlet()
566593
new DiskEncryptionSettings { Enabled = false };
567594

568595
// Single Pass
569-
// newer model, supported by newer extension versions and host functionality
596+
// newer model, supported by newer extension versions and host functionality
570597
// if SinglePassParameterSet is used, cmdlet will default to newer extension version
571598
// [first and only pass]
572599
// only one enable extension call will be issued from the cmdlet n
573-
// No AD identity information or protected settings will be passed to the extension
574-
// Host performs the necessary key vault operations and vm model updates
600+
// No AD identity information or protected settings will be passed to the extension
601+
// Host performs the necessary key vault operations and vm model updates
575602
// Dual Pass
576-
// older model, supported by older extension versions
603+
// older model, supported by older extension versions
577604
// if an AD ParameterSet is used, cmdlet will default to older extension version
578-
// [first pass]
605+
// [first pass]
579606
// AD identity information is passed into the VM via protected settings of the extension
580-
// VM uses the AD identity to authenticate and perform key vault operations
607+
// VM uses the AD identity to authenticate and perform key vault operations
581608
// VM returns result of key vault operation to caller via the extension status message
582609
// [second pass]
583610
// powershell reads extension status message returned from first pass
584611
// updates VM model with encryption settings
585-
// updates VM
612+
// updates VM
586613

587614
// First Pass
588615
AzureOperationResponse<VirtualMachineExtension> firstPass = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
@@ -608,7 +635,7 @@ public override void ExecuteCmdlet()
608635
}
609636
else
610637
{
611-
// Second pass
638+
// Second pass
612639
var secondPass = UpdateVmEncryptionSettings(encryptionSettingsBackup);
613640
WriteObject(ComputeAutoMapperProfile.Mapper.Map<PSAzureOperationResponse>(secondPass));
614641
}

0 commit comments

Comments
 (0)