Skip to content

Commit e6e4a93

Browse files
committed
Merge pull request #1 from SudhakaraReddyEvuri/AzureDiskEncryption
Azure disk encryption
2 parents 0bf565c + 3eebd4f commit e6e4a93

15 files changed

+209
-127
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ function Test-AzureDiskEncryptionExtension
874874
$user2 = "Bar12";
875875
$password2 = 'FoO@123' + $rgname;
876876
$aadClientID = '97f847ef-9e1e-4c7c-9ae7-1c0f7c78f761';
877-
$aadClientSecret = 'e8335678-2063-4620-afa3-5ba815e27fd3';
877+
$aadClientSecret = '';
878878
$diskEncryptionKeyVaultUrl = 'https://sureddvaultsc2.vault.azure.net'
879879
$diskEncryptionKeyVaultId = '/subscriptions/0ee9d577-9bc4-4a32-a4e8-c29981025378/resourceGroups/sureddrgsc2/providers/Microsoft.KeyVault/vaults/sureddvaultsc2';
880880
$volumeType = 'All';

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@
207207
<Compile Include="Extension\AzureDiskEncryption\AzureDiskEncryptionExtensionContext.cs" />
208208
<Compile Include="Extension\AzureDiskEncryption\AzureDiskEncryptionExtensionProtectedSettings.cs" />
209209
<Compile Include="Extension\AzureDiskEncryption\AzureDiskEncryptionExtensionPublicSettings.cs" />
210-
<Compile Include="Extension\AzureDiskEncryption\GetAzureDiskEncryptionExtension.cs" />
211210
<Compile Include="Extension\AzureDiskEncryption\GetAzureDiskEncryptionStatus.cs" />
212211
<Compile Include="Extension\AzureDiskEncryption\RemoveAzureDiskEncryptionExtension.cs" />
213212
<Compile Include="Extension\AzureDiskEncryption\SetAzureDiskEncryptionExtension.cs" />

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ public static class ProfileNouns
117117

118118
//AzureDiskEncryption
119119
public const string AzureDiskEncryptionExtension = "AzureRmVMDiskEncryptionExtension";
120-
public const string AzureDiskEncryptionStatus = "AzureRmVmDiskEncryptionStatus";
120+
public const string AzureDiskEncryptionStatus = "AzureRmVMDiskEncryptionStatus";
121121

122122
//AzureVMBackup
123123
public const string AzureVMBackup = "AzureRmVMBackup";
124124
public const string AzureVMBackupExtension = "AzureRmVMBackupExtension";
125+
125126
}
126127
}

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/AzureDiskEncryptionExtensionContext.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Newtonsoft.Json;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Newtonsoft.Json;
216
using System.Security;
317
using Microsoft.Azure.Commands.Compute.Models;
418

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/AzureDiskEncryptionExtensionProtectedSettings.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-

1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
216
namespace Microsoft.Azure.Commands.Compute.Extension.AzureDiskEncryption
317
{
418
class AzureDiskEncryptionExtensionProtectedSettings

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/AzureDiskEncryptionExtensionPublicSettings.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-

1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
216
namespace Microsoft.Azure.Commands.Compute.Extension.AzureDiskEncryption
317
{
418
public class AzureDiskEncryptionExtensionPublicSettings

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/GetAzureDiskEncryptionExtension.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/GetAzureDiskEncryptionStatus.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Microsoft.Azure.Commands.Compute.Common;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Compute.Common;
216
using Microsoft.Azure.Commands.Compute.Models;
317
using Microsoft.Azure.Management.Compute;
418
using System;

src/ResourceManager/Compute/Commands.Compute/Extension/AzureDiskEncryption/RemoveAzureDiskEncryptionExtension.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Microsoft.Azure.Commands.Compute.Common;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Compute.Common;
216
using Microsoft.Azure.Management.Compute;
317
using System.Management.Automation;
418

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

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Microsoft.Azure.Commands.Compute.Common;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Compute.Common;
216
using Microsoft.Azure.Commands.Compute.Models;
317
using Microsoft.Azure.Management.Compute;
418
using Microsoft.Azure.Management.Compute.Models;
@@ -151,6 +165,8 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
151165
[ValidateNotNullOrEmpty]
152166
public SwitchParameter Force { get; set; }
153167

168+
private string currentOSType = null;
169+
154170
private void ValidateInputParameters()
155171
{
156172
if (false == Uri.IsWellFormedUriString(DiskEncryptionKeyVaultId, UriKind.Absolute))
@@ -192,24 +208,24 @@ private string GetExtensionStatusMessage()
192208
ErrorCategory.InvalidResult,
193209
null));
194210
}
195-
bool publisherMismatch = false;
211+
bool publisherMatch = false;
196212
if (string.Equals(currentOSType, "Linux", StringComparison.InvariantCultureIgnoreCase))
197213
{
198214
if (returnedExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
199-
returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase))
215+
returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase))
200216
{
201-
publisherMismatch = true;
217+
publisherMatch = true;
202218
}
203219
}
204-
else if(string.Equals(currentOSType,"Windows",StringComparison.InvariantCultureIgnoreCase))
220+
else if (string.Equals(currentOSType, "Windows", StringComparison.InvariantCultureIgnoreCase))
205221
{
206222
if (returnedExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
207-
returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase))
223+
returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase))
208224
{
209-
publisherMismatch = true;
225+
publisherMatch = true;
210226
}
211227
}
212-
if (publisherMismatch)
228+
if (publisherMatch)
213229
{
214230
AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext(returnedExtension);
215231
if ((context == null) ||
@@ -305,13 +321,11 @@ private string GetExtensionProtectedSettings()
305321
return JsonConvert.SerializeObject(protectedSettings);
306322
}
307323

308-
private VirtualMachineExtension GetVmExtensionParameters()
324+
private VirtualMachineExtension GetVmExtensionParameters(VirtualMachine vmParameters)
309325
{
310326
string SettingString = GetExtensionPublicSettings();
311327
string ProtectedSettingString = GetExtensionProtectedSettings();
312328

313-
314-
VirtualMachine vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName)).VirtualMachine;
315329
if (vmParameters == null)
316330
{
317331
ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture, "Set-AzureDiskEncryptionExtension can enable encryption only on a VM that was already created ")),
@@ -353,7 +367,29 @@ private VirtualMachineExtension GetVmExtensionParameters()
353367
return vmExtensionParameters;
354368
}
355369

356-
private string currentOSType = null;
370+
private void CreateVMBackupForLinx()
371+
{
372+
try
373+
{
374+
AzureVMBackupExtensionUtil azureBackupExtensionUtil = new AzureVMBackupExtensionUtil();
375+
AzureVMBackupConfig vmConfig = new AzureVMBackupConfig();
376+
vmConfig.ResourceGroupName = ResourceGroupName;
377+
vmConfig.VMName = VMName;
378+
vmConfig.VirtualMachineExtensionType = VirtualMachineExtensionType;
379+
string tag = string.Format("{0}{1}", "AzureEnc", Guid.NewGuid().ToString());
380+
// this would create shapshot only for Linux box. and we should wait for the snapshot found.
381+
azureBackupExtensionUtil.CreateSnapshotForDisks(vmConfig, tag, this);
382+
383+
WriteInformation(new InformationRecord(string.Format("one snapshot for disks are created with tag,{0}", tag), string.Empty));
384+
}
385+
catch (AzureVMBackupException e)
386+
{
387+
ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture, e.ToString())),
388+
"InvalidResult",
389+
ErrorCategory.InvalidResult,
390+
null));
391+
}
392+
}
357393

358394
protected override void ProcessRecord()
359395
{
@@ -368,37 +404,16 @@ protected override void ProcessRecord()
368404

369405
currentOSType = virtualMachineResponse.StorageProfile.OSDisk.OperatingSystemType;
370406

371-
if(string.Equals(currentOSType,"Linux",StringComparison.InvariantCultureIgnoreCase))
407+
if (string.Equals(currentOSType, "Linux", StringComparison.InvariantCultureIgnoreCase))
372408
{
373-
try
374-
{
375-
AzureVMBackupExtensionUtil azureBackupExtensionUtil = new AzureVMBackupExtensionUtil();
376-
AzureVMBackupConfig vmConfig = new AzureVMBackupConfig();
377-
vmConfig.ResourceGroupName = ResourceGroupName;
378-
vmConfig.VMName = VMName;
379-
vmConfig.VirtualMachineExtensionType = VirtualMachineExtensionType;
380-
string tag = string.Format("{0}{1}", "AzureEnc", Guid.NewGuid().ToString());
381-
// this would create shapshot only for Linux box. and we should wait for the snapshot found.
382-
azureBackupExtensionUtil.CreateSnapshotForDisks(vmConfig, tag, this);
383-
384-
WriteInformation(new InformationRecord(string.Format("one snapshot for disks are created with tag,{0}",tag), string.Empty));
385-
}
386-
catch (AzureVMBackupException e)
387-
{
388-
ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture, e.ToString())),
389-
"InvalidResult",
390-
ErrorCategory.InvalidResult,
391-
null));
392-
}
409+
CreateVMBackupForLinx();
393410
}
394411

395-
VirtualMachineExtension parameters = GetVmExtensionParameters();
412+
VirtualMachineExtension parameters = GetVmExtensionParameters(virtualMachineResponse);
396413

397414
this.VirtualMachineExtensionClient.CreateOrUpdate(this.ResourceGroupName,
398-
this.VMName,
399-
parameters);
400-
401-
415+
this.VMName,
416+
parameters);
402417

403418
var op = UpdateVmEncryptionSettings();
404419
WriteObject(Mapper.Map<PSComputeLongRunningOperation>(op));

src/ResourceManager/Compute/Commands.Compute/Extension/AzureVMBackup/AzureVMBackupConfig.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
216
using System.Collections.Generic;
317
using System.Linq;
418
using System.Text;

src/ResourceManager/Compute/Commands.Compute/Extension/AzureVMBackup/AzureVMBackupException.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
216
using System.Collections.Generic;
317
using System.Linq;
418
using System.Text;

0 commit comments

Comments
 (0)