@@ -44,7 +44,7 @@ class ManagedIdentityTests {
44
44
private static ManagedIdentityApplication miApp ;
45
45
46
46
private String getSuccessfulResponse (String resource ) {
47
- long expiresOn = Instant . now (). plus ( 1 , ChronoUnit . HOURS ). getEpochSecond ();
47
+ long expiresOn = ( System . currentTimeMillis () / 1000 ) + ( 24 * 3600 ); //A long-lived, 24 hour token
48
48
return "{\" access_token\" :\" accesstoken\" ,\" expires_on\" :\" " + expiresOn + "\" ,\" resource\" :\" " + resource + "\" ,\" token_type\" :" +
49
49
"\" Bearer\" ,\" client_id\" :\" client_id\" }" ;
50
50
}
@@ -155,18 +155,22 @@ private HttpResponse expectedResponse(int statusCode, String response) {
155
155
void managedIdentity_GetManagedIdentitySource (ManagedIdentitySourceType source , String endpoint , ManagedIdentitySourceType expectedSource ) {
156
156
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
157
157
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
158
- ManagedIdentityClient .resetManagedIdentitySourceType ();
159
158
160
- ManagedIdentitySourceType managedIdentitySourceType = ManagedIdentityClient .getManagedIdentitySource ();
161
- assertEquals (expectedSource , managedIdentitySourceType );
159
+ miApp = ManagedIdentityApplication
160
+ .builder (ManagedIdentityId .systemAssigned ())
161
+ .build ();
162
+
163
+ ManagedIdentitySourceType miClientSourceType = ManagedIdentityClient .getManagedIdentitySource ();
164
+ ManagedIdentitySourceType miAppSourceType = miApp .managedIdentitySource ;
165
+ assertEquals (expectedSource , miClientSourceType );
166
+ assertEquals (expectedSource , miAppSourceType );
162
167
}
163
168
164
169
@ ParameterizedTest
165
170
@ MethodSource ("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createData" )
166
171
void managedIdentityTest_SystemAssigned_SuccessfulResponse (ManagedIdentitySourceType source , String endpoint , String resource ) throws Exception {
167
172
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
168
173
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
169
- ManagedIdentityClient .resetManagedIdentitySourceType ();
170
174
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
171
175
172
176
when (httpClientMock .send (expectedRequest (source , resource ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
@@ -201,7 +205,6 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
201
205
void managedIdentityTest_UserAssigned_SuccessfulResponse (ManagedIdentitySourceType source , String endpoint , ManagedIdentityId id ) throws Exception {
202
206
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
203
207
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
204
- ManagedIdentityClient .resetManagedIdentitySourceType ();
205
208
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
206
209
207
210
when (httpClientMock .send (expectedRequest (source , resource , id ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
@@ -222,12 +225,38 @@ void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceTy
222
225
verify (httpClientMock , times (1 )).send (any ());
223
226
}
224
227
228
+ @ Test
229
+ void managedIdentityTest_RefreshOnHalfOfExpiresOn () throws Exception {
230
+ //All managed identity flows use the same AcquireTokenByManagedIdentitySupplier where refreshOn is set,
231
+ // so any of the MI options should let us verify that it's being set correctly
232
+ IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (ManagedIdentitySourceType .APP_SERVICE , appServiceEndpoint );
233
+ ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
234
+ DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
235
+
236
+ when (httpClientMock .send (expectedRequest (ManagedIdentitySourceType .APP_SERVICE , resource ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
237
+
238
+ miApp = ManagedIdentityApplication
239
+ .builder (ManagedIdentityId .systemAssigned ())
240
+ .httpClient (httpClientMock )
241
+ .build ();
242
+
243
+ AuthenticationResult result = (AuthenticationResult ) miApp .acquireTokenForManagedIdentity (
244
+ ManagedIdentityParameters .builder (resource )
245
+ .build ()).get ();
246
+
247
+ long timestampSeconds = (System .currentTimeMillis () / 1000 );
248
+
249
+ assertNotNull (result .accessToken ());
250
+ assertEquals ((result .expiresOn () - timestampSeconds )/2 , result .refreshOn () - timestampSeconds );
251
+
252
+ verify (httpClientMock , times (1 )).send (any ());
253
+ }
254
+
225
255
@ ParameterizedTest
226
256
@ MethodSource ("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createDataUserAssignedNotSupported" )
227
257
void managedIdentityTest_UserAssigned_NotSupported (ManagedIdentitySourceType source , String endpoint , ManagedIdentityId id ) throws Exception {
228
258
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
229
259
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
230
- ManagedIdentityClient .resetManagedIdentitySourceType ();
231
260
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
232
261
233
262
miApp = ManagedIdentityApplication
@@ -264,7 +293,6 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
264
293
265
294
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
266
295
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
267
- ManagedIdentityClient .resetManagedIdentitySourceType ();
268
296
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
269
297
270
298
when (httpClientMock .send (expectedRequest (source , resource ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
@@ -298,7 +326,6 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
298
326
void managedIdentityTest_WrongScopes (ManagedIdentitySourceType source , String endpoint , String resource ) throws Exception {
299
327
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
300
328
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
301
- ManagedIdentityClient .resetManagedIdentitySourceType ();
302
329
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
303
330
304
331
if (environmentVariables .getEnvironmentVariable ("SourceType" ).equals (ManagedIdentitySourceType .CLOUD_SHELL .toString ())) {
@@ -337,7 +364,6 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
337
364
void managedIdentityTest_Retry (ManagedIdentitySourceType source , String endpoint , String resource ) throws Exception {
338
365
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
339
366
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
340
- ManagedIdentityClient .resetManagedIdentitySourceType ();
341
367
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
342
368
343
369
miApp = ManagedIdentityApplication
@@ -388,7 +414,6 @@ void managedIdentityTest_Retry(ManagedIdentitySourceType source, String endpoint
388
414
void managedIdentity_RequestFailed_NoPayload (ManagedIdentitySourceType source , String endpoint ) throws Exception {
389
415
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
390
416
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
391
- ManagedIdentityClient .resetManagedIdentitySourceType ();
392
417
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
393
418
394
419
when (httpClientMock .send (expectedRequest (source , resource ))).thenReturn (expectedResponse (500 , "" ));
@@ -423,7 +448,6 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
423
448
void managedIdentity_RequestFailed_NullResponse (ManagedIdentitySourceType source , String endpoint ) throws Exception {
424
449
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
425
450
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
426
- ManagedIdentityClient .resetManagedIdentitySourceType ();
427
451
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
428
452
429
453
when (httpClientMock .send (expectedRequest (source , resource ))).thenReturn (expectedResponse (200 , "" ));
@@ -458,7 +482,6 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
458
482
void managedIdentity_RequestFailed_UnreachableNetwork (ManagedIdentitySourceType source , String endpoint ) throws Exception {
459
483
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
460
484
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
461
- ManagedIdentityClient .resetManagedIdentitySourceType ();
462
485
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
463
486
464
487
when (httpClientMock .send (expectedRequest (source , resource ))).thenThrow (new SocketException ("A socket operation was attempted to an unreachable network." ));
@@ -492,7 +515,6 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
492
515
void azureArcManagedIdentity_MissingAuthHeader () throws Exception {
493
516
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (ManagedIdentitySourceType .AZURE_ARC , azureArcEndpoint );
494
517
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
495
- ManagedIdentityClient .resetManagedIdentitySourceType ();
496
518
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
497
519
498
520
HttpResponse response = new HttpResponse ();
@@ -531,7 +553,6 @@ void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
531
553
void managedIdentity_SharedCache (ManagedIdentitySourceType source , String endpoint ) throws Exception {
532
554
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (source , endpoint );
533
555
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
534
- ManagedIdentityClient .resetManagedIdentitySourceType ();
535
556
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
536
557
537
558
when (httpClientMock .send (expectedRequest (source , resource ))).thenReturn (expectedResponse (200 , getSuccessfulResponse (resource )));
@@ -572,7 +593,6 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
572
593
void azureArcManagedIdentity_InvalidAuthHeader () throws Exception {
573
594
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (ManagedIdentitySourceType .AZURE_ARC , azureArcEndpoint );
574
595
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
575
- ManagedIdentityClient .resetManagedIdentitySourceType ();
576
596
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
577
597
578
598
HttpResponse response = new HttpResponse ();
@@ -611,7 +631,6 @@ void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
611
631
void azureArcManagedIdentityAuthheaderValidationTest () throws Exception {
612
632
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper (ManagedIdentitySourceType .AZURE_ARC , azureArcEndpoint );
613
633
ManagedIdentityApplication .setEnvironmentVariables (environmentVariables );
614
- ManagedIdentityClient .resetManagedIdentitySourceType ();
615
634
DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
616
635
617
636
//Both a missing file and an invalid path structure should throw an exception
0 commit comments