Skip to content

Commit e47f120

Browse files
committed
Merge pull request #50 from hyonholee/de
Update Set-AzureRmOsDisk cmdlet for Disk Encryption
2 parents 93acb9c + 04d9c7b commit e47f120

File tree

10 files changed

+155
-27
lines changed

10 files changed

+155
-27
lines changed

src/ResourceManager/Compute/Commands.Compute.Test/Commands.Compute.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.1.0.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
6868
</Reference>
6969
<Reference Include="Microsoft.Azure.Management.Compute, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
70-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.9.0.0\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
71-
<Private>True</Private>
70+
<SpecificVersion>False</SpecificVersion>
71+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.9.1.0\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
7272
</Reference>
7373
<Reference Include="Microsoft.Azure.Management.Network, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
7474
<SpecificVersion>False</SpecificVersion>

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -398,27 +398,29 @@ function Get-SasUri
398398
# Get a Location according to resource provider.
399399
function Get-ResourceProviderLocation
400400
{
401-
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
402-
{
403-
$namespace = $provider.Split("/")[0]
404-
if($provider.Contains("/"))
405-
{
406-
$type = $provider.Substring($namespace.Length + 1)
407-
$location = Get-AzureRmResourceProvider -ProviderNamespace $namespace | where {$_.ResourceTypes[0].ResourceTypeName -eq $type}
401+
param ([string] $provider)
402+
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
403+
{
404+
$namespace = $provider.Split("/")[0]
405+
if($provider.Contains("/"))
406+
{
407+
$type = $provider.Substring($namespace.Length + 1)
408+
$location = Get-AzureRmResourceProvider -ProviderNamespace $namespace | where {$_.ResourceTypes[0].ResourceTypeName -eq $type}
408409

409-
if ($location -eq $null)
410-
{
411-
return "West US"
412-
} else
413-
{
414-
return $location.Locations[0]
415-
}
416-
}
410+
if ($location -eq $null)
411+
{
412+
return "West US"
413+
}
414+
else
415+
{
416+
return $location.Locations[0]
417+
}
418+
}
417419

418-
return "West US"
419-
}
420+
return "West US"
421+
}
420422

421-
return "WestUS"
423+
return "WestUS"
422424
}
423425

424426
function Get-ComputeVMLocation

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineProfileTests.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,22 @@ function Test-VirtualMachineProfileWithoutAUC
244244
$dataDiskVhdUri2 = "https://$stoname.blob.core.windows.net/test/data2.vhd";
245245
$dataDiskVhdUri3 = "https://$stoname.blob.core.windows.net/test/data3.vhd";
246246

247-
$p = Set-AzureRmVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption Empty;
247+
$dekUri = "https://testvault123.vault.azure.net/secrets/Test1/514ceb769c984379a7e0230bddaaaaaa";
248+
$dekId = "/subscriptions/" + $subid + "/resourceGroups/RgTest1/providers/Microsoft.KeyVault/vaults/TestVault123";
249+
$kekUri = "http://keyVaultName.vault.azure.net/secrets/secretName/secretVersion";
250+
$kekId = "/subscriptions/" + $subid + "/resourceGroups/RgTest1/providers/Microsoft.KeyVault/vaults/TestVault123";
251+
252+
$p = Set-AzureRmVMOSDisk -VM $p -Windows -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption Empty -DiskEncryptionKeyUrl $dekUri -DiskEncryptionKeyVaultId $dekId -KeyEncryptionKeyUrl $kekUri -KeyEncryptionKeyVaultId $kekId;
248253

249254
$p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 0 -VhdUri $dataDiskVhdUri1 -CreateOption Empty;
250255
$p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk2' -Caching 'ReadOnly' -DiskSizeInGB 11 -Lun 1 -VhdUri $dataDiskVhdUri2 -CreateOption Empty;
251256
$p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk3' -Caching 'ReadOnly' -DiskSizeInGB 12 -Lun 2 -VhdUri $dataDiskVhdUri3 -CreateOption Empty;
252257
$p = Remove-AzureRmVMDataDisk -VM $p -Name 'testDataDisk3';
253258

259+
Assert-AreEqual $p.StorageProfile.OSDisk.EncryptionSettings.DiskEncryptionKey.SourceVault.ReferenceUri $dekId
260+
Assert-AreEqual $p.StorageProfile.OSDisk.EncryptionSettings.DiskEncryptionKey.SecretUrl $dekUri
261+
Assert-AreEqual $p.StorageProfile.OSDisk.EncryptionSettings.KeyEncryptionKey.SourceVault.ReferenceUri $kekId
262+
Assert-AreEqual $p.StorageProfile.OSDisk.EncryptionSettings.KeyEncryptionKey.KeyUrl $kekUri
254263
Assert-AreEqual $p.StorageProfile.OSDisk.Caching $osDiskCaching;
255264
Assert-AreEqual $p.StorageProfile.OSDisk.Name $osDiskName;
256265
Assert-AreEqual $p.StorageProfile.OSDisk.VirtualHardDisk.Uri $osDiskVhdUri;

src/ResourceManager/Compute/Commands.Compute.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<package id="Microsoft.Azure.Gallery" version="2.6.2-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
10-
<package id="Microsoft.Azure.Management.Compute" version="9.0.0" targetFramework="net45" />
10+
<package id="Microsoft.Azure.Management.Compute" version="9.1.0" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Management.Network" version="2.0.13-preview" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
1313
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@
8181
<Private>True</Private>
8282
</Reference>
8383
<Reference Include="Microsoft.Azure.Management.Compute, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
84-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.9.0.0\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
85-
<Private>True</Private>
84+
<SpecificVersion>False</SpecificVersion>
85+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.9.1.0\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
8686
</Reference>
8787
<Reference Include="Microsoft.Azure.Management.Network, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
8888
<SpecificVersion>False</SpecificVersion>

src/ResourceManager/Compute/Commands.Compute/Common/ConstantStringTypes.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public static class HelpMessages
2828
public const string VMOSDiskCaching = "The virtual machine OS disk's caching.";
2929
public const string VMOSDiskWindowsOSType = "The virtual machine disk's OS is Windows.";
3030
public const string VMOSDiskLinuxOSType = "The virtual machine disk's OS is Linux.";
31+
public const string VMOSDiskDiskEncryptionKeyUrl = "the URL referencing a secret in a disk encryption key vault";
32+
public const string VMOSDiskDiskEncryptionKeyVaultId = "the Id of a disk encryption key vault";
33+
public const string VMOSDiskKeyEncryptionKeyUrl = "the URL referencing a key in a key encryption key vault";
34+
public const string VMOSDiskKeyEncryptionKeyVaultId = "the Id of a key encryption key Vault";
3135
public const string VMSourceImageUri = "The virtual machine OS disk's source image Uri.";
3236

3337
public const string VMDataDiskName = "The virtual machine data disk's name.";

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,7 @@ The file needs to be a PowerShell script (.ps1 or .psm1) or a ZIP archive (.zip)
452452
<value>Since the VM is created using premium storage, existing standard storage account, {0}, is used for boot diagnostics.</value>
453453
<comment>{0} = existing standard storage account name</comment>
454454
</data>
455+
<data name="VMOSDiskDiskEncryptionBothKekVaultIdAndKekUrlRequired" xml:space="preserve">
456+
<value>You have to specify either both of KeyEncryptionKeyVaultId and KeyEncryptionKeyUrl or none of them.</value>
457+
</data>
455458
</root>

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMOSDiskCommand.cs

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ namespace Microsoft.Azure.Commands.Compute
2525
[Cmdlet(
2626
VerbsCommon.Set,
2727
ProfileNouns.OSDisk,
28-
DefaultParameterSetName = WindowsParamSet),
28+
DefaultParameterSetName = DefaultParamSet),
2929
OutputType(
3030
typeof(PSVirtualMachine))]
3131
public class SetAzureVMOSDiskCommand : Microsoft.Azure.Commands.ResourceManager.Common.AzureRMCmdlet
3232
{
33+
protected const string DefaultParamSet = "DefaultParamSet";
3334
protected const string WindowsParamSet = "WindowsParamSet";
3435
protected const string LinuxParamSet = "LinuxParamSet";
36+
protected const string WindowsAndDiskEncryptionParameterSet = "WindowsDiskEncryptionParameterSet";
37+
protected const string LinuxAndDiskEncryptionParameterSet = "LinuxDiskEncryptionParameterSet";
3538

3639
[Alias("VMProfile")]
3740
[Parameter(
@@ -90,22 +93,96 @@ public class SetAzureVMOSDiskCommand : Microsoft.Azure.Commands.ResourceManager.
9093
Position = 6,
9194
ValueFromPipelineByPropertyName = true,
9295
HelpMessage = HelpMessages.VMOSDiskWindowsOSType)]
96+
[Parameter(
97+
ParameterSetName = WindowsAndDiskEncryptionParameterSet,
98+
Position = 6,
99+
ValueFromPipelineByPropertyName = true,
100+
HelpMessage = HelpMessages.VMOSDiskWindowsOSType)]
93101
public SwitchParameter Windows { get; set; }
94102

95103
[Parameter(
96104
ParameterSetName = LinuxParamSet,
97105
Position = 6,
98106
ValueFromPipelineByPropertyName = true,
99107
HelpMessage = HelpMessages.VMOSDiskLinuxOSType)]
108+
[Parameter(
109+
ParameterSetName = LinuxAndDiskEncryptionParameterSet,
110+
Position = 6,
111+
ValueFromPipelineByPropertyName = true,
112+
HelpMessage = HelpMessages.VMOSDiskLinuxOSType)]
100113
public SwitchParameter Linux { get; set; }
101114

115+
[Parameter(
116+
ParameterSetName = WindowsAndDiskEncryptionParameterSet,
117+
Mandatory = true,
118+
Position = 7,
119+
ValueFromPipelineByPropertyName = true,
120+
HelpMessage = HelpMessages.VMOSDiskDiskEncryptionKeyUrl)]
121+
[Parameter(
122+
ParameterSetName = LinuxAndDiskEncryptionParameterSet,
123+
Mandatory = true,
124+
Position = 7,
125+
ValueFromPipelineByPropertyName = true,
126+
HelpMessage = HelpMessages.VMOSDiskDiskEncryptionKeyUrl)]
127+
public string DiskEncryptionKeyUrl { get; set; }
128+
129+
[Parameter(
130+
ParameterSetName = WindowsAndDiskEncryptionParameterSet,
131+
Mandatory = true,
132+
Position = 8,
133+
ValueFromPipelineByPropertyName = true,
134+
HelpMessage = HelpMessages.VMOSDiskDiskEncryptionKeyVaultId)]
135+
[Parameter(
136+
ParameterSetName = LinuxAndDiskEncryptionParameterSet,
137+
Mandatory = true,
138+
Position = 8,
139+
ValueFromPipelineByPropertyName = true,
140+
HelpMessage = HelpMessages.VMOSDiskDiskEncryptionKeyVaultId)]
141+
public string DiskEncryptionKeyVaultId { get; set; }
142+
143+
[Parameter(
144+
ParameterSetName = WindowsAndDiskEncryptionParameterSet,
145+
Mandatory = false,
146+
Position = 9,
147+
ValueFromPipelineByPropertyName = true,
148+
HelpMessage = HelpMessages.VMOSDiskKeyEncryptionKeyUrl)]
149+
[Parameter(
150+
ParameterSetName = LinuxAndDiskEncryptionParameterSet,
151+
Mandatory = false,
152+
Position = 9,
153+
ValueFromPipelineByPropertyName = true,
154+
HelpMessage = HelpMessages.VMOSDiskKeyEncryptionKeyUrl)]
155+
public string KeyEncryptionKeyUrl { get; set; }
156+
157+
[Parameter(
158+
ParameterSetName = WindowsAndDiskEncryptionParameterSet,
159+
Mandatory = false,
160+
Position = 10,
161+
ValueFromPipelineByPropertyName = true,
162+
HelpMessage = HelpMessages.VMOSDiskKeyEncryptionKeyVaultId)]
163+
[Parameter(
164+
ParameterSetName = LinuxAndDiskEncryptionParameterSet,
165+
Mandatory = false,
166+
Position = 10,
167+
ValueFromPipelineByPropertyName = true,
168+
HelpMessage = HelpMessages.VMOSDiskKeyEncryptionKeyVaultId)]
169+
public string KeyEncryptionKeyVaultId { get; set; }
170+
102171
protected override void ProcessRecord()
103172
{
104173
if (this.VM.StorageProfile == null)
105174
{
106175
this.VM.StorageProfile = new StorageProfile();
107176
}
108177

178+
if ((string.IsNullOrEmpty(this.KeyEncryptionKeyVaultId) && !string.IsNullOrEmpty(this.KeyEncryptionKeyUrl))
179+
|| (!string.IsNullOrEmpty(this.KeyEncryptionKeyVaultId) && string.IsNullOrEmpty(this.KeyEncryptionKeyUrl)))
180+
{
181+
WriteError(new ErrorRecord(
182+
new Exception(Properties.Resources.VMOSDiskDiskEncryptionBothKekVaultIdAndKekUrlRequired),
183+
string.Empty, ErrorCategory.InvalidArgument, null));
184+
}
185+
109186
this.VM.StorageProfile.OSDisk = new OSDisk
110187
{
111188
Caching = this.Caching,
@@ -119,7 +196,31 @@ protected override void ProcessRecord()
119196
{
120197
Uri = this.SourceImageUri
121198
},
122-
CreateOption = this.CreateOption
199+
CreateOption = this.CreateOption,
200+
EncryptionSettings =
201+
(this.ParameterSetName.Equals(WindowsAndDiskEncryptionParameterSet) || this.ParameterSetName.Equals(LinuxAndDiskEncryptionParameterSet))
202+
? new DiskEncryptionSettings
203+
{
204+
DiskEncryptionKey = new KeyVaultSecretReference
205+
{
206+
SourceVault = new SourceVaultReference
207+
{
208+
ReferenceUri = this.DiskEncryptionKeyVaultId
209+
},
210+
SecretUrl = this.DiskEncryptionKeyUrl
211+
},
212+
KeyEncryptionKey = (this.KeyEncryptionKeyVaultId == null || this.KeyEncryptionKeyUrl == null)
213+
? null
214+
: new KeyVaultKeyReference
215+
{
216+
KeyUrl = this.KeyEncryptionKeyUrl,
217+
SourceVault = new SourceVaultReference
218+
{
219+
ReferenceUri = this.KeyEncryptionKeyVaultId
220+
},
221+
}
222+
}
223+
: null
123224
};
124225

125226
WriteObject(this.VM);

src/ResourceManager/Compute/Commands.Compute/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
1010
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
12-
<package id="Microsoft.Azure.Management.Compute" version="9.0.0" targetFramework="net45" />
12+
<package id="Microsoft.Azure.Management.Compute" version="9.1.0" targetFramework="net45" />
1313
<package id="Microsoft.Azure.Management.Network" version="2.0.13-preview" targetFramework="net45" />
1414
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
1515
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />

0 commit comments

Comments
 (0)