Skip to content

Commit 8eb5a0d

Browse files
committed
Address comments
1 parent 36dfe18 commit 8eb5a0d

File tree

6 files changed

+43
-53
lines changed

6 files changed

+43
-53
lines changed

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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import java.net.URISyntaxException;
1111
import java.util.Collections;
1212
import java.util.HashMap;
13-
import java.util.List;
14-
import java.util.Map;
1513

1614
class AppServiceManagedIdentitySource extends AbstractManagedIdentitySource{
1715

@@ -20,25 +18,22 @@ class AppServiceManagedIdentitySource extends AbstractManagedIdentitySource{
2018
// MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
2119
private static final String APP_SERVICE_MSI_API_VERSION = "2019-08-01";
2220
private static final String SECRET_HEADER_NAME = "X-IDENTITY-HEADER";
23-
private static URI endpointUri;
2421

25-
private URI endpoint;
26-
private String secret;
22+
private final URI MSI_ENDPOINT;
23+
private final String SECRET;
2724

2825
@Override
2926
public void createManagedIdentityRequest(String resource) {
30-
managedIdentityRequest.baseEndpoint = endpoint;
27+
managedIdentityRequest.baseEndpoint = MSI_ENDPOINT;
3128
managedIdentityRequest.method = HttpMethod.GET;
3229

3330
managedIdentityRequest.headers = new HashMap<>();
34-
managedIdentityRequest.headers.put(SECRET_HEADER_NAME, secret);
31+
managedIdentityRequest.headers.put(SECRET_HEADER_NAME, SECRET);
3532

3633
managedIdentityRequest.queryParameters = new HashMap<>();
3734
managedIdentityRequest.queryParameters.put("api-version", Collections.singletonList(APP_SERVICE_MSI_API_VERSION));
3835
managedIdentityRequest.queryParameters.put("resource", Collections.singletonList(resource));
3936

40-
String clientId = getManagedIdentityUserAssignedClientId();
41-
String resourceId = getManagedIdentityUserAssignedResourceId();
4237
if (!StringHelper.isNullOrBlank(getManagedIdentityUserAssignedClientId()))
4338
{
4439
LOG.info("[Managed Identity] Adding user assigned client id to the request.");
@@ -52,35 +47,34 @@ public void createManagedIdentityRequest(String resource) {
5247
}
5348
}
5449

55-
private AppServiceManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, URI endpoint, String secret)
50+
private AppServiceManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, URI msiEndpoint, String secret)
5651
{
5752
super(msalRequest, serviceBundle, ManagedIdentitySourceType.AppService);
58-
this.endpoint = endpoint;
59-
this.secret = secret;
53+
this.MSI_ENDPOINT = msiEndpoint;
54+
this.SECRET = secret;
6055
}
6156

62-
protected static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) {
57+
static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) {
6358

6459
IEnvironmentVariables environmentVariables = getEnvironmentVariables((ManagedIdentityParameters) msalRequest.requestContext().apiParameters());
6560
String msiSecret = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_HEADER);
6661
String msiEndpoint = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT);
6762

68-
return validateEnvironmentVariables(msiEndpoint, msiSecret)
69-
? new AppServiceManagedIdentitySource(msalRequest, serviceBundle, endpointUri, msiSecret)
70-
: null;
63+
URI validatedEndpoint = validateAndGetUri(msiEndpoint, msiSecret);
64+
return validatedEndpoint == null ? null
65+
: new AppServiceManagedIdentitySource(msalRequest, serviceBundle, validatedEndpoint, msiSecret);
7166
}
7267

73-
private static boolean validateEnvironmentVariables(String msiEndpoint, String secret)
68+
private static URI validateAndGetUri(String msiEndpoint, String secret)
7469
{
75-
endpointUri = null;
76-
7770
// if BOTH the env vars endpoint and secret values are null, this MSI provider is unavailable.
7871
if (StringHelper.isNullOrBlank(msiEndpoint) || StringHelper.isNullOrBlank(secret))
7972
{
8073
LOG.info("[Managed Identity] App service managed identity is unavailable.");
81-
return false;
74+
return null;
8275
}
8376

77+
URI endpointUri;
8478
try
8579
{
8680
endpointUri = new URI(msiEndpoint);
@@ -93,7 +87,7 @@ private static boolean validateEnvironmentVariables(String msiEndpoint, String s
9387
}
9488

9589
LOG.info("[Managed Identity] Environment variables validation passed for app service managed identity. Endpoint URI: {endpointUri}. Creating App Service managed identity.");
96-
return true;
90+
return endpointUri;
9791
}
9892

9993
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
import com.nimbusds.oauth2.sdk.util.URLUtils;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
98

@@ -16,14 +15,11 @@ class CloudShellManagedIdentitySource extends AbstractManagedIdentitySource{
1615

1716
private static final Logger LOG = LoggerFactory.getLogger(CloudShellManagedIdentitySource.class);
1817

19-
// MSI Constants. Docs for MSI are available here https://learn.microsoft.com/en-us/azure/cloud-shell/msi-authorization
20-
private static URI endpointUri;
21-
22-
private URI endpoint;
18+
private final URI MSI_ENDPOINT;
2319

2420
@Override
2521
public void createManagedIdentityRequest(String resource) {
26-
managedIdentityRequest.baseEndpoint = endpoint;
22+
managedIdentityRequest.baseEndpoint = MSI_ENDPOINT;
2723
managedIdentityRequest.method = HttpMethod.POST;
2824

2925
managedIdentityRequest.headers = new HashMap<>();
@@ -34,10 +30,10 @@ public void createManagedIdentityRequest(String resource) {
3430
managedIdentityRequest.bodyParameters.put("resource", Collections.singletonList(resource));
3531
}
3632

37-
private CloudShellManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, URI endpoint)
33+
private CloudShellManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, URI msiEndpoint)
3834
{
3935
super(msalRequest, serviceBundle, ManagedIdentitySourceType.CloudShell);
40-
this.endpoint = endpoint;
36+
this.MSI_ENDPOINT = msiEndpoint;
4137

4238
ManagedIdentityIdType idType =
4339
((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getIdType();
@@ -48,7 +44,7 @@ private CloudShellManagedIdentitySource(MsalRequest msalRequest, ServiceBundle s
4844
}
4945
}
5046

51-
protected static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) {
47+
static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) {
5248

5349
IEnvironmentVariables environmentVariables = getEnvironmentVariables((ManagedIdentityParameters) msalRequest.requestContext().apiParameters());
5450
String msiEndpoint = environmentVariables.getEnvironmentVariable(Constants.MSI_ENDPOINT);
@@ -61,14 +57,14 @@ protected static AbstractManagedIdentitySource create(MsalRequest msalRequest, S
6157
return null;
6258
}
6359

64-
return validateEnvironmentVariables(msiEndpoint)
65-
? new CloudShellManagedIdentitySource(msalRequest, serviceBundle, endpointUri)
66-
: null;
60+
URI validatedUri = validateAndGetUri(msiEndpoint);
61+
return validatedUri == null ? null
62+
: new CloudShellManagedIdentitySource(msalRequest, serviceBundle, validatedUri);
6763
}
6864

69-
private static boolean validateEnvironmentVariables(String msiEndpoint)
65+
private static URI validateAndGetUri(String msiEndpoint)
7066
{
71-
endpointUri = null;
67+
URI endpointUri = null;
7268

7369
try
7470
{
@@ -82,7 +78,7 @@ private static boolean validateEnvironmentVariables(String msiEndpoint)
8278
}
8379

8480
LOG.info("[Managed Identity] Environment variables validation passed for cloud shell managed identity. Endpoint URI: " + endpointUri + ". Creating cloud shell managed identity.");
85-
return true;
81+
return endpointUri;
8682
}
8783

8884
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ class IMDSManagedIdentitySource extends AbstractManagedIdentitySource{
2626
}
2727
}
2828

29-
private String imdsTokenPath = "/metadata/identity/oauth2/token";
30-
private String imdsApiVersion = "2018-02-01";
31-
private static String defaultMessage = "[Managed Identity] Service request failed.";
32-
33-
private static String identityUnavailableError = "[Managed Identity] Authentication unavailable. The requested identity has not been assigned to this resource.";
34-
private static String gatewayError = "[Managed Identity] Authentication unavailable. The request failed due to a gateway error.";
29+
private static final String IMDS_TOKEN_PATH = "/metadata/identity/oauth2/token";
30+
private static final String IMDS_API_VERSION = "2018-02-01";
3531

3632
private URI imdsEndpoint;
3733

@@ -51,7 +47,7 @@ public IMDSManagedIdentitySource(MsalRequest msalRequest,
5147
}
5248

5349
StringBuilder builder = new StringBuilder(environmentVariables.getEnvironmentVariable(Constants.AZURE_POD_IDENTITY_AUTHORITY_HOST));
54-
builder.append("/" + imdsTokenPath);
50+
builder.append("/" + IMDS_TOKEN_PATH);
5551
try {
5652
imdsEndpoint = new URI(builder.toString());
5753
} catch (URISyntaxException e) {
@@ -81,7 +77,7 @@ public void createManagedIdentityRequest(String resource) {
8177
managedIdentityRequest.headers.put("Metadata", "true");
8278

8379
managedIdentityRequest.queryParameters = new HashMap<>();
84-
managedIdentityRequest.queryParameters.put("api-version", Collections.singletonList(imdsApiVersion));
80+
managedIdentityRequest.queryParameters.put("api-version", Collections.singletonList(IMDS_API_VERSION));
8581
managedIdentityRequest.queryParameters.put("resource", Collections.singletonList(resource));
8682

8783
String clientId = getManagedIdentityUserAssignedClientId();
@@ -108,10 +104,10 @@ public ManagedIdentityResponse handleResponse(
108104
String baseMessage;
109105

110106
if(response.statusCode()== HttpURLConnection.HTTP_BAD_REQUEST){
111-
baseMessage = identityUnavailableError;
107+
baseMessage = MsalErrorMessage.IDENTITY_UNAVAILABLE_ERROR;
112108
}else if(response.statusCode()== HttpURLConnection.HTTP_BAD_GATEWAY ||
113109
response.statusCode()== HttpURLConnection.HTTP_GATEWAY_TIMEOUT){
114-
baseMessage = gatewayError;
110+
baseMessage = MsalErrorMessage.GATEWAY_ERROR;
115111
}else{
116112
baseMessage = null;
117113
}
@@ -125,7 +121,7 @@ public ManagedIdentityResponse handleResponse(
125121
message = message + " " + errorContentMessage;
126122

127123
LOG.error(String.format("Error message: %s Http status code: %s"), message, response.statusCode());
128-
throw new MsalManagedIdentityException("managed_identity_request_failed", message,
124+
throw new MsalManagedIdentityException(MsalError.MANAGED_IDENTITY_REQUEST_FAILED, message,
129125
ManagedIdentitySourceType.Imds);
130126
}
131127

@@ -137,7 +133,7 @@ private static String createRequestFailedMessage(IHttpResponse response, String
137133
{
138134
StringBuilder messageBuilder = new StringBuilder();
139135

140-
messageBuilder.append(StringHelper.isNullOrBlank(message) ? defaultMessage : message);
136+
messageBuilder.append(StringHelper.isNullOrBlank(message) ? MsalErrorMessage.DEFAULT_MESSAGE : message);
141137
messageBuilder.append("Status: ");
142138
messageBuilder.append(response.statusCode());
143139

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ class MsalErrorMessage {
1818
public static final String MANAGED_IDENTITY_USER_ASSIGNED_NOT_SUPPORTED = "[Managed Identity] User assigned identity is not supported by the %s Managed Identity. To authenticate with the system assigned identity use ManagedIdentityApplication.builder(ManagedIdentityId.systemAssigned()).build().";
1919

2020
public static final String SCOPES_REQUIRED = "At least one scope needs to be requested for this authentication flow. ";
21+
22+
public static final String DEFAULT_MESSAGE = "[Managed Identity] Service request failed.";
23+
24+
public static final String IDENTITY_UNAVAILABLE_ERROR = "[Managed Identity] Authentication unavailable. The requested identity has not been assigned to this resource.";
25+
public static final String GATEWAY_ERROR = "[Managed Identity] Authentication unavailable. The request failed due to a gateway error.";
2126
}

msal4j-sdk/src/samples/msi-sample-jar/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<manifest>
2727
<addClasspath>true</addClasspath>
2828
<classpathPrefix>lib/</classpathPrefix>
29-
<mainClass>com.microsoft.msi.App</mainClass>
29+
<mainClass>com.microsoft.msi.MsiTestApp</mainClass>
3030
</manifest>
3131
</archive>
3232
</configuration>
@@ -38,7 +38,7 @@
3838
<manifest>
3939
<addClasspath>true</addClasspath>
4040
<classpathPrefix>lib/</classpathPrefix>
41-
<mainClass>com.microsoft.msi.App</mainClass>
41+
<mainClass>com.microsoft.msi.MsiTestApp</mainClass>
4242
</manifest>
4343
</archive>
4444
<descriptorRefs>

msal4j-sdk/src/samples/msi-sample-jar/src/main/java/com/microsoft/msi/App.java renamed to msal4j-sdk/src/samples/msi-sample-jar/src/main/java/com/microsoft/msi/MsiTestApp.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
* To create a jar run mvn install -Dmaven.test.skip=true for msal4j pom and then msi-sample-jar pom.
1717
* Copy the jar with dependencies and run on a VM or cloud shell using java -jar msi-sample-jar-1.0.1-jar-with-dependencies.jar
1818
*/
19-
public class App
19+
public class MsiTestApp
2020
{
2121
public static void main( String[] args ) throws IOException {
22-
String response;
2322
String resource = "https://management.azure.com";
2423
int option = 1;
2524
BufferedReader reader = new BufferedReader(
2625
new InputStreamReader(System.in));
27-
final Logger logger = LoggerFactory.getLogger(App.class);
26+
final Logger logger = LoggerFactory.getLogger(MsiTestApp.class);
2827

2928
while (option != 0) {
3029
System.out.println("Enter one of the following options to create a managed identity application:");

0 commit comments

Comments
 (0)