15
15
using Microsoft . Azure . Commands . Compute . Common ;
16
16
using Microsoft . Azure . Commands . Compute . Models ;
17
17
using Microsoft . Azure . Management . Compute ;
18
- using Microsoft . Azure . Management . Compute . Models ;
19
18
using System ;
20
19
using System . Management . Automation ;
20
+ using Microsoft . Azure . Management . Compute . Models ;
21
+ using System . Globalization ;
21
22
22
23
namespace Microsoft . Azure . Commands . Compute . Extension . AzureDiskEncryption
23
24
{
@@ -44,16 +45,42 @@ public class GetAzureDiskEncryptionStatusCommand : VirtualMachineExtensionBaseCm
44
45
[ ValidateNotNullOrEmpty ]
45
46
public string VMName { get ; set ; }
46
47
47
- private bool IsOsVolumeEncrypted ( VirtualMachine vmParameters )
48
+ private OSType GetOSType ( VirtualMachine vmParameters )
48
49
{
49
- var osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings ( vmParameters ) ;
50
- if ( osVolumeEncryptionSettings != null )
50
+ if ( vmParameters == null || vmParameters . StorageProfile == null || vmParameters . StorageProfile . OsDisk == null )
51
51
{
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 ;
54
83
}
55
-
56
- return false ;
57
84
}
58
85
59
86
private DiskEncryptionSettings GetOsVolumeEncryptionSettings ( VirtualMachine vmParameters )
@@ -66,18 +93,38 @@ private DiskEncryptionSettings GetOsVolumeEncryptionSettings(VirtualMachine vmPa
66
93
}
67
94
return null ;
68
95
}
69
- private bool IsAzureDiskEncryptionExtension ( VirtualMachineExtension vmExtension )
96
+
97
+ private bool IsAzureDiskEncryptionExtension ( OSType osType , VirtualMachineExtension vmExtension )
70
98
{
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 )
76
100
{
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
+ }
79
110
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
+ }
81
128
}
82
129
83
130
private bool DataVolumeInExtensionConfig ( AzureDiskEncryptionExtensionContext adeExtension )
@@ -108,32 +155,39 @@ private bool ExtensionProvisioningSucceeded(AzureDiskEncryptionExtensionContext
108
155
return false ;
109
156
}
110
157
111
- private bool AreDataVolumesEncrypted ( VirtualMachine vmParameters )
158
+ private EncryptionStatus AreDataVolumesEncrypted ( VirtualMachine vmParameters )
112
159
{
113
160
if ( vmParameters == null || vmParameters . Resources == null )
114
161
{
115
- return false ;
162
+ return EncryptionStatus . Unknown ;
116
163
}
117
164
165
+ OSType osType = this . GetOSType ( vmParameters ) ;
118
166
foreach ( VirtualMachineExtension vmExtension in vmParameters . Resources )
119
167
{
120
- if ( IsAzureDiskEncryptionExtension ( vmExtension ) )
168
+ switch ( osType )
121
169
{
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 ) )
126
173
{
127
- if ( ExtensionProvisioningSucceeded ( adeExtension ) )
174
+ AzureDiskEncryptionExtensionContext adeExtension = new AzureDiskEncryptionExtensionContext ( vmExtension . ToPSVirtualMachineExtension ( this . ResourceGroupName ) ) ;
175
+ if ( DataVolumeInExtensionConfig ( adeExtension ) )
128
176
{
129
- return true ;
177
+ if ( ExtensionProvisioningSucceeded ( adeExtension ) )
178
+ {
179
+ return EncryptionStatus . Encrypted ;
180
+ }
130
181
}
131
182
}
132
- }
183
+ break ;
184
+ case OSType . Unknown :
185
+ return EncryptionStatus . Unknown ;
186
+ default :
187
+ return EncryptionStatus . Unknown ;
133
188
}
134
189
}
135
-
136
- return false ;
190
+ return EncryptionStatus . NotEncrypted ;
137
191
}
138
192
139
193
public override void ExecuteCmdlet ( )
@@ -144,19 +198,40 @@ public override void ExecuteCmdlet()
144
198
{
145
199
VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) ;
146
200
147
- bool osVolumeEncrypted = IsOsVolumeEncrypted ( vmParameters ) ;
201
+ EncryptionStatus osVolumeEncrypted = IsOsVolumeEncrypted ( vmParameters ) ;
148
202
DiskEncryptionSettings osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings ( vmParameters ) ;
149
- bool dataVolumesEncrypted = AreDataVolumesEncrypted ( vmParameters ) ;
203
+ EncryptionStatus dataVolumesEncrypted = AreDataVolumesEncrypted ( vmParameters ) ;
150
204
151
- AzureDiskEncryptionStatusContext encryptionStatus = new AzureDiskEncryptionStatusContext
205
+ OSType osType = GetOSType ( vmParameters ) ;
206
+ switch ( osType )
152
207
{
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
+ }
158
234
} ) ;
159
-
160
235
}
161
236
}
162
237
}
0 commit comments