-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Add cookieSessionStore option to support multi-replica deployments
#3016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
9
commits into
alpha
Choose a base branch
from
copilot/fix-csrf-token-validation
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+232
−4
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b2e6b8
Initial plan
Copilot 0d763dd
Add sessionStore option for multi-replica deployments
Copilot dbb1c94
Add tests for sessionStore functionality
Copilot 3b69608
Add example configurations for multi-replica deployments
Copilot ed88924
Remove example files and simplify README with session store list
Copilot 6c87a73
Add simple connect-redis example to README
Copilot a2170b4
Apply suggestion from @mtrezza
mtrezza ce564c8
Apply suggestion from @mtrezza
mtrezza faaeb2d
Rename sessionStore to cookieSessionStore across the codebase
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # Parse Dashboard Examples | ||
|
|
||
| This directory contains example configurations for Parse Dashboard in various deployment scenarios. | ||
|
|
||
| ## Multi-Replica Deployments | ||
|
|
||
| When running Parse Dashboard with multiple replicas behind a load balancer, you need to use a shared session store to ensure CSRF tokens and user sessions work correctly across all replicas. | ||
|
|
||
| ### Available Examples | ||
|
|
||
| 1. **[redis-session-store.js](./redis-session-store.js)** - Using Redis as the session store | ||
| - Fast, in-memory session storage | ||
| - Recommended for high-traffic deployments | ||
| - Requires: `connect-redis`, `redis` | ||
|
|
||
| 2. **[mongodb-session-store.js](./mongodb-session-store.js)** - Using MongoDB as the session store | ||
| - Persistent session storage | ||
| - Good if you already have MongoDB infrastructure | ||
| - Requires: `connect-mongo` | ||
|
|
||
| ### Quick Start | ||
|
|
||
| 1. Choose an example based on your infrastructure | ||
| 2. Install the required dependencies: | ||
| ```bash | ||
| # For Redis | ||
| npm install parse-dashboard connect-redis redis | ||
|
|
||
| # For MongoDB | ||
| npm install parse-dashboard connect-mongo | ||
| ``` | ||
| 3. Configure environment variables: | ||
| ```bash | ||
| # For Redis | ||
| export REDIS_URL="redis://localhost:6379" | ||
| export SESSION_SECRET="your-secret-key-change-this" | ||
| export PARSE_SERVER_URL="http://localhost:1337/parse" | ||
| export PARSE_APP_ID="myAppId" | ||
| export PARSE_MASTER_KEY="myMasterKey" | ||
|
|
||
| # For MongoDB | ||
| export MONGODB_URL="mongodb://localhost:27017/parse-dashboard-sessions" | ||
| export SESSION_SECRET="your-secret-key-change-this" | ||
| export PARSE_SERVER_URL="http://localhost:1337/parse" | ||
| export PARSE_APP_ID="myAppId" | ||
| export PARSE_MASTER_KEY="myMasterKey" | ||
| ``` | ||
| 4. Run the example: | ||
| ```bash | ||
| node examples/redis-session-store.js | ||
| # or | ||
| node examples/mongodb-session-store.js | ||
| ``` | ||
|
|
||
| ### Important Notes | ||
|
|
||
| - **`SESSION_SECRET` must be the same across all replicas** - This ensures session cookies work correctly | ||
| - **Configure your load balancer properly** - Set `trustProxy: true` when behind a reverse proxy | ||
| - **Health check endpoint** - All examples include a `/health` endpoint for load balancer health checks | ||
| - **Graceful shutdown** - Examples include proper cleanup handlers for SIGTERM and SIGINT signals | ||
|
|
||
| ### Kubernetes Deployment | ||
|
|
||
| For Kubernetes deployments, you can use ConfigMaps and Secrets to configure your dashboard: | ||
|
|
||
| ```yaml | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: parse-dashboard-config | ||
| data: | ||
| REDIS_URL: "redis://redis-service:6379" | ||
| PARSE_SERVER_URL: "http://parse-server-service:1337/parse" | ||
| PARSE_APP_ID: "myAppId" | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Secret | ||
| metadata: | ||
| name: parse-dashboard-secrets | ||
| type: Opaque | ||
| data: | ||
| SESSION_SECRET: <base64-encoded-secret> | ||
| PARSE_MASTER_KEY: <base64-encoded-master-key> | ||
| DASHBOARD_PASS: <base64-encoded-password> | ||
| ``` | ||
|
|
||
| ### Docker Compose | ||
|
|
||
| For Docker Compose deployments, you can use environment files: | ||
|
|
||
| ```yaml | ||
| version: '3.8' | ||
| services: | ||
| redis: | ||
| image: redis:latest | ||
| ports: | ||
| - "6379:6379" | ||
|
|
||
| parse-dashboard: | ||
| build: . | ||
| environment: | ||
| - REDIS_URL=redis://redis:6379 | ||
| - SESSION_SECRET=${SESSION_SECRET} | ||
| - PARSE_SERVER_URL=${PARSE_SERVER_URL} | ||
| - PARSE_APP_ID=${PARSE_APP_ID} | ||
| - PARSE_MASTER_KEY=${PARSE_MASTER_KEY} | ||
| ports: | ||
| - "4040:4040" | ||
| depends_on: | ||
| - redis | ||
| deploy: | ||
| replicas: 3 | ||
| ``` | ||
|
|
||
| ### Troubleshooting | ||
|
|
||
| **Issue: "CSRF token validation failed" errors** | ||
| - Ensure `SESSION_SECRET` is the same across all replicas | ||
| - Verify the session store is accessible from all replicas | ||
| - Check that `trustProxy` is enabled when behind a load balancer | ||
|
|
||
| **Issue: Sessions not persisting** | ||
| - Verify the session store connection is working | ||
| - Check session TTL configuration | ||
| - Ensure the session store has enough memory/storage | ||
|
|
||
| **Issue: High memory usage** | ||
| - Adjust session TTL to clean up expired sessions | ||
| - Use `touchAfter` option (MongoDB) to reduce update frequency | ||
| - Monitor session store metrics | ||
|
|
||
| For more information, see the [main README](../README.md#running-multiple-dashboard-replicas). |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * Example: Using Parse Dashboard with MongoDB Session Store for Multi-Replica Deployments | ||
| * | ||
| * This example shows how to configure Parse Dashboard with a MongoDB session store | ||
| * to support multiple dashboard replicas behind a load balancer without sticky sessions. | ||
| * | ||
| * Prerequisites: | ||
| * 1. Install required dependencies: | ||
| * npm install parse-dashboard connect-mongo | ||
| * | ||
| * 2. Have a MongoDB server running (e.g., mongodb://localhost:27017) | ||
| */ | ||
|
|
||
| const express = require('express'); | ||
| const ParseDashboard = require('parse-dashboard'); | ||
| const MongoStore = require('connect-mongo'); | ||
|
|
||
| // Configuration | ||
| const MONGODB_URL = process.env.MONGODB_URL || 'mongodb://localhost:27017/parse-dashboard-sessions'; | ||
| const PORT = process.env.PORT || 4040; | ||
| const SESSION_SECRET = process.env.SESSION_SECRET || 'your-secret-key-change-this'; | ||
|
|
||
| // Create MongoDB session store | ||
| const sessionStore = MongoStore.create({ | ||
| mongoUrl: MONGODB_URL, | ||
| collectionName: 'sessions', // Collection name for storing sessions | ||
| ttl: 86400, // Session TTL in seconds (24 hours) | ||
| autoRemove: 'native', // Let MongoDB's TTL index handle session cleanup | ||
| touchAfter: 3600, // Update session only once per hour (unless session data changes) | ||
| // Optional: Add connection options | ||
| mongoOptions: { | ||
| useNewUrlParser: true, | ||
| useUnifiedTopology: true, | ||
| } | ||
| }); | ||
|
|
||
| // Handle store connection events | ||
| sessionStore.on('error', (err) => { | ||
| console.error('MongoDB Session Store Error:', err); | ||
| }); | ||
|
|
||
| sessionStore.on('connected', () => { | ||
| console.log('Connected to MongoDB for session storage'); | ||
| }); | ||
|
|
||
| // Parse Dashboard configuration | ||
| const dashboardConfig = { | ||
| apps: [ | ||
| { | ||
| serverURL: process.env.PARSE_SERVER_URL || 'http://localhost:1337/parse', | ||
| appId: process.env.PARSE_APP_ID || 'myAppId', | ||
| masterKey: process.env.PARSE_MASTER_KEY || 'myMasterKey', | ||
| appName: process.env.PARSE_APP_NAME || 'My Parse App', | ||
| // Optional: GraphQL endpoint | ||
| // graphQLServerURL: 'http://localhost:1337/graphql', | ||
| }, | ||
| // Add more apps as needed | ||
| ], | ||
| users: [ | ||
| { | ||
| user: process.env.DASHBOARD_USER || 'admin', | ||
| pass: process.env.DASHBOARD_PASS || 'password', | ||
| // Optional: Restrict access to specific apps | ||
| // apps: [{ appId: 'myAppId' }] | ||
| }, | ||
| ], | ||
| // Optional: Use encrypted passwords (recommended for production) | ||
| // useEncryptedPasswords: true, | ||
| }; | ||
|
|
||
| // Dashboard options | ||
| const dashboardOptions = { | ||
| allowInsecureHTTP: process.env.ALLOW_INSECURE_HTTP === 'true', | ||
| cookieSessionSecret: SESSION_SECRET, // IMPORTANT: Must be the same across all replicas | ||
| cookieSessionMaxAge: 86400000, // Session cookie max age in milliseconds (24 hours) | ||
| sessionStore: sessionStore, // Use MongoDB session store | ||
| }; | ||
|
|
||
| // Create Express app | ||
| const app = express(); | ||
|
|
||
| // Trust proxy when running behind a load balancer | ||
| app.set('trust proxy', 1); | ||
|
|
||
| // Mount Parse Dashboard | ||
| app.use('/dashboard', ParseDashboard(dashboardConfig, dashboardOptions)); | ||
|
|
||
| // Health check endpoint (useful for load balancers) | ||
| app.get('/health', (req, res) => { | ||
| res.status(200).json({ status: 'ok' }); | ||
| }); | ||
|
|
||
| // Start server | ||
| const server = app.listen(PORT, () => { | ||
| console.log(`Parse Dashboard is now available at http://localhost:${PORT}/dashboard`); | ||
| console.log('Dashboard is configured with MongoDB session store for multi-replica support'); | ||
| }); | ||
|
|
||
| // Graceful shutdown | ||
| process.on('SIGTERM', () => { | ||
| console.log('SIGTERM signal received: closing HTTP server'); | ||
| server.close(() => { | ||
| console.log('HTTP server closed'); | ||
| sessionStore.close().then(() => { | ||
| console.log('MongoDB session store connection closed'); | ||
| process.exit(0); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| console.log('SIGINT signal received: closing HTTP server'); | ||
| server.close(() => { | ||
| console.log('HTTP server closed'); | ||
| sessionStore.close().then(() => { | ||
| console.log('MongoDB session store connection closed'); | ||
| process.exit(0); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.