@@ -81,98 +81,26 @@ def get_settings_value(self, key):
81
81
return key_value
82
82
83
83
def get_credential (self , client_class , ** kwargs ):
84
+ if _is_autorest_v3 (client_class ):
85
+ return get_credential (** kwargs )
84
86
tenant_id = os .environ .get ("AZURE_TENANT_ID" , getattr (os .environ , "TENANT_ID" , None ))
85
87
client_id = os .environ .get ("AZURE_CLIENT_ID" , getattr (os .environ , "CLIENT_ID" , None ))
86
88
secret = os .environ .get ("AZURE_CLIENT_SECRET" , getattr (os .environ , "CLIENT_SECRET" , None ))
87
89
88
- use_pwsh = os .environ .get ("AZURE_TEST_USE_PWSH_AUTH" , "false" )
89
- use_cli = os .environ .get ("AZURE_TEST_USE_CLI_AUTH" , "false" )
90
- use_vscode = os .environ .get ("AZURE_TEST_USE_VSCODE_AUTH" , "false" )
91
- use_azd = os .environ .get ("AZURE_TEST_USE_AZD_AUTH" , "false" )
92
- is_async = kwargs .pop ("is_async" , False )
93
-
94
90
# Return live credentials only in live mode
95
- if self .is_live :
96
- # User-based authentication through Azure PowerShell, if requested
97
- if use_pwsh .lower () == "true" :
98
- _LOGGER .info (
99
- "Environment variable AZURE_TEST_USE_PWSH_AUTH set to 'true'. Using AzurePowerShellCredential."
100
- )
101
- from azure .identity import AzurePowerShellCredential
102
-
103
- if is_async :
104
- from azure .identity .aio import AzurePowerShellCredential
105
- return AzurePowerShellCredential ()
106
- # User-based authentication through Azure CLI (az), if requested
107
- if use_cli .lower () == "true" :
108
- _LOGGER .info ("Environment variable AZURE_TEST_USE_CLI_AUTH set to 'true'. Using AzureCliCredential." )
109
- from azure .identity import AzureCliCredential
110
-
111
- if is_async :
112
- from azure .identity .aio import AzureCliCredential
113
- return AzureCliCredential ()
114
- # User-based authentication through Visual Studio Code, if requested
115
- if use_vscode .lower () == "true" :
116
- _LOGGER .info (
117
- "Environment variable AZURE_TEST_USE_VSCODE_AUTH set to 'true'. Using VisualStudioCodeCredential."
118
- )
119
- from azure .identity import VisualStudioCodeCredential
120
-
121
- if is_async :
122
- from azure .identity .aio import VisualStudioCodeCredential
123
- return VisualStudioCodeCredential ()
124
- # User-based authentication through Azure Developer CLI (azd), if requested
125
- if use_azd .lower () == "true" :
126
- _LOGGER .info (
127
- "Environment variable AZURE_TEST_USE_AZD_AUTH set to 'true'. Using AzureDeveloperCliCredential."
128
- )
129
- from azure .identity import AzureDeveloperCliCredential
130
-
131
- if is_async :
132
- from azure .identity .aio import AzureDeveloperCliCredential
133
- return AzureDeveloperCliCredential ()
134
-
91
+ if self .is_live :
135
92
# Service principal authentication
136
93
if tenant_id and client_id and secret :
137
- # Check for track 2 client
138
- if _is_autorest_v3 (client_class ):
139
- _LOGGER .info (
140
- "Service principal client ID, secret, and tenant ID detected. Using ClientSecretCredential.\n "
141
- "For user-based auth, set AZURE_TEST_USE_PWSH_AUTH or AZURE_TEST_USE_CLI_AUTH to 'true'."
142
- )
143
- from azure .identity import ClientSecretCredential
144
-
145
- if is_async :
146
- from azure .identity .aio import ClientSecretCredential
147
- return ClientSecretCredential (tenant_id = tenant_id , client_id = client_id , client_secret = secret )
148
- else :
149
- # Create msrestazure class
150
- from msrestazure .azure_active_directory import (
151
- ServicePrincipalCredentials ,
152
- )
153
-
154
- return ServicePrincipalCredentials (tenant = tenant_id , client_id = client_id , secret = secret )
155
-
156
- # Use DefaultAzureCredential for live tests
157
- from azure .identity import DefaultAzureCredential
158
- if is_async :
159
- from azure .identity .aio import DefaultAzureCredential
160
- return DefaultAzureCredential (exclude_managed_identity_credential = True )
94
+ # Create msrestazure class
95
+ from msrestazure .azure_active_directory import (
96
+ ServicePrincipalCredentials ,
97
+ )
98
+
99
+ return ServicePrincipalCredentials (tenant = tenant_id , client_id = client_id , secret = secret )
161
100
162
101
# For playback tests, return credentials that will accept playback `get_token` calls
163
102
else :
164
- if _is_autorest_v3 (client_class ):
165
- if is_async :
166
- if self .is_live :
167
- raise ValueError (
168
- "Async live doesn't support mgmt_setting_real, please set AZURE_TENANT_ID, "
169
- "AZURE_CLIENT_ID, AZURE_CLIENT_SECRET"
170
- )
171
- return AsyncFakeCredential ()
172
- else :
173
- return self .settings .get_azure_core_credentials ()
174
- else :
175
- return self .settings .get_credentials ()
103
+ return self .settings .get_credentials ()
176
104
177
105
def create_client_from_credential (self , client_class , credential , ** kwargs ):
178
106
@@ -261,3 +189,101 @@ def generate_sas(self, *args, **kwargs):
261
189
sas_func_pos_args = args [1 :]
262
190
token = sas_func (* sas_func_pos_args , ** kwargs )
263
191
return token
192
+
193
+ def get_credential (** kwargs ):
194
+ tenant_id = os .environ .get ("AZURE_TENANT_ID" , getattr (os .environ , "TENANT_ID" , None ))
195
+ client_id = os .environ .get ("AZURE_CLIENT_ID" , getattr (os .environ , "CLIENT_ID" , None ))
196
+ secret = os .environ .get ("AZURE_CLIENT_SECRET" , getattr (os .environ , "CLIENT_SECRET" , None ))
197
+
198
+ use_pwsh = os .environ .get ("AZURE_TEST_USE_PWSH_AUTH" , "false" )
199
+ use_cli = os .environ .get ("AZURE_TEST_USE_CLI_AUTH" , "false" )
200
+ use_vscode = os .environ .get ("AZURE_TEST_USE_VSCODE_AUTH" , "false" )
201
+ use_azd = os .environ .get ("AZURE_TEST_USE_AZD_AUTH" , "false" )
202
+ is_async = kwargs .pop ("is_async" , False )
203
+
204
+ # Return live credentials only in live mode
205
+ if is_live ():
206
+ # User-based authentication through Azure PowerShell, if requested
207
+ if use_pwsh .lower () == "true" :
208
+ _LOGGER .info (
209
+ "Environment variable AZURE_TEST_USE_PWSH_AUTH set to 'true'. Using AzurePowerShellCredential."
210
+ )
211
+ from azure .identity import AzurePowerShellCredential
212
+
213
+ if is_async :
214
+ from azure .identity .aio import AzurePowerShellCredential
215
+ return AzurePowerShellCredential (** kwargs )
216
+ # User-based authentication through Azure CLI (az), if requested
217
+ if use_cli .lower () == "true" :
218
+ _LOGGER .info ("Environment variable AZURE_TEST_USE_CLI_AUTH set to 'true'. Using AzureCliCredential." )
219
+ from azure .identity import AzureCliCredential
220
+
221
+ if is_async :
222
+ from azure .identity .aio import AzureCliCredential
223
+ return AzureCliCredential (** kwargs )
224
+ # User-based authentication through Visual Studio Code, if requested
225
+ if use_vscode .lower () == "true" :
226
+ _LOGGER .info (
227
+ "Environment variable AZURE_TEST_USE_VSCODE_AUTH set to 'true'. Using VisualStudioCodeCredential."
228
+ )
229
+ from azure .identity import VisualStudioCodeCredential
230
+
231
+ if is_async :
232
+ from azure .identity .aio import VisualStudioCodeCredential
233
+ return VisualStudioCodeCredential (** kwargs )
234
+ # User-based authentication through Azure Developer CLI (azd), if requested
235
+ if use_azd .lower () == "true" :
236
+ _LOGGER .info (
237
+ "Environment variable AZURE_TEST_USE_AZD_AUTH set to 'true'. Using AzureDeveloperCliCredential."
238
+ )
239
+ from azure .identity import AzureDeveloperCliCredential
240
+
241
+ if is_async :
242
+ from azure .identity .aio import AzureDeveloperCliCredential
243
+ return AzureDeveloperCliCredential (** kwargs )
244
+
245
+ # Service principal authentication
246
+ if tenant_id and client_id and secret :
247
+ _LOGGER .info (
248
+ "Service principal client ID, secret, and tenant ID detected. Using ClientSecretCredential.\n "
249
+ "For user-based auth, set AZURE_TEST_USE_PWSH_AUTH or AZURE_TEST_USE_CLI_AUTH to 'true'."
250
+ )
251
+ from azure .identity import ClientSecretCredential
252
+
253
+ if is_async :
254
+ from azure .identity .aio import ClientSecretCredential
255
+ return ClientSecretCredential (tenant_id = tenant_id , client_id = client_id , client_secret = secret , ** kwargs )
256
+
257
+ # If AzurePipelinesCredential is detected, use it.
258
+ service_connection_id = os .environ .get ("AZURESUBSCRIPTION_SERVICE_CONNECTION_ID" )
259
+ client_id = os .environ .get ("AZURESUBSCRIPTION_CLIENT_ID" )
260
+ tenant_id = os .environ .get ("AZURESUBSCRIPTION_TENANT_ID" )
261
+ system_access_token = os .environ .get ("SYSTEM_ACCESSTOKEN" )
262
+ if service_connection_id and client_id and tenant_id and system_access_token :
263
+ from azure .identity import AzurePipelinesCredential
264
+ if is_async :
265
+ from azure .identity .aio import AzurePipelinesCredential
266
+ return AzurePipelinesCredential (
267
+ tenant_id = tenant_id ,
268
+ client_id = client_id ,
269
+ service_connection_id = service_connection_id ,
270
+ system_access_token = system_access_token ,
271
+ ** kwargs
272
+ )
273
+ # This is for testing purposes only, to ensure that the AzurePipelinesCredential is used when available
274
+ # else:
275
+ # raise ValueError(
276
+ # "Environment variables not set for service principal authentication. "
277
+ # f"service_connection_id: {service_connection_id}, client_id: {client_id}, tenant_id: {tenant_id}, system_access_token: {system_access_token}"
278
+ # )
279
+ # Fall back to DefaultAzureCredential
280
+ from azure .identity import DefaultAzureCredential
281
+ if is_async :
282
+ from azure .identity .aio import DefaultAzureCredential
283
+ return DefaultAzureCredential (exclude_managed_identity_credential = True , ** kwargs )
284
+
285
+ # For playback tests, return credentials that will accept playback `get_token` calls
286
+ if is_async :
287
+ return AsyncFakeCredential ()
288
+ else :
289
+ return fake_settings .get_azure_core_credentials ()
0 commit comments