yuseok89 commented on code in PR #77788:
URL: https://github.com/apache/airflow/pull/77788#discussion_r4756883403


##########
providers/samba/src/airflow/providers/samba/fs/_smb_filesystem.py:
##########
@@ -0,0 +1,161 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import datetime
+from stat import S_ISDIR
+from typing import Any
+
+import smbclient
+from fsspec import AbstractFileSystem
+from fsspec.implementations.smb import (

Review Comment:
   The class docstring now links fsspec/filesystem_spec#2006. I opened 
fsspec/filesystem_spec#2557 for the `copy` one and jborean93/smbprotocol#372 
for a `makedirs` loop on a missing share, and linked each next to its override. 
I left fsspec without an upper bound, as the provider guidelines suggest, so 
these links are what tell us when the overrides can go.



##########
providers/samba/src/airflow/providers/samba/fs/_smb_filesystem.py:
##########
@@ -0,0 +1,161 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import datetime
+from stat import S_ISDIR
+from typing import Any
+
+import smbclient
+from fsspec import AbstractFileSystem
+from fsspec.implementations.smb import (
+    SMBFileOpener,
+    SMBFileSystem,
+    _as_temp_path,
+    _as_unc_path,
+    _share_has_path,
+)
+
+from airflow.providers.samba.hooks.samba import _get_auth_protocol
+
+
+class _SMBFileOpener(SMBFileOpener):
+    """``SMBFileOpener`` that also sends the session arguments when it commits 
or discards the file."""
+
+    def __init__(self, *args: Any, session_kwargs: dict[str, Any], **kwargs: 
Any) -> None:
+        self.session_kwargs = session_kwargs
+        super().__init__(*args, **session_kwargs, **kwargs)
+
+    def commit(self) -> None:
+        smbclient.replace(self.temp, self.path, port=self.port, 
**self.session_kwargs)
+
+    def discard(self) -> None:
+        smbclient.remove(self.temp, port=self.port, **self.session_kwargs)
+
+
+class _IsolatedSMBFileSystem(SMBFileSystem):
+    """
+    ``SMBFileSystem`` that keeps its own SMB connection and sends its 
credentials with every call.
+
+    fsspec registers a session once in smbclient's process-wide pool and then 
calls smbclient without
+    credentials, so smbclient uses the first session it finds for the server. 
Two connections to the same
+    server would then both act as whichever user connected first, and once the 
server drops the
+    connection, smbclient reconnects without credentials and every call fails. 
Like ``SambaHook``, this
+    filesystem keeps a pool of its own and passes the credentials on every 
call, which opens the session
+    on first use and again after the server drops it.
+    """
+
+    def __init__(self, *args: Any, auth_protocol: str = "negotiate", **kwargs: 
Any) -> None:
+        self.auth_protocol = _get_auth_protocol({}, auth_protocol)
+        self._connection_cache: dict[str, Any] = {}
+        super().__init__(*args, **kwargs)
+
+    @property
+    def _session_kwargs(self) -> dict[str, Any]:
+        return {
+            "username": self.username,
+            "password": self.password,
+            "encrypt": self.encrypt,
+            "connection_timeout": self.timeout,
+            "connection_cache": self._connection_cache,
+            "auth_protocol": self.auth_protocol,
+        }
+
+    def _connect(self) -> None:
+        """Do not register a session up front; each call opens it in this 
filesystem's own pool."""

Review Comment:
   Good point. `exists`, `isdir` and `isfile` now return False only for a 
missing path or share and raise anything else, so a wrong password or a share 
the user may not open fails right away. A missing share comes back as ENOENT on 
Samba and as STATUS_BAD_NETWORK_NAME on Azure Files, so both count as missing. 
This relies on smbprotocol 1.17.0, which reports a missing share as an OSError, 
so I raised the floor to 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]

Reply via email to