Skip to content

Commit

Permalink
Add DefaultAzureCredential support to AzureBatchHook (#33469)
Browse files Browse the repository at this point in the history
* feat(providers/microsoft): add DefaultAzureCredential support to AzureBatchHook

* fix(providers/microsfot): replace DefaultAzureCredential with AzureIdentityCredentialAdapter

azure-batch does not directly supports DefaultAzureCredential

* test(providers/microsoft): add test case test_fallback_to_azure_identity_credential_adppter_when_name_and_key_is_not_provided
  • Loading branch information
Lee-W authored Aug 25, 2023
1 parent 55ff4cf commit 947b504
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions airflow/providers/microsoft/azure/hooks/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook
from airflow.models import Connection
from airflow.providers.microsoft.azure.utils import get_field
from airflow.providers.microsoft.azure.utils import AzureIdentityCredentialAdapter, get_field
from airflow.utils import timezone


Expand Down Expand Up @@ -96,7 +96,15 @@ def get_conn(self):
if not batch_account_url:
raise AirflowException("Batch Account URL parameter is missing.")

credentials = batch_auth.SharedKeyCredentials(conn.login, conn.password)
credentials: batch_auth.SharedKeyCredentials | AzureIdentityCredentialAdapter
if all([conn.login, conn.password]):
credentials = batch_auth.SharedKeyCredentials(conn.login, conn.password)
else:
credentials = AzureIdentityCredentialAdapter(
None, resource_id="https://batch.core.windows.net/.default"
)
# credentials = AzureIdentityCredentialAdapter()

batch_client = BatchServiceClient(credentials, batch_url=batch_account_url)
return batch_client

Expand Down Expand Up @@ -344,7 +352,6 @@ def add_single_task_to_job(self, job_id: str, task: TaskAddParameter) -> None:
:param task: The task to add
"""
try:

self.connection.task.add(job_id=job_id, task=task)
except batch_models.BatchErrorException as err:
if not err.error or err.error.code != "TaskExists":
Expand Down
20 changes: 20 additions & 0 deletions tests/providers/microsoft/azure/hooks/test_azure_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from airflow.providers.microsoft.azure.hooks.batch import AzureBatchHook
from airflow.utils import db

MODULE = "airflow.providers.microsoft.azure.hooks.batch"


class TestAzureBatchHook:
# set up the test environment
Expand Down Expand Up @@ -67,6 +69,24 @@ def test_connection_and_client(self):
assert isinstance(hook._connection(), Connection)
assert isinstance(hook.get_conn(), BatchServiceClient)

@mock.patch(f"{MODULE}.batch_auth.SharedKeyCredentials")
@mock.patch(f"{MODULE}.AzureIdentityCredentialAdapter")
def test_fallback_to_azure_identity_credential_adppter_when_name_and_key_is_not_provided(
self, mock_azure_identity_credential_adapter, mock_shared_key_credentials
):
self.test_account_name = None
self.test_account_key = None

hook = AzureBatchHook(azure_batch_conn_id=self.test_vm_conn_id)
assert isinstance(hook.get_conn(), BatchServiceClient)
mock_azure_identity_credential_adapter.assert_called_with(
None, resource_id="https://batch.core.windows.net/.default"
)
assert not mock_shared_key_credentials.auth.called

self.test_account_name = "test_account_name"
self.test_account_key = "test_account_key"

def test_configure_pool_with_vm_config(self):
hook = AzureBatchHook(azure_batch_conn_id=self.test_vm_conn_id)
pool = hook.configure_pool(
Expand Down

0 comments on commit 947b504

Please sign in to comment.