Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions doc/install_troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ Before posting an issue, please review our list of [common issues](https://githu

These are issues we have closed because we cannot address them within the CLI due to platform or language limitations.

Opting in to broker authentication on WSL
-----------------------------------------

Azure CLI on Windows Subsystem for Linux (WSL) does not use a broker by default. The Linux package does not bundle the optional Microsoft Authentication Library (MSAL) broker runtime.

1. Run `az --version` inside WSL and find the **Python location** and **msal** version in its output. Install the broker extra for that MSAL version using that exact Python executable, not the Windows Python or an unrelated WSL `pip`. For example, with MSAL 1.39.0:

```bash
/path/to/azure-cli/python -m pip install 'msal[broker]==1.39.0'
```

The interpreter needs `pip`, permission to install packages, and a compatible runtime wheel. If your CLI package's environment cannot be modified, use a CLI installation in a writable Python environment with `pip`; installing the extra into a different interpreter will not enable broker support for your current CLI.

2. Make sure a Secret Service keyring is available and unlocked in the WSL session running Azure CLI. See the [MSAL WSL setup guide](https://learn.microsoft.com/en-us/entra/msal/python/advanced/linux-broker-py-wsl) for supported WSL versions and keyring setup.

3. Enable the broker and sign in:

```bash
az config set core.enable_broker_on_wsl=true
az login
```

When MSAL enables the WSL broker, `az login` can open its sign-in prompt without a Linux browser. `az login --use-device-code` still explicitly selects device-code sign-in, but does not disable broker initialization.

If MSAL reports that the broker runtime is missing, verify that you installed the extra into the CLI's Python environment. If the runtime is installed but reports a missing shared library, install the package that provides it. On Ubuntu 24.04, `libwebkit2gtk-4.1-0` provides `libwebkit2gtk-4.1.so.0` and `libubsan1` provides `libubsan.so.1`. These libraries let the runtime load; the sign-in prompt still uses the Windows broker, not a Linux browser.

MSAL does not automatically fall back when the runtime is missing. To opt out, run `az config set core.enable_broker_on_wsl=false` and unset `AZURE_CORE_ENABLE_BROKER_ON_WSL` if it is set.


lsb_release does not return the correct base distribution version
-----------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ def login(self,

user_identity = None
if interactive:
if not use_device_code and not can_launch_browser():
logger.info('No web browser is available. Fall back to device code.')
use_device_code = True

if not use_device_code and is_github_codespaces():
logger.info('GitHub Codespaces is detected. Fall back to device code.')
use_device_code = True

if not use_device_code and not can_launch_browser() and not identity.is_wsl_broker_enabled():
logger.info('No web browser or WSL broker is available. Fall back to device code.')
use_device_code = True

if use_device_code:
user_identity = identity.login_with_device_code(scopes=scopes, claims_challenge=claims_challenge)
else:
Expand Down Expand Up @@ -977,6 +977,8 @@ def _create_identity_instance(cli_ctx, authority, tenant_id=None, client_id=None
enable_broker_on_windows = cli_ctx.config.getboolean('core', 'enable_broker_on_windows', fallback=True)
# On macOS, broker authentication is opt-in. Use core.enable_broker_on_mac=true to enable it.
enable_broker_on_mac = cli_ctx.config.getboolean('core', 'enable_broker_on_mac', fallback=False)
# On WSL, broker authentication is opt-in and requires the optional MSAL broker runtime.
enable_broker_on_wsl = cli_ctx.config.getboolean('core', 'enable_broker_on_wsl', fallback=False)
from .telemetry import set_broker_info
set_broker_info(enable_broker_on_windows, enable_broker_on_mac)

Expand All @@ -988,4 +990,5 @@ def _create_identity_instance(cli_ctx, authority, tenant_id=None, client_id=None
use_msal_http_cache=use_msal_http_cache,
enable_broker_on_windows=enable_broker_on_windows,
enable_broker_on_mac=enable_broker_on_mac,
enable_broker_on_wsl=enable_broker_on_wsl,
instance_discovery=instance_discovery)
14 changes: 12 additions & 2 deletions src/azure-cli-core/azure/cli/core/auth/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class Identity: # pylint: disable=too-many-instance-attributes
_service_principal_store_instance = None

def __init__(self, authority, tenant_id=None, client_id=None, encrypt=False, use_msal_http_cache=True,
enable_broker_on_windows=None, enable_broker_on_mac=None, instance_discovery=None):
enable_broker_on_windows=None, enable_broker_on_mac=None, instance_discovery=None,
enable_broker_on_wsl=None):
"""
:param authority: Authentication authority endpoint. For example,
- AAD: https://login.microsoftonline.com
Expand All @@ -75,6 +76,7 @@ def __init__(self, authority, tenant_id=None, client_id=None, encrypt=False, use
self._use_msal_http_cache = use_msal_http_cache
self._enable_broker_on_windows = enable_broker_on_windows
self._enable_broker_on_mac = enable_broker_on_mac
self._enable_broker_on_wsl = enable_broker_on_wsl
self._instance_discovery = instance_discovery

# Build the authority in MSAL style
Expand Down Expand Up @@ -112,10 +114,10 @@ def _msal_app_kwargs(self):
@property
def _msal_public_app_kwargs(self):
"""kwargs for creating PublicClientApplication."""
# enable_broker_on_windows and enable_broker_on_mac can only be used on PublicClientApplication.
return {**self._msal_app_kwargs,
"enable_broker_on_windows": self._enable_broker_on_windows,
"enable_broker_on_mac": self._enable_broker_on_mac,
"enable_broker_on_wsl": self._enable_broker_on_wsl,
"enable_pii_log": True}

@property
Expand All @@ -127,6 +129,14 @@ def _msal_app(self):
self._msal_app_instance = PublicClientApplication(self.client_id, **self._msal_public_app_kwargs)
return self._msal_app_instance

def is_wsl_broker_enabled(self):
from azure.cli.core.util import is_wsl

# MSAL exposes no public broker-state API. Check its decision after initialization,
# since it can turn off broker use even when the CLI setting is enabled.
# pylint: disable-next=protected-access
return bool(self._enable_broker_on_wsl and is_wsl() and self._msal_app._enable_broker)

def _load_msal_token_cache(self):
# Store for user token persistence
cache = load_persisted_token_cache(self._token_cache_file, self._encrypt)
Expand Down
58 changes: 56 additions & 2 deletions src/azure-cli-core/azure/cli/core/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import base64
import json
import datetime
import os
import unittest
from copy import deepcopy
from unittest import mock
Expand Down Expand Up @@ -373,9 +374,11 @@ def test_login_fallback_to_device_code_no_browser(self, can_launch_browser_mock,

storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock)
subs = profile.login(True, None, None, False, None, use_device_code=True, allow_no_subscriptions=False)
with mock.patch.dict(os.environ, {'CODESPACES': 'false', 'AZURE_CORE_ENABLE_BROKER_ON_WSL': 'false'}):
subs = profile.login(True, None, None, False, None, allow_no_subscriptions=False)

# assert
can_launch_browser_mock.assert_called_once()
login_with_device_code_mock.assert_called_once()
self.assertEqual(self.subscription1_with_tenant_info_output, subs)

Expand All @@ -399,12 +402,51 @@ def test_login_fallback_to_device_code_github_codespaces(self, can_launch_browse

storage_mock = {'subscriptions': None}
profile = Profile(cli_ctx=cli, storage=storage_mock)
subs = profile.login(True, None, None, False, None, use_device_code=True, allow_no_subscriptions=False)
subs = profile.login(True, None, None, False, None, allow_no_subscriptions=False)

# assert
is_github_codespaces_mock.assert_called_once()
login_with_device_code_mock.assert_called_once()
self.assertEqual(self.subscription1_with_tenant_info_output, subs)

@mock.patch('azure.cli.core._profile.is_github_codespaces', return_value=False)
@mock.patch('azure.cli.core._profile.can_launch_browser', return_value=False)
@mock.patch('azure.cli.core.util.is_wsl', return_value=True)
@mock.patch('azure.cli.core.auth.identity.Identity._msal_app', new_callable=mock.PropertyMock)
@mock.patch('azure.cli.core.auth.identity.Identity.get_user_credential', autospec=True)
def test_login_without_browser_on_wsl(self, _, msal_app, is_wsl, can_launch_browser, is_codespaces):
from azure.cli.core.auth.identity import Identity

cases = [
# WSL, MSAL broker enabled, explicit device code, Codespaces, uses interactive auth
(True, True, False, False, True),
(True, False, False, False, False),
(True, True, True, False, False),
(False, True, False, False, False),
(True, True, False, True, False),
]
with mock.patch.dict(os.environ, {'AZURE_CORE_ENABLE_BROKER_ON_WSL': 'true'}):
for wsl, broker_enabled, explicit_device_code, codespaces, interactive in cases:
with self.subTest(wsl=wsl, broker_enabled=broker_enabled, device_code=explicit_device_code,
codespaces=codespaces):
msal_app.reset_mock()
can_launch_browser.reset_mock()
msal_app.return_value._enable_broker = broker_enabled
is_wsl.return_value = wsl
is_codespaces.return_value = codespaces
profile = Profile(cli_ctx=DummyCli(), storage={'subscriptions': None})
with mock.patch.object(Identity, 'login_with_auth_code',
return_value=self.user_identity_mock) as auth_code, \
mock.patch.object(Identity, 'login_with_device_code',
return_value=self.user_identity_mock) as device_code:
profile.login(True, None, None, False, self.tenant_id,
use_device_code=explicit_device_code, skip_subscription_discovery=True)

self.assertEqual(auth_code.call_count, int(interactive))
self.assertEqual(device_code.call_count, int(not interactive))
self.assertEqual(msal_app.call_count, int(wsl and not explicit_device_code and not codespaces))
self.assertEqual(can_launch_browser.call_count, int(not explicit_device_code and not codespaces))

@mock.patch('azure.cli.core._profile.SubscriptionFinder._create_subscription_client', autospec=True)
@mock.patch('azure.cli.core.auth.identity.Identity.get_user_credential', autospec=True)
@mock.patch('azure.cli.core.auth.identity.Identity.login_with_device_code', autospec=True)
Expand Down Expand Up @@ -1463,6 +1505,18 @@ def test_create_identity_instance_broker_on_mac_opt_in_enabled(self, identity_mo
_, kwargs = identity_mock.call_args
self.assertEqual(kwargs['enable_broker_on_mac'], True)

@mock.patch('azure.cli.core.auth.identity.Identity', autospec=True)
def test_create_identity_instance_wsl_broker_is_opt_in(self, identity_mock):
cli = DummyCli()
with mock.patch.dict(os.environ):
os.environ.pop('AZURE_CORE_ENABLE_BROKER_ON_WSL', None)
_create_identity_instance(cli, authority='https://login.microsoftonline.com')
self.assertFalse(identity_mock.call_args.kwargs['enable_broker_on_wsl'])

os.environ['AZURE_CORE_ENABLE_BROKER_ON_WSL'] = 'true'
_create_identity_instance(cli, authority='https://login.microsoftonline.com')
self.assertTrue(identity_mock.call_args.kwargs['enable_broker_on_wsl'])

@mock.patch('azure.cli.core._profile.SubscriptionFinder._create_subscription_client', autospec=True)
@mock.patch('azure.cli.core.auth.identity.Identity.get_user_credential', autospec=True)
def test_refresh_accounts_one_user_account(self, get_user_credential_mock, create_subscription_client_mock):
Expand Down
Loading