Skip to content

Commit 2dece3e

Browse files
authored
[Identity] Disable prompts on AZD Credential token commands (#42535)
This can cause timeouts in the credential. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 78b9e62 commit 2dece3e

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue where `AzureDeveloperCliCredential` would time out during token requests when `azd` prompts for user interaction. This issue commonly occurred in environments where the `AZD_DEBUG` environment variable was set, causing the Azure Developer CLI to display additional prompts that interfered with automated token acquisition. ([#42535](https://github.com/Azure/azure-sdk-for-python/pull/42535))
12+
1113
### Other Changes
1214

1315
## 1.24.0 (2025-08-07)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"Please visit https://aka.ms/azure-dev for installation instructions and then,"
2929
"once installed, authenticate to your Azure account using 'azd auth login'."
3030
)
31-
COMMAND_LINE = ["auth", "token", "--output", "json"]
31+
COMMAND_LINE = ["auth", "token", "--output", "json", "--no-prompt"]
3232
EXECUTABLE_NAME = "azd"
3333
NOT_LOGGED_IN = "Please run 'azd auth login' from a command prompt to authenticate before using this credential."
3434

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from azure.identity import AzureDeveloperCliCredential, CredentialUnavailableError
1010
from azure.identity._constants import EnvironmentVariables
11-
from azure.identity._credentials.azd_cli import CLI_NOT_FOUND, NOT_LOGGED_IN
11+
from azure.identity._credentials.azd_cli import CLI_NOT_FOUND, NOT_LOGGED_IN, COMMAND_LINE
1212
from azure.core.exceptions import ClientAuthenticationError
1313

1414
import subprocess
@@ -95,6 +95,33 @@ def test_get_token(get_token_method):
9595
assert token.expires_on == expected_expires_on
9696

9797

98+
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
99+
def test_command_list(get_token_method):
100+
"""The credential should formulate the command arg list correctly"""
101+
access_token = "access token"
102+
expected_expires_on = 1602015811
103+
successful_output = json.dumps(
104+
{
105+
"expiresOn": datetime.fromtimestamp(expected_expires_on).strftime("%Y-%m-%dT%H:%M:%SZ"),
106+
"token": access_token,
107+
"subscription": "some-guid",
108+
"tenant": "some-guid",
109+
"tokenType": "Bearer",
110+
}
111+
)
112+
113+
def fake_check_output(command_line, **_):
114+
assert command_line == ["azd"] + COMMAND_LINE + ["--scope", "scope"]
115+
return successful_output
116+
117+
with mock.patch("shutil.which", return_value="azd"):
118+
with mock.patch(CHECK_OUTPUT, fake_check_output):
119+
token = getattr(AzureDeveloperCliCredential(), get_token_method)("scope")
120+
121+
assert token.token == access_token
122+
assert token.expires_on == expected_expires_on
123+
124+
98125
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
99126
def test_cli_not_installed(get_token_method):
100127
"""The credential should raise CredentialUnavailableError when the CLI isn't installed"""

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from azure.identity import CredentialUnavailableError
1313
from azure.identity.aio import AzureDeveloperCliCredential
1414
from azure.identity._constants import EnvironmentVariables
15-
from azure.identity._credentials.azd_cli import CLI_NOT_FOUND, NOT_LOGGED_IN
15+
from azure.identity._credentials.azd_cli import CLI_NOT_FOUND, NOT_LOGGED_IN, COMMAND_LINE
1616
from azure.core.exceptions import ClientAuthenticationError
1717
import pytest
1818

@@ -125,6 +125,36 @@ async def test_get_token(get_token_method):
125125
assert token.expires_on == expected_expires_on
126126

127127

128+
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
129+
async def test_command_list(get_token_method):
130+
"""The credential should formulate the command arg list correctly"""
131+
access_token = "access token"
132+
expected_expires_on = 1602015811
133+
successful_output = json.dumps(
134+
{
135+
"expiresOn": datetime.fromtimestamp(expected_expires_on).strftime("%Y-%m-%dT%H:%M:%SZ"),
136+
"token": access_token,
137+
"subscription": "some-guid",
138+
"tenant": "some-guid",
139+
"tokenType": "Bearer",
140+
}
141+
)
142+
143+
def mock_exec_impl(*args, **_):
144+
# args[0] is the executable, args[1:] are the command line arguments
145+
command_line = list(args)
146+
assert command_line == ["azd"] + COMMAND_LINE + ["--scope", "scope"]
147+
return mock_exec(successful_output)()
148+
149+
with mock.patch("shutil.which", return_value="azd"):
150+
with mock.patch(SUBPROCESS_EXEC, mock_exec_impl):
151+
credential = AzureDeveloperCliCredential()
152+
token = await getattr(credential, get_token_method)("scope")
153+
154+
assert token.token == access_token
155+
assert token.expires_on == expected_expires_on
156+
157+
128158
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
129159
async def test_cli_not_installed(get_token_method):
130160
"""The credential should raise CredentialUnavailableError when the CLI isn't installed"""

0 commit comments

Comments
 (0)