Skip to content

Commit 36b7490

Browse files
authored
Merge pull request #712 from AzureAD/nebharg/MSI
Managed Identity Support
2 parents 9c7636b + 876a779 commit 36b7490

File tree

79 files changed

+3527
-433
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+3527
-433
lines changed

changelog.txt

+10
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@ Version 1.14.1
1313
- Improve timeout behavior for futures (#756)
1414
- Reduce verbosity of certain info logs (#756)
1515

16+
Version 1.14.4-beta
17+
=============
18+
- Beta support for MSI in Azure Arc (#730)
19+
- Beta support for MSI in Service Fabric (#729)
20+
- Fix Cloud Shell parsing issue (#750)
21+
1622
Version 1.14.0
1723
=============
1824
- GA release of MSAL Java Brokers package
1925
- Add support for acquiring bearer and proof-of-possession tokens using WAM as the broker (#590)
2026
- Default throttling time for password grant requests lowered to 5 seconds (#721)
2127
- Fix internal docs generation issue (#705)
2228

29+
Version 1.14.2-beta
30+
=============
31+
- Add support for Managed Identity (#712)
32+
2333
Version 1.14.1-beta
2434
=============
2535
- Add proof-of-possession token support

msal4j-sdk/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Find [the latest package in the Maven repository](https://mvnrepository.com/arti
2929
<groupId>com.microsoft.azure</groupId>
3030
<artifactId>msal4j</artifactId>
3131
<version>1.14.3</version>
32+
</dependency>
3233
```
3334
### Gradle
3435

msal4j-sdk/src/integrationtest/java/com.microsoft.aad.msal4j/AcquireTokenSilentIT.java

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ void acquireTokenSilent_LabAuthority_TokenNotRefreshed(String environment) throw
6969
// Check that access and id tokens are coming from cache
7070
assertEquals(result.accessToken(), acquireSilentResult.accessToken());
7171
assertEquals(result.idToken(), acquireSilentResult.idToken());
72+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
73+
assertEquals(TokenSource.CACHE, acquireSilentResult.metadata().tokenSource());
7274
}
7375

7476
@ParameterizedTest
@@ -92,6 +94,8 @@ void acquireTokenSilent_ForceRefresh(String environment) throws Exception {
9294

9395
// Check that new refresh and id tokens are being returned
9496
assertTokensAreNotEqual(result, resultAfterRefresh);
97+
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
98+
assertEquals(TokenSource.IDENTITY_PROVIDER, resultAfterRefresh.metadata().tokenSource());
9599
}
96100

97101
@ParameterizedTest
@@ -253,6 +257,8 @@ void acquireTokenSilent_WithRefreshOn(String environment) throws Exception {
253257
//Current time is after refreshOn, so token should be refreshed
254258
assertNotNull(resultSilentWithRefreshOn);
255259
assertTokensAreNotEqual(resultSilent, resultSilentWithRefreshOn);
260+
assertEquals(TokenSource.CACHE, resultSilent.metadata().tokenSource());
261+
assertEquals(TokenSource.IDENTITY_PROVIDER, resultSilentWithRefreshOn.metadata().tokenSource());
256262
}
257263

258264
@ParameterizedTest

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

+20-20
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

1919
class AadInstanceDiscoveryProvider {
2020

21-
private final static String DEFAULT_TRUSTED_HOST = "login.microsoftonline.com";
22-
private final static String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/v2.0/authorize";
23-
private final static String INSTANCE_DISCOVERY_ENDPOINT_TEMPLATE = "https://{host}:{port}/common/discovery/instance";
24-
private final static String INSTANCE_DISCOVERY_REQUEST_PARAMETERS_TEMPLATE = "?api-version=1.1&authorization_endpoint={authorizeEndpoint}";
25-
private final static String HOST_TEMPLATE_WITH_REGION = "{region}.login.microsoft.com";
26-
private final static String SOVEREIGN_HOST_TEMPLATE_WITH_REGION = "{region}.{host}";
27-
private final static String REGION_NAME = "REGION_NAME";
28-
private final static int PORT_NOT_SET = -1;
21+
private static final String DEFAULT_TRUSTED_HOST = "login.microsoftonline.com";
22+
private static final String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/v2.0/authorize";
23+
private static final String INSTANCE_DISCOVERY_ENDPOINT_TEMPLATE = "https://{host}:{port}/common/discovery/instance";
24+
private static final String INSTANCE_DISCOVERY_REQUEST_PARAMETERS_TEMPLATE = "?api-version=1.1&authorization_endpoint={authorizeEndpoint}";
25+
private static final String HOST_TEMPLATE_WITH_REGION = "{region}.login.microsoft.com";
26+
private static final String SOVEREIGN_HOST_TEMPLATE_WITH_REGION = "{region}.{host}";
27+
private static final String REGION_NAME = "REGION_NAME";
28+
private static final int PORT_NOT_SET = -1;
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";
@@ -62,11 +62,10 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
6262
boolean validateAuthority,
6363
MsalRequest msalRequest,
6464
ServiceBundle serviceBundle) {
65-
6665
String host = authorityUrl.getHost();
6766

68-
//If instanceDiscovery flag set to false, cache a basic instance metadata entry to skip future lookups
69-
if (!msalRequest.application().instanceDiscovery()) {
67+
//If instanceDiscovery flag set to false OR this is a managed identity scenario, cache a basic instance metadata entry to skip this and future lookups
68+
if (msalRequest.application() instanceof ManagedIdentityApplication || !((AbstractClientApplicationBase) msalRequest.application()).instanceDiscovery()) {
7069
if (cache.get(host) == null) {
7170
log.debug("Instance discovery set to false, caching a default entry.");
7271
cacheInstanceDiscoveryMetadata(host);
@@ -75,8 +74,8 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
7574
}
7675

7776
//If a region was set by an app developer or previously found through autodetection, adjust the authority host to use it
78-
if (shouldUseRegionalEndpoint(msalRequest) && msalRequest.application().azureRegion() != null) {
79-
host = getRegionalizedHost(authorityUrl.getHost(), msalRequest.application().azureRegion());
77+
if (shouldUseRegionalEndpoint(msalRequest) && ((AbstractClientApplicationBase) msalRequest.application()).azureRegion() != null) {
78+
host = getRegionalizedHost(authorityUrl.getHost(), ((AbstractClientApplicationBase) msalRequest.application()).azureRegion());
8079
}
8180

8281
//If there is no cached instance metadata, do instance discovery cache the result
@@ -91,18 +90,18 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
9190

9291
//If region autodetection is enabled and a specific region was not already set, set the application's
9392
// region to the discovered region so that future requests can skip the IMDS endpoint call
94-
if (msalRequest.application().azureRegion() == null
95-
&& msalRequest.application().autoDetectRegion()
93+
if (((AbstractClientApplicationBase) msalRequest.application()).azureRegion() == null
94+
&& ((AbstractClientApplicationBase) msalRequest.application()).autoDetectRegion()
9695
&& detectedRegion != null) {
9796
log.debug(String.format("Region autodetection found %s, this region will be used for future calls.", detectedRegion));
9897

99-
msalRequest.application().azureRegion = detectedRegion;
100-
host = getRegionalizedHost(authorityUrl.getHost(), msalRequest.application().azureRegion());
98+
((AbstractClientApplicationBase) msalRequest.application()).azureRegion = detectedRegion;
99+
host = getRegionalizedHost(authorityUrl.getHost(), ((AbstractClientApplicationBase) msalRequest.application()).azureRegion());
101100
}
102101

103102
cacheRegionInstanceMetadata(authorityUrl.getHost(), host);
104103
serviceBundle.getServerSideTelemetry().getCurrentRequest().regionOutcome(
105-
determineRegionOutcome(detectedRegion, msalRequest.application().azureRegion(), msalRequest.application().autoDetectRegion()));
104+
determineRegionOutcome(detectedRegion, ((AbstractClientApplicationBase) msalRequest.application()).azureRegion(), ((AbstractClientApplicationBase) msalRequest.application()).autoDetectRegion()));
106105
}
107106

108107
doInstanceDiscoveryAndCache(authorityUrl, validateAuthority, msalRequest, serviceBundle);
@@ -160,7 +159,8 @@ static void cacheInstanceDiscoveryMetadata(String host) {
160159

161160

162161
private static boolean shouldUseRegionalEndpoint(MsalRequest msalRequest){
163-
if (msalRequest.application().azureRegion() != null || msalRequest.application().autoDetectRegion()){
162+
if (((AbstractClientApplicationBase) msalRequest.application()).azureRegion() != null
163+
|| ((AbstractClientApplicationBase) msalRequest.application()).autoDetectRegion()){
164164
//This class type check is a quick and dirty fix to accommodate changes to the internal workings of the region API
165165
//
166166
//ESTS-R only supports a small, but growing, number of scenarios, and the original design failed silently whenever
@@ -296,7 +296,7 @@ private static IHttpResponse executeRequest(String requestUrl, Map<String, Strin
296296
requestUrl,
297297
headers);
298298

299-
return HttpHelper.executeHttpRequest(
299+
return serviceBundle.getHttpHelper().executeHttpRequest(
300300
httpRequest,
301301
msalRequest.requestContext(),
302302
serviceBundle);

0 commit comments

Comments
 (0)