OAuth Observability Migration Guide
This guide walks through applying OAuth 2.1 security fixes and observability enhancements from PR #106.Migration Overview
What's Being Migrated
- 2 Database Migrations: Atomic token locking + PKCE audit trail
- Observability Stack: Structured logging + Prometheus metrics
- Security Fixes: Race condition prevention + PKCE replay protection
- Zero Downtime: All changes are backward compatible
Pre-Migration Checklist
1
Backup Database
Create a full database backup before applying migrations:
2
Check Current Schema Version
Verify current migration state:
3
Verify Dependencies
Ensure all required services are running:
4
Review Open OAuth Flows
Check for in-progress OAuth flows:Note: Migration does NOT affect active flows. They will continue normally.
Migration Steps
Step 1: Update Code
- Production Deployment
- Staging/Testing
Step 2: Apply Database Migrations
0074: Atomic Refresh Token Marking
0074: Atomic Refresh Token Marking
Purpose: Fixes P0 race condition in refresh token reuse detectionChanges:Impact: Zero downtime - existing tokens continue working
- Adds
refresh_token_locked_atcolumn tomcp_server_oauth_tokens - Creates indexes for efficient lock queries
- Enables optimistic locking pattern
0075: PKCE State Audit and Constraints
0075: PKCE State Audit and Constraints
Purpose: Prevents PKCE state replay attacksChanges:Impact: Zero downtime - new security layer, no breaking changes
- Creates
oauth_pkce_states_audittable with 30-day retention - Adds PostgreSQL trigger to auto-audit deleted states
- Tracks used states to prevent replay
Step 3: Verify Migration Success
Step 4: Configure Environment Variables
Add observability configuration to.env:
.env
Step 5: Rolling Deployment
- Docker Deployment
- Systemd Deployment
- PM2 Deployment
Step 6: Verify Application Health
1
Check Application Startup
2
Verify Metrics Endpoint
3
Test OAuth Flow
4
Verify Security Features
Post-Migration Tasks
1. Set Up Grafana Dashboards
2. Configure Alerts
Add to Prometheusalert.rules.yml:
3. Update Promtail Configuration
See Update Promtail Config below.4. Monitor for Issues
Watch logs for 24-48 hours:Rollback Procedure
Step 1: Stop Application
Step 2: Restore Database
Step 3: Revert Code
Step 4: Restart Application
Step 5: Manual Migration Cleanup (if needed)
If you need to manually remove migrations without full restore:Troubleshooting
Migration fails with 'column already exists'
Migration fails with 'column already exists'
Cause: Migration was partially applied beforeSolution:
Metrics endpoint returns 404
Metrics endpoint returns 404
Cause: Metrics route not exposed or build issueSolution:
Logs not appearing in Loki
Logs not appearing in Loki
Cause: Promtail not configured or log format issueSolution:
PKCE cleanup not running
PKCE cleanup not running
Cause: Environment check preventing schedulerSolution:
High number of PKCE states not cleaned
High number of PKCE states not cleaned
Cause: Grace period protecting recent statesSolution:
Migration Validation Checklist
After migration, verify all items:- Database
- Application
- Observability
- Security
- Migrations 0074 and 0075 applied successfully
-
refresh_token_locked_atcolumn exists -
oauth_pkce_states_audittable exists - Audit trigger
pkce_state_audit_triggerexists - All existing tokens still valid
- No orphaned PKCE states
Support
Documentation
Full observability documentation
OAuth Security
OAuth 2.1 security implementation
Troubleshooting
General troubleshooting guide
GitHub Issues
Report migration issues
Summary
✅ Migration Complete when:- All database migrations applied
- Application running with no errors
- Metrics endpoint accessible
- Logs flowing to Loki
- OAuth flows working correctly
- Grafana dashboards showing data
- P0 security fixes for race conditions
- PKCE replay attack prevention
- Comprehensive observability stack
- Real-time security monitoring
- Production-ready OAuth 2.1 implementation

