Skip to content

Commit 2a47ba9

Browse files
authored
Python: Removed usage of DefaultAzureCredential (#12964)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> The usage of `DefaultAzureCredential` by default is unsafe and raises security issues. Instead, users should provide their own credential types such as `AzureCliCredential`, `ManagedIdentityCredential` and so on. More information here: https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.aio?view=azure-python In this PR: 1. Removed the usage of `DefaultAzureCredential` from authentication logic. 2. Replaced `DefaultAzureCredential` with `AzureCliCredential` in all examples. **Note**: this is breaking change for Azure users, who relies on `DefaultAzureCredential`. Users will need to update the code and pass the credential type of their choice by using `credential` parameter when initializing one of the Azure clients. Before: ```python chat_completion = AzureChatCompletion() ``` After: ```python chat_completion = AzureChatCompletion(credential=AzureCliCredential()) ``` ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent a02d604 commit 2a47ba9

File tree

194 files changed

+852
-442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+852
-442
lines changed

python/samples/concepts/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,15 @@ In Semantic Kernel for Python, we leverage Pydantic Settings to manage configura
265265
3. **Direct Constructor Input:**
266266
- As an alternative to environment variables and `.env` files, you can pass the required settings directly through the constructor of the AI Connector or Memory Connector.
267267

268-
## Microsoft Entra Token Authentication
268+
## Azure Authentication
269269

270-
To authenticate to your Azure resources using a Microsoft Entra Authentication Token, the `AzureChatCompletion` AI Service connector now supports this as a built-in feature. If you do not provide an API key -- either through an environment variable, a `.env` file, or the constructor -- and you also do not provide a custom `AsyncAzureOpenAI` client, an `ad_token`, or an `ad_token_provider`, the `AzureChatCompletion` connector will attempt to retrieve a token using the [`DefaultAzureCredential`](https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python).
270+
To authenticate to your Azure resources, you must provide one of the following authentication methods to successfully authenticate:
271+
272+
1. **AsyncTokenCredential** - provide one of the `AsyncTokenCredential` types (e.g. `AzureCliCredential`, `ManagedIdentityCredential`). More information here: [Credentials for asynchronous Azure SDK clients]("https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.aio?view=azure-python").
273+
2. **Custom AsyncAzureOpenAI client** - Pass a pre-configured client instance.
274+
3. **Access Token (`ad_token`)** - Provide a valid Microsoft Entra access token directly.
275+
4. **Token Provider (`ad_token_provider`)** - Provide a callable that returns a valid access token.
276+
5. **API Key** - Provide through an environment variable, a `.env` file, or the constructor.
271277

272278
To successfully retrieve and use the Entra Auth Token, you need the `Cognitive Services OpenAI Contributor` role assigned to your Azure OpenAI resource. By default, the `https://cognitiveservices.azure.com` token endpoint is used. You can override this endpoint by setting an environment variable `.env` variable as `AZURE_OPENAI_TOKEN_ENDPOINT` or by passing a new value to the `AzureChatCompletion` constructor as part of the `AzureOpenAISettings`.
273279

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_as_kernel_function.py

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

33
import asyncio
44

5-
from azure.identity.aio import DefaultAzureCredential
5+
from azure.identity import AzureCliCredential
66

77
from semantic_kernel import Kernel
88
from semantic_kernel.agents import (
@@ -69,9 +69,10 @@ async def main() -> None:
6969

7070
ai_agent_settings = AzureAIAgentSettings()
7171

72+
credential = AzureCliCredential()
73+
7274
async with (
73-
DefaultAzureCredential() as creds,
74-
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
75+
AzureAIAgent.create_client(credential=credential, endpoint=ai_agent_settings.endpoint) as client,
7576
):
7677
# Create the agent definition
7778
agent_definition = await client.agents.create_agent(
@@ -93,7 +94,7 @@ async def main() -> None:
9394
)
9495

9596
refund_agent = ChatCompletionAgent(
96-
service=AzureChatCompletion(),
97+
service=AzureChatCompletion(credential=credential),
9798
name="RefundAgent",
9899
instructions=(
99100
"You specialize in addressing customer inquiries regarding refunds. "
@@ -106,7 +107,7 @@ async def main() -> None:
106107
)
107108

108109
triage_agent = ChatCompletionAgent(
109-
service=AzureChatCompletion(),
110+
service=AzureChatCompletion(credential=credential),
110111
kernel=kernel,
111112
name="TriageAgent",
112113
instructions=(

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_auto_func_invocation_filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from typing import Annotated
55

6-
from azure.identity.aio import DefaultAzureCredential
6+
from azure.identity.aio import AzureCliCredential
77

88
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
99
from semantic_kernel.contents import ChatMessageContent, FunctionCallContent, FunctionResultContent
@@ -97,7 +97,7 @@ async def main() -> None:
9797
ai_agent_settings = AzureAIAgentSettings.create()
9898

9999
async with (
100-
DefaultAzureCredential() as creds,
100+
AzureCliCredential() as creds,
101101
AzureAIAgent.create_client(credential=creds) as client,
102102
):
103103
# 1. Create an agent on the Azure AI agent service

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_auto_func_invocation_filter_streaming.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from typing import Annotated
55

6-
from azure.identity.aio import DefaultAzureCredential
6+
from azure.identity.aio import AzureCliCredential
77

88
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
99
from semantic_kernel.contents import ChatMessageContent, FunctionCallContent, FunctionResultContent
@@ -97,7 +97,7 @@ async def main() -> None:
9797
ai_agent_settings = AzureAIAgentSettings.create()
9898

9999
async with (
100-
DefaultAzureCredential() as creds,
100+
AzureCliCredential() as creds,
101101
AzureAIAgent.create_client(credential=creds) as client,
102102
):
103103
# 1. Create an agent on the Azure AI agent service

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_azure_ai_search.py

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

66
from azure.ai.agents.models import AzureAISearchTool
77
from azure.ai.projects.models import ConnectionType
8-
from azure.identity.aio import DefaultAzureCredential
8+
from azure.identity.aio import AzureCliCredential
99

1010
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
1111

@@ -39,7 +39,7 @@ async def main() -> None:
3939
ai_agent_settings = AzureAIAgentSettings()
4040

4141
async with (
42-
DefaultAzureCredential() as creds,
42+
AzureCliCredential() as creds,
4343
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
4444
):
4545
ai_search_conn_id = ""

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_bing_grounding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44

55
from azure.ai.agents.models import BingGroundingTool
6-
from azure.identity.aio import DefaultAzureCredential
6+
from azure.identity.aio import AzureCliCredential
77

88
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
99
from semantic_kernel.contents import (
@@ -37,7 +37,7 @@ async def handle_intermediate_steps(message: ChatMessageContent) -> None:
3737

3838
async def main() -> None:
3939
async with (
40-
DefaultAzureCredential() as creds,
40+
AzureCliCredential() as creds,
4141
AzureAIAgent.create_client(credential=creds) as client,
4242
):
4343
# 1. Enter your Bing Grounding Connection Name

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_bing_grounding_streaming_with_message_callback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44

55
from azure.ai.agents.models import BingGroundingTool
6-
from azure.identity.aio import DefaultAzureCredential
6+
from azure.identity.aio import AzureCliCredential
77

88
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
99
from semantic_kernel.contents import (
@@ -42,7 +42,7 @@ async def handle_streaming_intermediate_steps(message: ChatMessageContent) -> No
4242

4343
async def main() -> None:
4444
async with (
45-
DefaultAzureCredential() as creds,
45+
AzureCliCredential() as creds,
4646
AzureAIAgent.create_client(credential=creds) as client,
4747
):
4848
# 1. Enter your Bing Grounding Connection Name

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_code_interpreter_streaming_with_message_callback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from functools import reduce
55

66
from azure.ai.agents.models import CodeInterpreterTool
7-
from azure.identity.aio import DefaultAzureCredential
7+
from azure.identity.aio import AzureCliCredential
88

99
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
1010
from semantic_kernel.contents import ChatMessageContent, StreamingChatMessageContent
@@ -27,7 +27,7 @@ async def handle_streaming_intermediate_steps(message: ChatMessageContent) -> No
2727

2828
async def main() -> None:
2929
async with (
30-
DefaultAzureCredential() as creds,
30+
AzureCliCredential() as creds,
3131
AzureAIAgent.create_client(credential=creds) as client,
3232
):
3333
# 1. Create an agent with a code interpreter on the Azure AI agent service

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_azure_ai_search.py

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

33
import asyncio
44

5-
from azure.identity.aio import DefaultAzureCredential
5+
from azure.identity.aio import AzureCliCredential
66

77
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
88
from semantic_kernel.contents.chat_message_content import ChatMessageContent
@@ -50,7 +50,7 @@
5050

5151
async def main():
5252
async with (
53-
DefaultAzureCredential() as creds,
53+
AzureCliCredential() as creds,
5454
AzureAIAgent.create_client(credential=creds) as client,
5555
):
5656
try:

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_bing_grounding.py

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

33
import asyncio
44

5-
from azure.identity.aio import DefaultAzureCredential
5+
from azure.identity.aio import AzureCliCredential
66

77
from semantic_kernel.agents import AgentRegistry, AzureAIAgent, AzureAIAgentSettings
88

@@ -43,7 +43,7 @@
4343

4444
async def main():
4545
async with (
46-
DefaultAzureCredential() as creds,
46+
AzureCliCredential() as creds,
4747
AzureAIAgent.create_client(credential=creds) as client,
4848
):
4949
try:

0 commit comments

Comments
 (0)