Skip to content

Commit 33dbe3e

Browse files
committed
Default to env var MSAL_FORCE_REGION
Add an alias DISABLE_MSAL_FORCE_REFRESH for False
1 parent 95a63a7 commit 33dbe3e

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

msal/application.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import warnings
77
from threading import Lock
8+
from typing import Optional # Needed in Python 3.7 & 3.8
89
import os
910

1011
from .oauth2cli import Client, JwtAssertionCreator
@@ -225,6 +226,7 @@ class ClientApplication(object):
225226
REMOVE_ACCOUNT_ID = "903"
226227

227228
ATTEMPT_REGION_DISCOVERY = True # "TryAutoDetect"
229+
DISABLE_MSAL_FORCE_REGION = False # Used in azure_region to disable MSAL_FORCE_REGION behavior
228230
_TOKEN_SOURCE = "token_source"
229231
_TOKEN_SOURCE_IDP = "identity_provider"
230232
_TOKEN_SOURCE_CACHE = "cache"
@@ -448,11 +450,14 @@ def __init__(
448450
Instructs MSAL to use the Entra regional token service. This legacy feature is only available to
449451
first-party applications. Only ``acquire_token_for_client()`` is supported.
450452
451-
Supports 3 values:
453+
Supports 4 values:
452454
453-
``azure_region=None`` - meaning no region is used. This is the default value.
454-
``azure_region="some_region"`` - meaning the specified region is used.
455-
``azure_region=True`` - meaning MSAL will try to auto-detect the region. This is not recommended.
455+
1. ``azure_region=None`` - This default value means no region is configured.
456+
MSAL will use the region defined in env var ``MSAL_FORCE_REGION``.
457+
2. ``azure_region="some_region"`` - meaning the specified region is used.
458+
3. ``azure_region=True`` - meaning
459+
MSAL will try to auto-detect the region. This is not recommended.
460+
4. ``azure_region=False`` - meaning MSAL will use no region.
456461
457462
.. note::
458463
Region auto-discovery has been tested on VMs and on Azure Functions. It is unreliable.
@@ -630,7 +635,10 @@ def __init__(
630635
except ValueError: # Those are explicit authority validation errors
631636
raise
632637
except Exception: # The rest are typically connection errors
633-
if validate_authority and azure_region and not oidc_authority:
638+
if validate_authority and not oidc_authority and (
639+
azure_region # Opted in to use region
640+
or (azure_region is None and os.getenv("MSAL_FORCE_REGION")) # Will use region
641+
):
634642
# Since caller opts in to use region, here we tolerate connection
635643
# errors happened during authority validation at non-region endpoint
636644
self.authority = Authority(
@@ -724,9 +732,11 @@ def _build_telemetry_context(
724732
self._telemetry_buffer, self._telemetry_lock, api_id,
725733
correlation_id=correlation_id, refresh_reason=refresh_reason)
726734

727-
def _get_regional_authority(self, central_authority):
728-
if not self._region_configured: # User did not opt-in to ESTS-R
735+
def _get_regional_authority(self, central_authority) -> Optional[Authority]:
736+
if self._region_configured is False: # User opts out of ESTS-R
729737
return None # Short circuit to completely bypass region detection
738+
if self._region_configured is None: # User did not make an ESTS-R choice
739+
self._region_configured = os.getenv("MSAL_FORCE_REGION") or None
730740
self._region_detected = self._region_detected or _detect_region(
731741
self.http_client if self._region_configured is not None else None)
732742
if (self._region_configured != self.ATTEMPT_REGION_DISCOVERY

tests/test_e2e.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,23 @@ def _test_acquire_token_for_client(self, configured_region, expected_region):
11301130
def test_acquire_token_for_client_should_hit_global_endpoint_by_default(self):
11311131
self._test_acquire_token_for_client(None, None)
11321132

1133-
def test_acquire_token_for_client_should_ignore_env_var_by_default(self):
1133+
def test_acquire_token_for_client_should_ignore_env_var_region_name_by_default(self):
11341134
os.environ["REGION_NAME"] = "eastus"
11351135
self._test_acquire_token_for_client(None, None)
11361136
del os.environ["REGION_NAME"]
11371137

1138+
@patch.dict(os.environ, {"MSAL_FORCE_REGION": "eastus"})
1139+
def test_acquire_token_for_client_should_use_env_var_msal_force_region_by_default(self):
1140+
self._test_acquire_token_for_client(None, "eastus")
1141+
1142+
@patch.dict(os.environ, {"MSAL_FORCE_REGION": "eastus"})
1143+
def test_acquire_token_for_client_should_prefer_the_explicit_region(self):
1144+
self._test_acquire_token_for_client("westus", "westus")
1145+
1146+
@patch.dict(os.environ, {"MSAL_FORCE_REGION": "eastus"})
1147+
def test_acquire_token_for_client_should_allow_opt_out_env_var_msal_force_region(self):
1148+
self._test_acquire_token_for_client(False, None)
1149+
11381150
def test_acquire_token_for_client_should_use_a_specified_region(self):
11391151
self._test_acquire_token_for_client("westus", "westus")
11401152

0 commit comments

Comments
 (0)