|
11 | 11 | import com.azure.identity.implementation.util.IdentityUtil; |
12 | 12 |
|
13 | 13 | import java.time.Duration; |
14 | | -import java.util.ArrayList; |
15 | 14 | import java.util.Arrays; |
| 15 | +import java.util.ArrayList; |
16 | 16 | import java.util.List; |
| 17 | +import java.util.Locale; |
17 | 18 | import java.util.Objects; |
18 | 19 | import java.util.concurrent.ExecutorService; |
19 | 20 |
|
@@ -272,44 +273,73 @@ private ArrayList<TokenCredential> getCredentialsChain() { |
272 | 273 | Configuration configuration = identityClientOptions.getConfiguration() == null |
273 | 274 | ? Configuration.getGlobalConfiguration().clone() |
274 | 275 | : identityClientOptions.getConfiguration(); |
275 | | - String selectedCredentials = configuration.get("AZURE_TOKEN_CREDENTIALS"); |
276 | | - boolean useProductionCredentials = false; |
277 | | - boolean useDeveloperCredentials = false; |
278 | | - if (!CoreUtils.isNullOrEmpty(selectedCredentials)) { |
279 | | - selectedCredentials = selectedCredentials.trim(); |
280 | | - if ("prod".equalsIgnoreCase(selectedCredentials)) { |
281 | | - useProductionCredentials = true; |
282 | | - } else if ("dev".equalsIgnoreCase(selectedCredentials)) { |
283 | | - useDeveloperCredentials = true; |
| 276 | + |
| 277 | + String selectedCredential = configuration.get("AZURE_TOKEN_CREDENTIALS"); |
| 278 | + ArrayList<TokenCredential> credentials = new ArrayList<>(8); |
| 279 | + |
| 280 | + if (!CoreUtils.isNullOrEmpty(selectedCredential)) { |
| 281 | + selectedCredential = selectedCredential.trim().toLowerCase(Locale.ROOT); |
| 282 | + |
| 283 | + // Use a map to associate credential names to their adders |
| 284 | + java.util.Map<String, Runnable> credentialMap = new java.util.HashMap<>(); |
| 285 | + credentialMap.put("prod", () -> addProdCredentials(credentials)); |
| 286 | + credentialMap.put("dev", () -> addDevCredentials(credentials)); |
| 287 | + credentialMap.put("environmentcredential", |
| 288 | + () -> credentials.add(new EnvironmentCredential(identityClientOptions.clone()))); |
| 289 | + credentialMap.put("workloadidentitycredential", () -> credentials.add(getWorkloadIdentityCredential())); |
| 290 | + credentialMap.put("managedidentitycredential", |
| 291 | + () -> credentials.add(new ManagedIdentityCredential(managedIdentityClientId, managedIdentityResourceId, |
| 292 | + null, identityClientOptions.clone()))); |
| 293 | + credentialMap.put("intellijcredential", |
| 294 | + () -> credentials.add(new IntelliJCredential(tenantId, identityClientOptions.clone()))); |
| 295 | + credentialMap.put("azureclicredential", |
| 296 | + () -> credentials.add(new AzureCliCredential(tenantId, identityClientOptions.clone()))); |
| 297 | + credentialMap.put("azurepowershellcredential", |
| 298 | + () -> credentials.add(new AzurePowerShellCredential(tenantId, identityClientOptions.clone()))); |
| 299 | + credentialMap.put("azuredeveloperclicredential", |
| 300 | + () -> credentials.add(new AzureDeveloperCliCredential(tenantId, identityClientOptions.clone()))); |
| 301 | + credentialMap.put("visualstudiocodecredential", |
| 302 | + () -> credentials.add(new VisualStudioCodeCredential(tenantId, identityClientOptions.clone()))); |
| 303 | + |
| 304 | + Runnable adder = credentialMap.get(selectedCredential); |
| 305 | + if (adder != null) { |
| 306 | + adder.run(); |
| 307 | + return credentials; |
284 | 308 | } else { |
285 | | - throw LOGGER.logExceptionAsError(new IllegalArgumentException( |
286 | | - "Invalid value for AZURE_TOKEN_CREDENTIALS. Valid values are 'prod' or 'dev'.")); |
| 309 | + throw LOGGER |
| 310 | + .logExceptionAsError(new IllegalArgumentException("Invalid value for AZURE_TOKEN_CREDENTIALS: '" |
| 311 | + + selectedCredential + "'. " + "Valid values are: 'prod', 'dev', or one of " |
| 312 | + + "[EnvironmentCredential, WorkloadIdentityCredential, ManagedIdentityCredential, " |
| 313 | + + "IntelliJCredential, AzureCliCredential, AzurePowerShellCredential, " |
| 314 | + + "AzureDeveloperCliCredential, VisualStudioCodeCredential] (case-insensitive). " |
| 315 | + + "To mitigate this issue, please refer to the troubleshooting guidelines here at " |
| 316 | + + "https://aka.ms/azure-identity-java-default-azure-credential-troubleshoot")); |
287 | 317 | } |
288 | 318 | } |
289 | | - if (!useProductionCredentials && !useDeveloperCredentials) { |
290 | | - useProductionCredentials = true; |
291 | | - useDeveloperCredentials = true; |
292 | | - } |
293 | 319 |
|
294 | | - ArrayList<TokenCredential> output = new ArrayList<TokenCredential>(7); |
295 | | - if (useProductionCredentials) { |
296 | | - output.add(new EnvironmentCredential(identityClientOptions.clone())); |
297 | | - output.add(getWorkloadIdentityCredential()); |
298 | | - output.add(new ManagedIdentityCredential(managedIdentityClientId, managedIdentityResourceId, null, |
299 | | - identityClientOptions.clone())); |
300 | | - } |
| 320 | + // Default case: full chain (prod + dev) |
| 321 | + addProdCredentials(credentials); |
| 322 | + addDevCredentials(credentials); |
| 323 | + return credentials; |
| 324 | + } |
301 | 325 |
|
302 | | - if (useDeveloperCredentials) { |
303 | | - output.add(new IntelliJCredential(tenantId, identityClientOptions.clone())); |
304 | | - output.add(new AzureCliCredential(tenantId, identityClientOptions.clone())); |
305 | | - output.add(new AzurePowerShellCredential(tenantId, identityClientOptions.clone())); |
306 | | - output.add(new AzureDeveloperCliCredential(tenantId, identityClientOptions.clone())); |
307 | | - if (IdentityUtil.isVsCodeBrokerAuthAvailable()) { |
308 | | - output.add(new VisualStudioCodeCredential(tenantId, identityClientOptions.clone())); |
309 | | - } |
310 | | - } |
| 326 | + // Helper to add prod credentials |
| 327 | + private void addProdCredentials(List<TokenCredential> credentials) { |
| 328 | + credentials.add(new EnvironmentCredential(identityClientOptions.clone())); |
| 329 | + credentials.add(getWorkloadIdentityCredential()); |
| 330 | + credentials.add(new ManagedIdentityCredential(managedIdentityClientId, managedIdentityResourceId, null, |
| 331 | + identityClientOptions.clone())); |
| 332 | + } |
311 | 333 |
|
312 | | - return output; |
| 334 | + // Helper to add dev credentials |
| 335 | + private void addDevCredentials(List<TokenCredential> credentials) { |
| 336 | + credentials.add(new IntelliJCredential(tenantId, identityClientOptions.clone())); |
| 337 | + credentials.add(new AzureCliCredential(tenantId, identityClientOptions.clone())); |
| 338 | + credentials.add(new AzurePowerShellCredential(tenantId, identityClientOptions.clone())); |
| 339 | + credentials.add(new AzureDeveloperCliCredential(tenantId, identityClientOptions.clone())); |
| 340 | + if (IdentityUtil.isVsCodeBrokerAuthAvailable()) { |
| 341 | + credentials.add(new VisualStudioCodeCredential(tenantId, identityClientOptions.clone())); |
| 342 | + } |
313 | 343 | } |
314 | 344 |
|
315 | 345 | private WorkloadIdentityCredential getWorkloadIdentityCredential() { |
|
0 commit comments