@@ -41,14 +41,20 @@ public class ManagedIdentityTests : TestBase
41
41
internal const string ExpectedCorrelationId = "Some GUID" ;
42
42
43
43
private readonly TestRetryPolicyFactory _testRetryPolicyFactory = new TestRetryPolicyFactory ( ) ;
44
+ private readonly TestValidatedProbeEndpointFactory _testValidatedProbeEndpointFactory = new TestValidatedProbeEndpointFactory ( ) ;
44
45
45
46
private void AddImdsV2CsrMockHandlerIfNeeded (
46
47
ManagedIdentitySource managedIdentitySource ,
47
- MockHttpManager httpManager )
48
+ MockHttpManager httpManager ,
49
+ bool success = false )
48
50
{
51
+ MockHttpMessageHandler csrResponse = success ?
52
+ MockHelpers . MockCsrResponse ( ) :
53
+ MockHelpers . MockCsrResponseFailure ( ) ;
54
+
49
55
if ( managedIdentitySource == ManagedIdentitySource . Imds )
50
56
{
51
- httpManager . AddMockHandler ( MockHelpers . MockCsrResponseFailure ( ) ) ;
57
+ httpManager . AddMockHandler ( csrResponse ) ;
52
58
}
53
59
}
54
60
@@ -111,10 +117,7 @@ public async Task SAMIHappyPathAsync(
111
117
112
118
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
113
119
. WithHttpManager ( httpManager ) ;
114
-
115
120
116
-
117
-
118
121
var mi = miBuilder . Build ( ) ;
119
122
120
123
httpManager . AddManagedIdentityMockHandler (
@@ -211,10 +214,7 @@ public async Task ManagedIdentityDifferentScopesTestAsync(
211
214
212
215
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
213
216
. WithHttpManager ( httpManager ) ;
214
-
215
-
216
217
217
-
218
218
var mi = miBuilder . Build ( ) ;
219
219
220
220
httpManager . AddManagedIdentityMockHandler (
@@ -272,10 +272,7 @@ public async Task ManagedIdentityForceRefreshTestAsync(
272
272
273
273
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
274
274
. WithHttpManager ( httpManager ) ;
275
-
276
275
277
-
278
-
279
276
var mi = miBuilder . Build ( ) ;
280
277
281
278
httpManager . AddManagedIdentityMockHandler (
@@ -335,10 +332,7 @@ public async Task ManagedIdentityWithClaimsAndCapabilitiesTestAsync(
335
332
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
336
333
. WithClientCapabilities ( TestConstants . ClientCapabilities )
337
334
. WithHttpManager ( httpManager ) ;
338
-
339
-
340
335
341
-
342
336
var mi = miBuilder . Build ( ) ;
343
337
344
338
httpManager . AddManagedIdentityMockHandler (
@@ -401,10 +395,7 @@ public async Task ManagedIdentityWithClaimsTestAsync(
401
395
402
396
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
403
397
. WithHttpManager ( httpManager ) ;
404
-
405
-
406
398
407
-
408
399
var mi = miBuilder . Build ( ) ;
409
400
410
401
httpManager . AddManagedIdentityMockHandler (
@@ -476,10 +467,7 @@ public async Task ManagedIdentityTestWrongScopeAsync(string resource, ManagedIde
476
467
477
468
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
478
469
. WithHttpManager ( httpManager ) ;
479
-
480
470
481
-
482
-
483
471
var mi = miBuilder . Build ( ) ;
484
472
485
473
httpManager . AddManagedIdentityMockHandler ( endpoint , resource , MockHelpers . GetMsiErrorResponse ( managedIdentitySource ) ,
@@ -519,8 +507,6 @@ public async Task ManagedIdentityTestErrorResponseParsing(string errorResponse,
519
507
520
508
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
521
509
. WithHttpManager ( httpManager ) ;
522
-
523
-
524
510
525
511
var mi = miBuilder . Build ( ) ;
526
512
@@ -585,10 +571,7 @@ public async Task ManagedIdentityErrorResponseNoPayloadTestAsync(ManagedIdentity
585
571
586
572
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
587
573
. WithHttpManager ( httpManager ) ;
588
-
589
574
590
-
591
-
592
575
var mi = miBuilder . Build ( ) ;
593
576
594
577
httpManager . AddManagedIdentityMockHandler ( endpoint , "scope" , "" ,
@@ -628,10 +611,7 @@ public async Task ManagedIdentityNullResponseAsync(ManagedIdentitySource managed
628
611
629
612
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
630
613
. WithHttpManager ( httpManager ) ;
631
-
632
-
633
614
634
-
635
615
var mi = miBuilder . Build ( ) ;
636
616
637
617
httpManager . AddManagedIdentityMockHandler (
@@ -652,6 +632,7 @@ await mi.AcquireTokenForManagedIdentity(Resource)
652
632
}
653
633
}
654
634
635
+ // add here
655
636
[ DataTestMethod ]
656
637
[ DataRow ( ManagedIdentitySource . AppService , AppServiceEndpoint ) ]
657
638
[ DataRow ( ManagedIdentitySource . Imds , ImdsEndpoint ) ]
@@ -669,10 +650,7 @@ public async Task ManagedIdentityUnreachableNetworkAsync(ManagedIdentitySource m
669
650
670
651
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
671
652
. WithHttpManager ( httpManager ) ;
672
-
673
-
674
653
675
-
676
654
var mi = miBuilder . Build ( ) ;
677
655
678
656
httpManager . AddFailingRequest ( new HttpRequestException ( "A socket operation was attempted to an unreachable network." ,
@@ -700,10 +678,7 @@ public async Task SystemAssignedManagedIdentityApiIdTestAsync()
700
678
701
679
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
702
680
. WithHttpManager ( httpManager ) ;
703
-
704
681
705
-
706
-
707
682
var mi = miBuilder . Build ( ) ;
708
683
709
684
httpManager . AddManagedIdentityMockHandler (
@@ -733,10 +708,7 @@ public async Task UserAssignedManagedIdentityApiIdTestAsync()
733
708
734
709
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . WithUserAssignedClientId ( TestConstants . ClientId ) )
735
710
. WithHttpManager ( httpManager ) ;
736
-
737
-
738
711
739
-
740
712
var mi = miBuilder . Build ( ) ;
741
713
742
714
httpManager . AddManagedIdentityMockHandler (
@@ -814,10 +786,7 @@ public async Task ManagedIdentityExpiresOnTestAsync(int expiresInHours, bool ref
814
786
815
787
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
816
788
. WithHttpManager ( httpManager ) ;
817
-
818
789
819
-
820
-
821
790
var mi = miBuilder . Build ( ) ;
822
791
823
792
httpManager . AddManagedIdentityMockHandler (
@@ -850,10 +819,7 @@ public async Task ManagedIdentityInvalidRefreshOnThrowsAsync()
850
819
851
820
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
852
821
. WithHttpManager ( httpManager ) ;
853
-
854
-
855
822
856
-
857
823
var mi = miBuilder . Build ( ) ;
858
824
859
825
httpManager . AddManagedIdentityMockHandler (
@@ -1124,10 +1090,7 @@ public async Task ManagedIdentityRequestTokensForDifferentScopesTestAsync(
1124
1090
var miBuilder = ManagedIdentityApplicationBuilder
1125
1091
. Create ( ManagedIdentityId . SystemAssigned )
1126
1092
. WithHttpManager ( httpManager ) ;
1127
-
1128
-
1129
1093
1130
-
1131
1094
var mi = miBuilder . Build ( ) ;
1132
1095
1133
1096
// Mock handler for the initial resource request
@@ -1161,35 +1124,36 @@ public async Task ManagedIdentityRequestTokensForDifferentScopesTestAsync(
1161
1124
}
1162
1125
1163
1126
[ DataTestMethod ]
1164
- [ DataRow ( ManagedIdentitySource . AppService ) ]
1165
- [ DataRow ( ManagedIdentitySource . Imds ) ]
1166
- [ DataRow ( ManagedIdentitySource . AzureArc ) ]
1167
- [ DataRow ( ManagedIdentitySource . CloudShell ) ]
1168
1127
[ DataRow ( ManagedIdentitySource . ServiceFabric ) ]
1128
+ [ DataRow ( ManagedIdentitySource . AppService ) ]
1169
1129
[ DataRow ( ManagedIdentitySource . MachineLearning ) ]
1130
+ [ DataRow ( ManagedIdentitySource . CloudShell ) ]
1131
+ [ DataRow ( ManagedIdentitySource . AzureArc ) ]
1132
+ [ DataRow ( ManagedIdentitySource . ImdsV2 ) ]
1133
+ [ DataRow ( ManagedIdentitySource . Imds ) ]
1170
1134
public async Task UnsupportedManagedIdentitySource_ThrowsExceptionDuringTokenAcquisitionAsync (
1171
1135
ManagedIdentitySource managedIdentitySource )
1172
1136
{
1173
1137
string UnsupportedEndpoint = "unsupported://endpoint" ;
1174
1138
1175
1139
using ( new EnvVariableContext ( ) )
1140
+ using ( var httpManager = new MockHttpManager ( ) )
1176
1141
{
1177
- // Set unsupported environment variable
1142
+ bool csrResponseSuccess = managedIdentitySource == ManagedIdentitySource . ImdsV2 ;
1143
+ AddImdsV2CsrMockHandlerIfNeeded ( managedIdentitySource , httpManager , csrResponseSuccess ) ;
1178
1144
SetEnvironmentVariables ( managedIdentitySource , UnsupportedEndpoint ) ;
1179
1145
1180
- // Create the Managed Identity Application
1181
- var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned ) ;
1146
+ var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
1147
+ . WithHttpManager ( httpManager )
1148
+ . WithValidatedProbeEndpointFactory ( _testValidatedProbeEndpointFactory ) ;
1182
1149
1183
- // Build the application
1184
1150
var mi = miBuilder . Build ( ) ;
1185
1151
1186
- // Attempt to acquire a token and verify an exception is thrown
1187
1152
MsalServiceException ex = await Assert . ThrowsExceptionAsync < MsalServiceException > ( async ( ) =>
1188
1153
await mi . AcquireTokenForManagedIdentity ( "https://management.azure.com" )
1189
1154
. ExecuteAsync ( )
1190
1155
. ConfigureAwait ( false ) ) . ConfigureAwait ( false ) ;
1191
1156
1192
- // Verify the exception details
1193
1157
Assert . IsNotNull ( ex ) ;
1194
1158
Assert . AreEqual ( MsalError . ManagedIdentityRequestFailed , ex . ErrorCode ) ;
1195
1159
}
@@ -1275,6 +1239,7 @@ public async Task MixedUserAndSystemAssignedManagedIdentityTestAsync()
1275
1239
}
1276
1240
}
1277
1241
1242
+ // add here
1278
1243
[ DataTestMethod ]
1279
1244
[ DataRow ( ManagedIdentitySource . AppService , AppServiceEndpoint , HttpStatusCode . NotFound ) ]
1280
1245
[ DataRow ( ManagedIdentitySource . AppService , AppServiceEndpoint , HttpStatusCode . RequestTimeout ) ]
@@ -1299,10 +1264,7 @@ public async Task ManagedIdentityRetryPolicyLifeTimeIsPerRequestAsync(
1299
1264
var miBuilder = ManagedIdentityApplicationBuilder . Create ( ManagedIdentityId . SystemAssigned )
1300
1265
. WithHttpManager ( httpManager )
1301
1266
. WithRetryPolicyFactory ( _testRetryPolicyFactory ) ;
1302
-
1303
- // Disable cache to avoid pollution
1304
1267
1305
-
1306
1268
var mi = miBuilder . Build ( ) ;
1307
1269
1308
1270
// Simulate permanent errors (to trigger the maximum number of retries)
0 commit comments