54
54
using System . Security . AccessControl ;
55
55
using System . Security . Principal ;
56
56
using Microsoft . Azure . Commands . Common . Strategies . Compute ;
57
+ using System . Security . Policy ;
58
+ using System . Text . RegularExpressions ;
57
59
58
60
namespace Microsoft . Azure . Commands . Compute
59
61
{
@@ -936,14 +938,12 @@ public void DefaultExecuteCmdlet()
936
938
&& this . VM . StorageProfile ? . ImageReference ? . SharedGalleryImageId == null ) //had to add this
937
939
{
938
940
defaultTrustedLaunchAndUefi ( ) ;
939
-
940
941
setTrustedLaunchImage ( ) ;
941
942
}
942
-
943
943
// Disk attached scenario for TL defaulting
944
944
// Determines if the disk has SecurityType enabled.
945
945
// If so, turns on TrustedLaunch for this VM.
946
- if ( this . VM . SecurityProfile ? . SecurityType == null
946
+ else if ( this . VM . SecurityProfile ? . SecurityType == null
947
947
&& this . VM . StorageProfile ? . OsDisk ? . ManagedDisk ? . Id != null )
948
948
{
949
949
var mDiskId = this . VM . StorageProfile ? . OsDisk ? . ManagedDisk . Id . ToString ( ) ;
@@ -957,48 +957,65 @@ public void DefaultExecuteCmdlet()
957
957
defaultTrustedLaunchAndUefi ( ) ;
958
958
}
959
959
}
960
-
961
- // Guest Attestation extension defaulting scenario check.
962
- // And SecureBootEnabled and VtpmEnabled defaulting scenario.
963
- if ( this . VM . SecurityProfile ? . SecurityType != null
964
- && ( this . VM . SecurityProfile ? . SecurityType ? . ToLower ( ) == ConstantValues . TrustedLaunchSecurityType
965
- || this . VM . SecurityProfile ? . SecurityType ? . ToLower ( ) == ConstantValues . ConfidentialVMSecurityType ) )
966
- {
967
- if ( this . VM ? . SecurityProfile ? . UefiSettings != null )
968
- {
969
- this . VM . SecurityProfile . UefiSettings . SecureBootEnabled = this . VM . SecurityProfile . UefiSettings . SecureBootEnabled ?? true ;
970
- this . VM . SecurityProfile . UefiSettings . VTpmEnabled = this . VM . SecurityProfile . UefiSettings . VTpmEnabled ?? true ;
971
- }
972
- else
973
- {
974
- this . VM . SecurityProfile . UefiSettings = new UefiSettings ( true , true ) ;
975
- }
976
- }
977
-
978
-
979
960
// ImageReference provided, TL defaulting occurs if image is Gen2.
980
- if ( this . VM . SecurityProfile ? . SecurityType == null
961
+ // This will handle when the Id is provided in a URI format and
962
+ // when the image segments are provided individually.
963
+ else if ( this . VM . SecurityProfile ? . SecurityType == null
981
964
&& this . VM . StorageProfile ? . ImageReference != null )
982
965
{
983
- if ( this . VM . StorageProfile ? . ImageReference ? . Id != null ) //This code should never happen apparently
966
+ if ( this . VM . StorageProfile ? . ImageReference ? . Id != null )
984
967
{
985
968
string imageRefString = this . VM . StorageProfile . ImageReference . Id . ToString ( ) ;
986
969
987
- var parts = imageRefString . Split ( new char [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries ) ;
988
-
989
- string imagePublisher = parts [ Array . IndexOf ( parts , "Publishers" ) + 1 ] ;
990
- string imageOffer = parts [ Array . IndexOf ( parts , "Offers" ) + 1 ] ;
991
- string imageSku = parts [ Array . IndexOf ( parts , "Skus" ) + 1 ] ;
992
- string imageVersion = parts [ Array . IndexOf ( parts , "Versions" ) + 1 ] ;
993
- //location is required when config object provided.
994
- var imgResponse = ComputeClient . ComputeManagementClient . VirtualMachineImages . GetWithHttpMessagesAsync (
995
- this . Location . Canonicalize ( ) ,
996
- imagePublisher ,
997
- imageOffer ,
998
- imageSku ,
999
- version : imageVersion ) . GetAwaiter ( ) . GetResult ( ) ;
1000
-
1001
- setHyperVGenForImageCheckAndTLDefaulting ( imgResponse ) ;
970
+ string galleryImgIdPattern = @"/subscriptions/(?<subscriptionId>[^/]+)/resourceGroups/(?<resourceGroup>[^/]+)/providers/Microsoft.Compute/galleries/(?<gallery>[^/]+)/images/(?<image>[^/]+)" ;
971
+ string managedImageIdPattern = @"/subscriptions/(?<subscriptionId>[^/]+)/resourceGroups/(?<resourceGroup>[^/]+)/providers/Microsoft.Compute/images/(?<image>[^/]+)" ;
972
+ string defaultExistingImagePattern = @"/Subscriptions/(?<subscriptionId>[^/]+)/Providers/Microsoft.Compute/Locations/(?<location>[^/]+)/Publishers/(?<publisher>[^/]+)/ArtifactTypes/VMImage/Offers/(?<offer>[^/]+)/Skus/(?<sku>[^/]+)/Versions/(?<version>[^/]+)" ;
973
+
974
+ //Gallery Id
975
+ Regex galleryRgx = new Regex ( galleryImgIdPattern , RegexOptions . IgnoreCase ) ;
976
+ Match galleryMatch = galleryRgx . Match ( imageRefString ) ;
977
+ // Managed Image Id
978
+ Regex managedImageRgx = new Regex ( managedImageIdPattern , RegexOptions . IgnoreCase ) ;
979
+ Match managedImageMatch = managedImageRgx . Match ( imageRefString ) ;
980
+ // Default Image Id
981
+ Regex defaultImageRgx = new Regex ( defaultExistingImagePattern , RegexOptions . IgnoreCase ) ;
982
+ Match defaultImageMatch = defaultImageRgx . Match ( imageRefString ) ;
983
+
984
+ if ( defaultImageMatch . Success )
985
+ {
986
+ var parts = imageRefString . Split ( new char [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries ) ;
987
+ // It's a default existing image
988
+ string imagePublisher = parts [ Array . IndexOf ( parts , "Publishers" ) + 1 ] ;
989
+ string imageOffer = parts [ Array . IndexOf ( parts , "Offers" ) + 1 ] ;
990
+ string imageSku = parts [ Array . IndexOf ( parts , "Skus" ) + 1 ] ;
991
+ string imageVersion = parts [ Array . IndexOf ( parts , "Versions" ) + 1 ] ;
992
+ //location is required when config object provided.
993
+ var imgResponse = ComputeClient . ComputeManagementClient . VirtualMachineImages . GetWithHttpMessagesAsync (
994
+ this . Location . Canonicalize ( ) ,
995
+ imagePublisher ,
996
+ imageOffer ,
997
+ imageSku ,
998
+ version : imageVersion ) . GetAwaiter ( ) . GetResult ( ) ;
999
+
1000
+ setHyperVGenForImageCheckAndTLDefaulting ( imgResponse ) ;
1001
+ }
1002
+ // This scenario might have additional logic added later, so making its own if check fo now.
1003
+ else if ( galleryMatch . Success || managedImageMatch . Success )
1004
+ {
1005
+ // do nothing, send message to use TL.
1006
+ if ( this . AsJobPresent ( ) == false ) // to avoid a failure when it is a job. Seems to fail when it is a job.
1007
+ {
1008
+ WriteInformation ( HelpMessages . TrustedLaunchUpgradeMessage , new string [ ] { "PSHOST" } ) ;
1009
+ }
1010
+ }
1011
+ else
1012
+ {
1013
+ // Default behavior is to remind customer to use TrustedLaunch.
1014
+ if ( this . AsJobPresent ( ) == false ) // to avoid a failure when it is a job. Seems to fail when it is a job.
1015
+ {
1016
+ WriteInformation ( HelpMessages . TrustedLaunchUpgradeMessage , new string [ ] { "PSHOST" } ) ;
1017
+ }
1018
+ }
1002
1019
}
1003
1020
else
1004
1021
{
@@ -1007,17 +1024,31 @@ public void DefaultExecuteCmdlet()
1007
1024
setHyperVGenForImageCheckAndTLDefaulting ( specificImageRespone ) ;
1008
1025
}
1009
1026
}
1010
-
1011
- if ( this . VM . SecurityProfile ? . SecurityType == ConstantValues . TrustedLaunchSecurityType
1012
- && this . VM . StorageProfile ? . ImageReference == null
1013
- && this . VM . StorageProfile ? . OsDisk ? . ManagedDisk ? . Id == null //had to add this
1014
- && this . VM . StorageProfile ? . ImageReference ? . SharedGalleryImageId == null )
1027
+ else if ( this . VM . SecurityProfile ? . SecurityType == ConstantValues . TrustedLaunchSecurityType
1028
+ && this . VM . StorageProfile ? . ImageReference == null
1029
+ && this . VM . StorageProfile ? . OsDisk ? . ManagedDisk ? . Id == null //had to add this
1030
+ && this . VM . StorageProfile ? . ImageReference ? . SharedGalleryImageId == null )
1015
1031
{
1016
1032
defaultTrustedLaunchAndUefi ( ) ;
1017
-
1018
1033
setTrustedLaunchImage ( ) ;
1019
1034
}
1020
1035
1036
+ // SecureBootEnabled and VtpmEnabled defaulting scenario.
1037
+ if ( this . VM . SecurityProfile ? . SecurityType != null
1038
+ && ( this . VM . SecurityProfile ? . SecurityType ? . ToLower ( ) == ConstantValues . TrustedLaunchSecurityType
1039
+ || this . VM . SecurityProfile ? . SecurityType ? . ToLower ( ) == ConstantValues . ConfidentialVMSecurityType ) )
1040
+ {
1041
+ if ( this . VM ? . SecurityProfile ? . UefiSettings != null )
1042
+ {
1043
+ this . VM . SecurityProfile . UefiSettings . SecureBootEnabled = this . VM . SecurityProfile . UefiSettings . SecureBootEnabled ?? true ;
1044
+ this . VM . SecurityProfile . UefiSettings . VTpmEnabled = this . VM . SecurityProfile . UefiSettings . VTpmEnabled ?? true ;
1045
+ }
1046
+ else
1047
+ {
1048
+ this . VM . SecurityProfile . UefiSettings = new UefiSettings ( true , true ) ;
1049
+ }
1050
+ }
1051
+
1021
1052
// Standard security type removing value since API does not support it yet.
1022
1053
if ( this . VM . SecurityProfile ? . SecurityType != null
1023
1054
&& this . VM . SecurityProfile ? . SecurityType ? . ToString ( ) . ToLower ( ) == ConstantValues . StandardSecurityType )
0 commit comments