Skip to content

Commit 65c2e27

Browse files
committed
Add logging and tests
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent e24dd7e commit 65c2e27

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

sdk/identity/azure-identity/azure/identity/_credentials/default.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ def __init__(self, **kwargs: Any) -> None: # pylint: disable=too-many-statement
212212
broker_credential_args["client_id"] = interactive_browser_client_id
213213

214214
credentials.append(broker_credential_class(**broker_credential_args))
215+
else:
216+
_LOGGER.info(
217+
"InteractiveBrowserBrokerCredential unavailable. Brokered authentication in DefaultAzureCredential is "
218+
"only available on Windows and requires the 'azure-identity-broker' package to be installed."
219+
)
215220

216221
within_dac.set(False)
217222
super(DefaultAzureCredential, self).__init__(*credentials)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
../../core/azure-core
2+
../azure-identity-broker
23
aiohttp>=3.0
34
typing_extensions>=3.7.2
45
-e ../../../tools/azure-sdk-tools

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55
import os
6+
import sys
67

78
from azure.core.credentials import AccessToken, AccessTokenInfo
89
from azure.identity import (
@@ -432,3 +433,22 @@ def test_validate_cloud_shell_credential_in_dac():
432433
DefaultAzureCredential(identity_config={"client_id": "foo"})
433434
DefaultAzureCredential(identity_config={"object_id": "foo"})
434435
DefaultAzureCredential(identity_config={"resource_id": "foo"})
436+
437+
438+
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="tests Windows-specific behavior")
439+
def test_broker_credential():
440+
"""Test that DefaultAzureCredential uses the broker credential when available"""
441+
with patch("azure.identity.broker.InteractiveBrowserBrokerCredential") as mock_credential:
442+
DefaultAzureCredential()
443+
assert mock_credential.call_count == 1, "InteractiveBrowserBrokerCredential should be instantiated once"
444+
445+
446+
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="tests Windows-specific behavior")
447+
def test_broker_credential_requirements_not_installed():
448+
"""Test that DefaultAzureCredential does not use the broker credential when requirements are not installed"""
449+
450+
with patch.dict("sys.modules", {"azure.identity.broker": None}):
451+
with patch("azure.identity.broker.InteractiveBrowserBrokerCredential") as mock_credential:
452+
DefaultAzureCredential()
453+
454+
assert mock_credential.call_count == 0, "InteractiveBrowserBrokerCredential should not be instantiated"

0 commit comments

Comments
 (0)