Skip to content

Commit 91fe3f0

Browse files
anshuljain26Anshul Jain
andauthored
Add Managed Identity Support in Azure Disk Encryption (#26652)
* Changes * Add UT's * Unwanted changes * Update markdown file * Modified code and handle comments * Update comments and removed unwanted code * update test case * fix comments * fix changelog * Addressed comments * fix changes * Removed unwanted changes * Throw exception when encryption identity is not part of assigned Identities * Remove unwanted changes * Remove changes not required * remove unwanted changes1 * fix remaining unwanted changes * fix identation issues * markdown check --------- Co-authored-by: Anshul Jain <[email protected]>
1 parent 0709a7b commit 91fe3f0

17 files changed

+23394
-9
lines changed

src/Compute/Compute.Test/ScenarioTests/ComputeTestCommon.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,47 @@ function Create-KeyVault
138138
return $properties
139139
}
140140

141+
# Create key vault resources
142+
function Create-KeyVaultWithAclEncryptionIdentity
143+
{
144+
Param
145+
(
146+
[Parameter(Mandatory=$true, Position=0)]
147+
[string] $resourceGroupName,
148+
[Parameter(Mandatory=$true, Position=1)]
149+
[string] $location,
150+
[Parameter(Mandatory=$false, Position=2)]
151+
[string] $vaultName,
152+
[Parameter(Mandatory=$false, Position=3)]
153+
[String] $userIdentityPrincipalId
154+
)
155+
156+
# initialize parameters if needed
157+
if ([string]::IsNullOrEmpty($resourceGroupName)) { $resourceGroupName = Get-ComputeTestResourceName }
158+
if ([string]::IsNullOrEmpty($location)) { $location = Get-ComputeVMLocation }
159+
if ([string]::IsNullOrEmpty($vaultName)) { $vaultName = 'kv' + $resourceGroupName }
160+
161+
# create vault
162+
$vault = New-AzKeyVault -VaultName $vaultName -ResourceGroupName $resourceGroupName -Location $location -Sku standard -DisableRbacAuthorization
163+
$vault = Get-AzKeyVault -VaultName $vaultName -ResourceGroupName $resourceGroupName
164+
165+
# create access policy
166+
$servicePrincipalName = (Get-AzContext).Account.Id
167+
Assert-NotNull $servicePrincipalName
168+
if (-not [string]::IsNullOrEmpty($userIdentityPrincipalId)) {
169+
Set-AzKeyVaultAccessPolicy -VaultName $vaultName -ResourceGroupName $resourceGroupName -ObjectId $userIdentityPrincipalId -PermissionsToKeys all -PermissionsToSecrets all
170+
}
171+
Set-AzKeyVaultAccessPolicy -VaultName $vaultName -ResourceGroupName $resourceGroupName -EnabledForDeployment -EnabledForTemplateDeployment
172+
173+
# return the newly created key vault properties
174+
$properties = New-Object PSObject -Property @{
175+
DiskEncryptionKeyVaultId = $vault.ResourceId
176+
DiskEncryptionKeyVaultUrl = $vault.VaultUri
177+
#KeyEncryptionKeyUrl = $kek.Key.kid
178+
}
179+
return $properties
180+
}
181+
141182
# Create a new virtual machine with other necessary resources configured
142183
function Create-VirtualMachine
143184
{

src/Compute/Compute.Test/ScenarioTests/VirtualMachineExtensionTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ public void TestAzureDiskEncryptionLnxManagedDisk()
156156
TestRunner.RunTestScript("Test-AzureDiskEncryptionLnxManagedDisk");
157157
}
158158

159+
[Fact]
160+
[Trait(Category.AcceptanceType, Category.CheckIn)]
161+
public void TestAzureDiskEncryptionWithEncryptionIdentityAddedInAzVmConfig()
162+
{
163+
TestRunner.RunTestScript("Test-AzureDiskEncryptionWithEncryptionIdentityAddedInAzVmConfig");
164+
}
165+
166+
[Fact]
167+
[Trait(Category.AcceptanceType, Category.CheckIn)]
168+
public void TestAzureDiskEncryptionWithEncryptionIdentityAddedInSetADECmdlet()
169+
{
170+
TestRunner.RunTestScript("Test-AzureDiskEncryptionWithEncryptionIdentityAddedInSetADECmdlet");
171+
}
172+
173+
[Fact]
174+
[Trait(Category.AcceptanceType, Category.CheckIn)]
175+
public void TestAzureDiskEncryptionWithIdentityNotSetInVirtualMachine()
176+
{
177+
TestRunner.RunTestScript("Test-AzureDiskEncryptionWithIdentityNotSetInVirtualMachine");
178+
}
179+
[Fact]
180+
[Trait(Category.AcceptanceType, Category.CheckIn)]
181+
public void TestAzureDiskEncryptionWithIdentityNotAckledInKeyVault()
182+
{
183+
TestRunner.RunTestScript("Test-AzureDiskEncryptionWithIdentityNotAckledInKeyVault");
184+
}
185+
159186
[Fact]
160187
[Trait(Category.AcceptanceType, Category.CheckIn)]
161188
public void TestVirtualMachineBginfoExtension()

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

Lines changed: 410 additions & 1 deletion
Large diffs are not rendered by default.

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,5 +675,19 @@ public void TestVMSetAzOSCredentialNullRef()
675675
{
676676
TestRunner.RunTestScript("Test-VMSetAzOSCredentialNullRef");
677677
}
678+
679+
[Fact]
680+
[Trait(Category.AcceptanceType, Category.CheckIn)]
681+
public void TestAddEncryptionIdentityInAzureVmConfig()
682+
{
683+
TestRunner.RunTestScript("Test-AddEncryptionIdentityInAzureVmConfig");
684+
}
685+
686+
[Fact]
687+
[Trait(Category.AcceptanceType, Category.CheckIn)]
688+
public void TestEncryptionIdentityNotPartOfAssignedIdentitiesInAzureVm()
689+
{
690+
TestRunner.RunTestScript("Test-EncryptionIdentityNotPartOfAssignedIdentitiesInAzureVm");
691+
}
678692
}
679693
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7727,3 +7727,165 @@ function Test-VMwithSSHKeyEd25519
77277727
Clean-ResourceGroup $rgname;
77287728
}
77297729
}
7730+
7731+
<#
7732+
.SYNOPSIS
7733+
Test Test-AddEncryptionIdentityInAzureVmConfig add encryptionIdentity for Azure disk encryption using managed Identity.
7734+
#>
7735+
function Test-AddEncryptionIdentityInAzureVmConfig{
7736+
$rgName = Get-ComputeTestResourceName;
7737+
try {
7738+
# create virtual machine
7739+
$loc = "eastus2euap";
7740+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
7741+
# VM Profile & Hardware
7742+
$vmsize = 'Standard_D2S_V3';
7743+
$vmname = 'vm' + $rgname;
7744+
$imagePublisher = "RedHat";
7745+
$imageOffer = "RHEL";
7746+
$imageSku = "92-gen2";
7747+
$encIdentity = "/subscriptions/759532d8-9991-4d04-878f-49f0f4804906/resourceGroups/linuxRhel-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testingazmsi";
7748+
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize -EncryptionIdentity $encIdentity -IdentityType UserAssigned -IdentityId $encIdentity;
7749+
7750+
Assert-AreEqual $p.HardwareProfile.VmSize $vmsize;
7751+
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
7752+
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
7753+
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
7754+
$subnetId = $vnet.Subnets[0].Id;
7755+
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Static -DomainNameLabel ('pubip' + $rgname);
7756+
$pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
7757+
$pubipId = $pubip.Id;
7758+
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
7759+
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
7760+
$nicId = $nic.Id;
7761+
Write-Verbose "Completed one instances";
7762+
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
7763+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
7764+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId;
7765+
7766+
$osDiskName = 'linuxOsDisk';
7767+
$osDiskCaching = 'ReadWrite';
7768+
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/linuxos.vhd";
7769+
$p = Set-AzVMOSDisk -VM $p -Name $osDiskName -Caching $osDiskCaching -CreateOption FromImage -Linux;
7770+
Assert-AreEqual $p.StorageProfile.OSDisk.Caching $osDiskCaching;
7771+
Assert-AreEqual $p.StorageProfile.OSDisk.Name $osDiskName;
7772+
# OS & Image
7773+
$user = "Foo12";
7774+
$password = $PLACEHOLDER;
7775+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force; <#[SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Credentials are used only for the duration of test. Resources are deleted at the end of the test.")]#>
7776+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
7777+
$computerName = 'test';
7778+
$vhdContainer = "https://$stoname.blob.core.windows.net/test";
7779+
7780+
$p = Set-AzVMOperatingSystem -VM $p -Linux -ComputerName $computerName -Credential $cred -DisablePasswordAuthentication;
7781+
Write-Verbose "Adding SSH public key for VM"
7782+
$sshPublicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9tGj7bjzqid3QP5YpH2+YGK8Or2KRZLdNuRGiFqgefGEF4uZrsKXeRXAXS7ia5CdCSIu020PDR69nPZq3dEQGp8GNMKXvfIBIpI++BISbT1jPuMVwEnI4JESGI4ay1glh1JtbRzQsktNjUGUYDxoOAYbtj3GU5lvw2CJ5WmobtcQbXLHWYqdDmTZQ7ry7l6GCjJSzye4IkwlQoGUql/T2iU2bLQyOCsFzcDEzFv6hVR8iFcV+eOJNHIkjCQz3Bw+tOTZbHMz1G95tSswdkrdwfMvR8fkWmby39lnFC+I7xcySQI6FMzaQZ7bA0tFGpp1JoThy5J5hBak5yOTqGBYL dummy@cc-1b92760a-6bb78476c6-h5cwh";
7783+
$sshPath = "/home/" + $user + "/.ssh/authorized_keys"
7784+
Add-AzVMSshPublicKey -VM $p -KeyData $sshPublicKey -Path $sshPath
7785+
Write-Verbose "Added SSH public key successfully."
7786+
$p = Set-AzVMSourceImage -VM $p -PublisherName $imagePublisher -Offer $imageOffer -Skus $imageSku -Version "latest"
7787+
Assert-AreEqual $p.OSProfile.AdminUsername $user;
7788+
Assert-AreEqual $p.OSProfile.ComputerName $computerName;
7789+
Assert-AreEqual $p.OSProfile.AdminPassword $password;
7790+
Assert-AreEqual $p.StorageProfile.ImageReference.Offer $imageOffer;
7791+
Assert-AreEqual $p.StorageProfile.ImageReference.Publisher $imagePublisher;
7792+
Assert-AreEqual $p.StorageProfile.ImageReference.Sku $imageSku;
7793+
$p = Set-AzVMBootDiagnostic -VM $p -Disable
7794+
7795+
# Virtual Machine
7796+
New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p;
7797+
$vm = Get-AzVM -ResourceGroupName $rgname -Name $vmname;
7798+
Write-Verbose "The value of the variable is: $vm"
7799+
Assert-AreEqual $vmname $vm.Name;
7800+
Assert-AreEqual "UserAssigned" $vm.Identity.Type
7801+
Assert-NotNull $vm.Identity.UserAssignedIdentities
7802+
Assert-AreEqual 1 $vm.Identity.UserAssignedIdentities.Count
7803+
Assert-True { $vm.Identity.UserAssignedIdentities.ContainsKey($encIdentity) }
7804+
Assert-NotNull $vm.Identity.UserAssignedIdentities[$encIdentity].PrincipalId
7805+
Assert-NotNull $vm.Identity.UserAssignedIdentities[$encIdentity].ClientId
7806+
Write-Verbose $vm.SecurityProfile;
7807+
Assert-NotNull $vm.SecurityProfile.EncryptionIdentity
7808+
Assert-AreEqual $encIdentity $vm.SecurityProfile.EncryptionIdentity.UserAssignedIdentityResourceId
7809+
7810+
}
7811+
finally {
7812+
clean-ResourceGroup $rgName;
7813+
}
7814+
}
7815+
7816+
<#
7817+
.SYNOPSIS
7818+
Test Test-EncryptionIdentityNotPartOfAssignedIdentitiesInAzureVm Throw Exceptions if the EncryptionIdentity
7819+
is not a part of assignedIdentities in a VM.
7820+
#>
7821+
function Test-EncryptionIdentityNotPartOfAssignedIdentitiesInAzureVm{
7822+
$rgName = Get-ComputeTestResourceName;
7823+
try {
7824+
# create virtual machine
7825+
$loc = "eastus2euap";
7826+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
7827+
# VM Profile & Hardware
7828+
$vmsize = 'Standard_D2S_V3';
7829+
$vmname = 'vm' + $rgname;
7830+
$imagePublisher = "RedHat";
7831+
$imageOffer = "RHEL";
7832+
$imageSku = "92-gen2";
7833+
$assignedIdentity = "/subscriptions/759532d8-9991-4d04-878f-49f0f4804906/resourceGroups/linuxRhel-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testingazmsi";
7834+
$encIdentity = "/subscriptions/759532d8-9991-4d04-878f-49f0f4804906/resourceGroups/linuxRhel-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testcliIdentity"
7835+
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize -EncryptionIdentity $encIdentity -IdentityType UserAssigned -IdentityId $assignedIdentity;
7836+
7837+
Assert-AreEqual $p.HardwareProfile.VmSize $vmsize;
7838+
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
7839+
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
7840+
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
7841+
$subnetId = $vnet.Subnets[0].Id;
7842+
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Static -DomainNameLabel ('pubip' + $rgname);
7843+
$pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
7844+
$pubipId = $pubip.Id;
7845+
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
7846+
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
7847+
$nicId = $nic.Id;
7848+
Write-Verbose "Completed one instances";
7849+
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
7850+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
7851+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId;
7852+
7853+
$osDiskName = 'linuxOsDisk';
7854+
$osDiskCaching = 'ReadWrite';
7855+
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/linuxos.vhd";
7856+
$p = Set-AzVMOSDisk -VM $p -Name $osDiskName -Caching $osDiskCaching -CreateOption FromImage -Linux;
7857+
Assert-AreEqual $p.StorageProfile.OSDisk.Caching $osDiskCaching;
7858+
Assert-AreEqual $p.StorageProfile.OSDisk.Name $osDiskName;
7859+
# OS & Image
7860+
$user = "Foo12";
7861+
$password = $PLACEHOLDER;
7862+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force; <#[SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Credentials are used only for the duration of test. Resources are deleted at the end of the test.")]#>
7863+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
7864+
$computerName = 'test';
7865+
$vhdContainer = "https://$stoname.blob.core.windows.net/test";
7866+
7867+
$p = Set-AzVMOperatingSystem -VM $p -Linux -ComputerName $computerName -Credential $cred -DisablePasswordAuthentication;
7868+
Write-Verbose "Adding SSH public key for VM"
7869+
$sshPublicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9tGj7bjzqid3QP5YpH2+YGK8Or2KRZLdNuRGiFqgefGEF4uZrsKXeRXAXS7ia5CdCSIu020PDR69nPZq3dEQGp8GNMKXvfIBIpI++BISbT1jPuMVwEnI4JESGI4ay1glh1JtbRzQsktNjUGUYDxoOAYbtj3GU5lvw2CJ5WmobtcQbXLHWYqdDmTZQ7ry7l6GCjJSzye4IkwlQoGUql/T2iU2bLQyOCsFzcDEzFv6hVR8iFcV+eOJNHIkjCQz3Bw+tOTZbHMz1G95tSswdkrdwfMvR8fkWmby39lnFC+I7xcySQI6FMzaQZ7bA0tFGpp1JoThy5J5hBak5yOTqGBYL dummy@cc-1b92760a-6bb78476c6-h5cwh";
7870+
$sshPath = "/home/" + $user + "/.ssh/authorized_keys"
7871+
Add-AzVMSshPublicKey -VM $p -KeyData $sshPublicKey -Path $sshPath
7872+
Write-Verbose "Added SSH public key successfully."
7873+
$p = Set-AzVMSourceImage -VM $p -PublisherName $imagePublisher -Offer $imageOffer -Skus $imageSku -Version "latest"
7874+
Assert-AreEqual $p.OSProfile.AdminUsername $user;
7875+
Assert-AreEqual $p.OSProfile.ComputerName $computerName;
7876+
Assert-AreEqual $p.OSProfile.AdminPassword $password;
7877+
Assert-AreEqual $p.StorageProfile.ImageReference.Offer $imageOffer;
7878+
Assert-AreEqual $p.StorageProfile.ImageReference.Publisher $imagePublisher;
7879+
Assert-AreEqual $p.StorageProfile.ImageReference.Sku $imageSku;
7880+
$p = Set-AzVMBootDiagnostic -VM $p -Disable
7881+
7882+
# Virtual Machine
7883+
Assert-ThrowsContains {New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p} `
7884+
"Encryption Identity should be an ARM Resource ID of one of the user assigned identities associated to the resource";
7885+
7886+
}
7887+
finally {
7888+
clean-ResourceGroup $rgName;
7889+
}
7890+
}
7891+

0 commit comments

Comments
 (0)