|
| 1 | +import os |
| 2 | +from azure.identity import ManagedIdentityCredential, DefaultAzureCredential |
| 3 | +from azure.identity.aio import ( |
| 4 | + ManagedIdentityCredential as AioManagedIdentityCredential, |
| 5 | + DefaultAzureCredential as AioDefaultAzureCredential, |
| 6 | +) |
| 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 ( |
| 24 | + AioDefaultAzureCredential() |
| 25 | + ) # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development |
| 26 | + else: |
| 27 | + return AioManagedIdentityCredential(client_id=client_id) |
| 28 | + |
| 29 | + |
| 30 | +def get_azure_credential(client_id=None): |
| 31 | + """ |
| 32 | + Returns an Azure credential based on the application environment. |
| 33 | +
|
| 34 | + If the environment is 'dev', it uses DefaultAzureCredential. |
| 35 | + Otherwise, it uses ManagedIdentityCredential. |
| 36 | +
|
| 37 | + Args: |
| 38 | + client_id (str, optional): The client ID for the Managed Identity Credential. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + Credential object: Either DefaultAzureCredential or ManagedIdentityCredential. |
| 42 | + """ |
| 43 | + if os.getenv("APP_ENV", "prod").lower() == "dev": |
| 44 | + return ( |
| 45 | + DefaultAzureCredential() |
| 46 | + ) # CodeQL [SM05139] Okay use of DefaultAzureCredential as it is only used in development |
| 47 | + else: |
| 48 | + return ManagedIdentityCredential(client_id=client_id) |
0 commit comments