@@ -618,6 +618,69 @@ public async Task SpecializeMSISidecar_Succeeds()
618618 p => Assert . StartsWith ( "Specialize MSI sidecar returned OK" , p ) ) ;
619619 }
620620
621+ [ Fact ]
622+ public async Task SpecializeMSISidecar_Succeeds_EncryptedMSIContextWithoutProvidedEndpoint ( )
623+ {
624+ var environment = new Dictionary < string , string > ( )
625+ {
626+ { EnvironmentSettingNames . MsiEndpoint , "http://localhost:8081" } ,
627+ { EnvironmentSettingNames . MsiSecret , "secret" }
628+ } ;
629+ var assignmentContext = new HostAssignmentContext
630+ {
631+ SiteId = 1234 ,
632+ SiteName = "TestSite" ,
633+ Environment = environment ,
634+ IsWarmupRequest = false ,
635+ MSIContext = new MSIContext ( ) ,
636+ EncryptedTokenServiceSpecializationPayload = "TestContext"
637+ } ;
638+
639+ var instanceManager = GetInstanceManagerForMSISpecialization ( assignmentContext , HttpStatusCode . OK , null ) ;
640+
641+ string error = await instanceManager . SpecializeMSISidecar ( assignmentContext ) ;
642+ Assert . Null ( error ) ;
643+
644+ var logs = _loggerProvider . GetAllLogMessages ( ) . Select ( p => p . FormattedMessage ) . ToArray ( ) ;
645+ Assert . Collection ( logs ,
646+ p => Assert . StartsWith ( "MSI enabled status: True" , p ) ,
647+ p => Assert . StartsWith ( "Using encrypted TokenService payload format" , p ) ,
648+ p => Assert . Equal ( $ "Specializing sidecar at http://localhost:8081{ ScriptConstants . LinuxEncryptedTokenServiceSpecializationStem } ", p ) ,
649+ p => Assert . StartsWith ( "Specialize MSI sidecar returned OK" , p ) ) ;
650+ }
651+
652+ [ Fact ]
653+ public async Task SpecializeMSISidecar_Succeeds_EncryptedMSIContextWithProvidedEndpoint ( )
654+ {
655+ var environment = new Dictionary < string , string > ( )
656+ {
657+ { EnvironmentSettingNames . MsiEndpoint , "http://localhost:8081" } ,
658+ { EnvironmentSettingNames . MsiSecret , "secret" }
659+ } ;
660+ var assignmentContext = new HostAssignmentContext
661+ {
662+ SiteId = 1234 ,
663+ SiteName = "TestSite" ,
664+ Environment = environment ,
665+ IsWarmupRequest = false ,
666+ MSIContext = new MSIContext ( ) ,
667+ EncryptedTokenServiceSpecializationPayload = "TestContext" ,
668+ TokenServiceApiEndpoint = "/api/TestEndpoint"
669+ } ;
670+
671+ var instanceManager = GetInstanceManagerForMSISpecialization ( assignmentContext , HttpStatusCode . OK , null ) ;
672+
673+ string error = await instanceManager . SpecializeMSISidecar ( assignmentContext ) ;
674+ Assert . Null ( error ) ;
675+
676+ var logs = _loggerProvider . GetAllLogMessages ( ) . Select ( p => p . FormattedMessage ) . ToArray ( ) ;
677+ Assert . Collection ( logs ,
678+ p => Assert . StartsWith ( "MSI enabled status: True" , p ) ,
679+ p => Assert . StartsWith ( "Using encrypted TokenService payload format" , p ) ,
680+ p => Assert . Equal ( $ "Specializing sidecar at http://localhost:8081{ assignmentContext . TokenServiceApiEndpoint } ", p ) ,
681+ p => Assert . StartsWith ( "Specialize MSI sidecar returned OK" , p ) ) ;
682+ }
683+
621684 [ Fact ]
622685 public async Task SpecializeMsiSidecar_RequiredPropertiesInPayload ( )
623686 {
@@ -751,7 +814,7 @@ public async Task DoesNotSpecializeMSISidecar_WhenMSIContextNull()
751814
752815 var meshServiceClient = new Mock < IMeshServiceClient > ( MockBehavior . Strict ) ;
753816 meshServiceClient . Setup ( c => c . NotifyHealthEvent ( ContainerHealthEventType . Fatal ,
754- It . Is < Type > ( t => t == typeof ( AtlasInstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext was empty" ) ) . Returns ( Task . CompletedTask ) ;
817+ It . Is < Type > ( t => t == typeof ( AtlasInstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext and EncryptedTokenServiceSpecializationPayload were empty" ) ) . Returns ( Task . CompletedTask ) ;
755818
756819 var instanceManager = GetInstanceManagerForMSISpecialization ( assignmentContext , HttpStatusCode . BadRequest , meshServiceClient . Object ) ;
757820
@@ -761,10 +824,10 @@ public async Task DoesNotSpecializeMSISidecar_WhenMSIContextNull()
761824 var logs = _loggerProvider . GetAllLogMessages ( ) . Select ( p => p . FormattedMessage ) . ToArray ( ) ;
762825 Assert . Collection ( logs ,
763826 p => Assert . StartsWith ( "MSI enabled status: True" , p ) ,
764- p => Assert . StartsWith ( "Skipping specialization of MSI sidecar since MSIContext was absent" , p ) ) ;
827+ p => Assert . StartsWith ( "Skipping specialization of MSI sidecar since MSIContext and EncryptedTokenServiceSpecializationPayload were absent" , p ) ) ;
765828
766829 meshServiceClient . Verify ( c => c . NotifyHealthEvent ( ContainerHealthEventType . Fatal ,
767- It . Is < Type > ( t => t == typeof ( AtlasInstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext was empty" ) , Times . Once ) ;
830+ It . Is < Type > ( t => t == typeof ( AtlasInstanceManager ) ) , "Could not specialize MSI sidecar since MSIContext and EncryptedTokenServiceSpecializationPayload were empty" ) , Times . Once ) ;
768831 }
769832
770833 [ Fact ]
@@ -1308,9 +1371,15 @@ private AtlasInstanceManager GetInstanceManagerForMSISpecialization(HostAssignme
13081371
13091372 var msiEndpoint = hostAssignmentContext . Environment [ EnvironmentSettingNames . MsiEndpoint ] + ScriptConstants . LinuxMSISpecializationStem ;
13101373
1374+ var defaultEncryptedMsiEndpoint = hostAssignmentContext . Environment [ EnvironmentSettingNames . MsiEndpoint ] + ScriptConstants . LinuxEncryptedTokenServiceSpecializationStem ;
1375+
1376+ var providedEncryptedMsiEndpoint = hostAssignmentContext . Environment [ EnvironmentSettingNames . MsiEndpoint ] + hostAssignmentContext . TokenServiceApiEndpoint ;
1377+
13111378 handlerMock . Protected ( ) . Setup < Task < HttpResponseMessage > > ( "SendAsync" ,
13121379 ItExpr . Is < HttpRequestMessage > ( request => request . Method == HttpMethod . Post
1313- && request . RequestUri . AbsoluteUri . Equals ( msiEndpoint )
1380+ && ( request . RequestUri . AbsoluteUri . Equals ( msiEndpoint )
1381+ || request . RequestUri . AbsoluteUri . Equals ( defaultEncryptedMsiEndpoint )
1382+ || request . RequestUri . AbsoluteUri . Equals ( providedEncryptedMsiEndpoint ) )
13141383 && request . Content != null ) ,
13151384 ItExpr . IsAny < CancellationToken > ( ) )
13161385 . Callback < HttpRequestMessage , CancellationToken > ( ( request , token ) => customAction ? . Invoke ( request , token ) )
0 commit comments