Skip to content

Commit c46e5d2

Browse files
committed
Make managedIdentitySource not static
1 parent 3b47606 commit c46e5d2

File tree

3 files changed

+15
-40
lines changed

3 files changed

+15
-40
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ManagedIdentityApplication extends AbstractApplicationBase implemen
2525
static TokenCache sharedTokenCache = new TokenCache();
2626

2727
@Getter(value = AccessLevel.PUBLIC)
28-
static ManagedIdentitySourceType managedIdentitySource = ManagedIdentityClient.getManagedIdentitySource();
28+
ManagedIdentitySourceType managedIdentitySource = ManagedIdentityClient.getManagedIdentitySource();
2929

3030
@Getter(value = AccessLevel.PACKAGE)
3131
static IEnvironmentVariables environmentVariables;

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityClient.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,24 @@
1212
class ManagedIdentityClient {
1313
private static final Logger LOG = LoggerFactory.getLogger(ManagedIdentityClient.class);
1414

15-
private static ManagedIdentitySourceType managedIdentitySourceType;
16-
17-
protected static void resetManagedIdentitySourceType() {
18-
managedIdentitySourceType = ManagedIdentitySourceType.NONE;
19-
}
20-
2115
static ManagedIdentitySourceType getManagedIdentitySource() {
22-
if (managedIdentitySourceType != null && managedIdentitySourceType != ManagedIdentitySourceType.NONE) {
23-
return managedIdentitySourceType;
24-
}
25-
2616
IEnvironmentVariables environmentVariables = AbstractManagedIdentitySource.getEnvironmentVariables();
2717

2818
if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT)) &&
2919
!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IDENTITY_HEADER))) {
3020
if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IDENTITY_SERVER_THUMBPRINT))) {
31-
managedIdentitySourceType = ManagedIdentitySourceType.SERVICE_FABRIC;
21+
return ManagedIdentitySourceType.SERVICE_FABRIC;
3222
} else {
33-
managedIdentitySourceType = ManagedIdentitySourceType.APP_SERVICE;
23+
return ManagedIdentitySourceType.APP_SERVICE;
3424
}
3525
} else if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.MSI_ENDPOINT))) {
36-
managedIdentitySourceType = ManagedIdentitySourceType.CLOUD_SHELL;
26+
return ManagedIdentitySourceType.CLOUD_SHELL;
3727
} else if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT)) &&
3828
!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.IMDS_ENDPOINT))) {
39-
managedIdentitySourceType = ManagedIdentitySourceType.AZURE_ARC;
29+
return ManagedIdentitySourceType.AZURE_ARC;
4030
} else {
41-
managedIdentitySourceType = ManagedIdentitySourceType.DEFAULT_TO_IMDS;
31+
return ManagedIdentitySourceType.DEFAULT_TO_IMDS;
4232
}
43-
44-
return managedIdentitySourceType;
4533
}
4634

4735
AbstractManagedIdentitySource managedIdentitySource;
@@ -64,11 +52,7 @@ ManagedIdentityResponse getManagedIdentityResponse(ManagedIdentityParameters par
6452
private static AbstractManagedIdentitySource createManagedIdentitySource(MsalRequest msalRequest,
6553
ServiceBundle serviceBundle) {
6654

67-
if (managedIdentitySourceType == null || managedIdentitySourceType == ManagedIdentitySourceType.NONE) {
68-
managedIdentitySourceType = getManagedIdentitySource();
69-
}
70-
71-
switch (managedIdentitySourceType) {
55+
switch (getManagedIdentitySource()) {
7256
case SERVICE_FABRIC:
7357
return ServiceFabricManagedIdentitySource.create(msalRequest, serviceBundle);
7458
case APP_SERVICE:

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/ManagedIdentityTests.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,22 @@ private HttpResponse expectedResponse(int statusCode, String response) {
155155
void managedIdentity_GetManagedIdentitySource(ManagedIdentitySourceType source, String endpoint, ManagedIdentitySourceType expectedSource) {
156156
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
157157
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
158-
ManagedIdentityClient.resetManagedIdentitySourceType();
159158

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);
162167
}
163168

164169
@ParameterizedTest
165170
@MethodSource("com.microsoft.aad.msal4j.ManagedIdentityTestDataProvider#createData")
166171
void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySourceType source, String endpoint, String resource) throws Exception {
167172
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
168173
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
169-
ManagedIdentityClient.resetManagedIdentitySourceType();
170174
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
171175

172176
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
@@ -201,7 +205,6 @@ void managedIdentityTest_SystemAssigned_SuccessfulResponse(ManagedIdentitySource
201205
void managedIdentityTest_UserAssigned_SuccessfulResponse(ManagedIdentitySourceType source, String endpoint, ManagedIdentityId id) throws Exception {
202206
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
203207
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
204-
ManagedIdentityClient.resetManagedIdentitySourceType();
205208
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
206209

207210
when(httpClientMock.send(expectedRequest(source, resource, id))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
@@ -228,7 +231,6 @@ void managedIdentityTest_RefreshOnHalfOfExpiresOn() throws Exception {
228231
// so any of the MI options should let us verify that it's being set correctly
229232
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.APP_SERVICE, appServiceEndpoint);
230233
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
231-
ManagedIdentityClient.resetManagedIdentitySourceType();
232234
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
233235

234236
when(httpClientMock.send(expectedRequest(ManagedIdentitySourceType.APP_SERVICE, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
@@ -255,7 +257,6 @@ void managedIdentityTest_RefreshOnHalfOfExpiresOn() throws Exception {
255257
void managedIdentityTest_UserAssigned_NotSupported(ManagedIdentitySourceType source, String endpoint, ManagedIdentityId id) throws Exception {
256258
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
257259
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
258-
ManagedIdentityClient.resetManagedIdentitySourceType();
259260
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
260261

261262
miApp = ManagedIdentityApplication
@@ -292,7 +293,6 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
292293

293294
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
294295
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
295-
ManagedIdentityClient.resetManagedIdentitySourceType();
296296
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
297297

298298
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
@@ -326,7 +326,6 @@ void managedIdentityTest_DifferentScopes_RequestsNewToken(ManagedIdentitySourceT
326326
void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String endpoint, String resource) throws Exception {
327327
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
328328
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
329-
ManagedIdentityClient.resetManagedIdentitySourceType();
330329
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
331330

332331
if (environmentVariables.getEnvironmentVariable("SourceType").equals(ManagedIdentitySourceType.CLOUD_SHELL.toString())) {
@@ -365,7 +364,6 @@ void managedIdentityTest_WrongScopes(ManagedIdentitySourceType source, String en
365364
void managedIdentityTest_Retry(ManagedIdentitySourceType source, String endpoint, String resource) throws Exception {
366365
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
367366
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
368-
ManagedIdentityClient.resetManagedIdentitySourceType();
369367
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
370368

371369
miApp = ManagedIdentityApplication
@@ -416,7 +414,6 @@ void managedIdentityTest_Retry(ManagedIdentitySourceType source, String endpoint
416414
void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, String endpoint) throws Exception {
417415
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
418416
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
419-
ManagedIdentityClient.resetManagedIdentitySourceType();
420417
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
421418

422419
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(500, ""));
@@ -451,7 +448,6 @@ void managedIdentity_RequestFailed_NoPayload(ManagedIdentitySourceType source, S
451448
void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source, String endpoint) throws Exception {
452449
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
453450
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
454-
ManagedIdentityClient.resetManagedIdentitySourceType();
455451
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
456452

457453
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, ""));
@@ -486,7 +482,6 @@ void managedIdentity_RequestFailed_NullResponse(ManagedIdentitySourceType source
486482
void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType source, String endpoint) throws Exception {
487483
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
488484
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
489-
ManagedIdentityClient.resetManagedIdentitySourceType();
490485
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
491486

492487
when(httpClientMock.send(expectedRequest(source, resource))).thenThrow(new SocketException("A socket operation was attempted to an unreachable network."));
@@ -520,7 +515,6 @@ void managedIdentity_RequestFailed_UnreachableNetwork(ManagedIdentitySourceType
520515
void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
521516
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AZURE_ARC, azureArcEndpoint);
522517
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
523-
ManagedIdentityClient.resetManagedIdentitySourceType();
524518
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
525519

526520
HttpResponse response = new HttpResponse();
@@ -559,7 +553,6 @@ void azureArcManagedIdentity_MissingAuthHeader() throws Exception {
559553
void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoint) throws Exception {
560554
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(source, endpoint);
561555
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
562-
ManagedIdentityClient.resetManagedIdentitySourceType();
563556
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
564557

565558
when(httpClientMock.send(expectedRequest(source, resource))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
@@ -600,7 +593,6 @@ void managedIdentity_SharedCache(ManagedIdentitySourceType source, String endpoi
600593
void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
601594
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AZURE_ARC, azureArcEndpoint);
602595
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
603-
ManagedIdentityClient.resetManagedIdentitySourceType();
604596
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
605597

606598
HttpResponse response = new HttpResponse();
@@ -639,7 +631,6 @@ void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
639631
void azureArcManagedIdentityAuthheaderValidationTest() throws Exception {
640632
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AZURE_ARC, azureArcEndpoint);
641633
ManagedIdentityApplication.setEnvironmentVariables(environmentVariables);
642-
ManagedIdentityClient.resetManagedIdentitySourceType();
643634
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
644635

645636
//Both a missing file and an invalid path structure should throw an exception

0 commit comments

Comments
 (0)