Skip to content

Commit e82075a

Browse files
committed
# Conflicts: # README.md # changelog.txt # msal4j-sdk/README.md # msal4j-sdk/bnd.bnd # msal4j-sdk/pom.xml # msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AadInstanceDiscoveryProvider.java # msal4j-sdk/src/samples/msal-b2c-web-sample/pom.xml # msal4j-sdk/src/samples/msal-obo-sample/pom.xml # msal4j-sdk/src/samples/msal-web-sample/pom.xml
2 parents 9d8ab0c + 9c7636b commit e82075a

23 files changed

+111
-139
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Quick links:
1616
The library supports the following Java environments:
1717
- Java 8 (or higher)
1818

19-
Current version - 1.14.4-beta
19+
Current version - 1.14.3
2020

2121
You can find the changes for each version in the [change log](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/main/msal4j-sdk/changelog.txt).
2222

@@ -28,13 +28,13 @@ Find [the latest package in the Maven repository](https://mvnrepository.com/arti
2828
<dependency>
2929
<groupId>com.microsoft.azure</groupId>
3030
<artifactId>msal4j</artifactId>
31-
<version>1.14.4-beta</version>
31+
<version>1.14.3</version>
3232
</dependency>
3333
```
3434
### Gradle
3535

3636
```gradle
37-
implementation group: 'com.microsoft.azure', name: 'com.microsoft.aad.msal4j', version: '1.14.4-beta'
37+
implementation group: 'com.microsoft.azure', name: 'com.microsoft.aad.msal4j', version: '1.14.3'
3838
```
3939

4040
## Usage

changelog.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
Version 1.14.3
2+
=============
3+
- Hotfix to update oauth2-oidc-sdk dependency (#781)
4+
5+
Version 1.14.2
6+
=============
7+
- Correct IMDS endpoint used for region discovery (#762)
8+
- Improve performance of instance/region discovery (#763)
9+
10+
Version 1.14.1
11+
=============
12+
- Remove key size requirements for certificates (#749)
13+
- Improve timeout behavior for futures (#756)
14+
- Reduce verbosity of certain info logs (#756)
15+
116
Version 1.14.4-beta
217
=============
318
- Beta support for MSI in Azure Arc (#730)

msal4j-brokers/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>org.testng</groupId>
5252
<artifactId>testng</artifactId>
53-
<version>7.1.0</version>
53+
<version>7.5.1</version>
5454
<scope>test</scope>
5555
</dependency>
5656
<dependency>
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>ch.qos.logback</groupId>
6363
<artifactId>logback-classic</artifactId>
64-
<version>1.2.3</version>
64+
<version>1.3.12</version>
6565
<scope>test</scope>
6666
</dependency>
6767
<dependency>

msal4j-sdk/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Quick links:
1616
The library supports the following Java environments:
1717
- Java 8 (or higher)
1818

19-
Current version - 1.14.4-beta
19+
Current version - 1.14.3
2020

2121
You can find the changes for each version in the [change log](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/master/changelog.txt).
2222

@@ -28,13 +28,13 @@ Find [the latest package in the Maven repository](https://mvnrepository.com/arti
2828
<dependency>
2929
<groupId>com.microsoft.azure</groupId>
3030
<artifactId>msal4j</artifactId>
31-
<version>1.14.4-beta</version>
31+
<version>1.14.3</version>
3232
</dependency>
3333
```
3434
### Gradle
3535

3636
```gradle
37-
compile group: 'com.microsoft.azure', name: 'msal4j', version: '1.14.4-beta'
37+
compile group: 'com.microsoft.azure', name: 'msal4j', version: '1.14.3'
3838
```
3939

4040
## Usage

msal4j-sdk/bnd.bnd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Export-Package: com.microsoft.aad.msal4j;version="1.14.4-beta"
1+
Export-Package: com.microsoft.aad.msal4j;version="1.14.3"
22
Automatic-Module-Name: com.microsoft.aad.msal4j

msal4j-sdk/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.microsoft.azure</groupId>
55
<artifactId>msal4j</artifactId>
6-
<version>1.14.4-beta</version>
6+
<version>1.14.3</version>
77
<packaging>jar</packaging>
88
<name>msal4j</name>
99
<description>
@@ -36,12 +36,12 @@
3636
<dependency>
3737
<groupId>com.nimbusds</groupId>
3838
<artifactId>oauth2-oidc-sdk</artifactId>
39-
<version>10.7.1</version>
39+
<version>11.9.1</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>net.minidev</groupId>
4343
<artifactId>json-smart</artifactId>
44-
<version>2.4.10</version>
44+
<version>2.5.0</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>org.slf4j</groupId>
@@ -137,7 +137,7 @@
137137
<dependency>
138138
<groupId>ch.qos.logback</groupId>
139139
<artifactId>logback-classic</artifactId>
140-
<version>1.2.3</version>
140+
<version>1.3.12</version>
141141
<scope>test</scope>
142142
</dependency>
143143
<dependency>

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

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AadInstanceDiscoveryProvider {
2929

3030
// For information of the current api-version refer: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service#versioning
3131
private static final String DEFAULT_API_VERSION = "2020-06-01";
32-
private static final String IMDS_ENDPOINT = "https://169.254.169.254/metadata/instance/compute/location?" + DEFAULT_API_VERSION + "&format=text";
32+
private static final String IMDS_ENDPOINT = "http://169.254.169.254/metadata/instance/compute/location?api-version=" + DEFAULT_API_VERSION + "&format=text";
3333

3434
private static final int IMDS_TIMEOUT = 2;
3535
private static final TimeUnit IMDS_TIMEOUT_UNIT = TimeUnit.SECONDS;
@@ -64,44 +64,47 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
6464
ServiceBundle serviceBundle) {
6565
String host = authorityUrl.getHost();
6666

67-
if (msalRequest.application() instanceof AbstractClientApplicationBase && shouldUseRegionalEndpoint(msalRequest)) {
68-
//Server side telemetry requires the result from region discovery when any part of the region API is used
69-
String detectedRegion = discoverRegion(msalRequest, serviceBundle);
70-
71-
if (((AbstractClientApplicationBase) msalRequest.application()).azureRegion() != null) {
72-
host = getRegionalizedHost(authorityUrl.getHost(),
73-
((AbstractClientApplicationBase) msalRequest.application()).azureRegion());
67+
//If instanceDiscovery flag set to false, cache a basic instance metadata entry to skip future lookups
68+
if (!msalRequest.application().instanceDiscovery()) {
69+
if (cache.get(host) == null) {
70+
log.debug("Instance discovery set to false, caching a default entry.");
71+
cacheInstanceDiscoveryMetadata(host);
7472
}
73+
return cache.get(host);
74+
}
7575

76-
//If region autodetection is enabled and a specific region not already set,
77-
// set the application's region to the discovered region so that future requests can skip the IMDS endpoint call
78-
if (null == ((AbstractClientApplicationBase) msalRequest.application()).azureRegion()
79-
&& ((AbstractClientApplicationBase) msalRequest.application()).autoDetectRegion()
80-
&& null != detectedRegion) {
81-
((AbstractClientApplicationBase) msalRequest.application()).azureRegion = detectedRegion;
82-
}
83-
cacheRegionInstanceMetadata(authorityUrl.getHost(), ((AbstractClientApplicationBase) msalRequest.application()).azureRegion());
84-
serviceBundle.getServerSideTelemetry().getCurrentRequest().regionOutcome(
85-
determineRegionOutcome(detectedRegion,
86-
((AbstractClientApplicationBase) msalRequest.application()).azureRegion(),
87-
((AbstractClientApplicationBase) msalRequest.application()).autoDetectRegion()));
76+
//If a region was set by an app developer or previously found through autodetection, adjust the authority host to use it
77+
if (shouldUseRegionalEndpoint(msalRequest) && msalRequest.application().azureRegion() != null) {
78+
host = getRegionalizedHost(authorityUrl.getHost(), msalRequest.application().azureRegion());
8879
}
8980

90-
InstanceDiscoveryMetadataEntry result = cache.get(host);
81+
//If there is no cached instance metadata, do instance discovery cache the result
82+
if (cache.get(host) == null) {
83+
log.debug("No cached instance metadata, will attempt instance discovery.");
9184

92-
if (result == null) {
93-
if(msalRequest.application() instanceof AbstractClientApplicationBase &&
94-
((AbstractClientApplicationBase) msalRequest.application()).instanceDiscovery()
95-
&& !instanceDiscoveryFailed){
96-
doInstanceDiscoveryAndCache(authorityUrl, validateAuthority, msalRequest, serviceBundle);
97-
} else {
98-
// instanceDiscovery flag is set to False. Do not perform instanceDiscovery.
99-
return InstanceDiscoveryMetadataEntry.builder().
100-
preferredCache(host).
101-
preferredNetwork(host).
102-
aliases(Collections.singleton(host)).
103-
build();
85+
if (shouldUseRegionalEndpoint(msalRequest)) {
86+
log.debug("Region API used, will attempt to discover Azure region.");
87+
88+
//Server side telemetry requires the result from region discovery when any part of the region API is used
89+
String detectedRegion = discoverRegion(msalRequest, serviceBundle);
90+
91+
//If region autodetection is enabled and a specific region was not already set, set the application's
92+
// region to the discovered region so that future requests can skip the IMDS endpoint call
93+
if (msalRequest.application().azureRegion() == null
94+
&& msalRequest.application().autoDetectRegion()
95+
&& detectedRegion != null) {
96+
log.debug(String.format("Region autodetection found %s, this region will be used for future calls.", detectedRegion));
97+
98+
msalRequest.application().azureRegion = detectedRegion;
99+
host = getRegionalizedHost(authorityUrl.getHost(), msalRequest.application().azureRegion());
100+
}
101+
102+
cacheRegionInstanceMetadata(authorityUrl.getHost(), host);
103+
serviceBundle.getServerSideTelemetry().getCurrentRequest().regionOutcome(
104+
determineRegionOutcome(detectedRegion, msalRequest.application().azureRegion(), msalRequest.application().autoDetectRegion()));
104105
}
106+
107+
doInstanceDiscoveryAndCache(authorityUrl, validateAuthority, msalRequest, serviceBundle);
105108
}
106109

107110
return cache.get(host);
@@ -132,7 +135,7 @@ static AadInstanceDiscoveryResponse parseInstanceDiscoveryMetadata(String instan
132135
return aadInstanceDiscoveryResponse;
133136
}
134137

135-
static void cacheInstanceDiscoveryMetadata(String host,
138+
static void cacheInstanceDiscoveryResponse(String host,
136139
AadInstanceDiscoveryResponse aadInstanceDiscoveryResponse) {
137140

138141
if (aadInstanceDiscoveryResponse != null && aadInstanceDiscoveryResponse.metadata() != null) {
@@ -142,6 +145,11 @@ static void cacheInstanceDiscoveryMetadata(String host,
142145
}
143146
}
144147
}
148+
149+
cacheInstanceDiscoveryMetadata(host);
150+
}
151+
152+
static void cacheInstanceDiscoveryMetadata(String host) {
145153
cache.putIfAbsent(host, InstanceDiscoveryMetadataEntry.builder().
146154
preferredCache(host).
147155
preferredNetwork(host).
@@ -171,14 +179,13 @@ private static boolean shouldUseRegionalEndpoint(MsalRequest msalRequest){
171179
return false;
172180
}
173181

174-
static void cacheRegionInstanceMetadata(String host, String region) {
182+
static void cacheRegionInstanceMetadata(String originalHost, String regionalHost) {
175183

176184
Set<String> aliases = new HashSet<>();
177-
aliases.add(host);
178-
String regionalHost = getRegionalizedHost(host, region);
185+
aliases.add(originalHost);
179186

180187
cache.putIfAbsent(regionalHost, InstanceDiscoveryMetadataEntry.builder().
181-
preferredCache(host).
188+
preferredCache(originalHost).
182189
preferredNetwork(regionalHost).
183190
aliases(aliases).
184191
build());
@@ -236,12 +243,10 @@ static AadInstanceDiscoveryResponse sendInstanceDiscoveryRequest(URL authorityUr
236243
MsalRequest msalRequest,
237244
ServiceBundle serviceBundle) {
238245

239-
IHttpResponse httpResponse = null;
240-
241246
String instanceDiscoveryRequestUrl = getInstanceDiscoveryEndpoint(authorityUrl) +
242247
formInstanceDiscoveryParameters(authorityUrl);
243248

244-
httpResponse = executeRequest(instanceDiscoveryRequestUrl, msalRequest.headers().getReadonlyHeaderMap(), msalRequest, serviceBundle);
249+
IHttpResponse httpResponse = executeRequest(instanceDiscoveryRequestUrl, msalRequest.headers().getReadonlyHeaderMap(), msalRequest, serviceBundle);
245250

246251
AadInstanceDiscoveryResponse response = JsonHelper.convertJsonToObject(httpResponse.body(), AadInstanceDiscoveryResponse.class);
247252

@@ -251,7 +256,8 @@ static AadInstanceDiscoveryResponse sendInstanceDiscoveryRequest(URL authorityUr
251256
throw MsalServiceExceptionFactory.fromHttpResponse(httpResponse);
252257
}
253258
// instance discovery failed due to reasons other than an invalid authority, do not perform instance discovery again in this environment.
254-
instanceDiscoveryFailed = true;
259+
log.debug("Instance discovery failed due to an unknown error, no more instance discovery attempts will be made.");
260+
cacheInstanceDiscoveryMetadata(authorityUrl.getHost());
255261
}
256262

257263
return response;
@@ -302,7 +308,7 @@ static String discoverRegion(MsalRequest msalRequest, ServiceBundle serviceBundl
302308

303309
//Check if the REGION_NAME environment variable has a value for the region
304310
if (System.getenv(REGION_NAME) != null) {
305-
log.info("Region found in environment variable: " + System.getenv(REGION_NAME));
311+
log.info(String.format("Region found in environment variable: %s",System.getenv(REGION_NAME)));
306312
currentRequest.regionSource(RegionTelemetry.REGION_SOURCE_ENV_VARIABLE.telemetryValue);
307313

308314
return System.getenv(REGION_NAME);
@@ -358,7 +364,7 @@ private static void doInstanceDiscoveryAndCache(URL authorityUrl,
358364
}
359365
}
360366

361-
cacheInstanceDiscoveryMetadata(authorityUrl.getHost(), aadInstanceDiscoveryResponse);
367+
cacheInstanceDiscoveryResponse(authorityUrl.getHost(), aadInstanceDiscoveryResponse);
362368
}
363369

364370
private static void validate(AadInstanceDiscoveryResponse aadInstanceDiscoveryResponse) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public T instanceDiscovery(boolean val) {
452452
);
453453

454454
if (aadAadInstanceDiscoveryResponse != null) {
455-
AadInstanceDiscoveryProvider.cacheInstanceDiscoveryMetadata(
455+
AadInstanceDiscoveryProvider.cacheInstanceDiscoveryResponse(
456456
authenticationAuthority.host,
457457
aadAadInstanceDiscoveryResponse);
458458
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public Set<IAccount> get() {
2626
(clientApplication.clientId());
2727

2828
} catch (Exception ex) {
29-
clientApplication.log.error(
30-
LogHelper.createMessage("Execution of " + this.getClass() + " failed.",
31-
msalRequest.headers().getHeaderCorrelationIdValue()), ex);
29+
clientApplication.log.warn(
30+
LogHelper.createMessage(
31+
String.format("Execution of %s failed: %s", this.getClass(), ex.getMessage()),
32+
msalRequest.headers().getHeaderCorrelationIdValue()));
3233

3334
throw new CompletionException(ex);
3435
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ private AuthenticationResult acquireTokenWithDeviceCode(DeviceCode deviceCode,
5353

5454
while (getCurrentSystemTimeInSeconds() < expirationTimeInSeconds) {
5555
if (deviceCodeFlowRequest.futureReference().get().isCancelled()) {
56-
throw new InterruptedException("Acquire token Device Code Flow was interrupted");
56+
throw new InterruptedException("Device code flow was cancelled before acquiring a token");
57+
}
58+
if (deviceCodeFlowRequest.futureReference().get().isCompletedExceptionally()) {
59+
throw new InterruptedException("Device code flow had an exception before acquiring a token");
5760
}
5861
try {
5962
return acquireTokenByAuthorisationGrantSupplier.execute();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private AuthorizationResult getAuthorizationResultFromHttpListener() {
209209
expirationTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 1;
210210
}
211211

212-
while (result == null && !interactiveRequest.futureReference().get().isCancelled()) {
212+
while (result == null && !interactiveRequest.futureReference().get().isDone()) {
213213
if (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) > expirationTime) {
214214
LOG.warn(String.format("Listener timed out after %S seconds, no authorization code was returned from the server during that time.", timeFromParameters));
215215
break;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ AuthenticationResult execute() throws Exception {
116116
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
117117
}
118118

119-
log.info("Returning token from cache");
119+
log.debug("Returning token from cache");
120120

121121
return res;
122122
}

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ public IAuthenticationResult get() {
9595
msalRequest.requestContext().correlationId(),
9696
error);
9797

98-
logException(ex);
98+
clientApplication.log.warn(
99+
LogHelper.createMessage(
100+
String.format("Execution of %s failed: %s", this.getClass(), ex.getMessage()),
101+
msalRequest.headers().getHeaderCorrelationIdValue()));
102+
99103
throw new CompletionException(ex);
100104
}
101105
}
@@ -135,26 +139,6 @@ private void logResult(AuthenticationResult result, HttpHeaders headers) {
135139
}
136140
}
137141

138-
private void logException(Exception ex) {
139-
140-
String logMessage = LogHelper.createMessage(
141-
"Execution of " + this.getClass() + " failed.",
142-
msalRequest.headers().getHeaderCorrelationIdValue());
143-
144-
if (ex instanceof MsalClientException) {
145-
MsalClientException exception = (MsalClientException) ex;
146-
if (exception.errorCode() != null && exception.errorCode().equalsIgnoreCase(AuthenticationErrorCode.CACHE_MISS)) {
147-
clientApplication.log.debug(logMessage, ex);
148-
return;
149-
}
150-
} else if (ex instanceof MsalAzureSDKException) {
151-
clientApplication.log.debug(ex.getMessage(), ex);
152-
return;
153-
}
154-
155-
clientApplication.log.error(logMessage, ex);
156-
}
157-
158142
private ApiEvent initializeApiEvent(MsalRequest msalRequest) {
159143
ApiEvent apiEvent = new ApiEvent(clientApplication.logPii());
160144
msalRequest.requestContext().telemetryRequestId(

0 commit comments

Comments
 (0)