RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(storage): correct Azure SPN object existence checks (#20003)
Wenchao An11146f4a1fa9
diff --git a/rag/utils/azure_spn_conn.py b/rag/utils/azure_spn_conn.pyindex 85148e72cf..bc197dc8ba 100644--- a/rag/utils/azure_spn_conn.py+++ b/rag/utils/azure_spn_conn.py@@ -104,9 +104,10 @@ class RAGFlowAzureSpnBlob:return Nonedef obj_exist(self, bucket, fnm, tenant_id=None):+ """Check the bucket-prefixed file, logging lookup errors as False."""blob = f"{bucket}/{fnm}"try:- client = self.conn.get_blob_client(f"{blob}")+ client = self.conn.get_file_client(f"{blob}")return client.exists()except Exception:logging.exception(f"Fail obj_exist {blob}")diff --git a/test/unit_test/rag/utils/test_azure_encrypted_storage.py b/test/unit_test/rag/utils/test_azure_encrypted_storage.pyindex 5b4d1fa83b..908794a5a8 100644--- a/test/unit_test/rag/utils/test_azure_encrypted_storage.py+++ b/test/unit_test/rag/utils/test_azure_encrypted_storage.py@@ -78,16 +78,14 @@ def test_encrypted_rm_deletes_same_object(storage, tenant_id):@pytest.mark.parametrize("tenant_id", [None, "tenant-a"])@pytest.mark.parametrize("exists", [False, True])def test_encrypted_obj_exist_preserves_backend_result(storage, tenant_id, exists):+ """The wrapper returns the backend's existence result for either tenant form."""adapter, name = storageif name == "azure_sas_conn":adapter.conn.get_blob_client.return_value.exists.return_value = existselse:adapter.conn.get_file_client.return_value.exists.return_value = exists- # The separate SPN get_blob_client defect remains out of scope: the real- # FileSystemClient spec rejects it, and the adapter currently returns False.- expected = adapter.obj_exist("kb", "file.txt")wrapper = EncryptedStorageWrapper(adapter, key="test-key")- assert wrapper.obj_exist("kb", "file.txt", tenant_id) is expected+ assert wrapper.obj_exist("kb", "file.txt", tenant_id) is existsdef test_direct_get_and_rm_remain_callable_without_tenant(storage):diff --git a/test/unit_test/rag/utils/test_azure_spn_exists.py b/test/unit_test/rag/utils/test_azure_spn_exists.pynew file mode 100644index 0000000000..50d34ae3bf--- /dev/null+++ b/test/unit_test/rag/utils/test_azure_spn_exists.py@@ -0,0 +1,108 @@+#+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed 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.+#++"""Exercise SPN existence checks with the real Data Lake SDK and a local peer."""++import importlib.util+import sys+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from pathlib import Path+from threading import Thread+from types import ModuleType+from urllib.parse import unquote, urlsplit++import pytest+from azure.storage.filedatalake import FileSystemClient++import common++pytestmark = pytest.mark.p2+++def file_peer():+ """Serve only HEAD requests, including missing and inaccessible objects."""+ paths = {"/container/kb/file.txt", "/container/kb/folder/space name.txt"}+ requests = []++ class Handler(BaseHTTPRequestHandler):+ """Return deterministic Data Lake existence responses for the SDK."""++ def log_message(self, *args):+ """Suppress the HTTP server's default stderr logging."""++ def do_HEAD(self):+ """Record the decoded file path and return its configured HTTP status."""+ path = unquote(urlsplit(self.path).path)+ requests.append(path)+ status = 200 if path in paths else 404+ error = "PathNotFound"+ if path == "/container/kb/denied.txt":+ status, error = 403, "AuthorizationPermissionMismatch"+ self.send_response(status)+ self.send_header("Content-Length", "0")+ self.send_header("x-ms-request-id", "local-test")+ if status != 200:+ self.send_header("x-ms-error-code", error)+ self.end_headers()++ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ thread = Thread(target=server.serve_forever, daemon=True)+ thread.start()+ try:+ yield f"http://127.0.0.1:{server.server_port}", requests+ finally:+ server.shutdown()+ server.server_close()+ thread.join()+++def storage(file_peer, monkeypatch):+ """Isolate service config and singleton setup; retain the real SDK methods."""+ settings = ModuleType("common.settings")+ monkeypatch.setitem(sys.modules, "common.settings", settings)+ monkeypatch.setattr(common, "settings", settings, raising=False)+ path = Path(__file__).resolve().parents[4] / "rag/utils/azure_spn_conn.py"+ spec = importlib.util.spec_from_file_location("test_azure_spn_exists_adapter", path)+ module = importlib.util.module_from_spec(spec)+ with monkeypatch.context() as load_patch:+ load_patch.setattr("common.decorator.singleton", lambda cls: cls)+ spec.loader.exec_module(module)+ adapter = module.RAGFlowAzureSpnBlob.__new__(module.RAGFlowAzureSpnBlob)+ adapter.conn = FileSystemClient(file_peer[0], "container", credential=None, retry_total=0, connection_timeout=2, read_timeout=2)+ try:+ yield adapter+ finally:+ adapter.conn.close()++[email protected]("tenant_id", [None, "tenant-a"])+ "bucket,filename,exists",+ [("kb", "file.txt", True), ("kb", "folder/space name.txt", True), ("kb", "missing.txt", False), ("other-kb", "file.txt", False)],+)+def test_obj_exist_checks_the_requested_file(storage, file_peer, bucket, filename, exists, tenant_id):+ """Existing, absent and same-name files in another bucket retain their status."""+ assert storage.obj_exist(bucket, filename, tenant_id) is exists+ assert file_peer[1] == [f"/container/{bucket}/{filename}"]+++def test_obj_exist_logs_service_errors(storage, file_peer, caplog):+ """The adapter still logs a failed request and returns False."""+ assert storage.obj_exist("kb", "denied.txt") is False+ assert file_peer[1] == ["/container/kb/denied.txt"]+ assert "Fail obj_exist kb/denied.txt" in caplog.text