|
| 1 | +"""Tests for OpenFGA-based custom MCP tool authorization in the RAG server. |
| 2 | +
|
| 3 | +Covers `authorize_mcp_tool_create` / `authorize_mcp_tool_manage`, which replaced |
| 4 | +the legacy coarse `require_role(Role.ADMIN)` gate on the |
| 5 | +POST/PUT/DELETE /v1/mcp/custom-tools endpoints (spec |
| 6 | +2026-06-03-unified-shareable-resource-rbac). Human callers are READONLY at the |
| 7 | +coarse layer, so authorization is resolved through OpenFGA on the `mcp_tool` |
| 8 | +and `team` types. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import pytest |
| 14 | +from fastapi import HTTPException |
| 15 | + |
| 16 | +from common.models.rbac import Role, UserContext |
| 17 | +from server import rbac |
| 18 | + |
| 19 | + |
| 20 | +def _user(subject: str | None = "alice-sub", role: str = Role.READONLY) -> UserContext: |
| 21 | + return UserContext( |
| 22 | + subject=subject, |
| 23 | + email="alice@example.com", |
| 24 | + role=role, |
| 25 | + is_authenticated=True, |
| 26 | + groups=[], |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(autouse=True) |
| 31 | +def _openfga_configured(monkeypatch: pytest.MonkeyPatch) -> None: |
| 32 | + monkeypatch.setenv("OPENFGA_HTTP", "http://openfga") |
| 33 | + monkeypatch.setenv("CAIPE_ORG_KEY", "caipe") |
| 34 | + |
| 35 | + async def _no_org_admin(user: UserContext) -> bool: |
| 36 | + return False |
| 37 | + |
| 38 | + async def _deny_all(user: UserContext, relation: str, object_type: str, object_id: str) -> bool: |
| 39 | + return False |
| 40 | + |
| 41 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _no_org_admin, raising=False) |
| 42 | + monkeypatch.setattr(rbac, "_openfga_check_object", _deny_all, raising=False) |
| 43 | + |
| 44 | + |
| 45 | +# --------------------------------------------------------------------------- |
| 46 | +# authorize_mcp_tool_manage (update / delete) |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_manage_allows_coarse_admin_without_pdp(monkeypatch: pytest.MonkeyPatch) -> None: |
| 52 | + async def _explode(*_args, **_kwargs): # pragma: no cover - must not run |
| 53 | + raise AssertionError("coarse ADMIN must not hit the PDP") |
| 54 | + |
| 55 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _explode, raising=False) |
| 56 | + monkeypatch.setattr(rbac, "_openfga_check_object", _explode, raising=False) |
| 57 | + |
| 58 | + await rbac.authorize_mcp_tool_manage(_user(role=Role.ADMIN), "tool-x") |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_manage_allows_org_admin(monkeypatch: pytest.MonkeyPatch) -> None: |
| 63 | + async def _org_admin(user: UserContext) -> bool: |
| 64 | + return True |
| 65 | + |
| 66 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _org_admin, raising=False) |
| 67 | + |
| 68 | + await rbac.authorize_mcp_tool_manage(_user(), "tool-x") |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_manage_allows_can_manage(monkeypatch: pytest.MonkeyPatch) -> None: |
| 73 | + calls: list[tuple[str, str, str]] = [] |
| 74 | + |
| 75 | + async def _check(user: UserContext, relation: str, object_type: str, object_id: str) -> bool: |
| 76 | + calls.append((relation, object_type, object_id)) |
| 77 | + return relation == "can_manage" and object_type == "mcp_tool" and object_id == "tool-x" |
| 78 | + |
| 79 | + monkeypatch.setattr(rbac, "_openfga_check_object", _check, raising=False) |
| 80 | + |
| 81 | + await rbac.authorize_mcp_tool_manage(_user(), "tool-x") |
| 82 | + assert ("can_manage", "mcp_tool", "tool-x") in calls |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_manage_denies_when_not_authorized() -> None: |
| 87 | + with pytest.raises(HTTPException) as exc: |
| 88 | + await rbac.authorize_mcp_tool_manage(_user(), "tool-x") |
| 89 | + assert exc.value.status_code == 403 |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_manage_fails_closed_on_pdp_error(monkeypatch: pytest.MonkeyPatch) -> None: |
| 94 | + async def _boom(user: UserContext) -> bool: |
| 95 | + raise RuntimeError("openfga down") |
| 96 | + |
| 97 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _boom, raising=False) |
| 98 | + |
| 99 | + with pytest.raises(HTTPException) as exc: |
| 100 | + await rbac.authorize_mcp_tool_manage(_user(), "tool-x") |
| 101 | + assert exc.value.status_code == 503 |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_manage_fails_closed_when_pdp_not_configured(monkeypatch: pytest.MonkeyPatch) -> None: |
| 106 | + monkeypatch.delenv("OPENFGA_HTTP", raising=False) |
| 107 | + with pytest.raises(HTTPException) as exc: |
| 108 | + await rbac.authorize_mcp_tool_manage(_user(), "tool-x") |
| 109 | + assert exc.value.status_code == 503 |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.asyncio |
| 113 | +async def test_manage_fails_closed_without_stable_subject() -> None: |
| 114 | + with pytest.raises(HTTPException) as exc: |
| 115 | + await rbac.authorize_mcp_tool_manage(_user(subject=None), "tool-x") |
| 116 | + assert exc.value.status_code == 503 |
| 117 | + |
| 118 | + |
| 119 | +# --------------------------------------------------------------------------- |
| 120 | +# authorize_mcp_tool_create |
| 121 | +# --------------------------------------------------------------------------- |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.asyncio |
| 125 | +async def test_create_allows_coarse_admin_without_pdp(monkeypatch: pytest.MonkeyPatch) -> None: |
| 126 | + async def _explode(*_args, **_kwargs): # pragma: no cover - must not run |
| 127 | + raise AssertionError("coarse ADMIN must not hit the PDP") |
| 128 | + |
| 129 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _explode, raising=False) |
| 130 | + monkeypatch.setattr(rbac, "_openfga_check_object", _explode, raising=False) |
| 131 | + |
| 132 | + await rbac.authorize_mcp_tool_create(_user(role=Role.ADMIN), "eti-sre-admins") |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.asyncio |
| 136 | +async def test_create_allows_org_admin(monkeypatch: pytest.MonkeyPatch) -> None: |
| 137 | + async def _org_admin(user: UserContext) -> bool: |
| 138 | + return True |
| 139 | + |
| 140 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _org_admin, raising=False) |
| 141 | + |
| 142 | + await rbac.authorize_mcp_tool_create(_user(), None) |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.asyncio |
| 146 | +async def test_create_allows_owner_team_member(monkeypatch: pytest.MonkeyPatch) -> None: |
| 147 | + calls: list[tuple[str, str, str]] = [] |
| 148 | + |
| 149 | + async def _check(user: UserContext, relation: str, object_type: str, object_id: str) -> bool: |
| 150 | + calls.append((relation, object_type, object_id)) |
| 151 | + return relation == "can_use" and object_type == "team" and object_id == "eti-sre-admins" |
| 152 | + |
| 153 | + monkeypatch.setattr(rbac, "_openfga_check_object", _check, raising=False) |
| 154 | + |
| 155 | + await rbac.authorize_mcp_tool_create(_user(), " eti-sre-admins ") |
| 156 | + assert ("can_use", "team", "eti-sre-admins") in calls |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.asyncio |
| 160 | +async def test_create_denies_non_member() -> None: |
| 161 | + with pytest.raises(HTTPException) as exc: |
| 162 | + await rbac.authorize_mcp_tool_create(_user(), "eti-sre-admins") |
| 163 | + assert exc.value.status_code == 403 |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.asyncio |
| 167 | +async def test_create_denies_when_no_owner_team_and_not_org_admin() -> None: |
| 168 | + with pytest.raises(HTTPException) as exc: |
| 169 | + await rbac.authorize_mcp_tool_create(_user(), None) |
| 170 | + assert exc.value.status_code == 403 |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.asyncio |
| 174 | +async def test_create_fails_closed_on_pdp_error(monkeypatch: pytest.MonkeyPatch) -> None: |
| 175 | + async def _boom(user: UserContext) -> bool: |
| 176 | + raise RuntimeError("openfga down") |
| 177 | + |
| 178 | + monkeypatch.setattr(rbac, "_openfga_check_org_admin", _boom, raising=False) |
| 179 | + |
| 180 | + with pytest.raises(HTTPException) as exc: |
| 181 | + await rbac.authorize_mcp_tool_create(_user(), "eti-sre-admins") |
| 182 | + assert exc.value.status_code == 503 |
0 commit comments