XMem icon indicating copy to clipboard operation
XMem copied to clipboard

Add durable control-plane storage

Open webhop123 opened this issue 6 months ago • 3 comments

Summary

  • add a Mongo-backed control-plane store for single-use MCP temp tokens, OAuth authorization codes, admin sessions, and shared rate-limit counters
  • move auth/admin/rate-limit paths off process-local dictionaries while keeping the development/test in-memory fallback
  • add coverage for single-use token consumption, admin sessions, shared rate-limit counters, and the rate-limit dependency path

Refs #161

Tests

  • .\.venv\Scripts\python -m ruff check src\database\control_plane_store.py src\api\routes\auth.py src\api\routes\admin.py src\api\dependencies.py tests\unit\test_control_plane_store.py tests\api\test_dependencies_and_routes.py
  • .\.venv\Scripts\python -m pytest tests\unit\test_control_plane_store.py tests\unit\test_database_stores.py tests\api\test_dependencies_and_routes.py -q
  • git diff --check

webhop123 avatar May 18 '26 12:05 webhop123

Addressed the review feedback in b9a5e1a:

  • moved the control-plane Mongo connection out of construction and made it lazy
  • moved rate-limit Mongo work off the event loop and replaced the read/modify/write path with a single find_one_and_update pipeline
  • removed the old in-process limiter and its test

Local checks:

  • ruff check src/database/control_plane_store.py src/api/dependencies.py tests/unit/test_control_plane_store.py tests/api/test_dependencies_and_routes.py
  • pytest tests/unit/test_control_plane_store.py tests/unit/test_database_stores.py tests/api/test_dependencies_and_routes.py -q

webhop123 avatar May 18 '26 12:05 webhop123

Addressed the requested items in c955b9b:

  • added async control-plane store wrappers and switched auth/admin routes to await them
  • added Mongo-path coverage for single-use tokens and the atomic rate-limit update/pruning logic
  • fixed the Bandit SARIF setup and guarded privileged Danger/staging jobs for fork PRs where write permissions/secrets are unavailable

Current checks: Bandit, dependency audit, tests, e2e, Docker, API diff, and label are passing. Danger/staging are skipped on this fork PR; staging can still run from a trusted same-repo branch.

webhop123 avatar May 18 '26 12:05 webhop123

Greptile Summary

This PR introduces ControlPlaneStore, a MongoDB-backed singleton that replaces all in-process dictionaries used for MCP temp tokens, OAuth auth codes, admin sessions, and per-user rate-limit counters. An in-memory fallback is retained for development and test, and production is hardened to refuse the fallback.

  • Single-use tokens and admin sessions are stored as hashed records in a control_plane_records collection with a TTL index; consumption is atomic via find_one_and_delete.
  • Rate limiting uses a MongoDB aggregation pipeline (find_one_and_update with upsert) that prunes, counts, and appends hits atomically; the sync wrapper catches MongoDB exceptions and fails open for the rate-limit path only.
  • _verify_admin_token is promoted to async def to await the store call, and all auth/admin helpers are converted to async accordingly.

Confidence Score: 4/5

Safe to merge with one area worth addressing: the token and session MongoDB paths have no exception handling for transient post-connect failures, unlike the rate-limit path.

The rate-limit path now wraps MongoDB calls and fails open, which was the main concern raised previously. However, consume_single_use_token, get_admin_session, and create_admin_session let PyMongo exceptions propagate directly. A brief MongoDB hiccup after startup would make all admin endpoints and token-exchange requests return HTTP 500 for its duration.

src/database/control_plane_store.py — specifically the synchronous token and session methods that invoke MongoDB without a try/except.

Important Files Changed

Filename Overview
src/database/control_plane_store.py New core module: MongoDB-backed store for single-use tokens, admin sessions, and rate-limit counters with in-memory fallback; atomic aggregation pipeline for rate limiting; _check_rate_limit_sync wraps MongoDB calls in try/except but token/session methods do not.
src/api/dependencies.py Removes in-process _SlidingWindowRateLimiter and replaces _rate_limiter.check() with control_plane_store.check_rate_limit(); straightforward delegation with no logic change.
src/api/routes/admin.py Replaces _admin_sessions dict with control_plane_store; _verify_admin_token converted to async; session creation, lookup, and deletion all delegate to the new store correctly.
src/api/routes/auth.py Replaces _mcp_temp_tokens and _oauth_auth_codes dicts with control_plane_store; all helper functions correctly converted to async and callers updated to await.
tests/unit/test_control_plane_store.py New unit tests covering single-use token consumption, admin sessions, rate-limit counters (memory and Mongo paths), MongoDB failure handling, and production-mode memory fallback refusal.
tests/api/test_dependencies_and_routes.py Old _SlidingWindowRateLimiter unit test replaced with an integration test for enforce_rate_limit via a FakeControlPlaneStore monkeypatch; correctly validates 200 then 429 behaviour.
.github/workflows/danger.yml Adds fork-gate condition to the Danger job, preventing it from running on external PRs where secrets would be unavailable.

Fix All in Cursor Fix All in Codex Fix All in Claude Code

Reviews (2): Last reviewed commit: "Merge main and harden control-plane rate..." | Re-trigger Greptile

greptile-apps avatar May 23 '26 09:05 greptile-apps