Skip to content

Commit aa7a26f

Browse files
Prajwal-MicrosoftPavan-MicrosoftRoopan-MicrosoftAjitPadhi-Microsoftross-p-smith
authored
feat: WAF implementation for CWYD (#1916)
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Pavan-Microsoft <[email protected]> Co-authored-by: Roopan-Microsoft <[email protected]> Co-authored-by: Ajit Padhi <[email protected]> Co-authored-by: Roopan P M <[email protected]> Co-authored-by: Ross Smith <[email protected]> Co-authored-by: gpickett <[email protected]> Co-authored-by: Francia Riesco <[email protected]> Co-authored-by: Francia Riesco <[email protected]> Co-authored-by: Harmanpreet-Microsoft <[email protected]> Co-authored-by: UtkarshMishra-Microsoft <[email protected]> Co-authored-by: Priyanka-Microsoft <[email protected]> Co-authored-by: Prasanjeet-Microsoft <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kiran-Siluveru-Microsoft <[email protected]> Co-authored-by: Prashant-Microsoft <[email protected]> Co-authored-by: Rohini-Microsoft <[email protected]> Co-authored-by: Avijit-Microsoft <[email protected]> Co-authored-by: RaviKiran-Microsoft <[email protected]> Co-authored-by: Somesh Joshi <[email protected]> Co-authored-by: Himanshi Agrawal <[email protected]> Co-authored-by: pradeepjha-microsoft <[email protected]> Co-authored-by: Harmanpreet Kaur <[email protected]> Co-authored-by: Bangarraju-Microsoft <[email protected]> Co-authored-by: Harsh-Microsoft <[email protected]> Co-authored-by: Kanchan-Microsoft <[email protected]> Co-authored-by: Cristopher Coronado <[email protected]> Co-authored-by: Cristopher Coronado Moreira <[email protected]> Co-authored-by: Vamshi-Microsoft <[email protected]> Co-authored-by: Thanusree-Microsoft <[email protected]> Co-authored-by: Niraj Chaudhari (Persistent Systems Inc) <[email protected]> Co-authored-by: Rohini-Microsoft <[email protected]>
1 parent 9d86faa commit aa7a26f

File tree

95 files changed

+66054
-13395
lines changed

Some content is hidden

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

95 files changed

+66054
-13395
lines changed

azure.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name: chat-with-your-data-solution-accelerator
44
metadata:
5-
5+
66
hooks:
77
postprovision:
88
# run: ./infra/prompt-flow/create-prompt-flow.sh

code/backend/batch/utilities/chat_history/database_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_conversation_client():
2525
f"https://{env_helper.AZURE_COSMOSDB_ACCOUNT}.documents.azure.com:443/"
2626
)
2727
credential = (
28-
get_azure_credential()
28+
get_azure_credential(env_helper.MANAGED_IDENTITY_CLIENT_ID)
2929
if not env_helper.AZURE_COSMOSDB_ACCOUNT_KEY
3030
else env_helper.AZURE_COSMOSDB_ACCOUNT_KEY
3131
)

code/backend/batch/utilities/chat_history/postgresdbservice.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncpg
33
from datetime import datetime, timezone
44
from ..helpers.azure_credential_utils import get_azure_credential
5+
from ..helpers.env_helper import EnvHelper
56

67
from .database_client_base import DatabaseClientBase
78

@@ -13,6 +14,7 @@ class PostgresConversationClient(DatabaseClientBase):
1314
def __init__(
1415
self, user: str, host: str, database: str, enable_message_feedback: bool = False
1516
):
17+
self.env_helper = EnvHelper()
1618
self.user = user
1719
self.host = host
1820
self.database = database
@@ -21,7 +23,7 @@ def __init__(
2123

2224
async def connect(self):
2325
try:
24-
credential = get_azure_credential()
26+
credential = get_azure_credential(self.env_helper.MANAGED_IDENTITY_CLIENT_ID)
2527
token = credential.get_token(
2628
"https://ossrdbms-aad.database.windows.net/.default"
2729
).token
@@ -31,7 +33,7 @@ async def connect(self):
3133
database=self.database,
3234
password=token,
3335
port=5432,
34-
ssl="require",
36+
ssl=True,
3537
)
3638
except Exception as e:
3739
logger.error("Failed to connect to PostgreSQL: %s", e)

code/backend/batch/utilities/helpers/azure_blob_storage_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_queue_client():
2525
return QueueClient(
2626
account_url=f"https://{env_helper.AZURE_BLOB_ACCOUNT_NAME}.queue.core.windows.net/",
2727
queue_name=env_helper.DOCUMENT_PROCESSING_QUEUE_NAME,
28-
credential=get_azure_credential(),
28+
credential=get_azure_credential(env_helper.MANAGED_IDENTITY_CLIENT_ID),
2929
message_encode_policy=BinaryBase64EncodePolicy(),
3030
)
3131

@@ -56,7 +56,7 @@ def __init__(
5656
if self.auth_type == "rbac":
5757
self.account_key = None
5858
self.blob_service_client = BlobServiceClient(
59-
account_url=self.endpoint, credential=get_azure_credential()
59+
account_url=self.endpoint, credential=get_azure_credential(env_helper.MANAGED_IDENTITY_CLIENT_ID)
6060
)
6161
self.user_delegation_key = self.request_user_delegation_key(
6262
blob_service_client=self.blob_service_client

code/backend/batch/utilities/helpers/azure_computer_vision_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, env_helper: EnvHelper) -> None:
2727
self.model_version = (
2828
env_helper.AZURE_COMPUTER_VISION_VECTORIZE_IMAGE_MODEL_VERSION
2929
)
30+
self.managed_identity_client_id = env_helper.MANAGED_IDENTITY_CLIENT_ID
3031

3132
def vectorize_image(self, image_url: str) -> list[float]:
3233
logger.info(f"Making call to computer vision to vectorize image: {image_url}")
@@ -57,7 +58,7 @@ def __make_request(self, path: str, body) -> Response:
5758
headers["Ocp-Apim-Subscription-Key"] = self.key
5859
else:
5960
token_provider = get_bearer_token_provider(
60-
get_azure_credential(), self.__TOKEN_SCOPE
61+
get_azure_credential(self.managed_identity_client_id), self.__TOKEN_SCOPE
6162
)
6263
headers["Authorization"] = "Bearer " + token_provider()
6364

code/backend/batch/utilities/helpers/azure_form_recognizer_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self) -> None:
1919
if env_helper.AZURE_AUTH_TYPE == "rbac":
2020
self.document_analysis_client = DocumentAnalysisClient(
2121
endpoint=self.AZURE_FORM_RECOGNIZER_ENDPOINT,
22-
credential=get_azure_credential(),
22+
credential=get_azure_credential(env_helper.MANAGED_IDENTITY_CLIENT_ID),
2323
headers={
2424
"x-ms-useragent": "chat-with-your-data-solution-accelerator/1.0.0"
2525
},

code/backend/batch/utilities/helpers/azure_postgres_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def _create_search_client(self):
2424
dbname = self.env_helper.POSTGRESQL_DATABASE
2525

2626
# Acquire the access token
27-
credential = get_azure_credential()
27+
credential = get_azure_credential(self.env_helper.MANAGED_IDENTITY_CLIENT_ID)
2828
access_token = credential.get_token(
2929
"https://ossrdbms-aad.database.windows.net/.default"
3030
)
3131

3232
# Use the token in the connection string
3333
conn_string = (
34-
f"host={host} user={user} dbname={dbname} password={access_token.token}"
34+
f"host={host} user={user} dbname={dbname} password={access_token.token} sslmode=require"
3535
)
3636
self.conn = psycopg2.connect(conn_string)
3737
logger.info("Connected to Azure PostgreSQL successfully.")

code/backend/batch/utilities/helpers/azure_search_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _search_credential(self):
4949
if self.env_helper.is_auth_type_keys():
5050
return AzureKeyCredential(self.env_helper.AZURE_SEARCH_KEY)
5151
else:
52-
return get_azure_credential()
52+
return get_azure_credential(self.env_helper.MANAGED_IDENTITY_CLIENT_ID)
5353

5454
def _create_search_client(
5555
self, search_credential: Union[AzureKeyCredential, get_azure_credential]
@@ -285,7 +285,7 @@ def get_conversation_logger(self):
285285
]
286286

287287
if self.env_helper.AZURE_AUTH_TYPE == "rbac":
288-
credential = get_azure_credential()
288+
credential = get_azure_credential(self.env_helper.MANAGED_IDENTITY_CLIENT_ID)
289289
return AzureSearch(
290290
azure_search_endpoint=self.env_helper.AZURE_SEARCH_SERVICE,
291291
azure_search_key=None, # Remove API key

code/backend/batch/utilities/helpers/env_helper.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ def __load_config(self, **kwargs) -> None:
3535
self.secretHelper = SecretHelper()
3636

3737
self.LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper()
38+
self.APP_ENV = os.getenv("APP_ENV", "Prod").lower()
3839

3940
# Azure
4041
self.AZURE_SUBSCRIPTION_ID = os.getenv("AZURE_SUBSCRIPTION_ID", "")
4142
self.AZURE_RESOURCE_GROUP = os.getenv("AZURE_RESOURCE_GROUP", "")
43+
self.MANAGED_IDENTITY_CLIENT_ID = os.getenv("MANAGED_IDENTITY_CLIENT_ID", "")
44+
self.MANAGED_IDENTITY_RESOURCE_ID = os.getenv("MANAGED_IDENTITY_RESOURCE_ID", "")
4245

4346
# Azure Search
4447
self.AZURE_SEARCH_SERVICE = os.getenv("AZURE_SEARCH_SERVICE", "")
@@ -217,7 +220,7 @@ def __load_config(self, **kwargs) -> None:
217220
)
218221

219222
self.AZURE_TOKEN_PROVIDER = get_bearer_token_provider(
220-
get_azure_credential(), "https://cognitiveservices.azure.com/.default"
223+
get_azure_credential(self.MANAGED_IDENTITY_CLIENT_ID), "https://cognitiveservices.azure.com/.default"
221224
)
222225
self.ADVANCED_IMAGE_PROCESSING_MAX_IMAGES = self.get_env_var_int(
223226
"ADVANCED_IMAGE_PROCESSING_MAX_IMAGES", 1
@@ -234,7 +237,7 @@ def __load_config(self, **kwargs) -> None:
234237
self.AZURE_COMPUTER_VISION_VECTORIZE_IMAGE_MODEL_VERSION = os.getenv(
235238
"AZURE_COMPUTER_VISION_VECTORIZE_IMAGE_MODEL_VERSION", "2023-04-15"
236239
)
237-
self.FUNCTION_KEY = os.getenv("FUNCTION_KEY", "")
240+
self.FUNCTION_KEY = self.secretHelper.get_secret("FUNCTION_KEY")
238241

239242
# Initialize Azure keys based on authentication type and environment settings.
240243
# When AZURE_AUTH_TYPE is "rbac", azure keys are None or an empty string.
@@ -243,7 +246,6 @@ def __load_config(self, **kwargs) -> None:
243246
self.AZURE_OPENAI_API_KEY = ""
244247
self.AZURE_SPEECH_KEY = None
245248
self.AZURE_COMPUTER_VISION_KEY = None
246-
self.FUNCTION_KEY = self.secretHelper.get_secret("FUNCTION_KEY")
247249
else:
248250
self.AZURE_SEARCH_KEY = self.secretHelper.get_secret("AZURE_SEARCH_KEY")
249251
self.AZURE_OPENAI_API_KEY = self.secretHelper.get_secret(
@@ -429,8 +431,11 @@ def __init__(self) -> None:
429431
self.USE_KEY_VAULT = os.getenv("USE_KEY_VAULT", "").lower() == "true"
430432
self.secret_client = None
431433
if self.USE_KEY_VAULT:
434+
vault_endpoint = os.environ.get("AZURE_KEY_VAULT_ENDPOINT")
435+
if not vault_endpoint:
436+
raise ValueError("AZURE_KEY_VAULT_ENDPOINT environment variable is required when USE_KEY_VAULT is true")
432437
self.secret_client = SecretClient(
433-
os.environ.get("AZURE_KEY_VAULT_ENDPOINT"), get_azure_credential()
438+
vault_endpoint, get_azure_credential(client_id=os.getenv("MANAGED_IDENTITY_CLIENT_ID", None))
434439
)
435440

436441
def get_secret(self, secret_name: str) -> str:

code/backend/batch/utilities/helpers/llm_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def get_sk_service_settings(self, service: AzureChatCompletion):
166166
def get_ml_client(self):
167167
if not hasattr(self, "_ml_client"):
168168
self._ml_client = MLClient(
169-
get_azure_credential(),
169+
get_azure_credential(self.env_helper.MANAGED_IDENTITY_CLIENT_ID),
170170
self.env_helper.AZURE_SUBSCRIPTION_ID,
171171
self.env_helper.AZURE_RESOURCE_GROUP,
172172
self.env_helper.AZURE_ML_WORKSPACE_NAME,

0 commit comments

Comments
 (0)