Skip to content

Commit 10c5077

Browse files
committed
Merge remote-tracking branch 'origin/nebharg/MsiAzureArc' into nebharg/MsiAzureArc
2 parents 35c89b9 + b55de8e commit 10c5077

File tree

5 files changed

+148
-54
lines changed

5 files changed

+148
-54
lines changed

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

Lines changed: 26 additions & 15 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

@@ -45,23 +51,23 @@ private static URI validateAndGetUri(String identityEndpoint, String imdsEndpoin
4551
} catch (URISyntaxException e) {
4652
throw new MsalManagedIdentityException(MsalError.INVALID_MANAGED_IDENTITY_ENDPOINT, String.format(
4753
MsalErrorMessage.MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR, "IDENTITY_ENDPOINT", identityEndpoint, AZURE_ARC),
48-
ManagedIdentitySourceType.AzureArc);
54+
ManagedIdentitySourceType.AZURE_ARC);
4955
}
5056

5157
LOG.info("[Managed Identity] Creating Azure Arc managed identity. Endpoint URI: " + endpointUri);
5258
return endpointUri;
5359
}
5460

5561
private AzureArcManagedIdentitySource(URI endpoint, MsalRequest msalRequest, ServiceBundle serviceBundle){
56-
super(msalRequest, serviceBundle, ManagedIdentitySourceType.AzureArc);
62+
super(msalRequest, serviceBundle, ManagedIdentitySourceType.AZURE_ARC);
5763
this.MSI_ENDPOINT = endpoint;
5864

5965
ManagedIdentityIdType idType =
6066
((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getIdType();
61-
if (idType != ManagedIdentityIdType.SystemAssigned) {
67+
if (idType != ManagedIdentityIdType.SYSTEM_ASSIGNED) {
6268
throw new MsalManagedIdentityException(MsalError.USER_ASSIGNED_MANAGED_IDENTITY_NOT_SUPPORTED,
6369
String.format(MsalErrorMessage.MANAGED_IDENTITY_USER_ASSIGNED_NOT_SUPPORTED, AZURE_ARC),
64-
ManagedIdentitySourceType.CloudShell);
70+
ManagedIdentitySourceType.AZURE_ARC);
6571
}
6672
}
6773

@@ -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,
95-
ManagedIdentitySourceType.AzureArc);
100+
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,
106-
ManagedIdentitySourceType.AzureArc);
110+
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/ManagedIdentityTestDataProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public static Stream<Arguments> createData() {
2121
ManagedIdentityTests.resource),
2222
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
2323
ManagedIdentityTests.resourceDefaultSuffix),
24-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
24+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
2525
ManagedIdentityTests.resource),
26-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
26+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
2727
ManagedIdentityTests.resourceDefaultSuffix),
2828
Arguments.of(ManagedIdentitySourceType.IMDS, ManagedIdentityTests.IMDS_ENDPOINT,
2929
ManagedIdentityTests.resource),
@@ -49,11 +49,11 @@ public static Stream<Arguments> createDataUserAssignedNotSupported() {
4949
return Stream.of(
5050
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
5151
ManagedIdentityId.userAssignedClientId(CLIENT_ID)),
52-
Arguments.of(ManagedIdentitySourceType.CloudShell, ManagedIdentityTests.cloudShellEndpoint,
52+
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
5353
ManagedIdentityId.userAssignedResourceId(RESOURCE_ID)),
54-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
54+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
5555
ManagedIdentityId.userAssignedClientId(CLIENT_ID)),
56-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
56+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
5757
ManagedIdentityId.userAssignedResourceId(RESOURCE_ID)));
5858
}
5959

@@ -67,9 +67,9 @@ public static Stream<Arguments> createDataWrongScope() {
6767
"user.read"),
6868
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint,
6969
"https://management.core.windows.net//user_impersonation"),
70-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
70+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
7171
"user.read"),
72-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint,
72+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint,
7373
"https://management.core.windows.net//user_impersonation"),
7474
Arguments.of(ManagedIdentitySourceType.IMDS, ManagedIdentityTests.IMDS_ENDPOINT,
7575
"user.read"),
@@ -79,7 +79,7 @@ public static Stream<Arguments> createDataWrongScope() {
7979

8080
public static Stream<Arguments> createDataError() {
8181
return Stream.of(
82-
Arguments.of(ManagedIdentitySourceType.AzureArc, ManagedIdentityTests.azureArcEndpoint),
82+
Arguments.of(ManagedIdentitySourceType.AZURE_ARC, ManagedIdentityTests.azureArcEndpoint),
8383
Arguments.of(ManagedIdentitySourceType.APP_SERVICE, ManagedIdentityTests.appServiceEndpoint),
8484
Arguments.of(ManagedIdentitySourceType.CLOUD_SHELL, ManagedIdentityTests.cloudShellEndpoint),
8585
Arguments.of(ManagedIdentitySourceType.IMDS, ManagedIdentityTests.IMDS_ENDPOINT));

0 commit comments

Comments
 (0)