You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2023/11/01 08:41:01 UTC

(airflow) branch main updated: feat(provider/azure): add managed identity support to asb hook (#35324)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 20afbf8036 feat(provider/azure): add managed identity support to asb hook (#35324)
20afbf8036 is described below

commit 20afbf8036f1310393c51ded69d4ed9032adf425
Author: Wei Lee <we...@gmail.com>
AuthorDate: Wed Nov 1 16:40:53 2023 +0800

    feat(provider/azure): add managed identity support to asb hook (#35324)
---
 airflow/providers/microsoft/azure/hooks/asb.py     | 36 +++++++++++++++++++---
 .../connections/asb.rst                            | 15 +++++++--
 tests/providers/microsoft/azure/hooks/test_asb.py  |  4 +--
 3 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/airflow/providers/microsoft/azure/hooks/asb.py b/airflow/providers/microsoft/azure/hooks/asb.py
index 34b21b4f23..e81bc84527 100644
--- a/airflow/providers/microsoft/azure/hooks/asb.py
+++ b/airflow/providers/microsoft/azure/hooks/asb.py
@@ -16,14 +16,16 @@
 # under the License.
 from __future__ import annotations
 
-from typing import Any
+from typing import TYPE_CHECKING, Any
 
-from azure.identity import DefaultAzureCredential
 from azure.servicebus import ServiceBusClient, ServiceBusMessage, ServiceBusSender
 from azure.servicebus.management import QueueProperties, ServiceBusAdministrationClient
 
 from airflow.hooks.base import BaseHook
-from airflow.providers.microsoft.azure.utils import get_field
+from airflow.providers.microsoft.azure.utils import get_default_azure_credential, get_field
+
+if TYPE_CHECKING:
+    from azure.identity import DefaultAzureCredential
 
 
 class BaseAzureServiceBusHook(BaseHook):
@@ -51,6 +53,12 @@ class BaseAzureServiceBusHook(BaseHook):
                 lazy_gettext("Fully Qualified Namespace"), widget=BS3TextFieldWidget()
             ),
             "credential": PasswordField(lazy_gettext("Credential"), widget=BS3TextFieldWidget()),
+            "managed_identity_client_id": StringField(
+                lazy_gettext("Managed Identity Client ID"), widget=BS3TextFieldWidget()
+            ),
+            "workload_identity_tenant_id": StringField(
+                lazy_gettext("Workload Identity Tenant ID"), widget=BS3TextFieldWidget()
+            ),
         }
 
     @staticmethod
@@ -65,6 +73,8 @@ class BaseAzureServiceBusHook(BaseHook):
                 ),
                 "credential": "credential",
                 "schema": "Endpoint=sb://<Resource group>.servicebus.windows.net/;SharedAccessKeyName=<AccessKeyName>;SharedAccessKey=<SharedAccessKey>",
+                "managed_identity_client_id": "Managed Identity Client ID",
+                "workload_identity_tenant_id": "Workload Identity Tenant ID",
             },
         }
 
@@ -106,7 +116,15 @@ class AdminClientHook(BaseAzureServiceBusHook):
             credential: str | DefaultAzureCredential = self._get_field(extras=extras, field_name="credential")
             fully_qualified_namespace = self._get_field(extras=extras, field_name="fully_qualified_namespace")
             if not credential:
-                credential = DefaultAzureCredential()
+                managed_identity_client_id = self._get_field(
+                    extras=extras, field_name="managed_identity_client_id"
+                )
+                workload_identity_tenant_id = self._get_field(
+                    extras=extras, field_name="workload_identity_tenant_id"
+                )
+                credential = get_default_azure_credential(
+                    managed_identity_client_id, workload_identity_tenant_id
+                )
             client = ServiceBusAdministrationClient(
                 fully_qualified_namespace=fully_qualified_namespace,
                 credential=credential,  # type: ignore[arg-type]
@@ -190,7 +208,15 @@ class MessageHook(BaseAzureServiceBusHook):
             credential: str | DefaultAzureCredential = self._get_field(extras=extras, field_name="credential")
             fully_qualified_namespace = self._get_field(extras=extras, field_name="fully_qualified_namespace")
             if not credential:
-                credential = DefaultAzureCredential()
+                managed_identity_client_id = self._get_field(
+                    extras=extras, field_name="managed_identity_client_id"
+                )
+                workload_identity_tenant_id = self._get_field(
+                    extras=extras, field_name="workload_identity_tenant_id"
+                )
+                credential = get_default_azure_credential(
+                    managed_identity_client_id, workload_identity_tenant_id
+                )
             client = ServiceBusClient(
                 fully_qualified_namespace=fully_qualified_namespace,
                 credential=credential,  # type: ignore[arg-type]
diff --git a/docs/apache-airflow-providers-microsoft-azure/connections/asb.rst b/docs/apache-airflow-providers-microsoft-azure/connections/asb.rst
index 848232836a..a280d6c9c1 100644
--- a/docs/apache-airflow-providers-microsoft-azure/connections/asb.rst
+++ b/docs/apache-airflow-providers-microsoft-azure/connections/asb.rst
@@ -27,11 +27,12 @@ The Microsoft Azure Service Bus connection type enables the Azure Service Bus In
 Authenticating to Azure Service Bus
 ------------------------------------
 
-There are two ways to authenticate and authorize access to Azure Service Bus resources:
+There are three ways to authenticate and authorize access to Azure Service Bus resources:
 
 1. Use a `Connection String`_
    i.e. Use connection string Field to add ``Connection String`` in the Airflow connection.
-2. Fallback on DefaultAzureCredential_.
+2. Use managed identity by setting ``managed_identity_client_id``, ``workload_identity_tenant_id`` (under the hook, it uses DefaultAzureCredential_ with these arguments)
+3. Fallback on DefaultAzureCredential_.
    This includes a mechanism to try different options to authenticate: Managed System Identity, environment variables, authentication through Azure CLI and etc.
    ``fully_qualified_namespace`` is required in this authentication mechanism.
 
@@ -56,6 +57,16 @@ Fully Qualified Namespace (optional)
    Use the key ``fully_qualified_namespace`` to pass in the Connection ID .
    This is required when falling back to DefaultAzureCredential_.
 
+Managed Identity Client ID (optional)
+    The client ID of a user-assigned managed identity. If provided with ``workload_identity_tenant_id``, they'll pass to DefaultAzureCredential_.
+
+Workload Identity Tenant ID (optional)
+    ID of the application's Microsoft Entra tenant. Also called its "directory" ID. If provided with ``managed_identity_client_id``, they'll pass to DefaultAzureCredential_.
+
+
 .. _Connection String: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-quickstart-portal#get-the-connection-string
 .. _DefaultAzureCredential: https://docs.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential
 .. _Get connection string: https://docs.microsoft.com/en-gb/azure/service-bus-messaging/service-bus-create-namespace-portal#get-the-connection-string
+
+.. spelling:word-list::
+    Entra
diff --git a/tests/providers/microsoft/azure/hooks/test_asb.py b/tests/providers/microsoft/azure/hooks/test_asb.py
index 6f241f438e..4f1939aa8b 100644
--- a/tests/providers/microsoft/azure/hooks/test_asb.py
+++ b/tests/providers/microsoft/azure/hooks/test_asb.py
@@ -60,7 +60,7 @@ class TestAdminClientHook:
         hook = AdminClientHook(azure_service_bus_conn_id=self.conn_id)
         assert isinstance(hook.get_conn(), ServiceBusAdministrationClient)
 
-    @mock.patch("airflow.providers.microsoft.azure.hooks.asb.DefaultAzureCredential")
+    @mock.patch("airflow.providers.microsoft.azure.hooks.asb.get_default_azure_credential")
     @mock.patch("airflow.providers.microsoft.azure.hooks.asb.AdminClientHook.get_connection")
     def test_get_conn_fallback_to_default_azure_credential_when_schema_is_not_provided(
         self, mock_connection, mock_default_azure_credential
@@ -171,7 +171,7 @@ class TestMessageHook:
         hook = MessageHook(azure_service_bus_conn_id=self.conn_id)
         assert isinstance(hook.get_conn(), ServiceBusClient)
 
-    @mock.patch("airflow.providers.microsoft.azure.hooks.asb.DefaultAzureCredential")
+    @mock.patch("airflow.providers.microsoft.azure.hooks.asb.get_default_azure_credential")
     @mock.patch("airflow.providers.microsoft.azure.hooks.asb.MessageHook.get_connection")
     def test_get_conn_fallback_to_default_azure_credential_when_schema_is_not_provided(
         self, mock_connection, mock_default_azure_credential