|
| 1 | +"""IBM AMS auth: JWT source switches from cookie to header under RBAC. |
| 2 | +
|
| 3 | +Covers the RBAC-gated token source in ``_get_ibm_user`` (``src/dependencies.py``): |
| 4 | +when JWT-role sync is enabled the end-user JWT is read from the gateway-forwarded |
| 5 | +header named by ``get_jwt_auth_header()``; when RBAC is off the existing |
| 6 | +``ibm-openrag-session`` cookie flow is preserved. ``decode_ibm_jwt`` is |
| 7 | +monkeypatched so no real token is needed. |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | +from types import SimpleNamespace |
| 13 | + |
| 14 | +import pytest |
| 15 | +from fastapi import HTTPException |
| 16 | + |
| 17 | +ROOT = Path(__file__).resolve().parent.parent.parent.parent |
| 18 | +SRC = ROOT / "src" |
| 19 | +if str(SRC) not in sys.path: |
| 20 | + sys.path.insert(0, str(SRC)) |
| 21 | + |
| 22 | +import auth.ibm_auth as ibm_auth # noqa: E402 |
| 23 | +import config.settings as app_settings # noqa: E402 |
| 24 | +from dependencies import _get_ibm_user # noqa: E402 |
| 25 | + |
| 26 | +COOKIE_NAME = "ibm-openrag-session" |
| 27 | + |
| 28 | + |
| 29 | +class _FakeRequest: |
| 30 | + """Minimal stand-in for starlette Request used by ``_get_ibm_user``.""" |
| 31 | + |
| 32 | + def __init__(self, headers: dict | None = None, cookies: dict | None = None): |
| 33 | + self.headers = headers or {} |
| 34 | + self.cookies = cookies or {} |
| 35 | + self.state = SimpleNamespace() |
| 36 | + # Empty services -> the connector lookup in Option 1 no-ops. |
| 37 | + self.app = SimpleNamespace(state=SimpleNamespace(services={})) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(autouse=True) |
| 41 | +def _env(monkeypatch): |
| 42 | + monkeypatch.setenv("OPENRAG_JWT_ROLES_CLAIM", "openrag_roles") |
| 43 | + monkeypatch.setenv("OPENRAG_ROLE_CLAIM_ADMIN", "admin") |
| 44 | + monkeypatch.setenv("OPENRAG_ROLE_CLAIM_DEVELOPER", "manager") |
| 45 | + monkeypatch.setenv("OPENRAG_ROLE_CLAIM_USER", "user") |
| 46 | + monkeypatch.delenv("OPENRAG_ROLE_CLAIM_VIEWER", raising=False) |
| 47 | + monkeypatch.setenv("OPENRAG_JWT_AUTH_HEADER", "X-OpenRAG-JWT") |
| 48 | + # Keep the Option -1 env override from short-circuiting the function. |
| 49 | + monkeypatch.setattr(app_settings, "PLATFORM_USERNAME", None) |
| 50 | + monkeypatch.setattr(app_settings, "PLATFORM_PASSWORD", None) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_rbac_on_reads_jwt_from_header(monkeypatch): |
| 55 | + monkeypatch.setenv("OPENRAG_RBAC_ENFORCE", "true") |
| 56 | + claims = {"sub": "s1", "username": "alice", "display_name": "Alice", "openrag_roles": ["admin"]} |
| 57 | + monkeypatch.setattr(ibm_auth, "decode_ibm_jwt", lambda tok: claims if tok == "tok" else None) |
| 58 | + # Cookie present too — it must be ignored when RBAC is on. |
| 59 | + req = _FakeRequest(headers={"X-OpenRAG-JWT": "Bearer tok"}, cookies={COOKIE_NAME: "COOKIE"}) |
| 60 | + |
| 61 | + user = await _get_ibm_user(req, required=True) |
| 62 | + |
| 63 | + assert user is not None |
| 64 | + assert user.user_id == "alice" |
| 65 | + assert user.name == "Alice" |
| 66 | + assert user.jwt_token == "Bearer tok" # built from the header token, not the cookie |
| 67 | + assert req.state.jwt_roles == ["admin"] |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_rbac_off_reads_jwt_from_cookie(monkeypatch): |
| 72 | + monkeypatch.setenv("OPENRAG_RBAC_ENFORCE", "false") |
| 73 | + claims = {"sub": "s2", "username": "bob"} |
| 74 | + monkeypatch.setattr(ibm_auth, "decode_ibm_jwt", lambda tok: claims if tok == "ctok" else None) |
| 75 | + # Header present too — it must be ignored when RBAC is off. |
| 76 | + req = _FakeRequest(headers={"X-OpenRAG-JWT": "Bearer HEADERTOK"}, cookies={COOKIE_NAME: "ctok"}) |
| 77 | + |
| 78 | + user = await _get_ibm_user(req, required=True) |
| 79 | + |
| 80 | + assert user is not None |
| 81 | + assert user.user_id == "bob" |
| 82 | + assert user.jwt_token == "Bearer ctok" |
| 83 | + assert req.state.jwt_roles is None # legacy default-role path under RBAC off |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +async def test_rbac_on_missing_header_401(monkeypatch): |
| 88 | + monkeypatch.setenv("OPENRAG_RBAC_ENFORCE", "true") |
| 89 | + |
| 90 | + def _boom(tok): # decode must not run when no header token is present |
| 91 | + raise AssertionError("decode_ibm_jwt should not be called without a header token") |
| 92 | + |
| 93 | + monkeypatch.setattr(ibm_auth, "decode_ibm_jwt", _boom) |
| 94 | + # Cookie present but ignored under RBAC -> no token -> unauthenticated. |
| 95 | + req = _FakeRequest(headers={}, cookies={COOKIE_NAME: "ctok"}) |
| 96 | + |
| 97 | + with pytest.raises(HTTPException) as exc: |
| 98 | + await _get_ibm_user(req, required=True) |
| 99 | + assert exc.value.status_code == 401 |
0 commit comments