|
16 | 16 | SpeechSynthesizer,
|
17 | 17 | )
|
18 | 18 | from azure.core.exceptions import ResourceNotFoundError
|
19 |
| -from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider |
| 19 | +from azure.identity.aio import ( |
| 20 | + AzureDeveloperCliCredential, |
| 21 | + ManagedIdentityCredential, |
| 22 | + get_bearer_token_provider, |
| 23 | +) |
20 | 24 | from azure.monitor.opentelemetry import configure_azure_monitor
|
21 | 25 | from azure.search.documents.aio import SearchClient
|
22 | 26 | from azure.search.documents.indexes.aio import SearchIndexClient
|
@@ -436,11 +440,21 @@ async def setup_clients():
|
436 | 440 | USE_SPEECH_OUTPUT_BROWSER = os.getenv("USE_SPEECH_OUTPUT_BROWSER", "").lower() == "true"
|
437 | 441 | USE_SPEECH_OUTPUT_AZURE = os.getenv("USE_SPEECH_OUTPUT_AZURE", "").lower() == "true"
|
438 | 442 |
|
439 |
| - # Use the current user identity to authenticate with Azure OpenAI, AI Search and Blob Storage (no secrets needed, |
440 |
| - # just use 'az login' locally, and managed identity when deployed on Azure). If you need to use keys, use separate AzureKeyCredential instances with the |
441 |
| - # keys for each service |
442 |
| - # If you encounter a blocking error during a DefaultAzureCredential resolution, you can exclude the problematic credential by using a parameter (ex. exclude_shared_token_cache_credential=True) |
443 |
| - azure_credential = DefaultAzureCredential(exclude_shared_token_cache_credential=True) |
| 443 | + # Use the current user identity for keyless authentication to Azure services. |
| 444 | + # This assumes you use 'azd auth login' locally, and managed identity when deployed on Azure. |
| 445 | + # The managed identity is setup in the infra/ folder. |
| 446 | + azure_credential: Union[AzureDeveloperCliCredential, ManagedIdentityCredential] |
| 447 | + if os.getenv("WEBSITE_HOSTNAME"): # Environment variable set on Azure Web Apps |
| 448 | + current_app.logger.info("Setting up Azure credential using ManagedIdentityCredential") |
| 449 | + azure_credential = ManagedIdentityCredential() |
| 450 | + elif AZURE_TENANT_ID: |
| 451 | + current_app.logger.info( |
| 452 | + "Setting up Azure credential using AzureDeveloperCliCredential with tenant_id %s", AZURE_TENANT_ID |
| 453 | + ) |
| 454 | + azure_credential = AzureDeveloperCliCredential(tenant_id=AZURE_TENANT_ID, process_timeout=60) |
| 455 | + else: |
| 456 | + current_app.logger.info("Setting up Azure credential using AzureDeveloperCliCredential for home tenant") |
| 457 | + azure_credential = AzureDeveloperCliCredential(process_timeout=60) |
444 | 458 |
|
445 | 459 | # Set up clients for AI Search and Storage
|
446 | 460 | search_client = SearchClient(
|
|
0 commit comments