Skip to content

Commit 692d6f0

Browse files
committed
Merge pull request #1 from andyliuliming/dev
encryption status check for azure linux vm.
2 parents 8bcf538 + 63ef694 commit 692d6f0

File tree

7 files changed

+199
-47
lines changed

7 files changed

+199
-47
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
<Compile Include="Extension\AzureDiskEncryption\AzureDiskEncryptionExtensionPublicSettings.cs" />
226226
<Compile Include="Extension\AzureDiskEncryption\DisableAzureDiskEncryption.cs" />
227227
<Compile Include="Extension\AzureDiskEncryption\GetAzureDiskEncryptionStatus.cs" />
228+
<Compile Include="Extension\AzureDiskEncryption\OSType.cs" />
228229
<Compile Include="Extension\AzureDiskEncryption\RemoveAzureDiskEncryptionExtension.cs" />
229230
<Compile Include="Extension\AzureDiskEncryption\SetAzureDiskEncryptionExtension.cs" />
230231
<Compile Include="Extension\AzureVMBackup\AzureVMBackupExtensionUtil.cs" />

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

Lines changed: 113 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
using Microsoft.Azure.Commands.Compute.Common;
1616
using Microsoft.Azure.Commands.Compute.Models;
1717
using Microsoft.Azure.Management.Compute;
18-
using Microsoft.Azure.Management.Compute.Models;
1918
using System;
2019
using System.Management.Automation;
20+
using Microsoft.Azure.Management.Compute.Models;
21+
using System.Globalization;
2122

2223
namespace Microsoft.Azure.Commands.Compute.Extension.AzureDiskEncryption
2324
{
@@ -44,16 +45,42 @@ public class GetAzureDiskEncryptionStatusCommand : VirtualMachineExtensionBaseCm
4445
[ValidateNotNullOrEmpty]
4546
public string VMName { get; set; }
4647

47-
private bool IsOsVolumeEncrypted(VirtualMachine vmParameters)
48+
private OSType GetOSType(VirtualMachine vmParameters)
4849
{
49-
var osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings(vmParameters);
50-
if (osVolumeEncryptionSettings != null)
50+
if (vmParameters == null || vmParameters.StorageProfile == null || vmParameters.StorageProfile.OsDisk == null)
5151
{
52-
return (osVolumeEncryptionSettings.Enabled == true
53-
&& !string.IsNullOrWhiteSpace(osVolumeEncryptionSettings.DiskEncryptionKey.SecretUrl));
52+
return OSType.Unknown;
53+
}
54+
else
55+
{
56+
if (OperatingSystemTypes.Linux == vmParameters.StorageProfile.OsDisk.OsType)
57+
{
58+
return OSType.Linux;
59+
}
60+
if (OperatingSystemTypes.Windows == vmParameters.StorageProfile.OsDisk.OsType)
61+
{
62+
return OSType.Windows;
63+
}
64+
return OSType.Unknown;
65+
}
66+
}
67+
private EncryptionStatus IsOsVolumeEncrypted(VirtualMachine vmParameters)
68+
{
69+
OSType osType = this.GetOSType(vmParameters);
70+
switch (osType)
71+
{
72+
case OSType.Windows:
73+
if (GetOsVolumeEncryptionSettings(vmParameters) != null)
74+
{
75+
return EncryptionStatus.Encrypted;
76+
}
77+
else
78+
{
79+
return EncryptionStatus.NotEncrypted;
80+
}
81+
default:
82+
return EncryptionStatus.Unknown;
5483
}
55-
56-
return false;
5784
}
5885

5986
private DiskEncryptionSettings GetOsVolumeEncryptionSettings(VirtualMachine vmParameters)
@@ -66,18 +93,38 @@ private DiskEncryptionSettings GetOsVolumeEncryptionSettings(VirtualMachine vmPa
6693
}
6794
return null;
6895
}
69-
private bool IsAzureDiskEncryptionExtension(VirtualMachineExtension vmExtension)
96+
97+
private bool IsAzureDiskEncryptionExtension(OSType osType, VirtualMachineExtension vmExtension)
7098
{
71-
if ((vmExtension != null) &&
72-
(vmExtension.Publisher != null) &&
73-
(vmExtension.VirtualMachineExtensionType != null) &&
74-
(vmExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase)) &&
75-
(vmExtension.VirtualMachineExtensionType.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase)))
99+
switch (osType)
76100
{
77-
return true;
78-
}
101+
case OSType.Windows:
102+
if ((vmExtension != null) &&
103+
(vmExtension.Publisher != null) &&
104+
(vmExtension.VirtualMachineExtensionType != null) &&
105+
(vmExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase)) &&
106+
(vmExtension.VirtualMachineExtensionType.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase)))
107+
{
108+
return true;
109+
}
79110

80-
return false;
111+
return false;
112+
case OSType.Linux:
113+
if ((vmExtension != null) &&
114+
(vmExtension.Publisher != null) &&
115+
(vmExtension.VirtualMachineExtensionType != null) &&
116+
(vmExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase)) &&
117+
(vmExtension.VirtualMachineExtensionType.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase)))
118+
{
119+
return true;
120+
}
121+
122+
return false;
123+
case OSType.Unknown:
124+
return false;
125+
default:
126+
return false;
127+
}
81128
}
82129

83130
private bool DataVolumeInExtensionConfig(AzureDiskEncryptionExtensionContext adeExtension)
@@ -108,32 +155,39 @@ private bool ExtensionProvisioningSucceeded(AzureDiskEncryptionExtensionContext
108155
return false;
109156
}
110157

111-
private bool AreDataVolumesEncrypted(VirtualMachine vmParameters)
158+
private EncryptionStatus AreDataVolumesEncrypted(VirtualMachine vmParameters)
112159
{
113160
if (vmParameters == null || vmParameters.Resources == null)
114161
{
115-
return false;
162+
return EncryptionStatus.Unknown;
116163
}
117164

165+
OSType osType = this.GetOSType(vmParameters);
118166
foreach (VirtualMachineExtension vmExtension in vmParameters.Resources)
119167
{
120-
if (IsAzureDiskEncryptionExtension(vmExtension))
168+
switch (osType)
121169
{
122-
AzureDiskEncryptionExtensionContext adeExtension = new AzureDiskEncryptionExtensionContext(vmExtension.ToPSVirtualMachineExtension(this.ResourceGroupName));
123-
if (DataVolumeInExtensionConfig(adeExtension))
124-
{
125-
if (adeExtension.EncryptionOperation.Equals(AzureDiskEncryptionExtensionConstants.enableEncryptionOperation, StringComparison.InvariantCultureIgnoreCase))
170+
case OSType.Windows:
171+
case OSType.Linux:
172+
if (IsAzureDiskEncryptionExtension(osType, vmExtension))
126173
{
127-
if (ExtensionProvisioningSucceeded(adeExtension))
174+
AzureDiskEncryptionExtensionContext adeExtension = new AzureDiskEncryptionExtensionContext(vmExtension.ToPSVirtualMachineExtension(this.ResourceGroupName));
175+
if (DataVolumeInExtensionConfig(adeExtension))
128176
{
129-
return true;
177+
if (ExtensionProvisioningSucceeded(adeExtension))
178+
{
179+
return EncryptionStatus.Encrypted;
180+
}
130181
}
131182
}
132-
}
183+
break;
184+
case OSType.Unknown:
185+
return EncryptionStatus.Unknown;
186+
default:
187+
return EncryptionStatus.Unknown;
133188
}
134189
}
135-
136-
return false;
190+
return EncryptionStatus.NotEncrypted;
137191
}
138192

139193
public override void ExecuteCmdlet()
@@ -144,19 +198,40 @@ public override void ExecuteCmdlet()
144198
{
145199
VirtualMachine vmParameters = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName));
146200

147-
bool osVolumeEncrypted = IsOsVolumeEncrypted(vmParameters);
201+
EncryptionStatus osVolumeEncrypted = IsOsVolumeEncrypted(vmParameters);
148202
DiskEncryptionSettings osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings(vmParameters);
149-
bool dataVolumesEncrypted = AreDataVolumesEncrypted(vmParameters);
203+
EncryptionStatus dataVolumesEncrypted = AreDataVolumesEncrypted(vmParameters);
150204

151-
AzureDiskEncryptionStatusContext encryptionStatus = new AzureDiskEncryptionStatusContext
205+
OSType osType = GetOSType(vmParameters);
206+
switch (osType)
152207
{
153-
OsVolumeEncrypted = osVolumeEncrypted,
154-
OsVolumeEncryptionSettings = osVolumeEncryptionSettings,
155-
DataVolumesEncrypted = dataVolumesEncrypted
156-
};
157-
WriteObject(encryptionStatus);
208+
case OSType.Windows:
209+
AzureDiskEncryptionStatusContext encryptionStatus = new AzureDiskEncryptionStatusContext
210+
{
211+
OsVolumeEncrypted = osVolumeEncrypted,
212+
OsVolumeEncryptionSettings = osVolumeEncryptionSettings,
213+
DataVolumesEncrypted = dataVolumesEncrypted
214+
};
215+
WriteObject(encryptionStatus);
216+
break;
217+
case OSType.Linux:
218+
AzureDiskEncryptionStatusLinuxContext encryptionStatusLinux = new AzureDiskEncryptionStatusLinuxContext
219+
{
220+
OsVolumeEncrypted = osVolumeEncrypted,
221+
OsVolumeEncryptionSettings = null,
222+
DataVolumesEncrypted = dataVolumesEncrypted,
223+
DataVolumeEncryptionSettings = osVolumeEncryptionSettings
224+
};
225+
WriteObject(encryptionStatusLinux);
226+
break;
227+
case OSType.Unknown:
228+
ThrowTerminatingError(new ErrorRecord(new ApplicationException(string.Format(CultureInfo.CurrentUICulture, "OS type unknown.")),
229+
"InvalidResult",
230+
ErrorCategory.InvalidResult,
231+
null));
232+
break;
233+
}
158234
});
159-
160235
}
161236
}
162237
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Commands.Compute.Extension.AzureDiskEncryption
8+
{
9+
enum OSType
10+
{
11+
Windows,
12+
Linux,
13+
Unknown
14+
}
15+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class AzureVMBackupErrorCodes
2525
public const int TimeOut = 1;
2626
public const int OSNotSupported = 2;
2727
public const int WrongBlobUriFormat = 3;
28+
public const int NoSnapshotFound = 4;
2829
}
2930

3031
public class AzureVMBackupException : Exception

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
using Microsoft.Azure.Commands.Compute.Extension.AzureDiskEncryption;
1717
using Microsoft.Azure.Commands.Compute.Models;
1818
using Microsoft.Azure.Commands.Compute.StorageServices;
19-
using Microsoft.Azure.ServiceManagemenet.Common;
20-
using Microsoft.Azure.ServiceManagemenet.Common.Models;
2119
using Microsoft.Azure.Management.Compute;
2220
using Microsoft.Azure.Management.Compute.Models;
2321
using Microsoft.Azure.Management.Storage;
@@ -32,9 +30,9 @@
3230
using System.Text;
3331
using System.Threading;
3432
using System.Threading.Tasks;
33+
using Microsoft.Azure.Commands.Compute.Common;
3534
using Microsoft.Azure.Commands.Common.Authentication;
3635
using Microsoft.Azure.Commands.Common.Authentication.Models;
37-
using Microsoft.Azure.Commands.Compute.Common;
3836

3937
namespace Microsoft.Azure.Commands.Compute.Extension.AzureVMBackup
4038
{
@@ -174,10 +172,18 @@ public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, Vir
174172
List<string> blobUris = this.GetDiskBlobUris(virtualMachineResponse.Body);
175173

176174
Dictionary<string, string> snapshotQuery = new Dictionary<string, string>();
177-
List<CloudPageBlob> snapshots = this.FindSnapshot(blobUris, snapshotQuery, storageCredentialsFactory);
178-
foreach (CloudPageBlob snapshot in snapshots)
175+
snapshotQuery.Add(backupExtensionMetadataName, snapshotTag);
176+
List <CloudPageBlob> snapshots = this.FindSnapshot(blobUris, snapshotQuery, storageCredentialsFactory);
177+
if (snapshots == null || snapshots.Count == 0)
179178
{
180-
snapshot.Delete();
179+
throw new AzureVMBackupException(AzureVMBackupErrorCodes.NoSnapshotFound, "snapshot with the tag not found.");
180+
}
181+
else
182+
{
183+
foreach (CloudPageBlob snapshot in snapshots)
184+
{
185+
snapshot.Delete();
186+
}
181187
}
182188
}
183189

src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.format.ps1xml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,36 @@
636636
</ListEntries>
637637
</ListControl>
638638
</View>
639-
639+
<View>
640+
<Name>Microsoft.Azure.Commands.Compute.Models.AzureDiskEncryptionStatusLinuxContext</Name>
641+
<OutOfBand />
642+
<ViewSelectedBy>
643+
<TypeName>Microsoft.Azure.Commands.Compute.Models.AzureDiskEncryptionStatusLinuxContext</TypeName>
644+
</ViewSelectedBy>
645+
<ListControl>
646+
<ListEntries>
647+
<ListEntry>
648+
<ListItems>
649+
<ListItem>
650+
<Label>OsVolumeEncrypted</Label>
651+
<PropertyName>OsVolumeEncrypted</PropertyName>
652+
</ListItem>
653+
<ListItem>
654+
<Label>OsVolumeEncryptionSettings</Label>
655+
<PropertyName>OsVolumeEncryptionSettingsText</PropertyName>
656+
</ListItem>
657+
<ListItem>
658+
<Label>DataVolumesEncrypted</Label>
659+
<PropertyName>DataVolumesEncrypted</PropertyName>
660+
</ListItem>
661+
<ListItem>
662+
<Label>DataVolumeEncryptionSettings</Label>
663+
<PropertyName>DataVolumeEncryptionSettingsText</PropertyName>
664+
</ListItem>
665+
</ListItems>
666+
</ListEntry>
667+
</ListEntries>
668+
</ListControl>
669+
</View>
640670
</ViewDefinitions>
641671
</Configuration>

src/ResourceManager/Compute/Commands.Compute/Models/AzureDiskEncryptionStatusContext.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,40 @@
33

44
namespace Microsoft.Azure.Commands.Compute.Models
55
{
6+
enum EncryptionStatus
7+
{
8+
Encrypted,
9+
NotEncrypted,
10+
Unknown
11+
}
12+
613
class AzureDiskEncryptionStatusContext
714
{
8-
public bool OsVolumeEncrypted { get; set; }
15+
public EncryptionStatus OsVolumeEncrypted { get; set; }
16+
public DiskEncryptionSettings OsVolumeEncryptionSettings { get; set; }
17+
public EncryptionStatus DataVolumesEncrypted { get; set; }
18+
[JsonIgnore]
19+
public string OsVolumeEncryptionSettingsText
20+
{
21+
get { return JsonConvert.SerializeObject(OsVolumeEncryptionSettings, Formatting.Indented); }
22+
}
23+
}
24+
class AzureDiskEncryptionStatusLinuxContext
25+
{
26+
public EncryptionStatus OsVolumeEncrypted { get; set; }
927
public DiskEncryptionSettings OsVolumeEncryptionSettings { get; set; }
28+
public DiskEncryptionSettings DataVolumeEncryptionSettings { get; set; }
1029

1130
[JsonIgnore]
1231
public string OsVolumeEncryptionSettingsText
1332
{
1433
get { return JsonConvert.SerializeObject(OsVolumeEncryptionSettings, Formatting.Indented); }
1534
}
16-
public bool DataVolumesEncrypted { get; set;}
35+
[JsonIgnore]
36+
public string DataVolumeEncryptionSettingsText
37+
{
38+
get { return JsonConvert.SerializeObject(DataVolumeEncryptionSettings, Formatting.Indented); }
39+
}
40+
public EncryptionStatus DataVolumesEncrypted { get; set; }
1741
}
1842
}

0 commit comments

Comments
 (0)