@@ -7727,3 +7727,165 @@ function Test-VMwithSSHKeyEd25519
7727
7727
Clean - ResourceGroup $rgname ;
7728
7728
}
7729
7729
}
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