5
5
6
6
from datetime import datetime
7
7
import json
8
+ import logging
8
9
import os
9
10
import re
10
11
import shutil
19
20
from .._internal import resolve_tenant , within_dac , validate_tenant_id , validate_scope
20
21
from .._internal .decorators import log_get_token
21
22
23
+
24
+ _LOGGER = logging .getLogger (__name__ )
25
+
22
26
CLI_NOT_FOUND = (
23
27
"Azure Developer CLI could not be found. "
24
28
"Please visit https://aka.ms/azure-dev for installation instructions and then,"
25
29
"once installed, authenticate to your Azure account using 'azd auth login'."
26
30
)
27
- COMMAND_LINE = "azd auth token --output json --scope {}"
31
+ COMMAND_LINE = [ " auth" , " token" , " --output" , " json" ]
28
32
EXECUTABLE_NAME = "azd"
29
33
NOT_LOGGED_IN = "Please run 'azd auth login' from a command prompt to authenticate before using this credential."
30
34
@@ -160,17 +164,18 @@ def _get_token_base(
160
164
for scope in scopes :
161
165
validate_scope (scope )
162
166
163
- commandString = " --scope " .join (scopes )
164
- command = COMMAND_LINE .format (commandString )
167
+ command_args = COMMAND_LINE .copy ()
168
+ for scope in scopes :
169
+ command_args += ["--scope" , scope ]
165
170
tenant = resolve_tenant (
166
171
default_tenant = self .tenant_id ,
167
172
tenant_id = tenant_id ,
168
173
additionally_allowed_tenants = self ._additionally_allowed_tenants ,
169
174
** kwargs ,
170
175
)
171
176
if tenant :
172
- command += " --tenant-id " + tenant
173
- output = _run_command (command , self ._process_timeout )
177
+ command_args += [ " --tenant-id" , tenant ]
178
+ output = _run_command (command_args , self ._process_timeout )
174
179
175
180
token = parse_token (output )
176
181
if not token :
@@ -236,15 +241,13 @@ def sanitize_output(output: str) -> str:
236
241
return re .sub (r"\"token\": \"(.*?)(\"|$)" , "****" , output )
237
242
238
243
239
- def _run_command (command : str , timeout : int ) -> str :
244
+ def _run_command (command_args : List [ str ] , timeout : int ) -> str :
240
245
# Ensure executable exists in PATH first. This avoids a subprocess call that would fail anyway.
241
- if shutil .which (EXECUTABLE_NAME ) is None :
246
+ azd_path = shutil .which (EXECUTABLE_NAME )
247
+ if not azd_path :
242
248
raise CredentialUnavailableError (message = CLI_NOT_FOUND )
243
249
244
- if sys .platform .startswith ("win" ):
245
- args = ["cmd" , "/c" , command ]
246
- else :
247
- args = ["/bin/sh" , "-c" , command ]
250
+ args = [azd_path ] + command_args
248
251
try :
249
252
working_directory = get_safe_working_dir ()
250
253
@@ -257,6 +260,7 @@ def _run_command(command: str, timeout: int) -> str:
257
260
"timeout" : timeout ,
258
261
}
259
262
263
+ _LOGGER .debug ("Executing subprocess with the following arguments %s" , args )
260
264
return subprocess .check_output (args , ** kwargs )
261
265
except subprocess .CalledProcessError as ex :
262
266
# non-zero return from shell
0 commit comments