Skip to content

Commit 2b2598e

Browse files
commiting the new changes
1 parent 45b3630 commit 2b2598e

17 files changed

Lines changed: 302 additions & 30 deletions

File tree

infra/main.bicep

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,10 @@ module avmContainerApp 'br/public:avm/res/app/container-app:0.17.0' = {
807807
name: 'APP_CONFIG_ENDPOINT'
808808
value: ''
809809
}
810+
{
811+
name: 'APP_ENV'
812+
value: 'prod'
813+
}
810814
]
811815
}
812816
]
@@ -851,6 +855,10 @@ module avmContainerApp_API 'br/public:avm/res/app/container-app:0.17.0' = {
851855
name: 'APP_CONFIG_ENDPOINT'
852856
value: ''
853857
}
858+
{
859+
name: 'APP_ENV'
860+
value: 'prod'
861+
}
854862
]
855863
probes: [
856864
// Liveness Probe - Checks if the app is still running
@@ -1266,6 +1274,10 @@ module avmContainerApp_update 'br/public:avm/res/app/container-app:0.17.0' = {
12661274
name: 'APP_CONFIG_ENDPOINT'
12671275
value: avmAppConfig.outputs.endpoint
12681276
}
1277+
{
1278+
name: 'APP_ENV'
1279+
value: 'prod'
1280+
}
12691281
]
12701282
}
12711283
]
@@ -1321,6 +1333,10 @@ module avmContainerApp_API_update 'br/public:avm/res/app/container-app:0.17.0' =
13211333
name: 'APP_CONFIG_ENDPOINT'
13221334
value: avmAppConfig.outputs.endpoint
13231335
}
1336+
{
1337+
name: 'APP_ENV'
1338+
value: 'prod'
1339+
}
13241340
]
13251341
probes: [
13261342
// Liveness Probe - Checks if the app is still running
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
import os
5+
from azure.identity import ManagedIdentityCredential, DefaultAzureCredential
6+
from azure.identity.aio import ManagedIdentityCredential as AioManagedIdentityCredential, DefaultAzureCredential as AioDefaultAzureCredential
7+
8+
9+
async def get_azure_credential_async(client_id=None):
10+
"""
11+
Returns an Azure credential asynchronously based on the application environment.
12+
13+
If the environment is 'dev', it uses AioDefaultAzureCredential.
14+
Otherwise, it uses AioManagedIdentityCredential.
15+
16+
Args:
17+
client_id (str, optional): The client ID for the Managed Identity Credential.
18+
19+
Returns:
20+
Credential object: Either AioDefaultAzureCredential or AioManagedIdentityCredential.
21+
"""
22+
if os.getenv("APP_ENV", "prod").lower() == 'dev':
23+
return AioDefaultAzureCredential() # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
24+
else:
25+
return AioManagedIdentityCredential(client_id=client_id)
26+
27+
28+
def get_azure_credential(client_id=None):
29+
"""
30+
Returns an Azure credential based on the application environment.
31+
32+
If the environment is 'dev', it uses DefaultAzureCredential.
33+
Otherwise, it uses ManagedIdentityCredential.
34+
35+
Args:
36+
client_id (str, optional): The client ID for the Managed Identity Credential.
37+
38+
Returns:
39+
Credential object: Either DefaultAzureCredential or ManagedIdentityCredential.
40+
"""
41+
if os.getenv("APP_ENV", "prod").lower() == 'dev':
42+
return DefaultAzureCredential() # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development
43+
else:
44+
return ManagedIdentityCredential(client_id=client_id)

src/ContentProcessor/src/libs/application/application_context.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from azure.identity import DefaultAzureCredential
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
from helpers.azure_credential_utils import get_azure_credential
24

35
from libs.application.application_configuration import AppConfiguration
46
from libs.base.application_models import AppModelBase
@@ -11,10 +13,10 @@ class AppContext(AppModelBase):
1113
"""
1214

1315
configuration: AppConfiguration = None
14-
credential: DefaultAzureCredential = None
16+
credential: get_azure_credential = None
1517

1618
def set_configuration(self, configuration: AppConfiguration):
1719
self.configuration = configuration
1820

19-
def set_credential(self, credential: DefaultAzureCredential):
21+
def set_credential(self, credential: get_azure_credential):
2022
self.credential = credential

src/ContentProcessor/src/libs/azure_helper/app_configuration.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
14
import os
25

36
from azure.appconfiguration import AzureAppConfigurationClient
4-
from azure.identity import DefaultAzureCredential
7+
from helpers.azure_credential_utils import get_azure_credential
58

69

710
class AppConfigurationHelper:
8-
credential: DefaultAzureCredential = None
11+
credential: get_azure_credential = None
912
app_config_endpoint: str = None
1013
app_config_client: AzureAppConfigurationClient = None
1114

1215
def __init__(self, app_config_endpoint: str):
13-
self.credential = DefaultAzureCredential()
16+
self.credential = get_azure_credential()
1417
self.app_config_endpoint = app_config_endpoint
1518
self._initialize_client()
1619

src/ContentProcessor/src/libs/azure_helper/azure_openai.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
from azure.identity import get_bearer_token_provider
5+
from helpers.azure_credential_utils import get_azure_credential
26
from openai import AzureOpenAI
37

48

59
def get_openai_client(azure_openai_endpoint: str) -> AzureOpenAI:
6-
credential = DefaultAzureCredential()
10+
credential = get_azure_credential()
711
token_provider = get_bearer_token_provider(
812
credential, "https://cognitiveservices.azure.com/.default"
913
)

src/ContentProcessor/src/libs/azure_helper/content_understanding.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
from pathlib import Path
88

99
import requests
10-
from azure.identity import DefaultAzureCredential
10+
from helpers.azure_credential_utils import get_azure_credential
1111
from requests.models import Response
1212

1313
COGNITIVE_SERVICES_SCOPE = "https://cognitiveservices.azure.com/.default"
1414

1515

1616
class AzureContentUnderstandingHelper:
17-
credential: DefaultAzureCredential = None
17+
credential: get_azure_credential = None
1818

1919
def __init__(
2020
self,
2121
endpoint: str,
2222
api_version: str = "2024-12-01-preview",
2323
x_ms_useragent: str = "cps-contentunderstanding/client",
2424
):
25-
self.credential = DefaultAzureCredential()
25+
self.credential = get_azure_credential()
2626

2727
if not api_version:
2828
raise ValueError("API version must be provided.")

src/ContentProcessor/src/libs/azure_helper/storage_blob.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
from typing import IO, Union
55

6-
from azure.identity import DefaultAzureCredential
6+
from helpers.azure_credential_utils import get_azure_credential
77
from azure.storage.blob import BlobServiceClient
88

99

1010
class StorageBlobHelper:
11-
credential: DefaultAzureCredential = None
11+
credential: get_azure_credential = None
1212
blob_service_client: BlobServiceClient = None
1313

1414
@staticmethod
1515
def get(account_url: str, container_name: str = None):
1616
return StorageBlobHelper(account_url=account_url, container_name=container_name)
1717

1818
def __init__(self, account_url: str, container_name=None):
19-
self.credential = DefaultAzureCredential()
19+
self.credential = get_azure_credential()
2020
self.blob_service_client = BlobServiceClient(
2121
account_url=account_url, credential=self.credential
2222
)

src/ContentProcessor/src/libs/pipeline/pipeline_queue_helper.py

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

66
from azure.core.exceptions import ResourceNotFoundError
7-
from azure.identity import DefaultAzureCredential
7+
from helpers.azure_credential_utils import get_azure_credential
88
from azure.storage.queue import QueueClient, QueueMessage
99

1010
from libs.pipeline import pipeline_step_helper
@@ -28,7 +28,7 @@ def invalidate_queue(queue_client: QueueClient):
2828

2929

3030
def create_or_get_queue_client(
31-
queue_name: str, accouont_url: str, credential: DefaultAzureCredential
31+
queue_name: str, accouont_url: str, credential: get_azure_credential
3232
) -> QueueClient:
3333
queue_client = QueueClient(
3434
account_url=accouont_url, queue_name=queue_name, credential=credential
@@ -55,7 +55,7 @@ def has_messages(queue_client: QueueClient) -> bool:
5555

5656

5757
def pass_data_pipeline_to_next_step(
58-
data_pipeline: DataPipeline, account_url: str, credential: DefaultAzureCredential
58+
data_pipeline: DataPipeline, account_url: str, credential: get_azure_credential
5959
):
6060
next_step_name = pipeline_step_helper.get_next_step_name(
6161
data_pipeline.pipeline_status, data_pipeline.pipeline_status.active_step
@@ -70,7 +70,7 @@ def pass_data_pipeline_to_next_step(
7070

7171

7272
def _create_queue_client(
73-
account_url: str, queue_name: str, credential: DefaultAzureCredential
73+
account_url: str, queue_name: str, credential: get_azure_credential
7474
) -> QueueClient:
7575
queue_client = QueueClient(
7676
account_url=account_url, queue_name=queue_name, credential=credential

src/ContentProcessor/src/libs/utils/remote_module_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import importlib.util
55
import sys
66

7-
from azure.identity import DefaultAzureCredential
7+
from helpers.azure_credential_utils import get_azure_credential
88
from azure.storage.blob import BlobServiceClient
99

1010

@@ -27,7 +27,7 @@ def load_schema_from_blob(
2727

2828
def _download_blob_content(container_name, blob_name, account_url):
2929
# Create the BlobServiceClient object which will be used to create a container client
30-
credential = DefaultAzureCredential()
30+
credential = get_azure_credential()
3131
blob_service_client = BlobServiceClient(
3232
account_url=account_url, credential=credential
3333
)

src/ContentProcessor/src/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import sys
77

8-
from azure.identity import DefaultAzureCredential
8+
from helpers.azure_credential_utils import get_azure_credential
99

1010
from libs.base.application_main import AppMainBase
1111
from libs.process_host import handler_type_loader
@@ -29,7 +29,7 @@ def __init__(self, **data):
2929

3030
def _initialize_application(self):
3131
# Add Azure Credential
32-
self.application_context.set_credential(DefaultAzureCredential())
32+
self.application_context.set_credential(get_azure_credential())
3333

3434
async def run(self, test_mode: bool = False):
3535
# Get Process lists from the configuration - ex. ["extract", "transform", "evaluate", "save", "custom1", "custom2"....]

0 commit comments

Comments
 (0)