Skip to content

fix: add IBM user to the session#1300

Merged
edwinjosechittilappilly merged 3 commits intorelease-saas-0.1from
fix-owner_name-ingest
Mar 31, 2026
Merged

fix: add IBM user to the session#1300
edwinjosechittilappilly merged 3 commits intorelease-saas-0.1from
fix-owner_name-ingest

Conversation

@edwinjosechittilappilly
Copy link
Copy Markdown
Collaborator

This pull request updates the user authentication flow to ensure that users authenticated via IBM AMS cookie are properly tracked in the session_manager. Now, after a user is authenticated with IBM AMS, the user is added to the session_manager.users dictionary if they are not already present.

Session management improvements:

  • After authenticating a user with IBM AMS cookie in get_current_user, the user is added to session_manager.users if not already tracked.
  • Similarly, in get_optional_user, the user is added to session_manager.users if they have a user_id and are not already present.

@github-actions github-actions bot added the backend 🔷 Issues related to backend services (OpenSearch, Langflow, APIs) label Mar 30, 2026
@github-actions github-actions bot added the bug 🔴 Something isn't working. label Mar 30, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the FastAPI authentication dependencies so IBM AMS cookie-authenticated users are persisted in the in-memory SessionManager.users registry, aligning IBM auth behavior with other auth modes that rely on session_manager lookups.

Changes:

  • Store IBM-authenticated users in session_manager.users during get_current_user.
  • Store IBM-authenticated users in session_manager.users during get_optional_user.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

logger.debug("[IBM Auth] IBM auth mode enabled, getting current user")
return await _get_ibm_user(request, required=True)
user = await _get_ibm_user(request, required=True)
if user and user.user_id not in session_manager.users:
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the IBM auth branch, _get_ibm_user(required=True) can still return None when an IBM session cookie is present but the JWT is missing the required sub claim (see _get_ibm_user which logs a warning and returns None). get_current_user would then return None despite being a required-auth dependency, potentially letting requests proceed unauthenticated or causing downstream AttributeErrors. Consider raising HTTPException(401, ...) here when user is falsy (or when user.user_id is missing).

Suggested change
if user and user.user_id not in session_manager.users:
# `_get_ibm_user` may still return None (for example, when JWT is missing
# a required claim). In required-auth contexts, treat this as unauthenticated.
if not user or not getattr(user, "user_id", None):
logger.warning(
"[IBM Auth] Failed to obtain authenticated user from IBM session; "
"returning HTTP 401"
)
raise HTTPException(status_code=401, detail="Authentication required")
if user.user_id not in session_manager.users:

Copilot uses AI. Check for mistakes.
logger.debug("[IBM Auth] IBM auth mode enabled, getting optional user")
return await _get_ibm_user(request, required=False)
user = await _get_ibm_user(request, required=False)
if user and user.user_id and user.user_id not in session_manager.users:
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_ibm_user constructs a fresh User each request and SessionManager.get_user_opensearch_client notes IBM credentials may rotate per-request. With the current if ... not in session_manager.users guard, the cached session_manager.users[user_id] entry will never be refreshed, so any downstream code that reads users from the session manager can see stale jwt_token/credentials or last_login. Consider updating/replacing the existing entry (or at least refreshing the rotating fields) on every successful IBM-authenticated request.

Suggested change
if user and user.user_id and user.user_id not in session_manager.users:
if user and user.user_id:
# Always refresh cached IBM user to keep rotating credentials/jwt/last_login current

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot added bug 🔴 Something isn't working. and removed bug 🔴 Something isn't working. labels Mar 30, 2026
Copy link
Copy Markdown
Collaborator

@mpawlow mpawlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review 1

  • See PR comments: (a) to (c)

logger.debug("[IBM Auth] IBM auth mode enabled, getting current user")
return await _get_ibm_user(request, required=True)
user = await _get_ibm_user(request, required=True)
if user and user.user_id not in session_manager.users:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(b) [Normal] Stale IBM credentials never refreshed in session cache

Problem

  • IBM credentials can rotate on every request
  • Traefik injects fresh X-IBM-LH-Credentials on each forwarded call.
  • The new code only populates session_manager.users when the user_id is not already present
  • Once a user is cached, subsequent requests with rotated credentials do not update the stored User object.
  • Any component that calls session_manager.get_user(user_id) will receive the original (potentially stale) User instance
  • For OpenSearch client construction, IBM mode already bypasses the cache (if IBM_AUTH_ENABLED: return clients.create_user_opensearch_client(jwt_token)) so that specific path is safe.
  • But the pattern is fragile and any new call-site that reads credentials from the cached user would silently use stale data.
  • Also affects: src/dependencies.py:301–303 (get_optional_user)

Solution

  • Always overwrite the entry, regardless of whether it already exists. IBM users have no concept of a "login session" — each request is the authoritative source of truth:
if user and user.user_id:
  session_manager.users[user.user_id] = user

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is done by the FE and the IBM APi key is stored locally in json

logger.debug("[IBM Auth] IBM auth mode enabled, getting current user")
return await _get_ibm_user(request, required=True)
user = await _get_ibm_user(request, required=True)
if user and user.user_id not in session_manager.users:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(c) [Minor] Unbounded growth of session_manager.users in IBM multi-user deployments

  • This is Minor severity. Please feel free to ignore or optionally implement

Problem

  • session_manager.users is an in-memory plain dict with no eviction policy.
  • For IBM deployments with many unique users, this dict will grow indefinitely for the lifetime of the process.
  • Note: This is a pre-existing structural issue; however, this PR extends the pattern to IBM auth, which is the primary auth mode in multi-tenant IBM deployments where user churn may be higher.
  • Also affects src/dependencies.py:301–303

Solution

  • Introduce a bounded LRU cache or TTL-based eviction in SessionManager
    • e.g., use cachetools.TTLCache or cachetools.LRUCache as a drop-in replacement for the users dict.

Add a truthiness check for user.user_id in get_current_user's IBM auth path before inserting into session_manager.users. This prevents adding users with a None/empty user_id as a dict key, avoiding potential errors or incorrect session map entries when IBM auth is enabled.
@github-actions github-actions bot added bug 🔴 Something isn't working. and removed bug 🔴 Something isn't working. labels Mar 31, 2026
Copy link
Copy Markdown
Collaborator

@mpawlow mpawlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review 2

  • ✅ LGTM / Approved

@github-actions github-actions bot added the lgtm label Mar 31, 2026
@edwinjosechittilappilly edwinjosechittilappilly merged commit c89fa17 into release-saas-0.1 Mar 31, 2026
8 checks passed
@edwinjosechittilappilly edwinjosechittilappilly deleted the fix-owner_name-ingest branch March 31, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend 🔷 Issues related to backend services (OpenSearch, Langflow, APIs) bug 🔴 Something isn't working. lgtm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants