@@ -701,7 +701,7 @@ public async Task DoesNotSpecializeMSISidecar_WhenMSIContextNull()
701701
702702 var meshServiceClient = new Mock < IMeshServiceClient > ( MockBehavior . Strict ) ;
703703 meshServiceClient . Setup ( c => c . NotifyHealthEvent ( ContainerHealthEventType . Fatal ,
704- It . Is < Type > ( t => t == typeof ( InstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext was empty" ) ) . Returns ( Task . CompletedTask ) ;
704+ It . Is < Type > ( t => t == typeof ( InstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext and EncryptedTokenServiceSpecializationPayload were empty" ) ) . Returns ( Task . CompletedTask ) ;
705705
706706 var instanceManager = GetInstanceManagerForMSISpecialization ( assignmentContext , HttpStatusCode . BadRequest , meshServiceClient . Object ) ;
707707
@@ -711,10 +711,73 @@ public async Task DoesNotSpecializeMSISidecar_WhenMSIContextNull()
711711 var logs = _loggerProvider . GetAllLogMessages ( ) . Select ( p => p . FormattedMessage ) . ToArray ( ) ;
712712 Assert . Collection ( logs ,
713713 p => Assert . StartsWith ( "MSI enabled status: True" , p ) ,
714- p => Assert . StartsWith ( "Skipping specialization of MSI sidecar since MSIContext was absent" , p ) ) ;
714+ p => Assert . StartsWith ( "Skipping specialization of MSI sidecar since MSIContext and EncryptedTokenServiceSpecializationPayload were absent" , p ) ) ;
715715
716716 meshServiceClient . Verify ( c => c . NotifyHealthEvent ( ContainerHealthEventType . Fatal ,
717- It . Is < Type > ( t => t == typeof ( InstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext was empty" ) , Times . Once ) ;
717+ It . Is < Type > ( t => t == typeof ( InstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext and EncryptedTokenServiceSpecializationPayload were empty" ) , Times . Once ) ;
718+ }
719+
720+ [ Fact ]
721+ public async Task SpecializeMSISidecar_Succeeds_EncryptedMSIContextWithoutProvidedEndpoint ( )
722+ {
723+ var environment = new Dictionary < string , string > ( )
724+ {
725+ { EnvironmentSettingNames . MsiEndpoint , "http://localhost:8081" } ,
726+ { EnvironmentSettingNames . MsiSecret , "secret" }
727+ } ;
728+ var assignmentContext = new HostAssignmentContext
729+ {
730+ SiteId = 1234 ,
731+ SiteName = "TestSite" ,
732+ Environment = environment ,
733+ IsWarmupRequest = false ,
734+ MSIContext = new MSIContext ( ) ,
735+ EncryptedTokenServiceSpecializationPayload = "TestContext"
736+ } ;
737+
738+ var instanceManager = GetInstanceManagerForMSISpecialization ( assignmentContext , HttpStatusCode . OK , null ) ;
739+
740+ string error = await instanceManager . SpecializeMSISidecar ( assignmentContext ) ;
741+ Assert . Null ( error ) ;
742+
743+ var logs = _loggerProvider . GetAllLogMessages ( ) . Select ( p => p . FormattedMessage ) . ToArray ( ) ;
744+ Assert . Collection ( logs ,
745+ p => Assert . StartsWith ( "MSI enabled status: True" , p ) ,
746+ p => Assert . StartsWith ( "Using encrypted TokenService payload format" , p ) ,
747+ p => Assert . Equal ( $ "Specializing sidecar at http://localhost:8081{ ScriptConstants . LinuxEncryptedTokenServiceSpecializationStem } ", p ) ,
748+ p => Assert . StartsWith ( "Specialize MSI sidecar returned OK" , p ) ) ;
749+ }
750+
751+ [ Fact ]
752+ public async Task SpecializeMSISidecar_Succeeds_EncryptedMSIContextWithProvidedEndpoint ( )
753+ {
754+ var environment = new Dictionary < string , string > ( )
755+ {
756+ { EnvironmentSettingNames . MsiEndpoint , "http://localhost:8081" } ,
757+ { EnvironmentSettingNames . MsiSecret , "secret" }
758+ } ;
759+ var assignmentContext = new HostAssignmentContext
760+ {
761+ SiteId = 1234 ,
762+ SiteName = "TestSite" ,
763+ Environment = environment ,
764+ IsWarmupRequest = false ,
765+ MSIContext = new MSIContext ( ) ,
766+ EncryptedTokenServiceSpecializationPayload = "TestContext" ,
767+ TokenServiceApiEndpoint = "/api/TestEndpoint"
768+ } ;
769+
770+ var instanceManager = GetInstanceManagerForMSISpecialization ( assignmentContext , HttpStatusCode . OK , null ) ;
771+
772+ string error = await instanceManager . SpecializeMSISidecar ( assignmentContext ) ;
773+ Assert . Null ( error ) ;
774+
775+ var logs = _loggerProvider . GetAllLogMessages ( ) . Select ( p => p . FormattedMessage ) . ToArray ( ) ;
776+ Assert . Collection ( logs ,
777+ p => Assert . StartsWith ( "MSI enabled status: True" , p ) ,
778+ p => Assert . StartsWith ( "Using encrypted TokenService payload format" , p ) ,
779+ p => Assert . Equal ( $ "Specializing sidecar at http://localhost:8081{ assignmentContext . TokenServiceApiEndpoint } ", p ) ,
780+ p => Assert . StartsWith ( "Specialize MSI sidecar returned OK" , p ) ) ;
718781 }
719782
720783 [ Fact ]
@@ -1258,9 +1321,15 @@ private InstanceManager GetInstanceManagerForMSISpecialization(HostAssignmentCon
12581321
12591322 var msiEndpoint = hostAssignmentContext . Environment [ EnvironmentSettingNames . MsiEndpoint ] + ScriptConstants . LinuxMSISpecializationStem ;
12601323
1324+ var defaultEncryptedMsiEndpoint = hostAssignmentContext . Environment [ EnvironmentSettingNames . MsiEndpoint ] + ScriptConstants . LinuxEncryptedTokenServiceSpecializationStem ;
1325+
1326+ var providedEncryptedMsiEndpoint = hostAssignmentContext . Environment [ EnvironmentSettingNames . MsiEndpoint ] + hostAssignmentContext . TokenServiceApiEndpoint ;
1327+
12611328 handlerMock . Protected ( ) . Setup < Task < HttpResponseMessage > > ( "SendAsync" ,
12621329 ItExpr . Is < HttpRequestMessage > ( request => request . Method == HttpMethod . Post
1263- && request . RequestUri . AbsoluteUri . Equals ( msiEndpoint )
1330+ && ( request . RequestUri . AbsoluteUri . Equals ( msiEndpoint )
1331+ || request . RequestUri . AbsoluteUri . Equals ( defaultEncryptedMsiEndpoint )
1332+ || request . RequestUri . AbsoluteUri . Equals ( providedEncryptedMsiEndpoint ) )
12641333 && request . Content != null ) ,
12651334 ItExpr . IsAny < CancellationToken > ( ) ) . ReturnsAsync ( new HttpResponseMessage
12661335 {
0 commit comments