Skip to content

Commit 44cccd1

Browse files
fix: Use DefaultAzureCredential in dev, Managed Identity in prod
2 parents 92054d5 + de999cd commit 44cccd1

37 files changed

+376
-146
lines changed

docs/workshop/docs/workshop/Challenge-3-and-4/knowledge_mining_api.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"import pyodbc\n",
2929
"from dotenv import load_dotenv\n",
3030
"\n",
31-
"from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider\n",
31+
"from azure.identity.aio import AzureCliCredential, get_bearer_token_provider\n",
3232
"from azure.ai.agents.models import TruncationObject\n",
3333
"\n",
3434
"from semantic_kernel.functions.kernel_function_decorator import kernel_function\n",
@@ -61,7 +61,7 @@
6161
" mid_id = os.getenv(\"SQLDB_USER_MID\")\n",
6262
"\n",
6363
" try:\n",
64-
" async with DefaultAzureCredential() as credential:\n",
64+
" async with AzureCliCredential() as credential:\n",
6565
" token = await credential.get_token(\"https://database.windows.net/.default\")\n",
6666
" token_bytes = token.token.encode(\"utf-16-LE\")\n",
6767
" token_struct = struct.pack(\n",
@@ -128,7 +128,7 @@
128128
"\n",
129129
" try:\n",
130130
" token_provider = get_bearer_token_provider(\n",
131-
" DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
131+
" AzureCliCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
132132
" )\n",
133133
" token = await token_provider()\n",
134134
" client = openai.AzureOpenAI(\n",
@@ -175,7 +175,7 @@
175175
" Only return the generated SQL query. Do not return anything else.'''\n",
176176
" \n",
177177
" token_provider = get_bearer_token_provider(\n",
178-
" DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
178+
" AzureCliCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
179179
" )\n",
180180
" token = await token_provider()\n",
181181
" client = openai.AzureOpenAI(\n",
@@ -212,7 +212,7 @@
212212
" ):\n",
213213
" try:\n",
214214
" token_provider = get_bearer_token_provider(\n",
215-
" DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
215+
" AzureCliCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
216216
" )\n",
217217
" token = await token_provider()\n",
218218
" client = openai.AzureOpenAI(\n",
@@ -313,7 +313,7 @@
313313
"async def main() -> None:\n",
314314
" ai_agent_settings = AzureAIAgentSettings()\n",
315315
" async with (\n",
316-
" DefaultAzureCredential() as creds,\n",
316+
" AzureCliCredential() as creds,\n",
317317
" AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,\n",
318318
" ):\n",
319319
" AGENT_INSTRUCTIONS = '''You are a helpful assistant.\n",

docs/workshop/docs/workshop/Challenge-5/notebooks/video_chapter_generation.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@
117117
")\n",
118118
"from python.content_understanding_client import AzureContentUnderstandingClient\n",
119119
"\n",
120-
"from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",
121-
"credential = DefaultAzureCredential()\n",
120+
"from azure.identity import AzureCliCredential, get_bearer_token_provider\n",
121+
"credential = AzureCliCredential()\n",
122122
"token_provider = get_bearer_token_provider(credential, AUTHENTICATION_URL)\n",
123123
"\n",
124124
"# The analyzer template is used to define the schema of the output\n",

docs/workshop/docs/workshop/Challenge-5/notebooks/video_tag_generation.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@
117117
")\n",
118118
"from python.content_understanding_client import AzureContentUnderstandingClient\n",
119119
"\n",
120-
"from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",
121-
"credential = DefaultAzureCredential()\n",
120+
"from azure.identity import AzureCliCredential, get_bearer_token_provider\n",
121+
"credential = AzureCliCredential()\n",
122122
"token_provider = get_bearer_token_provider(credential, AUTHENTICATION_URL)\n",
123123
"\n",
124124
"# The analyzer template is used to define the schema of the output\n",

docs/workshop/docs/workshop/Challenge-5/python/utility.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from openai import AzureOpenAI
77
import tiktoken
8-
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
8+
from azure.identity import AzureCliCredential, get_bearer_token_provider
99
from tenacity import retry, wait_random_exponential, stop_after_attempt
1010
from pydantic import BaseModel, Field
1111

@@ -158,7 +158,7 @@ def __init__(
158158
if aoai_api_key is None or aoai_api_key == "":
159159
print("Using Entra ID/AAD to authenticate")
160160
token_provider = get_bearer_token_provider(
161-
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
161+
AzureCliCredential(), "https://cognitiveservices.azure.com/.default"
162162
)
163163

164164
self.client = AzureOpenAI(

docs/workshop/docs/workshop/Challenge-6/Content_safety_evaluation.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"metadata": {},
3737
"outputs": [],
3838
"source": [
39-
"from azure.identity import DefaultAzureCredential\n",
39+
"from azure.identity import AzureCliCredential\n",
4040
"\n",
4141
"azure_ai_project = {\n",
4242
" \"subscription_id\": os.environ.get(\"AZURE_SUBSCRIPTION_ID\"),\n",
@@ -122,8 +122,8 @@
122122
"outputs": [],
123123
"source": [
124124
"from azure.ai.evaluation.simulator import AdversarialScenario\n",
125-
"from azure.identity import DefaultAzureCredential\n",
126-
"credential = DefaultAzureCredential()\n",
125+
"from azure.identity import AzureCliCredential\n",
126+
"credential = AzureCliCredential()\n",
127127
"\n",
128128
"scenario = AdversarialScenario.ADVERSARIAL_QA\n",
129129
"adversarial_simulator = AdversarialSimulator(azure_ai_project=azure_ai_project, credential=credential)\n",
@@ -150,7 +150,7 @@
150150
"from azure.ai.evaluation import ContentSafetyEvaluator\n",
151151
"import pandas as pd\n",
152152
"\n",
153-
"credential = DefaultAzureCredential()\n",
153+
"credential = AzureCliCredential()\n",
154154
"# instantiate an evaluator with image and multi-modal support\n",
155155
"safety_evaluator = ContentSafetyEvaluator(credential=credential, azure_ai_project=azure_ai_project)\n",
156156
"\n",

infra/main.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ module backend_docker 'deploy_backend_docker.bicep' = {
269269
APPLICATIONINSIGHTS_CONNECTION_STRING: aifoundry.outputs.applicationInsightsConnectionString
270270
DUMMY_TEST: 'True'
271271
SOLUTION_NAME: solutionPrefix
272+
APP_ENV: 'Prod'
272273
}
273274
}
274275
scope: resourceGroup(resourceGroup().name)

infra/main.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"_generator": {
66
"name": "bicep",
77
"version": "0.36.177.2456",
8-
"templateHash": "832352662399817246"
8+
"templateHash": "13989630346581833993"
99
}
1010
},
1111
"parameters": {
@@ -137,7 +137,7 @@
137137
"type": "location",
138138
"usageName": [
139139
"OpenAI.GlobalStandard.gpt-4o-mini,150",
140-
"OpenAI.Standard.text-embedding-ada-002,80"
140+
"OpenAI.GlobalStandard.text-embedding-ada-002,80"
141141
]
142142
},
143143
"description": "Location for AI Foundry deployment. This is the location where the AI Foundry resources will be deployed."
@@ -377,7 +377,7 @@
377377
"abbrs": "[variables('$fxv#0')]",
378378
"solutionLocation": "[if(empty(parameters('AZURE_LOCATION')), resourceGroup().location, parameters('AZURE_LOCATION'))]",
379379
"useLocalBuildLower": "[toLower(parameters('useLocalBuild'))]",
380-
"uniqueId": "[toLower(uniqueString(subscription().id, parameters('environmentName'), variables('solutionLocation')))]",
380+
"uniqueId": "[toLower(uniqueString(subscription().id, parameters('environmentName'), variables('solutionLocation'), resourceGroup().name))]",
381381
"solutionPrefix": "[format('km{0}', padLeft(take(variables('uniqueId'), 12), 12, '0'))]",
382382
"containerRegistryName": "[format('{0}{1}', variables('abbrs').containers.containerRegistry, variables('solutionPrefix'))]",
383383
"containerRegistryNameCleaned": "[replace(variables('containerRegistryName'), '-', '')]",
@@ -681,7 +681,7 @@
681681
"_generator": {
682682
"name": "bicep",
683683
"version": "0.36.177.2456",
684-
"templateHash": "18139801220201504120"
684+
"templateHash": "16364162048483949784"
685685
}
686686
},
687687
"parameters": {
@@ -988,7 +988,7 @@
988988
"name": "[parameters('embeddingModel')]",
989989
"model": "[parameters('embeddingModel')]",
990990
"sku": {
991-
"name": "Standard",
991+
"name": "GlobalStandard",
992992
"capacity": "[parameters('embeddingDeploymentCapacity')]"
993993
},
994994
"raiPolicyName": "Microsoft.Default"
@@ -1698,7 +1698,7 @@
16981698
"_generator": {
16991699
"name": "bicep",
17001700
"version": "0.36.177.2456",
1701-
"templateHash": "7364575792801916457"
1701+
"templateHash": "2162775944115849065"
17021702
}
17031703
},
17041704
"parameters": {
@@ -1909,7 +1909,7 @@
19091909
"_generator": {
19101910
"name": "bicep",
19111911
"version": "0.36.177.2456",
1912-
"templateHash": "7364575792801916457"
1912+
"templateHash": "2162775944115849065"
19131913
}
19141914
},
19151915
"parameters": {
@@ -3205,7 +3205,8 @@
32053205
"DISPLAY_CHART_DEFAULT": "False",
32063206
"APPLICATIONINSIGHTS_CONNECTION_STRING": "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, resourceGroup().name), 'Microsoft.Resources/deployments', 'deploy_ai_foundry'), '2022-09-01').outputs.applicationInsightsConnectionString.value]",
32073207
"DUMMY_TEST": "True",
3208-
"SOLUTION_NAME": "[variables('solutionPrefix')]"
3208+
"SOLUTION_NAME": "[variables('solutionPrefix')]",
3209+
"APP_ENV": "Prod"
32093210
}
32103211
}
32113212
},
@@ -3216,7 +3217,7 @@
32163217
"_generator": {
32173218
"name": "bicep",
32183219
"version": "0.36.177.2456",
3219-
"templateHash": "6279741508955918821"
3220+
"templateHash": "13824837150745689508"
32203221
}
32213222
},
32223223
"parameters": {
@@ -3676,7 +3677,7 @@
36763677
"_generator": {
36773678
"name": "bicep",
36783679
"version": "0.36.177.2456",
3679-
"templateHash": "7364575792801916457"
3680+
"templateHash": "2162775944115849065"
36803681
}
36813682
},
36823683
"parameters": {
@@ -3919,7 +3920,7 @@
39193920
"_generator": {
39203921
"name": "bicep",
39213922
"version": "0.36.177.2456",
3922-
"templateHash": "12963922539753840423"
3923+
"templateHash": "2667250685903499862"
39233924
}
39243925
},
39253926
"parameters": {

infra/process_data_scripts.bicep

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
param solutionLocation string
22
param keyVaultName string
3-
param identity string
3+
param managedIdentityResourceId string
4+
param managedIdentityClientId string
45

56
var baseUrl = 'https://raw.githubusercontent.com/microsoft/Conversation-Knowledge-Mining-Solution-Accelerator/main/'
67

@@ -11,13 +12,13 @@ resource process_data_scripts 'Microsoft.Resources/deploymentScripts@2020-10-01'
1112
identity: {
1213
type: 'UserAssigned'
1314
userAssignedIdentities: {
14-
'${identity}' : {}
15+
'${managedIdentityResourceId}' : {}
1516
}
1617
}
1718
properties: {
1819
azCliVersion: '2.52.0'
1920
primaryScriptUri: '${baseUrl}infra/scripts/process_data_scripts.sh'
20-
arguments: '${baseUrl} ${keyVaultName}' // Specify any arguments for the script
21+
arguments: '${baseUrl} ${keyVaultName} ${managedIdentityClientId}' // Specify any arguments for the script
2122
timeout: 'PT1H' // Specify the desired timeout duration
2223
retentionInterval: 'PT1H' // Specify the desired retention interval
2324
cleanupPreference:'OnSuccess'

infra/scripts/fabric_scripts/create_fabric_items.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from azure.identity import DefaultAzureCredential
1+
from azure.identity import ManagedIdentityCredential
22
import base64
33
import json
44
import requests
@@ -9,7 +9,7 @@
99
import time
1010

1111

12-
# credential = DefaultAzureCredential()
12+
# credential = ManagedIdentityCredential()
1313
from azure.identity import AzureCliCredential
1414
credential = AzureCliCredential()
1515

infra/scripts/index_scripts/01_create_search_index.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from azure.identity import DefaultAzureCredential
21
from azure.keyvault.secrets import SecretClient
32
from azure.search.documents.indexes import SearchIndexClient
43
from azure.search.documents.indexes.models import (
@@ -15,6 +14,7 @@
1514
SemanticField,
1615
SearchIndex
1716
)
17+
from azure_credential_utils import get_azure_credential
1818

1919
# === Configuration ===
2020
KEY_VAULT_NAME = 'kv_to-be-replaced'
@@ -28,12 +28,12 @@ def get_secrets_from_kv(secret_name: str) -> str:
2828
2929
Args:
3030
secret_name (str): Name of the secret.
31-
credential (DefaultAzureCredential): Credential with access to Key Vault.
31+
credential (ManagedIdentityCredential): Credential with access to Key Vault.
3232
3333
Returns:
3434
str: The secret value.
3535
"""
36-
kv_credential = DefaultAzureCredential(managed_identity_client_id=MANAGED_IDENTITY_CLIENT_ID)
36+
kv_credential = get_azure_credential(client_id=MANAGED_IDENTITY_CLIENT_ID)
3737
secret_client = SecretClient(
3838
vault_url=f"https://{KEY_VAULT_NAME}.vault.azure.net/",
3939
credential=kv_credential
@@ -49,7 +49,7 @@ def create_search_index():
4949
- Semantic search using prioritized fields
5050
"""
5151
# Shared credential
52-
credential = DefaultAzureCredential(managed_identity_client_id=MANAGED_IDENTITY_CLIENT_ID)
52+
credential = get_azure_credential(client_id=MANAGED_IDENTITY_CLIENT_ID)
5353

5454
# Retrieve secrets from Key Vault
5555
search_endpoint = get_secrets_from_kv("AZURE-SEARCH-ENDPOINT")
@@ -121,4 +121,4 @@ def create_search_index():
121121
print(f"Search index '{result.name}' created or updated successfully.")
122122

123123

124-
create_search_index()
124+
create_search_index()

0 commit comments

Comments
 (0)