Skip to content

Commit b55de8e

Browse files
committed
Fix after manual testing
1 parent 36a007e commit b55de8e

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
import java.io.FileReader;
10+
import java.io.IOException;
911
import java.net.HttpURLConnection;
1012
import java.net.URI;
1113
import java.net.URISyntaxException;
14+
import java.nio.charset.StandardCharsets;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
1218
import java.util.Collections;
1319
import java.util.HashMap;
1420

@@ -82,31 +88,36 @@ public void createManagedIdentityRequest(String resource)
8288
@Override
8389
public ManagedIdentityResponse handleResponse(
8490
ManagedIdentityParameters parameters,
85-
IHttpResponse response)
86-
{
91+
IHttpResponse response) {
92+
8793
LOG.info("[Managed Identity] Response received. Status code: {response.StatusCode}");
8894

89-
if (response.statusCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
90-
{
91-
if(!response.headers().containsKey("WWW-Authenticate")){
95+
if (response.statusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
96+
if(!response.headers().containsKey("Www-Authenticate")) {
9297
LOG.error("[Managed Identity] WWW-Authenticate header is expected but not found.");
9398
throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_REQUEST_FAILED,
9499
MsalErrorMessage.MANAGED_IDENTITY_NO_CHALLENGE_ERROR,
95100
ManagedIdentitySourceType.AZURE_ARC);
96101
}
97102

98-
String challenge = response.headers().get("WWW-Authenticate").get(0);
103+
String challenge = response.headers().get("Www-Authenticate").get(0);
99104
String[] splitChallenge = challenge.split("=");
100105

101-
if (splitChallenge.length != 2)
102-
{
106+
if (splitChallenge.length != 2) {
103107
LOG.error("[Managed Identity] The WWW-Authenticate header for Azure arc managed identity is not an expected format.");
104108
throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_REQUEST_FAILED,
105109
MsalErrorMessage.MANAGED_IDENTITY_INVALID_CHALLENGE,
106110
ManagedIdentitySourceType.AZURE_ARC);
107111
}
108112

109-
String authHeaderValue = "Basic " + splitChallenge[1];
113+
Path path = Paths.get(splitChallenge[1]);
114+
115+
String authHeaderValue = null;
116+
try {
117+
authHeaderValue = "Basic " + new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
118+
} catch (IOException e) {
119+
throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_FILE_READ_ERROR, e.getMessage(), ManagedIdentitySourceType.AZURE_ARC);
120+
}
110121

111122
createManagedIdentityRequest(parameters.resource);
112123

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public class MsalError {
3232
* Managed Identity endpoint is not reachable.
3333
*/
3434
public static final String MANAGED_IDENTITY_UNREACHABLE_NETWORK = "managed_identity_unreachable_network";
35+
36+
public static final String MANAGED_IDENTITY_FILE_READ_ERROR = "managed_identity_file_read_error";
3537
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.mockito.junit.jupiter.MockitoExtension;
1616

1717
import java.net.SocketException;
18+
import java.net.URISyntaxException;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
1821
import java.time.Instant;
1922
import java.time.temporal.ChronoUnit;
2023
import java.util.Collections;
@@ -508,4 +511,38 @@ void azureArcManagedIdentity_InvalidAuthHeader() throws Exception {
508511
fail("MsalManagedIdentityException is expected but not thrown.");
509512
verify(httpClientMock, times(1)).send(any());
510513
}
514+
515+
@Test
516+
void azureArcManagedIdentityAuthheaderTest() throws Exception {
517+
Path path = Paths.get(this.getClass().getResource("/msi-azure-arc-secret.txt").toURI());
518+
IEnvironmentVariables environmentVariables = new EnvironmentVariablesHelper(ManagedIdentitySourceType.AZURE_ARC, azureArcEndpoint);
519+
DefaultHttpClient httpClientMock = mock(DefaultHttpClient.class);
520+
521+
// Mock 401 response that returns www-authenticate header
522+
HttpResponse response = new HttpResponse();
523+
response.statusCode(HttpStatus.SC_UNAUTHORIZED);
524+
response.headers().put("Www-Authenticate", Collections.singletonList("Basic realm=" + path));
525+
526+
when(httpClientMock.send(eq(expectedRequest(ManagedIdentitySourceType.AZURE_ARC, resource)))).thenReturn(response);
527+
528+
// Mock the response when Authorization header is sent in request
529+
HttpRequest expectedRequest = expectedRequest(ManagedIdentitySourceType.AZURE_ARC, resource);
530+
expectedRequest.headers().put("Authorization", "Basic secret");
531+
when(httpClientMock.send(eq(expectedRequest))).thenReturn(expectedResponse(200, getSuccessfulResponse(resource)));
532+
533+
miApp = ManagedIdentityApplication
534+
.builder(ManagedIdentityId.systemAssigned())
535+
.httpClient(httpClientMock)
536+
.build();
537+
538+
// Clear caching to avoid cross test pollution.
539+
miApp.tokenCache().accessTokens.clear();
540+
541+
IAuthenticationResult result = miApp.acquireTokenForManagedIdentity(
542+
ManagedIdentityParameters.builder(resource)
543+
.environmentVariables(environmentVariables)
544+
.build()).get();
545+
546+
assertNotNull(result.accessToken());
547+
}
511548
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secret

0 commit comments

Comments
 (0)