Skip to content

Commit c834d8b

Browse files
committed
Default to env var MSAL_FORCE_REGION
1 parent 95a63a7 commit c834d8b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

msal/application.py

Lines changed: 13 additions & 5 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
@@ -448,11 +449,13 @@ def __init__(
448449
Instructs MSAL to use the Entra regional token service. This legacy feature is only available to
449450
first-party applications. Only ``acquire_token_for_client()`` is supported.
450451
451-
Supports 3 values:
452+
Supports 4 values:
452453
453-
``azure_region=None`` - meaning no region is used. This is the default value.
454+
``azure_region=None`` - This default value means no region is configured.
455+
MSAL will use the region defined in env var ``MSAL_FORCE_REGION``.
454456
``azure_region="some_region"`` - meaning the specified region is used.
455457
``azure_region=True`` - meaning MSAL will try to auto-detect the region. This is not recommended.
458+
``azure_region=False`` - meaning MSAL will use no region.
456459
457460
.. note::
458461
Region auto-discovery has been tested on VMs and on Azure Functions. It is unreliable.
@@ -630,7 +633,10 @@ def __init__(
630633
except ValueError: # Those are explicit authority validation errors
631634
raise
632635
except Exception: # The rest are typically connection errors
633-
if validate_authority and azure_region and not oidc_authority:
636+
if validate_authority and not oidc_authority and (
637+
azure_region # Opted in to use region
638+
or (azure_region is None and os.getenv("MSAL_FORCE_REGION")) # Will use region
639+
):
634640
# Since caller opts in to use region, here we tolerate connection
635641
# errors happened during authority validation at non-region endpoint
636642
self.authority = Authority(
@@ -724,9 +730,11 @@ def _build_telemetry_context(
724730
self._telemetry_buffer, self._telemetry_lock, api_id,
725731
correlation_id=correlation_id, refresh_reason=refresh_reason)
726732

727-
def _get_regional_authority(self, central_authority):
728-
if not self._region_configured: # User did not opt-in to ESTS-R
733+
def _get_regional_authority(self, central_authority) -> Optional[Authority]:
734+
if self._region_configured is False: # User opts out of ESTS-R
729735
return None # Short circuit to completely bypass region detection
736+
if self._region_configured is None: # User did not make an ESTS-R choice
737+
self._region_configured = os.getenv("MSAL_FORCE_REGION") or None
730738
self._region_detected = self._region_detected or _detect_region(
731739
self.http_client if self._region_configured is not None else None)
732740
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)