dabla commented on code in PR #75815:
URL: https://github.com/apache/airflow/pull/75815#discussion_r4759215111
##########
providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/data_lake.py:
##########
@@ -382,6 +390,51 @@ def get_conn(self) -> DataLakeServiceClient:
**extra,
)
+ async def get_async_conn(self) -> AsyncDataLakeServiceClient:
+ """
+ Return an async DataLakeServiceClient, built from the connection as
:meth:`get_conn` builds its client.
+
+ The connection is read with the async accessors, so the event loop is
not blocked. Unlike
+ :meth:`get_conn`, the client is not cached: an async client belongs to
the event loop it
+ was created on, so close it (``async with``) when done, and close its
credential if it has
+ a ``close`` method (an Active Directory or default credential).
+ """
+ conn = await get_async_connection(self.conn_id)
+ extra = dict(await get_async_extra_dejson(conn) or {})
+
+ connection_string = self._get_field(extra, "connection_string")
+ if connection_string:
+ # connection_string auth takes priority
+ return
AsyncDataLakeServiceClient.from_connection_string(connection_string, **extra)
+
+ credential: AsyncCredentials | str
+ tenant = self._get_field(extra, "tenant_id")
+ if tenant:
+ # use Active Directory auth
+ credential = AsyncClientSecretCredential(
+ tenant_id=tenant,
+ client_id=cast("str", conn.login),
+ client_secret=cast("str", conn.password),
+ proxies=extra.get("proxies", {}),
+ )
+ elif conn.password:
+ credential = conn.password
+ else:
+ credential = get_async_default_azure_credential(
+ managed_identity_client_id=self._get_field(extra,
"managed_identity_client_id"),
+ workload_identity_tenant_id=self._get_field(extra,
"workload_identity_tenant_id"),
+ )
Review Comment:
Thanks for the review! Good catch, that branch had no test. Added
`test_aload_bytes_with_the_default_credential` in 23034af3197: it checks that
`managed_identity_client_id` and `workload_identity_tenant_id` from the
connection extras reach `get_async_default_azure_credential`, that the client
is built with the resulting credential, and that the credential is closed after
the upload.
##########
providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_data_lake.py:
##########
@@ -318,6 +325,145 @@ def test_proxies_passed_to_credentials(self, mock_conn):
assert self.find_policy(conn, ProxyPolicy) is not None
assert self.find_policy(conn, ProxyPolicy).proxies["https"] ==
"https://proxy:80"
+ @mock.patch(f"{MODULE}.AzureDataLakeStorageV2Hook.get_file_system")
+ def test_load_bytes(self, mock_get_file_system):
+ mock_get_file_system.return_value =
mock.create_autospec(FileSystemClient, instance=True)
+ hook = AzureDataLakeStorageV2Hook(adls_conn_id=self.conn_id)
+
+ hook.load_bytes(self.file_system_name, "dir/file.jsonl", b"data",
overwrite=True)
+
+ mock_get_file_system.assert_called_once_with(self.file_system_name)
+ file_system = mock_get_file_system.return_value
+ file_system.get_file_client.assert_called_once_with("dir/file.jsonl")
+
file_system.get_file_client.return_value.upload_data.assert_called_once_with(b"data",
overwrite=True)
+
+ @staticmethod
+ def async_service_client():
+ """An async DataLakeServiceClient mock whose file client records the
upload."""
+ client = mock.create_autospec(AsyncDataLakeServiceClient,
instance=True)
+ client.__aenter__.return_value = client
+ file_system = mock.create_autospec(AsyncFileSystemClient,
instance=True)
+ file_system.get_file_client.return_value = mock.create_autospec(
+ AsyncDataLakeFileClient, instance=True
+ )
+ client.get_file_system_client.return_value = file_system
+ return client
+
+ async def run_aload_bytes(self, connection: Connection):
+ """Run aload_bytes() against ``connection``; return the client class
and credential class mocks."""
+ client = self.async_service_client()
+ client_cls = mock.create_autospec(AsyncDataLakeServiceClient,
return_value=client)
+ client_cls.from_connection_string.return_value = client
+ credential_cls = mock.create_autospec(AsyncClientSecretCredential)
+ client.credential = credential_cls.return_value
Review Comment:
You're right, the helper always handed the client a token credential, so the
no-`close` path never ran. As of 23034af3197 the mock client holds the
credential it was built with: the account key, the token credential, or `None`
after `from_connection_string`. The account-key and connection-string tests
assert that, and they fail when the `close` guard in `aload_bytes` is removed.
Thanks for spotting it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]