Skip to content

Commit

Permalink
Fix Azure#26857 for aio.AzureCliCredential
Browse files Browse the repository at this point in the history
  • Loading branch information
micromaomao committed Oct 22, 2022
1 parent d3b59b9 commit 9858324
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ async def _run_command(command: str) -> str:
proc = await asyncio.create_subprocess_exec(
*args,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
stderr=asyncio.subprocess.PIPE,
cwd=working_directory,
env=dict(os.environ, AZURE_CORE_NO_COLOR="true")
)
stdout, _ = await asyncio.wait_for(proc.communicate(), 10)
stdout, stderr = await asyncio.wait_for(proc.communicate(), 10)
output = stdout.decode()
stderr = stderr.decode()
except OSError as ex:
# failed to execute 'cmd' or '/bin/sh'; CLI may or may not be installed
error = CredentialUnavailableError(message="Failed to execute '{}'".format(args[0]))
Expand All @@ -117,11 +118,11 @@ async def _run_command(command: str) -> str:
if proc.returncode == 0:
return output

if proc.returncode == 127 or output.startswith("'az' is not recognized"):
if proc.returncode == 127 or stderr.startswith("'az' is not recognized"):
raise CredentialUnavailableError(CLI_NOT_FOUND)

if "az login" in output or "az account set" in output:
if "az login" in stderr or "az account set" in stderr:
raise CredentialUnavailableError(message=NOT_LOGGED_IN)

message = sanitize_output(output) if output else "Failed to invoke Azure CLI"
message = sanitize_output(stderr) if stderr else "Failed to invoke Azure CLI"
raise ClientAuthenticationError(message=message)
18 changes: 9 additions & 9 deletions sdk/identity/azure-identity/tests/test_cli_credential_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ async def test_get_token():
async def test_cli_not_installed_linux():
"""The credential should raise CredentialUnavailableError when the CLI isn't installed"""

output = "/bin/sh: 1: az: not found"
with mock.patch(SUBPROCESS_EXEC, mock_exec(output, return_code=127)):
stderr = "/bin/sh: 1: az: not found"
with mock.patch(SUBPROCESS_EXEC, mock_exec(stderr=stderr, return_code=127)):
with pytest.raises(CredentialUnavailableError, match=CLI_NOT_FOUND):
credential = AzureCliCredential()
await credential.get_token("scope")
Expand All @@ -111,8 +111,8 @@ async def test_cli_not_installed_linux():
async def test_cli_not_installed_windows():
"""The credential should raise CredentialUnavailableError when the CLI isn't installed"""

output = "'az' is not recognized as an internal or external command, operable program or batch file."
with mock.patch(SUBPROCESS_EXEC, mock_exec(output, return_code=1)):
stderr = "'az' is not recognized as an internal or external command, operable program or batch file."
with mock.patch(SUBPROCESS_EXEC, mock_exec(stderr=stderr, return_code=1)):
with pytest.raises(CredentialUnavailableError, match=CLI_NOT_FOUND):
credential = AzureCliCredential()
await credential.get_token("scope")
Expand All @@ -130,8 +130,8 @@ async def test_cannot_execute_shell():
async def test_not_logged_in():
"""When the CLI isn't logged in, the credential should raise CredentialUnavailableError"""

output = "ERROR: Please run 'az login' to setup account."
with mock.patch(SUBPROCESS_EXEC, mock_exec(output, return_code=1)):
stderr = "ERROR: Please run 'az login' to setup account."
with mock.patch(SUBPROCESS_EXEC, mock_exec(stderr=stderr, return_code=1)):
with pytest.raises(CredentialUnavailableError, match=NOT_LOGGED_IN):
credential = AzureCliCredential()
await credential.get_token("scope")
Expand All @@ -140,9 +140,9 @@ async def test_not_logged_in():
async def test_unexpected_error():
"""When the CLI returns an unexpected error, the credential should raise an error containing the CLI's output"""

output = "something went wrong"
with mock.patch(SUBPROCESS_EXEC, mock_exec(output, return_code=42)):
with pytest.raises(ClientAuthenticationError, match=output):
stderr = "something went wrong"
with mock.patch(SUBPROCESS_EXEC, mock_exec(stderr=stderr, return_code=42)):
with pytest.raises(ClientAuthenticationError, match=stderr):
credential = AzureCliCredential()
await credential.get_token("scope")

Expand Down

0 comments on commit 9858324

Please sign in to comment.