fix(auth): eliminate duplicate DB sessions in auth and RBAC middleware#3886
Open
MohanLaksh wants to merge 1 commit intomainfrom
Open
fix(auth): eliminate duplicate DB sessions in auth and RBAC middleware#3886MohanLaksh wants to merge 1 commit intomainfrom
MohanLaksh wants to merge 1 commit intomainfrom
Conversation
Implements session reuse pattern from PR #3600 and PR #3813 to achieve 1 shared database session per request across all middleware layers. **Changes:** - Auth middleware: Added _get_or_create_session() helper to reuse request.state.db from ObservabilityMiddleware (lines 134, 159, 213) - RBAC middleware: Updated deprecated get_db() to accept optional request parameter and reuse middleware session when available - Transaction control: Delegated all commit/rollback to get_db() per PR #3813 (removed db.commit() from auth middleware) - Added 7 unit tests for auth session reuse patterns - Added 7 unit tests for RBAC get_db() deprecation - Added 6 integration tests for end-to-end session sharing validation **Impact:** - Reduces session creation from 4-6 per request to 1 per request - Prevents connection pool exhaustion under load - Achieves 100% test coverage (435 statements, 0 missing) **Security:** - Transaction isolation maintained (get_db() controls all commits) - Connection invalidation for PgBouncer compatibility - Backwards compatible (existing dependency overrides work) Closes #3622 Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
Collaborator
Author
|
@ja8zyjits , Can you please help me review this PR? |
Member
|
Thanks @MohanLaksh. The session reuse pattern via
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After PR #3600 fixed duplicate session creation in observability middleware and PR #3813 established proper transaction control, auth and RBAC middleware remain the last components creating duplicate database sessions.
Current State:
get_db()that creates 1 session ✗get_db()✓Total: Up to 4-6 sessions per authenticated request
Issues:
Solution
Apply the established session-sharing pattern from PR #3600 to auth and RBAC middleware.
Implementation
1. Auth Middleware (
auth_middleware.py)_get_or_create_session()helper functionrequest.state.dbfrom ObservabilityMiddleware(session, owned)tuple to track ownershipdb.commit()calls (transaction control delegated toget_db())2. RBAC Middleware (
rbac.py)get_db()signature:def get_db(request: Request = None)request.state.dband reuses if availableDeprecationWarningto guide migrationmain.py:get_db()(line 3089)3. Session Lifecycle (No changes to middleware order)
Fallback Strategy:
get_db()in route handler creates sessionTest Coverage
Unit Tests (14 new tests):
get_db()deprecation and reuseIntegration Tests (6 new tests):
get_db()integration with session sharingCoverage: 100% (435 statements, 0 missing lines) - Exceeds 95% CI/CD requirement
Impact
Performance:
Security:
get_db()sole authority for commit/rollback)Compatibility:
Quality Checks
All checks passed:
Files Changed
mcpgateway/middleware/auth_middleware.py(session reuse implementation)mcpgateway/middleware/rbac.py(deprecated get_db() update)tests/unit/mcpgateway/middleware/test_auth_middleware.py(7 new tests)tests/unit/mcpgateway/middleware/test_rbac.py(7 new tests)tests/integration/test_middleware_session_sharing.py(6 new integration tests)Stats: 5 files changed, 857 insertions(+), 30 deletions(-)
Closes #3622