Skip to content

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

Python

[WIP] Per-user OpenSearch client & IBM auth checks, Adds new creds for IBM Users

Edwin Jose001030a1e55b
diff --git a/src/api/settings.py b/src/api/settings.py
index a23b1a7c7..9332b8c18 100644
--- a/src/api/settings.py
+++ b/src/api/settings.py
@@ -1122,13 +1122,17 @@ async def onboarding(
# Initialize the OpenSearch index if embedding model is configured
if body.embedding_model or body.embedding_provider:
try:
- # Import here to avoid circular imports
from main import init_index_when_ready
+ from config.settings import IBM_AUTH_ENABLED, clients as app_clients
+
+ opensearch_client = None
+ if IBM_AUTH_ENABLED and user and user.jwt_token:
+ opensearch_client = app_clients.create_user_opensearch_client(user.jwt_token)
logger.info(
"Initializing OpenSearch index after onboarding configuration"
)
- await init_index_when_ready()
+ await init_index_when_ready(opensearch_client)
logger.info("OpenSearch index initialization completed successfully")
except Exception as e:
logger.error(
diff --git a/src/dependencies.py b/src/dependencies.py
index 8253ab4aa..defaacdc6 100644
--- a/src/dependencies.py
+++ b/src/dependencies.py
@@ -14,6 +14,7 @@ Usage:
):
...
"""
+
import dataclasses
from typing import Optional
@@ -29,6 +30,7 @@ logger = get_logger(__name__)
# Service dependencies
# ─────────────────────────────────────────────
+
def get_services(request: Request) -> dict:
return request.app.state.services
@@ -89,6 +91,7 @@ def get_flows_service(services: dict = Depends(get_services)):
# IBM AMS authentication helper
# ─────────────────────────────────────────────
+
async def _get_ibm_user(request: Request, required: bool) -> Optional["User"]:
"""Authenticate via IBM AMS.
@@ -109,6 +112,13 @@ async def _get_ibm_user(request: Request, required: bool) -> Optional["User"]:
# ── Option 0: Configurable credentials header (Traefik production) ───
lh_credentials = request.headers.get(IBM_CREDENTIALS_HEADER, "")
+ if not lh_credentials:
+ logger.warning("IBM credentials header not found in request headers")
+ raise HTTPException(status_code=401, detail="IBM authentication required")
+ if not lh_credentials.startswith("Basic "):
+ logger.warning("IBM credentials header is not a Basic credential")
+ raise HTTPException(status_code=401, detail="IBM authentication required")
+
ibm_token = request.cookies.get(IBM_SESSION_COOKIE_NAME)
user_id = None
email = None
@@ -201,6 +211,7 @@ async def _get_ibm_user(request: Request, required: bool) -> Optional["User"]:
# Authentication dependencies
# ─────────────────────────────────────────────
+
async def get_current_user(
request: Request,
session_manager=Depends(get_session_manager),
@@ -272,8 +283,14 @@ async def get_optional_user(
user = session_manager.get_user_from_token(auth_token)
# get_effective_jwt_token handles anonymous JWT creation if needed
- effective_token = session_manager.get_effective_jwt_token(user.user_id, auth_token) if user else None
- user_with_token = dataclasses.replace(user, jwt_token=effective_token) if user else None
+ effective_token = (
+ session_manager.get_effective_jwt_token(user.user_id, auth_token)
+ if user
+ else None
+ )
+ user_with_token = (
+ dataclasses.replace(user, jwt_token=effective_token) if user else None
+ )
request.state.user = user_with_token
return user_with_token
diff --git a/src/main.py b/src/main.py
index c2572fa9c..a68510721 100644
--- a/src/main.py
+++ b/src/main.py
@@ -138,7 +138,7 @@ class _VisibleTextHTMLParser(HTMLParser):
return " ".join(self._chunks)
-async def wait_for_opensearch():
+async def wait_for_opensearch(opensearch_client=None):
"""Wait for OpenSearch to be ready, delegating to the shared utility."""
from utils.opensearch_utils import (
wait_for_opensearch as _wait_for_opensearch,
@@ -146,7 +146,7 @@ async def wait_for_opensearch():
)
try:
- await _wait_for_opensearch(clients.opensearch)
+ await _wait_for_opensearch(opensearch_client or clients.opensearch)
await TelemetryClient.send_event(
Category.OPENSEARCH_SETUP, MessageId.ORB_OS_CONN_ESTABLISHED
)
@@ -215,10 +215,11 @@ async def _ensure_opensearch_index():
# The service can still function, document operations might fail later
-async def init_index():
+async def init_index(opensearch_client=None):
"""Initialize OpenSearch index and security roles"""
+ os_client = opensearch_client or clients.opensearch
try:
- await wait_for_opensearch()
+ await wait_for_opensearch(opensearch_client)
# Get the configured embedding model from user configuration
config = get_openrag_config()
@@ -236,8 +237,8 @@ async def init_index():
# Create documents index
index_name = get_index_name()
- if not await clients.opensearch.indices.exists(index=index_name):
- await clients.opensearch.indices.create(
+ if not await os_client.indices.exists(index=index_name):
+ await os_client.indices.create(
index=index_name, body=dynamic_index_body
)
logger.info(
@@ -277,10 +278,10 @@ async def init_index():
}
}
- if not await clients.opensearch.indices.exists(
+ if not await os_client.indices.exists(
index=knowledge_filter_index_name
):
- await clients.opensearch.indices.create(
+ await os_client.indices.create(
index=knowledge_filter_index_name, body=knowledge_filter_index_body
)
logger.info(
@@ -297,8 +298,8 @@ async def init_index():
)
# Create API keys index for public API authentication
- if not await clients.opensearch.indices.exists(index=API_KEYS_INDEX_NAME):
- await clients.opensearch.indices.create(
+ if not await os_client.indices.exists(index=API_KEYS_INDEX_NAME):
+ await os_client.indices.create(
index=API_KEYS_INDEX_NAME, body=API_KEYS_INDEX_BODY
)
logger.info("Created API keys index", index_name=API_KEYS_INDEX_NAME)
@@ -308,7 +309,7 @@ async def init_index():
index_name=API_KEYS_INDEX_NAME,
)
- # Configure alerting plugin security settings
+ # Configure alerting plugin security settings (admin-level, always uses global client)
await configure_alerting_security()
except Exception as e:
@@ -324,10 +325,10 @@ async def init_index():
raise e
-async def init_index_when_ready():
+async def init_index_when_ready(opensearch_client=None):
"""Wait for the OpenSearch service to be ready and then initialize the OpenSearch index."""
- await wait_for_opensearch()
- await init_index()
+ await wait_for_opensearch(opensearch_client)
+ await init_index(opensearch_client)
def generate_jwt_keys():