Skip to main content

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
Recommended Migration Window: Off-peak hours (optional, but recommended for first deployment)Zero Downtime: ✅ Yes - rolling deployment supported

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

Step 2: Apply Database Migrations

Important: Use Drizzle Kit commands, NOT direct SQL execution
Migrations Applied:
Purpose: Fixes P0 race condition in refresh token reuse detectionChanges:
  • Adds refresh_token_locked_at column to mcp_server_oauth_tokens
  • Creates indexes for efficient lock queries
  • Enables optimistic locking pattern
SQL:
Impact: Zero downtime - existing tokens continue working
Purpose: Prevents PKCE state replay attacksChanges:
  • Creates oauth_pkce_states_audit table with 30-day retention
  • Adds PostgreSQL trigger to auto-audit deleted states
  • Tracks used states to prevent replay
SQL:
Impact: Zero downtime - new security layer, no breaking changes

Step 3: Verify Migration Success

Step 4: Configure Environment Variables

Add observability configuration to .env:
.env

Step 5: Rolling 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 Prometheus alert.rules.yml:

3. Update Promtail Configuration

See Update Promtail Config below.

4. Monitor for Issues

Watch logs for 24-48 hours:

Rollback Procedure

Only needed if critical issues arise. Observability changes are additive and safe.

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:
Manual cleanup should only be done as a last resort. Prefer full database restore.

Troubleshooting

Cause: Migration was partially applied beforeSolution:
Cause: Metrics route not exposed or build issueSolution:
Cause: Promtail not configured or log format issueSolution:
Cause: Environment check preventing schedulerSolution:
Cause: Grace period protecting recent statesSolution:

Migration Validation Checklist

After migration, verify all items:
  • Migrations 0074 and 0075 applied successfully
  • refresh_token_locked_at column exists
  • oauth_pkce_states_audit table exists
  • Audit trigger pkce_state_audit_trigger exists
  • 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
🎉 You now have:
  • P0 security fixes for race conditions
  • PKCE replay attack prevention
  • Comprehensive observability stack
  • Real-time security monitoring
  • Production-ready OAuth 2.1 implementation