18
18
19
19
class AadInstanceDiscoveryProvider {
20
20
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 ;
29
29
30
30
// For information of the current api-version refer: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service#versioning
31
31
private static final String DEFAULT_API_VERSION = "2020-06-01" ;
@@ -62,11 +62,10 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
62
62
boolean validateAuthority ,
63
63
MsalRequest msalRequest ,
64
64
ServiceBundle serviceBundle ) {
65
-
66
65
String host = authorityUrl .getHost ();
67
66
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 ()) {
70
69
if (cache .get (host ) == null ) {
71
70
log .debug ("Instance discovery set to false, caching a default entry." );
72
71
cacheInstanceDiscoveryMetadata (host );
@@ -75,8 +74,8 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
75
74
}
76
75
77
76
//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 ());
80
79
}
81
80
82
81
//If there is no cached instance metadata, do instance discovery cache the result
@@ -91,18 +90,18 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
91
90
92
91
//If region autodetection is enabled and a specific region was not already set, set the application's
93
92
// 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 ()
96
95
&& detectedRegion != null ) {
97
96
log .debug (String .format ("Region autodetection found %s, this region will be used for future calls." , detectedRegion ));
98
97
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 ());
101
100
}
102
101
103
102
cacheRegionInstanceMetadata (authorityUrl .getHost (), host );
104
103
serviceBundle .getServerSideTelemetry ().getCurrentRequest ().regionOutcome (
105
- determineRegionOutcome (detectedRegion , msalRequest .application ().azureRegion (), msalRequest .application ().autoDetectRegion ()));
104
+ determineRegionOutcome (detectedRegion , (( AbstractClientApplicationBase ) msalRequest .application ()) .azureRegion (), (( AbstractClientApplicationBase ) msalRequest .application () ).autoDetectRegion ()));
106
105
}
107
106
108
107
doInstanceDiscoveryAndCache (authorityUrl , validateAuthority , msalRequest , serviceBundle );
@@ -160,7 +159,8 @@ static void cacheInstanceDiscoveryMetadata(String host) {
160
159
161
160
162
161
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 ()){
164
164
//This class type check is a quick and dirty fix to accommodate changes to the internal workings of the region API
165
165
//
166
166
//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
296
296
requestUrl ,
297
297
headers );
298
298
299
- return HttpHelper .executeHttpRequest (
299
+ return serviceBundle . getHttpHelper () .executeHttpRequest (
300
300
httpRequest ,
301
301
msalRequest .requestContext (),
302
302
serviceBundle );
0 commit comments