💫 Toolkit to help you get started with SDD or any other process!
fix(auth): resolve az via shutil.which so azure-cli token works on Windows (#3709)
Ali jawwad596a31ee0f1e
diff --git a/src/specify_cli/authentication/azure_devops.py b/src/specify_cli/authentication/azure_devops.pyindex 907f1b5e2..91578bd41 100644--- a/src/specify_cli/authentication/azure_devops.py+++ b/src/specify_cli/authentication/azure_devops.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport base64import json as _jsonimport os+import shutilimport subprocessfrom typing import TYPE_CHECKING@@ -71,9 +72,27 @@ class AzureDevOpsAuth(AuthProvider):def _acquire_via_az_cli() -> str | None:"""Run ``az account get-access-token`` and return the access token."""try:+ # Windows: ``subprocess.run`` calls ``CreateProcess``, which does+ # not consult ``PATHEXT``, so a bare ``"az"`` (installed as+ # ``az.cmd``) fails with ``WinError 2`` even after ``az login``.+ # Resolve via ``shutil.which`` (which honors ``PATHEXT``) so the+ # ``.cmd`` shim works. On POSIX this is a harmless lookup that+ # returns the same executable.+ #+ # Require an ABSOLUTE result: on Windows ``shutil.which`` prepends+ # the current directory to the search path (unless+ # ``NoDefaultCurrentDirectoryInExePath`` is set), so a stray+ # ``.\az.cmd`` in the working directory would otherwise be resolved+ # ahead of the real Azure CLI and run for a credential operation. A+ # legitimate install always resolves to an absolute path, so this+ # costs nothing; falling back to the bare ``"az"`` preserves the+ # prior behavior (and the existing OSError path) when ``az`` is+ # absent.+ resolved = shutil.which("az")+ az = resolved if resolved and os.path.isabs(resolved) else "az"result = subprocess.run( # noqa: S603, S607[- "az",+ az,"account","get-access-token","--resource",diff --git a/tests/test_authentication.py b/tests/test_authentication.pyindex 9edbd9860..523b0c4f3 100644--- a/tests/test_authentication.py+++ b/tests/test_authentication.py@@ -535,6 +535,57 @@ class TestAzureDevOpsAuth:with patch("specify_cli.authentication.azure_devops.subprocess.run", return_value=result):assert AzureDevOpsAuth().resolve_token(entry) is None+ def test_resolve_token_azure_cli_resolves_executable(self):+ """The az executable is resolved via shutil.which before invocation, so+ the .cmd/.bat shim on Windows (CreateProcess ignores PATHEXT) is used."""+ from unittest.mock import patch, MagicMock+ entry = AuthConfigEntry(+ hosts=("dev.azure.com",), provider="azure-devops", auth="azure-cli",+ )+ # Build the absolute path with the HOST's rules: the production code+ # calls os.path.isabs(), so a hardcoded Windows path would read as+ # RELATIVE on POSIX runners and silently exercise the fallback branch+ # instead of the one under test.+ resolved_path = os.path.join(os.path.abspath(os.sep), "opt", "az", "az.CMD")+ assert os.path.isabs(resolved_path)+ result = MagicMock()+ result.returncode = 0+ result.stdout = '{"accessToken": "tok"}'+ with patch(+ "specify_cli.authentication.azure_devops.shutil.which",+ return_value=resolved_path,+ ), patch(+ "specify_cli.authentication.azure_devops.subprocess.run",+ return_value=result,+ ) as run:+ assert AzureDevOpsAuth().resolve_token(entry) == "tok"+ argv = run.call_args.args[0]+ assert argv[0] == resolved_path+ assert argv[1:4] == ["account", "get-access-token", "--resource"]++ @pytest.mark.parametrize("which_result", [None, r".\az.CMD", "az.cmd", "./az"])+ def test_resolve_token_azure_cli_falls_back_to_bare_az(self, which_result):+ """Fall back to the bare "az" when shutil.which finds nothing OR returns a+ NON-ABSOLUTE path. On Windows shutil.which searches the current directory+ first, so a stray .\\az.cmd must never be executed for a credential+ operation; the bare name also preserves the not-installed OSError path."""+ from unittest.mock import patch, MagicMock+ entry = AuthConfigEntry(+ hosts=("dev.azure.com",), provider="azure-devops", auth="azure-cli",+ )+ result = MagicMock()+ result.returncode = 0+ result.stdout = '{"accessToken": "tok"}'+ with patch(+ "specify_cli.authentication.azure_devops.shutil.which",+ return_value=which_result,+ ), patch(+ "specify_cli.authentication.azure_devops.subprocess.run",+ return_value=result,+ ) as run:+ assert AzureDevOpsAuth().resolve_token(entry) == "tok"+ assert run.call_args.args[0][0] == "az", which_result+def test_resolve_token_azure_cli_not_installed_returns_none(self):"""azure-cli returns None when az is not installed."""from unittest.mock import patch