From 40017631336d5630a03a68c0bf568b73438790d0 Mon Sep 17 00:00:00 2001 From: Sai Sharan Tangeda Date: Thu, 26 Sep 2024 16:30:41 +0530 Subject: [PATCH] feat: update storage account creds --- setup.py | 2 +- vishwa_labs_fastapi_utils/cloud/az_blob.py | 26 ++++++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 051a08c..163bfb0 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ long_description = readme.read() setup( name='vishwa-fastapi-utils', - version="0.0.4", + version="0.0.5", author="Sai Sharan Tangeda", author_email="saisarantangeda@gmail.com", description="Base SDK for FastAPI Utils", diff --git a/vishwa_labs_fastapi_utils/cloud/az_blob.py b/vishwa_labs_fastapi_utils/cloud/az_blob.py index a71e5ee..5a7e163 100644 --- a/vishwa_labs_fastapi_utils/cloud/az_blob.py +++ b/vishwa_labs_fastapi_utils/cloud/az_blob.py @@ -1,5 +1,5 @@ import os -from azure.identity import ClientSecretCredential +from azure.identity import ClientSecretCredential, DefaultAzureCredential from azure.storage.blob import BlobServiceClient from pathlib import Path @@ -11,13 +11,25 @@ def __init__(self): def get_blob_service_client(self): """Authenticate using Service Principal and return the BlobServiceClient.""" - credential = ClientSecretCredential( - tenant_id=os.getenv("AZURE_TENANT_ID"), - client_id=os.getenv("AZURE_CLIENT_ID"), - client_secret=os.getenv("AZURE_CLIENT_SECRET")) + tenant_id = os.getenv("AZURE_TENANT_ID") + client_id = os.getenv("AZURE_CLIENT_ID") + client_secret = os.getenv("AZURE_CLIENT_SECRET") + storage_account_url = os.getenv("AZURE_STORAGE_ACCOUNT_URL") - blob_service_client = BlobServiceClient(account_url=os.getenv("AZURE_STORAGE_ACCOUNT_URL"), - credential=credential) + # Check if service principal credentials are available in the environment + if tenant_id and client_id and client_secret: + print("Using Service Principal credentials from environment variables.") + credential = ClientSecretCredential( + tenant_id=tenant_id, + client_id=client_id, + client_secret=client_secret + ) + else: + print("Service Principal credentials not found. Falling back to DefaultAzureCredential.") + credential = DefaultAzureCredential() + + # Create BlobServiceClient using the determined credentials + blob_service_client = BlobServiceClient(account_url=storage_account_url, credential=credential) return blob_service_client