Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.Management.Compute.Models;
using System.Collections.Generic;
using Xunit;

namespace Compute.Tests.DiskRPTests
Expand All @@ -14,6 +15,13 @@ public void Disk_CRUD_EmptyDisk()
Disk_CRUD_Execute(DiskCreateOption.Empty, "Disk_CRUD_EmptyDisk", diskSizeGB: 5);
}

[Fact]
public void Disk_CRUD_EmptyDisk_Zones()
{
string supportedZoneLocation = "eastus2";
Disk_CRUD_Execute(DiskCreateOption.Empty, "Disk_CRUD_EmptyDisk_Zones", diskSizeGB: 5, location: supportedZoneLocation, zones: new List<string> { "1" });
}

[Fact]
public void Disk_CRUD_ImportDisk()
{
Expand Down
40 changes: 31 additions & 9 deletions src/SDKs/Compute/Compute.Tests/DiskRPTests/DiskRPTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DiskRPTestsBase : VMTestBase
private string DiskRPLocation = ComputeManagementTestUtilities.DefaultLocation.ToLower();

#region Execution
protected void Disk_CRUD_Execute(DiskCreateOption diskCreateOption, string methodName, int? diskSizeGB = null, string location = null)
protected void Disk_CRUD_Execute(DiskCreateOption diskCreateOption, string methodName, int? diskSizeGB = null, string location = null, IList<string> zones = null)
{
using (MockContext context = MockContext.Start(this.GetType().FullName, methodName))
{
Expand All @@ -31,7 +31,7 @@ protected void Disk_CRUD_Execute(DiskCreateOption diskCreateOption, string metho
// Data
var rgName = TestUtilities.GenerateName(TestPrefix);
var diskName = TestUtilities.GenerateName(DiskNamePrefix);
Disk disk = GenerateDefaultDisk(diskCreateOption, rgName, diskSizeGB);
Disk disk = GenerateDefaultDisk(diskCreateOption, rgName, diskSizeGB, zones);

try
{
Expand Down Expand Up @@ -64,11 +64,15 @@ protected void Disk_CRUD_Execute(DiskCreateOption diskCreateOption, string metho
Validate(disk, diskOut, DiskRPLocation);

// Patch
const string tagKey = "tageKey";
var updatedisk = new DiskUpdate();
updatedisk.Tags = new Dictionary<string, string>() { { tagKey, "tagvalue" } };
diskOut = m_CrpClient.Disks.Update(rgName, diskName, updatedisk);
Validate(disk, diskOut, DiskRPLocation);
// TODO: Bug 9865640 - DiskRP doesn't follow patch semantics for zones: skip this for zones
if (zones == null)
{
const string tagKey = "tageKey";
var updatedisk = new DiskUpdate();
updatedisk.Tags = new Dictionary<string, string>() { { tagKey, "tagvalue" } };
diskOut = m_CrpClient.Disks.Update(rgName, diskName, updatedisk);
Validate(disk, diskOut, DiskRPLocation);
}

// Get
diskOut = m_CrpClient.Disks.Get(rgName, diskName);
Expand Down Expand Up @@ -324,7 +328,7 @@ protected void Snapshot_List_Execute(DiskCreateOption diskCreateOption, string m
#region Generation
public static readonly GrantAccessData AccessDataDefault = new GrantAccessData { Access = AccessLevel.Read, DurationInSeconds = 1000 };

protected Disk GenerateDefaultDisk(DiskCreateOption diskCreateOption, string rgName, int? diskSizeGB = null)
protected Disk GenerateDefaultDisk(DiskCreateOption diskCreateOption, string rgName, int? diskSizeGB = null, IList<string> zones = null)
{
Disk disk;

Expand All @@ -333,10 +337,12 @@ protected Disk GenerateDefaultDisk(DiskCreateOption diskCreateOption, string rgN
case DiskCreateOption.Empty:
disk = GenerateBaseDisk(diskCreateOption);
disk.DiskSizeGB = diskSizeGB;
disk.Zones = zones;
break;
case DiskCreateOption.Import:
disk = GenerateImportDisk(diskCreateOption, rgName);
disk.DiskSizeGB = diskSizeGB;
disk.Zones = zones;
break;
default:
throw new ArgumentOutOfRangeException("diskCreateOption", diskCreateOption, "Unsupported option provided.");
Expand Down Expand Up @@ -433,6 +439,7 @@ private void Validate(Snapshot snapshotExpected, Snapshot snapshotActual, bool d

// snapshot properties
Assert.Equal(snapshotExpected.Sku.Name, snapshotActual.Sku.Name);
Assert.True(snapshotActual.ManagedBy == null);
Assert.NotNull(snapshotActual.ProvisioningState);
if (snapshotExpected.OsType != null) //these properties are not mandatory for the client
{
Expand Down Expand Up @@ -467,7 +474,6 @@ private void Validate(Snapshot snapshotExpected, Snapshot snapshotActual, bool d
{
Assert.Null(imgRefAct);
}

}

protected void Validate(Disk diskExpected, Disk diskActual, string location, bool diskHydrated = false)
Expand Down Expand Up @@ -510,6 +516,22 @@ protected void Validate(Disk diskExpected, Disk diskActual, string location, boo
{
Assert.Null(imgRefAct);
}

// Zones
IList<string> zonesExp = diskExpected.Zones;
IList<string> zonesAct = diskActual.Zones;
if (zonesExp != null)
{
Assert.Equal(zonesExp.Count, zonesAct.Count);
foreach (string zone in zonesExp)
{
Assert.True(zonesAct.Contains(zone, StringComparer.OrdinalIgnoreCase));
}
}
else
{
Assert.Null(zonesAct);
}
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
Param(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in the repo?

[Parameter(Mandatory = $true,
HelpMessage="Name of the resource group to which the KeyVault belongs to. A new resource group with this name will be created if one doesn't exist")]
[ValidateNotNullOrEmpty()]
[string]$resourceGroupName,

[Parameter(Mandatory = $true,
HelpMessage="Name of the KeyVault in which encryption keys are to be placed. A new vault with this name will be created if one doesn't exist")]
[ValidateNotNullOrEmpty()]
[string]$keyVaultName,

[Parameter(Mandatory = $true,
HelpMessage="Location of the KeyVault. Important note: Make sure the KeyVault and VMs to be encrypted are in the same region / location.")]
[ValidateNotNullOrEmpty()]
[string]$location,

[Parameter(Mandatory = $true,
HelpMessage="Name of the AAD application that will be used to write secrets to KeyVault. A new application with this name will be created if one doesn't exist. If this app already exists, pass aadClientSecret parameter to the script")]
[ValidateNotNullOrEmpty()]
[string]$aadAppName,

[Parameter(Mandatory = $false,
HelpMessage="Client secret of the AAD application that was created earlier")]
[ValidateNotNullOrEmpty()]
[string]$aadClientSecret,

[Parameter(Mandatory = $false,
HelpMessage="Identifier of the Azure subscription to be used. Default subscription will be used if not specified.")]
[ValidateNotNullOrEmpty()]
[string]$subscriptionId,

[Parameter(Mandatory = $false,
HelpMessage="Name of optional key encryption key in KeyVault. A new key with this name will be created if one doesn't exist")]
[ValidateNotNullOrEmpty()]
[string]$keyEncryptionKeyName

)

########################################################################################################################
# Section1: Log-in to Azure and select appropriate subscription.
########################################################################################################################


# Write-Host 'Please log into Azure now' -foregroundcolor Green;
# Login-AzureRmAccount -ErrorAction "Stop" 1> $null;

# if($subscriptionId)
# {
# Select-AzureRmSubscription -SubscriptionId $subscriptionId;
# }


########################################################################################################################
# Section2: Create AAD app . Fill in $aadClientSecret variable if AAD app was already created
########################################################################################################################


# Check if AAD app with $aadAppName was already created
$SvcPrincipals = (Get-AzureRmADServicePrincipal -SearchString $aadAppName);
if(-not $SvcPrincipals)
{
# Create a new AD application if not created before
$identifierUri = [string]::Format("http://localhost:8080/{0}",[Guid]::NewGuid().ToString("N"));
$defaultHomePage = 'http://contoso.com';
$now = [System.DateTime]::Now;
$oneYearFromNow = $now.AddYears(1);
$aadClientSecret = [Guid]::NewGuid();

Write-Host "Creating new AAD application ($aadAppName)";
$ADApp = New-AzureRmADApplication -DisplayName $aadAppName -HomePage $defaultHomePage -IdentifierUris $identifierUri -StartDate $now -EndDate $oneYearFromNow -Password $aadClientSecret;
$servicePrincipal = New-AzureRmADServicePrincipal -ApplicationId $ADApp.ApplicationId;
$SvcPrincipals = (Get-AzureRmADServicePrincipal -SearchString $aadAppName);
if(-not $SvcPrincipals)
{
# AAD app wasn't created
Write-Error "Failed to create AAD app $aadAppName. Please log-in to Azure using Login-AzureRmAccount and try again";
return;
}
$aadClientID = $servicePrincipal.ApplicationId;
Write-Host "Created a new AAD Application ($aadAppName) with ID: $aadClientID ";
}
else
{
if(-not $aadClientSecret)
{
$aadClientSecret = Read-Host -Prompt "Aad application ($aadAppName) was already created, input corresponding aadClientSecret and hit ENTER. It can be retrieved from https://manage.windowsazure.com portal" ;
}
if(-not $aadClientSecret)
{
Write-Error "Aad application ($aadAppName) was already created. Re-run the script by supplying aadClientSecret parameter with corresponding secret from https://manage.windowsazure.com portal";
return;
}
$aadClientID = $SvcPrincipals[0].ApplicationId;
}

# Before proceeding to Section3, make sure $aadClientID and $aadClientSecret have valid values
########################################################################################################################
# Section3: Create KeyVault or setup existing keyVault
########################################################################################################################

Try
{
$resGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue;
}
Catch [System.ArgumentException]
{
Write-Host "Couldn't find resource group: ($resourceGroupName)";
$resGroup = $null;
}

#Create a new resource group if it doesn't exist
if (-not $resGroup)
{
Write-Host "Creating new resource group: ($resourceGroupName)";
$resGroup = New-AzureRmResourceGroup -Name $resourceGroupName -Location $location;
Write-Host "Created a new resource group named $resourceGroupName to place keyVault";
}

Try
{
$keyVault = Get-AzureRmKeyVault -VaultName $keyVaultName -ErrorAction SilentlyContinue;
}
Catch [System.ArgumentException]
{
Write-Host "Couldn't find Key Vault: $keyVaultName";
$keyVault = $null;
}

#Create a new vault if vault doesn't exist
if (-not $keyVault)
{
Write-Host "Creating new key vault: ($keyVaultName)";
$keyVault = New-AzureRmKeyVault -VaultName $keyVaultName -ResourceGroupName $resourceGroupName -Sku Standard -Location $location;
Write-Host "Created a new KeyVault named $keyVaultName to store encryption keys";
}
# Specify privileges to the vault for the AAD application - https://msdn.microsoft.com/en-us/library/mt603625.aspx
Set-AzureRmKeyVaultAccessPolicy -VaultName $keyVaultName -ServicePrincipalName $aadClientID -PermissionsToKeys wrapKey -PermissionsToSecrets set;

Set-AzureRmKeyVaultAccessPolicy -VaultName $keyVaultName -EnabledForDiskEncryption;

$diskEncryptionKeyVaultUrl = $keyVault.VaultUri;
$keyVaultResourceId = $keyVault.ResourceId;

if($keyEncryptionKeyName)
{
Try
{
$kek = Get-AzureKeyVaultKey -VaultName $keyVaultName -Name $keyEncryptionKeyName -ErrorAction SilentlyContinue;
}
Catch [Microsoft.Azure.KeyVault.KeyVaultClientException]
{
Write-Host "Couldn't find key encryption key named : $keyEncryptionKeyName in Key Vault: $keyVaultName";
$kek = $null;
}

if(-not $kek)
{
Write-Host "Creating new key encryption key named:$keyEncryptionKeyName in Key Vault: $keyVaultName";
$kek = Add-AzureKeyVaultKey -VaultName $keyVaultName -Name $keyEncryptionKeyName -Destination Software -ErrorAction SilentlyContinue;
Write-Host "Created key encryption key named:$keyEncryptionKeyName in Key Vault: $keyVaultName";
}

$keyEncryptionKeyUrl = $kek.Key.Kid;
}

########################################################################################################################
# Section4: Displays values that should be used while enabling encryption. Please note these down
########################################################################################################################
Write-Host "Please note down below aadClientID, aadClientSecret, diskEncryptionKeyVaultUrl, keyVaultResourceId values that will be needed to enable encryption on your VMs " -foregroundcolor Green;
Write-Host "`t aadClientID: $aadClientID" -foregroundcolor Green;
Write-Host "`t aadClientSecret: $aadClientSecret" -foregroundcolor Green;
Write-Host "`t diskEncryptionKeyVaultUrl: $diskEncryptionKeyVaultUrl" -foregroundcolor Green;
Write-Host "`t keyVaultResourceId: $keyVaultResourceId" -foregroundcolor Green;
if($keyEncryptionKeyName)
{
Write-Host "`t keyEncryptionKeyURL: $keyEncryptionKeyUrl" -foregroundcolor Green;
}
Write-Host "Please Press [Enter] after saving values displayed above. They are needed to enable encryption using Set-AzureRmVmDiskEncryptionExtension cmdlet" -foregroundcolor Green;
Read-Host;

########################################################################################################################
# For each VM you want to encrypt, run the below cmdlet
# $vmName = 'Name of VM to encrypt';
# Set-AzureRmVMDiskEncryptionExtension -ResourceGroupName $resourceGroupName -VMName $vmName -AadClientID $aadClientID -AadClientSecret $aadClientSecret -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $keyVaultResourceId;
########################################################################################################################
Loading