Skip to content

Commit babe142

Browse files
committed
Change ATTEMPT_REGION_DISCOVERY to boolean, to facilitate the runtime opt-in/opt-out
1 parent 6053380 commit babe142

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

msal/application.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ClientApplication(object):
109109
GET_ACCOUNTS_ID = "902"
110110
REMOVE_ACCOUNT_ID = "903"
111111

112-
ATTEMPT_REGION_DISCOVERY = "TryAutoDetect"
112+
ATTEMPT_REGION_DISCOVERY = True # "TryAutoDetect"
113113

114114
def __init__(
115115
self, client_id,
@@ -242,7 +242,8 @@ def __init__(
242242
(However MSAL Python does not support managed identity,
243243
so this one does not apply.)
244244
245-
3. An app authenticated by Subject Name/Issuer (SNI).
245+
3. An app authenticated by
246+
`Subject Name/Issuer (SNI) <https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/60>`_.
246247
247248
4. An app which already onboard to the region's allow-list.
248249
@@ -258,10 +259,22 @@ def __init__(
258259
259260
An app running inside Azure Functions and Azure VM can use a special keyword
260261
``ClientApplication.ATTEMPT_REGION_DISCOVERY`` to auto-detect region.
261-
(Attempting this on a non-VM could hang indefinitely.
262-
Make sure you configure a short timeout,
263-
or provide a custom http_client which has a short timeout.
264-
That way, the latency would be under your control.)
262+
263+
.. note::
264+
265+
Setting ``azure_region`` to non-``None`` for an app running
266+
outside of Azure Function/VM could hang indefinitely.
267+
268+
You should consider opting in/out region behavior on-demand,
269+
by loading ``azure_region=None`` or ``azure_region="westus"``
270+
or ``azure_region=True`` (which means opt-in and auto-detect)
271+
from your per-deployment configuration, and then do
272+
``app = ConfidentialClientApplication(..., azure_region=azure_region)``.
273+
274+
Alternatively, you can configure a short timeout,
275+
or provide a custom http_client which has a short timeout.
276+
That way, the latency would be under your control,
277+
but still less performant than opting out of region feature.
265278
"""
266279
self.client_id = client_id
267280
self.client_credential = client_credential

msal/region.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ def _detect_region_of_azure_vm(http_client):
2929
)
3030
logger.info(
3131
"Connecting to IMDS {}. "
32-
"You may want to use a shorter timeout on your http_client".format(url))
32+
"It may take a while if you are running outside of Azure. "
33+
"You should consider opting in/out region behavior on-demand, "
34+
'by loading a boolean flag "is_deployed_in_azure" '
35+
'from your per-deployment config and then do '
36+
'"app = ConfidentialClientApplication(..., '
37+
'azure_region=is_deployed_in_azure)"'.format(url))
3338
try:
3439
# https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service?tabs=linux#instance-metadata
3540
resp = http_client.get(url, headers={"Metadata": "true"})

tests/test_e2e.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,13 @@ class WorldWideRegionalEndpointTestCase(LabBasedTestCase):
750750
def test_acquire_token_for_client_should_hit_regional_endpoint(self):
751751
"""This is the only grant supported by regional endpoint, for now"""
752752
self.app = get_lab_app( # Regional endpoint only supports confidential client
753-
## Would fail the OIDC Discovery
754-
#authority="https://westus2.login.microsoftonline.com/"
755-
# "72f988bf-86f1-41af-91ab-2d7cd011db47", # Microsoft tenant ID
756753

754+
## FWIW, the MSAL<1.12 versions could use this to achieve similar result
757755
#authority="https://westus.login.microsoft.com/microsoft.onmicrosoft.com",
758756
#validate_authority=False,
759-
760757
authority="https://login.microsoftonline.com/microsoft.onmicrosoft.com",
761758
azure_region=self.region, # Explicitly use this region, regardless of detection
759+
762760
timeout=2, # Short timeout makes this test case responsive on non-VM
763761
)
764762
scopes = ["https://graph.microsoft.com/.default"]

0 commit comments

Comments
 (0)