Skip to content

Commit 1b72057

Browse files
committed
Refactor Azure credential handling and remove Cosmos memory kernel
Moved Azure credential logic from common.auth.azure_credential_utils.py into AppConfig, removing the utility module and updating all references to use AppConfig's methods. Deleted the CosmosMemoryContext implementation in context/cosmos_memory_kernel.py. Cleaned up unused imports and updated credential usage in affected modules for consistency.
1 parent ed9fe72 commit 1b72057

File tree

7 files changed

+24
-1062
lines changed

7 files changed

+24
-1062
lines changed

src/backend/common/auth/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/backend/common/auth/azure_credential_utils.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/backend/common/config/app_config.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from typing import Optional
55

66
from azure.ai.projects.aio import AIProjectClient
7-
from azure.cosmos.aio import CosmosClient
8-
from common.auth.azure_credential_utils import get_azure_credential
7+
from azure.identity import ManagedIdentityCredential, DefaultAzureCredential
98
from dotenv import load_dotenv
109

1110
# Load environment variables from .env file
@@ -74,6 +73,26 @@ def __init__(self):
7473
self._cosmos_database = None
7574
self._ai_project_client = None
7675

76+
def get_azure_credential(cself, client_id=None):
77+
"""
78+
Returns an Azure credential based on the application environment.
79+
80+
If the environment is 'dev', it uses DefaultAzureCredential.
81+
Otherwise, it uses ManagedIdentityCredential.
82+
83+
Args:
84+
client_id (str, optional): The client ID for the Managed Identity Credential.
85+
86+
Returns:
87+
Credential object: Either DefaultAzureCredential or ManagedIdentityCredential.
88+
"""
89+
if self.APP_ENV == "dev":
90+
return (
91+
DefaultAzureCredential()
92+
) # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
93+
else:
94+
return ManagedIdentityCredential(client_id=client_id)
95+
7796
def get_azure_credentials(self):
7897
"""Retrieve Azure credentials, either from environment variables or managed identity."""
7998
if self._azure_credentials is None:
@@ -83,7 +102,7 @@ def get_azure_credentials(self):
83102
async def get_access_token(self) -> str:
84103
"""Get Azure access token for API calls."""
85104
try:
86-
credential = get_azure_credential()
105+
credential = self.get_azure_credentials()
87106
token = credential.get_token(self.AZURE_COGNITIVE_SERVICES)
88107
return token.token
89108
except Exception as e:

src/backend/common/services/json_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
StartingTask,
2222
)
2323

24-
from common.auth.azure_credential_utils import get_azure_credential
24+
2525
from common.config.app_config import config
2626
from common.database.database_base import DatabaseBase
2727

@@ -37,7 +37,7 @@ def __init__(self, memory_context: Optional[DatabaseBase] = None):
3737
# Search validation configuration
3838
self.search_endpoint = config.AZURE_SEARCH_ENDPOINT
3939

40-
self.search_credential = get_azure_credential()
40+
self.search_credential = config.get_azure_credentials()
4141

4242
# Model validation configuration
4343
self.subscription_id = config.AZURE_AI_SUBSCRIPTION_ID

src/backend/common/utils/utils_kernel.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import semantic_kernel as sk
1111

1212
# Import the credential utility
13-
from common.auth.azure_credential_utils import get_azure_credential
1413
from common.config.app_config import config
1514

1615
# Import agent factory and the new AppConfig

0 commit comments

Comments
 (0)