Skip to content

Commit 02dd5b7

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

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

msal/application.py

Lines changed: 16 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
@@ -448,11 +449,14 @@ 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="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.
454+
1. ``azure_region=None`` - This default value means no region is configured.
455+
MSAL will use the region defined in env var ``MSAL_FORCE_REGION``.
456+
2. ``azure_region="some_region"`` - meaning the specified region is used.
457+
3. ``azure_region=True`` - meaning
458+
MSAL will try to auto-detect the region. This is not recommended.
459+
4. ``azure_region=False`` - meaning MSAL will use no region.
456460
457461
.. note::
458462
Region auto-discovery has been tested on VMs and on Azure Functions. It is unreliable.
@@ -630,7 +634,10 @@ def __init__(
630634
except ValueError: # Those are explicit authority validation errors
631635
raise
632636
except Exception: # The rest are typically connection errors
633-
if validate_authority and azure_region and not oidc_authority:
637+
if validate_authority and not oidc_authority and (
638+
azure_region # Opted in to use region
639+
or (azure_region is None and os.getenv("MSAL_FORCE_REGION")) # Will use region
640+
):
634641
# Since caller opts in to use region, here we tolerate connection
635642
# errors happened during authority validation at non-region endpoint
636643
self.authority = Authority(
@@ -724,9 +731,11 @@ def _build_telemetry_context(
724731
self._telemetry_buffer, self._telemetry_lock, api_id,
725732
correlation_id=correlation_id, refresh_reason=refresh_reason)
726733

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