Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: return 5xx instead of unhandled error when index-admin service token is missing (#2382)

Nathan Bakaree238025e81ce
diff --git a/src/api/upload.py b/src/api/upload.py
index cb2590e4a..04c0415a8 100644
--- a/src/api/upload.py
+++ b/src/api/upload.py
@@ -94,7 +94,11 @@ async def upload_path(
from api.documents import _ensure_index_exists
- await _ensure_index_exists(jwt_token)
+ try:
+ await _ensure_index_exists(jwt_token)
+ except RuntimeError:
+ logger.exception("[INGEST] Index preflight check failed for upload_path")
+ return JSONResponse({"error": "Index preflight check failed"}, status_code=500)
task_id = await task_service.create_upload_task(
owner_user_id,
@@ -221,7 +225,11 @@ async def upload_bucket(
from api.documents import _ensure_index_exists
- await _ensure_index_exists(jwt_token)
+ try:
+ await _ensure_index_exists(jwt_token)
+ except RuntimeError:
+ logger.exception("[INGEST] Index preflight check failed for upload_bucket")
+ return JSONResponse({"error": "Index preflight check failed"}, status_code=500)
processor = S3FileProcessor(
task_service.document_service,
diff --git a/tests/unit/api/test_upload_index_preflight.py b/tests/unit/api/test_upload_index_preflight.py
new file mode 100644
index 000000000..9c6347500
--- /dev/null
+++ b/tests/unit/api/test_upload_index_preflight.py
@@ -0,0 +1,111 @@
+"""Regression tests for GitHub issue #2138.
+
+_ensure_index_exists() raises RuntimeError when OPENRAG_SERVICE_TOKEN is
+missing in saas/on_prem mode. upload_path and upload_bucket must turn that
+into a controlled 5xx JSONResponse instead of an unhandled exception, and
+must not create a task/processor when the check fails.
+"""
+
+import json
+import sys
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+ROOT = Path(__file__).resolve().parent.parent.parent.parent
+SRC = ROOT / "src"
+if str(SRC) not in sys.path:
+ sys.path.insert(0, str(SRC))
+
+
+def _json(response):
+ return json.loads(response.body.decode())
+
+
+def _make_user():
+ return SimpleNamespace(
+ user_id="alice", name="Alice", email="[email protected]", jwt_token="token"
+ )
+
+
+async def test_upload_path_returns_5xx_and_skips_task_when_index_check_fails(monkeypatch, tmp_path):
+ from api import documents as documents_api
+ from api import upload as upload_api
+
+ (tmp_path / "doc.txt").write_text("hello")
+
+ monkeypatch.setattr(
+ documents_api,
+ "_ensure_index_exists",
+ AsyncMock(
+ side_effect=RuntimeError(
+ "OPENRAG_SERVICE_TOKEN is required for the index-admin OpenSearch client in saas mode."
+ )
+ ),
+ )
+
+ task_service = MagicMock()
+ task_service.create_upload_task = AsyncMock(return_value="task-should-not-run")
+
+ response = await upload_api.upload_path(
+ upload_api.UploadPathBody(path=str(tmp_path)),
+ task_service=task_service,
+ session_manager=MagicMock(),
+ user=_make_user(),
+ )
+
+ assert 500 <= response.status_code < 600
+ body = _json(response)
+ assert body["error"] == "Index preflight check failed"
+ # The internal RuntimeError detail must not leak to the client
+ # (CodeQL py/stack-trace-exposure).
+ assert "OPENRAG_SERVICE_TOKEN" not in body["error"]
+ task_service.create_upload_task.assert_not_awaited()
+
+
+async def test_upload_bucket_returns_5xx_and_skips_task_when_index_check_fails(monkeypatch):
+ from api import documents as documents_api
+ from api import upload as upload_api
+
+ monkeypatch.setenv("AWS_ACCESS_KEY_ID", "fake-key")
+ monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "fake-secret")
+
+ monkeypatch.setattr(
+ documents_api,
+ "_ensure_index_exists",
+ AsyncMock(
+ side_effect=RuntimeError(
+ "OPENRAG_SERVICE_TOKEN is required for the index-admin OpenSearch client in saas mode."
+ )
+ ),
+ )
+
+ fake_paginator = MagicMock()
+ fake_paginator.paginate.return_value = [{"Contents": [{"Key": "file1.txt"}]}]
+ fake_s3_client = MagicMock()
+ fake_s3_client.get_paginator.return_value = fake_paginator
+ monkeypatch.setattr(upload_api.boto3, "client", MagicMock(return_value=fake_s3_client))
+
+ task_service = MagicMock()
+ task_service.create_custom_task = AsyncMock(return_value="task-should-not-run")
+
+ response = await upload_api.upload_bucket(
+ upload_api.UploadBucketBody(s3_url="s3://my-bucket/prefix"),
+ task_service=task_service,
+ models_service=MagicMock(),
+ docling_service=MagicMock(),
+ session_manager=MagicMock(),
+ user=_make_user(),
+ )
+
+ assert 500 <= response.status_code < 600
+ body = _json(response)
+ assert body["error"] == "Index preflight check failed"
+ # The internal RuntimeError detail must not leak to the client
+ # (CodeQL py/stack-trace-exposure).
+ assert "OPENRAG_SERVICE_TOKEN" not in body["error"]
+ task_service.create_custom_task.assert_not_awaited()