Skip to content

Managed Identity Support #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
60bbf7c
Add initial code for MSI
neha-bhargava Jun 20, 2023
6688149
Update the MsalRequest object flow
neha-bhargava Aug 9, 2023
9992111
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
neha-bhargava Aug 10, 2023
7d3f2e0
Update successful test and refactor request flow
neha-bhargava Aug 15, 2023
eb36681
Add more tests
neha-bhargava Aug 23, 2023
d7364ba
Minor changes
neha-bhargava Aug 28, 2023
68a5180
Address comments
neha-bhargava Sep 5, 2023
91ea7ce
Fix test with correct exception
neha-bhargava Sep 5, 2023
9e3dcb3
Address comments
neha-bhargava Sep 6, 2023
2a264bb
Address comments
neha-bhargava Sep 6, 2023
aa7857a
Merge pull request #661 from AzureAD/nebharg/MsiInitial
neha-bhargava Sep 11, 2023
392c93e
Missed to add these changes to the merged PR
neha-bhargava Sep 11, 2023
76578e2
Merge pull request #709 from AzureAD/nebharg/MsiInitial
neha-bhargava Sep 11, 2023
0913e04
Add code and unit tests for cloud shell
neha-bhargava Sep 18, 2023
36dfe18
Add sample app to test msi on cloud shell or VM
neha-bhargava Sep 19, 2023
8eb5a0d
Address comments
neha-bhargava Sep 21, 2023
5f5e32e
Merge pull request #713 from AzureAD/nebharg/MsiCloudShell
neha-bhargava Oct 2, 2023
450757a
MSI Refactoring (#715)
Avery-Dunn Oct 5, 2023
aba4d59
Version number changes for 1.14.2-beta release (#723)
Avery-Dunn Oct 8, 2023
2ff1b2c
Static token cache for MSI (#722)
Avery-Dunn Oct 9, 2023
1f7fa9d
Update cache keys for MSI scenarios (#746)
Avery-Dunn Nov 7, 2023
cc7f1c6
Add azure arc managed identity (#730)
neha-bhargava Nov 9, 2023
ad1362d
Fix MSI error response parsing issue (#750)
Avery-Dunn Nov 21, 2023
a2d00ef
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
Avery-Dunn Nov 27, 2023
68808ed
Version updates for 1.14.4-beta (#752)
Avery-Dunn Nov 27, 2023
083b620
Merge branch 'nebharg/MSI' of https://github.com/AzureAD/microsoft-au…
Avery-Dunn Jan 11, 2024
24ed6ca
Replace MsalManagedIdentityException with regular MsalServiceException
Avery-Dunn Feb 20, 2024
4c00656
Improve logging
Avery-Dunn Feb 20, 2024
f108901
Clean up unexpected exception handling
Avery-Dunn Feb 20, 2024
0ae2e40
Implement Managed Identity retry logic
Avery-Dunn Feb 26, 2024
c24bd8a
Use Managed Identity cert thumbprint in requests
Avery-Dunn Feb 27, 2024
c35ebe4
Merge pull request #782 from AzureAD/avdunn/msi-logs-and-exceptions
Avery-Dunn Feb 29, 2024
d011292
Refactor to not reference implementation classes in base class
Avery-Dunn Mar 10, 2024
e7c60f6
Refactor to not reference implementation classes in base class
Avery-Dunn Mar 10, 2024
6e77df7
Remove HttpClient from ServiceBundle
Avery-Dunn Mar 10, 2024
f75d52b
Add unit test for retry logic
Avery-Dunn Mar 10, 2024
f9da1e0
Remove unused method
Avery-Dunn Mar 10, 2024
f775318
Merge pull request #790 from AzureAD/avdunn/mi-retry
Avery-Dunn Mar 11, 2024
7d26cd5
Adjust retry policy for MSI
Avery-Dunn Mar 14, 2024
da43da4
Make cert validation only apply to Service Fabric flow
Avery-Dunn Mar 14, 2024
93a06d5
Fix service fabric source checks
Avery-Dunn Mar 14, 2024
7cc637f
Revert unnecessary changes
Avery-Dunn Mar 14, 2024
13fadd4
Merge branch 'nebharg/MSI' of https://github.com/AzureAD/microsoft-au…
Avery-Dunn Mar 14, 2024
f575ece
Resolve merge conflicts
Avery-Dunn Mar 14, 2024
c8d33fd
Add comment
Avery-Dunn Mar 14, 2024
4c9340c
Merge pull request #798 from AzureAD/avdunn/mi-retry
Avery-Dunn Mar 15, 2024
0bcd65b
Add resource to headers
Avery-Dunn Mar 18, 2024
8e2945d
PR feedback
Avery-Dunn Mar 19, 2024
1f16b0c
Merge pull request #800 from AzureAD/avdunn/cloudshell-fix
Avery-Dunn Mar 20, 2024
9d8ab0c
Merge pull request #791 from AzureAD/avdunn/cert-service-fabric
Avery-Dunn Mar 20, 2024
e82075a
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
Avery-Dunn Mar 25, 2024
a8ab857
Fix merge conflicts
Avery-Dunn Mar 25, 2024
127b4b9
Avoid false positive credscan flag
Avery-Dunn Mar 25, 2024
876a779
Avoid false positive credscan flag
Avery-Dunn Mar 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ Version 1.14.1
- Improve timeout behavior for futures (#756)
- Reduce verbosity of certain info logs (#756)

Version 1.14.4-beta
=============
- Beta support for MSI in Azure Arc (#730)
- Beta support for MSI in Service Fabric (#729)
- Fix Cloud Shell parsing issue (#750)

Version 1.14.0
=============
- GA release of MSAL Java Brokers package
- Add support for acquiring bearer and proof-of-possession tokens using WAM as the broker (#590)
- Default throttling time for password grant requests lowered to 5 seconds (#721)
- Fix internal docs generation issue (#705)

Version 1.14.2-beta
=============
- Add support for Managed Identity (#712)

Version 1.14.1-beta
=============
- Add proof-of-possession token support
Expand Down
1 change: 1 addition & 0 deletions msal4j-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Find [the latest package in the Maven repository](https://mvnrepository.com/arti
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.14.3</version>
</dependency>
```
### Gradle

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ void acquireTokenSilent_LabAuthority_TokenNotRefreshed(String environment) throw
// Check that access and id tokens are coming from cache
assertEquals(result.accessToken(), acquireSilentResult.accessToken());
assertEquals(result.idToken(), acquireSilentResult.idToken());
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
assertEquals(TokenSource.CACHE, acquireSilentResult.metadata().tokenSource());
}

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

// Check that new refresh and id tokens are being returned
assertTokensAreNotEqual(result, resultAfterRefresh);
assertEquals(TokenSource.IDENTITY_PROVIDER, result.metadata().tokenSource());
assertEquals(TokenSource.IDENTITY_PROVIDER, resultAfterRefresh.metadata().tokenSource());
}

@ParameterizedTest
Expand Down Expand Up @@ -253,6 +257,8 @@ void acquireTokenSilent_WithRefreshOn(String environment) throws Exception {
//Current time is after refreshOn, so token should be refreshed
assertNotNull(resultSilentWithRefreshOn);
assertTokensAreNotEqual(resultSilent, resultSilentWithRefreshOn);
assertEquals(TokenSource.CACHE, resultSilent.metadata().tokenSource());
assertEquals(TokenSource.IDENTITY_PROVIDER, resultSilentWithRefreshOn.metadata().tokenSource());
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

class AadInstanceDiscoveryProvider {

private final static String DEFAULT_TRUSTED_HOST = "login.microsoftonline.com";
private final static String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/v2.0/authorize";
private final static String INSTANCE_DISCOVERY_ENDPOINT_TEMPLATE = "https://{host}:{port}/common/discovery/instance";
private final static String INSTANCE_DISCOVERY_REQUEST_PARAMETERS_TEMPLATE = "?api-version=1.1&authorization_endpoint={authorizeEndpoint}";
private final static String HOST_TEMPLATE_WITH_REGION = "{region}.login.microsoft.com";
private final static String SOVEREIGN_HOST_TEMPLATE_WITH_REGION = "{region}.{host}";
private final static String REGION_NAME = "REGION_NAME";
private final static int PORT_NOT_SET = -1;
private static final String DEFAULT_TRUSTED_HOST = "login.microsoftonline.com";
private static final String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/v2.0/authorize";
private static final String INSTANCE_DISCOVERY_ENDPOINT_TEMPLATE = "https://{host}:{port}/common/discovery/instance";
private static final String INSTANCE_DISCOVERY_REQUEST_PARAMETERS_TEMPLATE = "?api-version=1.1&authorization_endpoint={authorizeEndpoint}";
private static final String HOST_TEMPLATE_WITH_REGION = "{region}.login.microsoft.com";
private static final String SOVEREIGN_HOST_TEMPLATE_WITH_REGION = "{region}.{host}";
private static final String REGION_NAME = "REGION_NAME";
private static final int PORT_NOT_SET = -1;

// For information of the current api-version refer: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service#versioning
private static final String DEFAULT_API_VERSION = "2020-06-01";
Expand Down Expand Up @@ -62,11 +62,10 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
boolean validateAuthority,
MsalRequest msalRequest,
ServiceBundle serviceBundle) {

String host = authorityUrl.getHost();

//If instanceDiscovery flag set to false, cache a basic instance metadata entry to skip future lookups
if (!msalRequest.application().instanceDiscovery()) {
//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
if (msalRequest.application() instanceof ManagedIdentityApplication || !((AbstractClientApplicationBase) msalRequest.application()).instanceDiscovery()) {
if (cache.get(host) == null) {
log.debug("Instance discovery set to false, caching a default entry.");
cacheInstanceDiscoveryMetadata(host);
Expand All @@ -75,8 +74,8 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
}

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

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

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

msalRequest.application().azureRegion = detectedRegion;
host = getRegionalizedHost(authorityUrl.getHost(), msalRequest.application().azureRegion());
((AbstractClientApplicationBase) msalRequest.application()).azureRegion = detectedRegion;
host = getRegionalizedHost(authorityUrl.getHost(), ((AbstractClientApplicationBase) msalRequest.application()).azureRegion());
}

cacheRegionInstanceMetadata(authorityUrl.getHost(), host);
serviceBundle.getServerSideTelemetry().getCurrentRequest().regionOutcome(
determineRegionOutcome(detectedRegion, msalRequest.application().azureRegion(), msalRequest.application().autoDetectRegion()));
determineRegionOutcome(detectedRegion, ((AbstractClientApplicationBase) msalRequest.application()).azureRegion(), ((AbstractClientApplicationBase) msalRequest.application()).autoDetectRegion()));
}

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


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

return HttpHelper.executeHttpRequest(
return serviceBundle.getHttpHelper().executeHttpRequest(
httpRequest,
msalRequest.requestContext(),
serviceBundle);
Expand Down
Loading