Skip to content

Commit b2043fa

Browse files
authored
[Identity] Update instance discovery error message (Azure#36932)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent c78042e commit b2043fa

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

sdk/identity/azure-identity/azure/identity/_internal/msal_credentials.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(
3232
disable_instance_discovery: Optional[bool] = None,
3333
tenant_id: Optional[str] = None,
3434
enable_support_logging: Optional[bool] = None,
35-
**kwargs: Any
35+
**kwargs: Any,
3636
) -> None:
3737
self._instance_discovery = None if disable_instance_discovery is None else not disable_instance_discovery
3838
self._authority = normalize_authority(authority) if authority else get_default_authority()
@@ -104,17 +104,26 @@ def _get_app(self, **kwargs: Any) -> msal.ClientApplication:
104104
token_cache = self._initialize_cache(is_cae=bool(kwargs.get("enable_cae")))
105105

106106
if tenant_id not in client_applications_map:
107-
client_applications_map[tenant_id] = app_class(
108-
client_id=self._client_id,
109-
client_credential=self._client_credential,
110-
client_capabilities=capabilities,
111-
authority="{}/{}".format(self._authority, tenant_id),
112-
azure_region=self._regional_authority,
113-
token_cache=token_cache,
114-
http_client=self._client,
115-
instance_discovery=self._instance_discovery,
116-
enable_pii_log=self._enable_support_logging,
117-
)
107+
try:
108+
client_applications_map[tenant_id] = app_class(
109+
client_id=self._client_id,
110+
client_credential=self._client_credential,
111+
client_capabilities=capabilities,
112+
authority="{}/{}".format(self._authority, tenant_id),
113+
azure_region=self._regional_authority,
114+
token_cache=token_cache,
115+
http_client=self._client,
116+
instance_discovery=self._instance_discovery,
117+
enable_pii_log=self._enable_support_logging,
118+
)
119+
except ValueError as ex:
120+
if "invalid_instance" in str(ex):
121+
raise ValueError( # pylint: disable=raise-missing-from
122+
f"The authority provided, {self._authority}, is not well-known. If this authority is valid "
123+
"and trustworthy, you can disable this check by passing in "
124+
"'disable_instance_discovery=True' when constructing the credential."
125+
)
126+
raise
118127

119128
return client_applications_map[tenant_id]
120129

sdk/identity/azure-identity/tests/test_instance_discovery.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
import pytest
6+
57
from azure.identity._internal.msal_credentials import MsalCredential
8+
from azure.core.exceptions import ServiceRequestError
69

710

811
def test_instance_discovery():
@@ -19,3 +22,24 @@ def test_instance_discovery():
1922
)
2023
app = credential._get_app()
2124
assert app._instance_discovery
25+
26+
27+
def test_unknown_authority():
28+
credential = MsalCredential(
29+
client_id="CLIENT_ID",
30+
authority="unknown.authority",
31+
)
32+
with pytest.raises(ValueError) as ex:
33+
credential._get_app()
34+
assert "disable_instance_discovery" in str(ex)
35+
36+
credential = MsalCredential(
37+
client_id="CLIENT_ID",
38+
authority="unknown.authority",
39+
disable_instance_discovery=True,
40+
)
41+
42+
with pytest.raises(ServiceRequestError):
43+
# Instance discovery is disabled, so the credential should not attempt to validate the authority, and instead
44+
# attempt to use the authority as given. This is fail since unknown.authority is not resolvable.
45+
credential._get_app()

0 commit comments

Comments
 (0)