Migration Proxy - Cloudflare Workers-based traffic router for gradual SSO backend migration
A production-ready proxy service that enables zero-downtime migration from an old Single Sign-On (SSO) authentication backend to a new one. This worker intelligently routes authentication requests based on user preference while maintaining backward compatibility.
The SSO Worker acts as an intelligent traffic router during the migration from a legacy SSO backend to a new one. It provides:
- Gradual user migration - Users can opt-in to the new backend via query parameter
- Seamless fallback - Easy rollback to old backend if issues arise
- Zero downtime - No service interruption during migration
Important: This is a temporary migration tool. Once all users are migrated to the new backend and the old backend is decommissioned, this worker will be sunset.
The worker sits between clients and the SSO backends, routing traffic based on a backend_selector cookie:
Client Request
|
v
SSO Worker (Cloudflare Edge)
|
+---> Cookie = "new" OR ?new query param ---> NEW Backend
|
+---> No cookie OR ?old query param --------> OLD Backend
|
- Cookie name:
backend_selector - Default: Routes to old backend
- When
backend_selector=new: Routes to new backend - Secure, HttpOnly, SameSite=Lax cookies with 1-year expiration
?new- Sets backend selector cookie and redirects (307) to new backend?old- Deletes backend selector cookie and redirects (307) to old backend- Preserves other query parameters during redirect
- Preserves HTTP method (GET, POST, PUT, DELETE, etc.)
- Forwards all headers and request body
- Maintains query parameters
- Handles trailing slashes correctly
- Old Backend:
https://old-backend.example.com - New Backend:
https://new-backend.example.com - Usage: Local testing and development
- Old Backend: Configure
OLD_BACKEND_URLinwrangler.jsonc - New Backend: Configure
NEW_BACKEND_URLinwrangler.jsonc - Usage: Pre-production testing with real backend instances
- Old Backend: Configure
OLD_BACKEND_URLinwrangler.jsonc - New Backend: Configure
NEW_BACKEND_URLinwrangler.jsonc - Usage: Live production traffic
- Wrangler CLI installed
- Cloudflare account with Workers enabled
- Node.js 18+ and npm
# Deploy to production (default)
npm run deploy
# Deploy to staging
npx wrangler deploy --env staging
# Deploy to development
npx wrangler deploy --env dev# Check deployment status
npx wrangler deployments list
# View logs
npx wrangler tail# Install dependencies
npm install
# Generate TypeScript types from wrangler config
npm run cf-typegen# Start local development server (dev environment)
npm start
# or
npm run dev
# The worker will be available at http://localhost:8787# Run all tests
npm test
# Watch mode (re-run tests on file changes)
npm run test:watch
# Generate coverage report
npm run test:coverageThe test suite includes 40+ test cases covering:
- Query parameter redirects (?new/?old)
- Cookie-based routing
- UserFromToken parallel requests
- Edge cases and error handling
# Type check
npm run lintThis project uses Prettier for code formatting:
- 140 character line width
- Tabs for indentation
- Single quotes
Visit any SSO URL with the ?new query parameter:
https://your-sso-worker.workers.dev/some-endpoint?new
This sets a cookie and redirects you to the new backend. All subsequent requests will use the new backend.
Visit any SSO URL with the ?old query parameter:
https://your-sso-worker.workers.dev/some-endpoint?old
This removes the cookie and redirects you to the old backend.
# Test redirect to new backend
curl -i "http://localhost:8787/SsoAuthApi/Login?new"
# Observe Set-Cookie header in response
# Follow redirect to new backend
# Test redirect to old backend
curl -i "http://localhost:8787/SsoAuthApi/Login?old"Current Status: Production deployment active
Migration Phases:
- Phase 1 (Current): Worker deployed, both backends operational
- Phase 2: Gradual user opt-in to new backend via communications
- Phase 3: Monitor error rates and user feedback
- Phase 4: Force-migrate remaining users to new backend
- Phase 5: Decommission old backend
- Phase 6: Sunset this worker, direct traffic to new backend
Sunset Plan: Once all users are migrated and the old backend is decommissioned, this worker will be removed and traffic will be routed directly to the new backend.
COOKIE_NAME = 'backend_selector'
- Proper cookie parsing (no substring matching vulnerability)
- Request timeout protection with AbortController
- Secure cookie attributes (HttpOnly, Secure, SameSite)
- No credential leakage between backends
- Preserves error responses from backends