Skip to content

Commit 9e3dcb3

Browse files
committed
Address comments
1 parent 91ea7ce commit 9e3dcb3

13 files changed

+72
-86
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313

14-
class AppServiceManagedIdentity extends AbstractManagedIdentitySource{
14+
class AppServiceManagedIdentitySource extends AbstractManagedIdentitySource{
1515

16-
private static final Logger LOG = LoggerFactory.getLogger(AppServiceManagedIdentity.class);
16+
private static final Logger LOG = LoggerFactory.getLogger(AppServiceManagedIdentitySource.class);
1717

1818
// MSI Constants. Docs for MSI are available here https://docs.microsoft.com/azure/app-service/overview-managed-identity
1919
private static final String APP_SERVICE_MSI_API_VERSION = "2019-08-01";
@@ -51,7 +51,7 @@ public void createManagedIdentityRequest(String resource) {
5151
managedIdentityRequest.queryParameters = queryParameters;
5252
}
5353

54-
private AppServiceManagedIdentity(MsalRequest msalRequest, ServiceBundle serviceBundle, URI endpoint, String secret)
54+
private AppServiceManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle, URI endpoint, String secret)
5555
{
5656
super(msalRequest, serviceBundle, ManagedIdentitySourceType.AppService);
5757
this.endpoint = endpoint;
@@ -61,11 +61,11 @@ private AppServiceManagedIdentity(MsalRequest msalRequest, ServiceBundle service
6161
protected static AbstractManagedIdentitySource create(MsalRequest msalRequest, ServiceBundle serviceBundle) {
6262

6363
IEnvironmentVariables environmentVariables = getEnvironmentVariables((ManagedIdentityParameters) msalRequest.requestContext().apiParameters());
64-
String msiSecret = environmentVariables.getEnvironmentVariable(IEnvironmentVariables.IDENTITY_HEADER);
65-
String msiEndpoint = environmentVariables.getEnvironmentVariable(IEnvironmentVariables.IDENTITY_ENDPOINT);
64+
String msiSecret = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_HEADER);
65+
String msiEndpoint = environmentVariables.getEnvironmentVariable(Constants.IDENTITY_ENDPOINT);
6666

6767
return validateEnvironmentVariables(msiEndpoint, msiSecret)
68-
? new AppServiceManagedIdentity(msalRequest, serviceBundle, endpointUri, msiSecret)
68+
? new AppServiceManagedIdentitySource(msalRequest, serviceBundle, endpointUri, msiSecret)
6969
: null;
7070
}
7171

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ final class Constants {
1414
public static final String MANAGED_IDENTITY_CLIENT_ID = "client_id";
1515
public static final String MANAGED_IDENTITY_RESOURCE_ID = "mi_res_id";
1616

17+
public static final String IDENTITY_ENDPOINT = "IDENTITY_ENDPOINT";
18+
public static final String IDENTITY_HEADER = "IDENTITY_HEADER";
19+
public static final String AZURE_POD_IDENTITY_AUTHORITY_HOST = "AZURE_POD_IDENTITY_AUTHORITY_HOST";
20+
public static final String IMDS_ENDPOINT = "IMDS_ENDPOINT";
21+
public static final String MSI_ENDPOINT = "MSI_ENDPOINT";
22+
public static final String IDENTITY_SERVER_THUMBPRINT = "IDENTITY_SERVER_THUMBPRINT";
23+
1724
}

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

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

44
package com.microsoft.aad.msal4j;
55

6+
import lombok.EqualsAndHashCode;
67
import lombok.Getter;
78
import lombok.experimental.Accessors;
89

@@ -16,6 +17,7 @@
1617
*/
1718
@Getter
1819
@Accessors(fluent = true)
20+
@EqualsAndHashCode
1921
public class HttpRequest {
2022

2123
/**
@@ -87,36 +89,4 @@ private URL createUrlFromString(String stringUrl) {
8789

8890
return url;
8991
}
90-
91-
// Adding this for unit tests to be able to compare expected and actual request.
92-
@Override
93-
public boolean equals(Object o) {
94-
if (o == this)
95-
return true;
96-
97-
if (!(o instanceof HttpRequest))
98-
return false;
99-
100-
HttpRequest otherRequest = (HttpRequest) o;
101-
102-
return this.url.equals(otherRequest.url)
103-
&& this.httpMethod.equals(otherRequest.httpMethod)
104-
&& ((this.headers == null && otherRequest.headers == null) || this.headers.equals(otherRequest.headers))
105-
&& ((this.body == null && otherRequest.body == null) || this.body.equals(otherRequest.body));
106-
}
107-
108-
@Override
109-
public final int hashCode() {
110-
int result = 17;
111-
if (httpMethod != null) {
112-
result = 31 * result + httpMethod.hashCode();
113-
}
114-
if (headers != null) {
115-
result = 31 * result + headers.hashCode();
116-
}
117-
if (body != null) {
118-
result = 31 * result + body.hashCode();
119-
}
120-
return result;
121-
}
12292
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
public interface IEnvironmentVariables {
7-
String IDENTITY_ENDPOINT = "IDENTITY_ENDPOINT";
8-
public static final String IDENTITY_HEADER = "IDENTITY_HEADER";
9-
public static final String AZURE_POD_IDENTITY_AUTHORITY_HOST = "AZURE_POD_IDENTITY_AUTHORITY_HOST";
10-
public static final String IMDS_ENDPOINT = "IMDS_ENDPOINT";
11-
public static final String MSI_ENDPOINT = "MSI_ENDPOINT";
12-
public static final String IDENTITY_SERVER_THUMBPRINT = "IDENTITY_SERVER_THUMBPRINT";
6+
interface IEnvironmentVariables {
7+
138

149
String getEnvironmentVariable(String envVariable);
1510
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414

15-
public class IMDSManagedIdentity extends AbstractManagedIdentitySource{
15+
class IMDSManagedIdentitySource extends AbstractManagedIdentitySource{
1616

1717
// IMDS constants. Docs for IMDS are available here https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-http
18-
private final static Logger LOG = LoggerFactory.getLogger(IMDSManagedIdentity.class);
19-
private static URI DEFAULT_IMDS_ENDPOINT;
18+
private static final Logger LOG = LoggerFactory.getLogger(IMDSManagedIdentitySource.class);
19+
private static final URI DEFAULT_IMDS_ENDPOINT;
2020

2121
static {
2222
try {
2323
DEFAULT_IMDS_ENDPOINT = new URI("http://169.254.169.254/metadata/identity/oauth2/token");
2424
} catch (URISyntaxException e) {
25-
throw new RuntimeException(e);
25+
throw new MsalManagedIdentityException(MsalError.INVALID_MANAGED_IDENTITY_ENDPOINT, ManagedIdentitySourceType.Imds);
2626
}
2727
}
2828

@@ -35,27 +35,32 @@ public class IMDSManagedIdentity extends AbstractManagedIdentitySource{
3535

3636
private URI imdsEndpoint;
3737

38-
public IMDSManagedIdentity(MsalRequest msalRequest,
39-
ServiceBundle serviceBundle) {
38+
public IMDSManagedIdentitySource(MsalRequest msalRequest,
39+
ServiceBundle serviceBundle) {
4040
super(msalRequest, serviceBundle, ManagedIdentitySourceType.Imds);
4141
ManagedIdentityParameters parameters = (ManagedIdentityParameters) msalRequest.requestContext().apiParameters();
4242
IEnvironmentVariables environmentVariables = ((ManagedIdentityParameters) msalRequest.requestContext().apiParameters()).environmentVariables == null ?
4343
new EnvironmentVariables() :
4444
parameters.environmentVariables;
45-
if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(IEnvironmentVariables.AZURE_POD_IDENTITY_AUTHORITY_HOST))){
46-
LOG.info("[Managed Identity] Environment variable AZURE_POD_IDENTITY_AUTHORITY_HOST for IMDS returned endpoint: " + environmentVariables.getEnvironmentVariable(IEnvironmentVariables.AZURE_POD_IDENTITY_AUTHORITY_HOST));
45+
if (!StringHelper.isNullOrBlank(environmentVariables.getEnvironmentVariable(Constants.AZURE_POD_IDENTITY_AUTHORITY_HOST))){
46+
LOG.info("[Managed Identity] Environment variable AZURE_POD_IDENTITY_AUTHORITY_HOST for IMDS returned endpoint: " + environmentVariables.getEnvironmentVariable(Constants.AZURE_POD_IDENTITY_AUTHORITY_HOST));
4747
try {
48-
imdsEndpoint = new URI(environmentVariables.getEnvironmentVariable(IEnvironmentVariables.AZURE_POD_IDENTITY_AUTHORITY_HOST));
48+
imdsEndpoint = new URI(environmentVariables.getEnvironmentVariable(Constants.AZURE_POD_IDENTITY_AUTHORITY_HOST));
4949
} catch (URISyntaxException e) {
5050
throw new RuntimeException(e);
5151
}
5252

53-
StringBuilder builder = new StringBuilder(environmentVariables.getEnvironmentVariable(IEnvironmentVariables.AZURE_POD_IDENTITY_AUTHORITY_HOST));
53+
StringBuilder builder = new StringBuilder(environmentVariables.getEnvironmentVariable(Constants.AZURE_POD_IDENTITY_AUTHORITY_HOST));
5454
builder.append("/" + imdsTokenPath);
5555
try {
5656
imdsEndpoint = new URI(builder.toString());
5757
} catch (URISyntaxException e) {
58-
throw new RuntimeException(e);
58+
throw new MsalManagedIdentityException(MsalError.INVALID_MANAGED_IDENTITY_ENDPOINT,
59+
String.format(MsalErrorMessage.MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR,
60+
Constants.AZURE_POD_IDENTITY_AUTHORITY_HOST,
61+
builder.toString(),
62+
ManagedIdentitySourceType.Imds),
63+
ManagedIdentitySourceType.Imds);
5964
}
6065
}
6166
else

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
@@ -20,7 +20,7 @@ public class ManagedIdentityApplication extends AbstractClientApplicationBase im
2020
private String resource;
2121

2222
@Getter
23-
private ManagedIdentityId managedIdentityId;
23+
private final ManagedIdentityId managedIdentityId;
2424

2525
private ManagedIdentityApplication(Builder builder) {
2626
super(builder);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public ManagedIdentityResponse getManagedIdentityResponse(ManagedIdentityParamet
3434
private static AbstractManagedIdentitySource createManagedIdentitySource(MsalRequest msalRequest,
3535
ServiceBundle serviceBundle) throws Exception {
3636
AbstractManagedIdentitySource managedIdentitySource;
37-
if ((managedIdentitySource = AppServiceManagedIdentity.create(msalRequest, serviceBundle)) != null) {
37+
if ((managedIdentitySource = AppServiceManagedIdentitySource.create(msalRequest, serviceBundle)) != null) {
3838
return managedIdentitySource;
3939
} else {
40-
return new IMDSManagedIdentity(msalRequest, serviceBundle);
40+
return new IMDSManagedIdentitySource(msalRequest, serviceBundle);
4141
}
4242
}
4343
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
public enum ManagedIdentityIdType {
6+
enum ManagedIdentityIdType {
77

88
SystemAssigned,
99
ClientId,

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,33 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
class MsalError {
6+
/**
7+
* Error code returned as a property in MsalException.
8+
*/
9+
public class MsalError {
10+
11+
/**
12+
* Invalid managed identity endpoint.
13+
*/
714
public static final String INVALID_MANAGED_IDENTITY_ENDPOINT = "invalid_managed_identity_endpoint";
815

16+
/**
17+
* User assigned managed identity is not supported for this source.
18+
*/
919
public static final String USER_ASSIGNED_MANAGED_IDENTITY_NOT_SUPPORTED = "user_assigned_managed_identity_not_supported";
1020

21+
/**
22+
* Managed Identity error response was received.
23+
*/
1124
public static final String MANAGED_IDENTITY_REQUEST_FAILED = "managed_identity_request_failed";
1225

13-
public static final String SCOPES_REQUIRED = "scopes_required_client_credentials";
26+
/**
27+
* Resource is required to fetch a token using managed identity.
28+
*/
29+
public static final String RESOURCE_REQUIRED_MANAGED_IDENTITY = "resource_required_managed_identity";
1430

31+
/**
32+
* Managed Identity endpoint is not reachable.
33+
*/
1534
public static final String MANAGED_IDENTITY_UNREACHABLE_NETWORK = "managed_identity_unreachable_network";
1635
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
public class MsalErrorMessage {
6+
class MsalErrorMessage {
77

8-
public static final String MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR = "[Managed Identity] The environment variable {0} contains an invalid Uri {1} in {2} managed identity source.";
8+
public static final String MANAGED_IDENTITY_ENDPOINT_INVALID_URI_ERROR = "[Managed Identity] The environment variable %s contains an invalid Uri %s in %s managed identity source.";
99

1010
public static final String MANAGED_IDENTITY_NO_CHALLENGE_ERROR = "[Managed Identity] Did not receive expected WWW-Authenticate header in the response from Azure Arc Managed Identity Endpoint.";
1111

1212
public static final String MANAGED_IDENTITY_INVALID_CHALLENGE = "[Managed Identity] The WWW-Authenticate header in the response from Azure Arc Managed Identity Endpoint did not match the expected format.";
1313

1414
public static final String MANAGED_IDENTITY_UNEXPECTED_RESPONSE = "[Managed Identity] Unexpected exception occurred when parsing the response. See the inner exception for details.";
1515

16-
public static final String MANAGED_IDENTITY_ENPOINT_INVALID_URI_ERROR = "[Managed Identity] The environment variable {0} contains an invalid Uri {1} in {2} managed identity source.";
17-
1816
public static final String MANAGED_IDENTITY_USER_ASSIGNED_NOT_CONFIGURABLE_AT_RUNTIME = "[Managed Identity] Service Fabric user assigned managed identity ClientId or ResourceId is not configurable at runtime.";
1917

20-
public static final String MANAGED_IDENTITY_USER_ASSIGNED_NOT_SUPPORTED = "[Managed Identity] User assigned identity is not supported by the {0} Managed Identity. To authenticate with the system assigned identity omit the client id in ManagedIdentityApplicationBuilder.Create().";
18+
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 omit the client id in ManagedIdentityApplicationBuilder.Create().";
2119

2220
public static final String SCOPES_REQUIRED = "At least one scope needs to be requested for this authentication flow. ";
2321
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@ public MsalManagedIdentityException(String errorCode, String errorMessage, Manag
1414
super(errorMessage, errorCode);
1515
this.managedIdentitySourceType = sourceType;
1616
}
17-
public MsalManagedIdentityException(String message, String error) {
18-
super(message, error);
19-
}
20-
21-
public MsalManagedIdentityException(ErrorResponse errorResponse, Map<String, List<String>> httpHeaders) {
22-
super(errorResponse, httpHeaders);
23-
}
2417

25-
public MsalManagedIdentityException(AadInstanceDiscoveryResponse discoveryResponse) {
26-
super(discoveryResponse);
18+
public MsalManagedIdentityException(String errorCode, ManagedIdentitySourceType sourceType) {
19+
this(errorCode, "", sourceType);
2720
}
2821
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ public class EnvironmentVariablesHelper implements IEnvironmentVariables {
1414

1515
switch (source) {
1616
case AppService:
17-
mockedEnvironmentVariables.put(IEnvironmentVariables.IDENTITY_ENDPOINT, endpoint);
18-
mockedEnvironmentVariables.put(IEnvironmentVariables.IDENTITY_HEADER, "secret");
17+
mockedEnvironmentVariables.put(Constants.IDENTITY_ENDPOINT, endpoint);
18+
mockedEnvironmentVariables.put(Constants.IDENTITY_HEADER, "secret");
1919
break;
2020

2121
case Imds:
22-
mockedEnvironmentVariables.put(EnvironmentVariables.IMDS_ENDPOINT, endpoint);
22+
mockedEnvironmentVariables.put(Constants.IMDS_ENDPOINT, endpoint);
2323
break;
2424

2525
case ServiceFabric:
26-
mockedEnvironmentVariables.put(IEnvironmentVariables.IDENTITY_ENDPOINT, endpoint);
27-
mockedEnvironmentVariables.put(IEnvironmentVariables.IDENTITY_HEADER, "secret");
28-
mockedEnvironmentVariables.put(IEnvironmentVariables.IDENTITY_SERVER_THUMBPRINT, "thumbprint");
26+
mockedEnvironmentVariables.put(Constants.IDENTITY_ENDPOINT, endpoint);
27+
mockedEnvironmentVariables.put(Constants.IDENTITY_HEADER, "secret");
28+
mockedEnvironmentVariables.put(Constants.IDENTITY_SERVER_THUMBPRINT, "thumbprint");
2929
break;
3030

3131
case CloudShell:
32-
mockedEnvironmentVariables.put(IEnvironmentVariables.MSI_ENDPOINT, endpoint);
32+
mockedEnvironmentVariables.put(Constants.MSI_ENDPOINT, endpoint);
3333
break;
3434

3535
case AzureArc:
36-
mockedEnvironmentVariables.put(IEnvironmentVariables.IDENTITY_ENDPOINT, endpoint);
37-
mockedEnvironmentVariables.put(IEnvironmentVariables.IMDS_ENDPOINT, endpoint);
36+
mockedEnvironmentVariables.put(Constants.IDENTITY_ENDPOINT, endpoint);
37+
mockedEnvironmentVariables.put(Constants.IMDS_ENDPOINT, endpoint);
3838
break;
3939
}
4040
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package com.microsoft.aad.msal4j;
55

6-
import org.apache.http.HttpException;
76
import org.junit.jupiter.api.TestInstance;
87
import org.junit.jupiter.api.extension.ExtendWith;
98
import org.junit.jupiter.params.ParameterizedTest;

0 commit comments

Comments
 (0)