Repository navigation
Expand file tree
/
Copy pathsetup-production.sh
More file actions
executable file
Β·459 lines (356 loc) Β· 10.7 KB
/
Copy pathsetup-production.sh
File metadata and controls
executable file
Β·459 lines (356 loc) Β· 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/bin/bash
# Sync Production Setup Script
# Sets up the production-ready Sync platform with all enhancements
set -e
echo "π Setting up Sync Production Platform..."
echo "========================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
success() {
echo -e "${GREEN}β
$1${NC}"
}
warning() {
echo -e "${YELLOW}β οΈ $1${NC}"
}
error() {
echo -e "${RED}β $1${NC}"
}
# Check if Node.js is installed
if ! command -v node &> /dev/null; then
error "Node.js is not installed. Please install Node.js 18+ first."
exit 1
fi
# Check Node.js version
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 18 ]; then
error "Node.js version 18+ is required. Current version: $(node -v)"
exit 1
fi
success "Node.js $(node -v) is installed"
# Install dependencies
log "Installing dependencies..."
npm install
success "Dependencies installed"
# Create environment file if it doesn't exist
if [ ! -f .env ]; then
log "Creating .env file..."
cat > .env << EOF
# Sync Production Environment Variables
# Server Configuration
PORT=3001
HOST=0.0.0.0
LOG_LEVEL=info
# Database (replace with your production database URL)
DATABASE_URL=postgresql://username:password@localhost:5432/sync_production
# Encryption (generate a secure key for production)
ENCRYPTION_KEY=your-secure-encryption-key-here
# JWT Secret (generate a secure secret for production)
JWT_SECRET=your-secure-jwt-secret-here
# OpenAI API Key (add your actual API key)
OPENAI_API_KEY=your-openai-api-key-here
# AI Service URL
AI_SERVICE_URL=http://localhost:3002
# Staging Configuration (set to 'true' for staging environment)
STAGING=false
STAGING_USERNAME=admin
STAGING_PASSWORD=your-secure-staging-password
# Security
NODE_ENV=production
EOF
warning "Created .env file with default values. Please update with your actual values."
else
success ".env file already exists"
fi
# Create staging environment file
if [ ! -f .env.staging ]; then
log "Creating .env.staging file..."
cat > .env.staging << EOF
# Sync Staging Environment Variables
# Server Configuration
PORT=3001
HOST=0.0.0.0
LOG_LEVEL=debug
# Database (staging database)
DATABASE_URL=postgresql://username:password@localhost:5432/sync_staging
# Encryption (staging key)
ENCRYPTION_KEY=staging-encryption-key
# JWT Secret (staging secret)
JWT_SECRET=staging-jwt-secret
# OpenAI API Key (staging API key)
OPENAI_API_KEY=your-staging-openai-api-key
# AI Service URL
AI_SERVICE_URL=http://localhost:3002
# Staging Configuration
STAGING=true
STAGING_USERNAME=staging-admin
STAGING_PASSWORD=staging-secure-password
# Security
NODE_ENV=staging
EOF
success "Created .env.staging file"
fi
# Build the project
log "Building the project..."
npm run build
success "Project built successfully"
# Create production start script
log "Creating production start script..."
cat > start-production.sh << 'EOF'
#!/bin/bash
# Sync Production Start Script
echo "π Starting Sync Production Server..."
# Load environment variables
if [ -f .env ]; then
export $(cat .env | grep -v '^#' | xargs)
fi
# Start the production server
cd services/api
node dist/production-server.js
EOF
chmod +x start-production.sh
success "Created production start script"
# Create staging start script
log "Creating staging start script..."
cat > start-staging.sh << 'EOF'
#!/bin/bash
# Sync Staging Start Script
echo "π§ Starting Sync Staging Server..."
# Load staging environment variables
if [ -f .env.staging ]; then
export $(cat .env.staging | grep -v '^#' | xargs)
fi
# Start the staging server
cd services/api
node dist/production-server.js
EOF
chmod +x start-staging.sh
success "Created staging start script"
# Create test script
log "Creating test script..."
cat > test-production.sh << 'EOF'
#!/bin/bash
# Sync Production Test Script
echo "π§ͺ Running Production Tests..."
# Start the server in background
./start-production.sh &
SERVER_PID=$!
# Wait for server to start
sleep 5
# Run tests
node test-production-comprehensive.js
# Stop the server
kill $SERVER_PID
echo "β
Production tests completed"
EOF
chmod +x test-production.sh
success "Created test script"
# Create deployment checklist
log "Creating deployment checklist..."
cat > DEPLOYMENT-CHECKLIST.md << 'EOF'
# Sync Production Deployment Checklist
## Pre-Deployment
- [ ] Update `.env` with production values
- [ ] Set secure `ENCRYPTION_KEY` (32+ characters)
- [ ] Set secure `JWT_SECRET` (32+ characters)
- [ ] Add your OpenAI API key
- [ ] Configure production database URL
- [ ] Set `NODE_ENV=production`
- [ ] Set `STAGING=false`
## Security
- [ ] Enable HTTPS in production
- [ ] Configure proper CORS settings
- [ ] Set up rate limiting for production traffic
- [ ] Configure firewall rules
- [ ] Set up monitoring and alerting
- [ ] Enable audit logging
## Database
- [ ] Set up PostgreSQL database
- [ ] Run database migrations
- [ ] Configure database backups
- [ ] Set up connection pooling
- [ ] Configure database monitoring
## Testing
- [ ] Run production tests: `./test-production.sh`
- [ ] Test crypto health endpoint
- [ ] Test turn-taking locks
- [ ] Test rate limiting
- [ ] Test safety boundaries
- [ ] Test delete functionality
## Monitoring
- [ ] Set up application monitoring
- [ ] Configure error tracking
- [ ] Set up performance monitoring
- [ ] Configure log aggregation
- [ ] Set up health checks
## Staging Environment
- [ ] Deploy to staging first
- [ ] Test all features in staging
- [ ] Verify staging controls work
- [ ] Test with staging credentials
- [ ] Validate robots.txt and no-index
## Production Deployment
- [ ] Deploy to production
- [ ] Verify all endpoints work
- [ ] Test with real users
- [ ] Monitor error rates
- [ ] Check performance metrics
## Post-Deployment
- [ ] Monitor for 24 hours
- [ ] Check error logs
- [ ] Verify all features work
- [ ] Test backup and recovery
- [ ] Document any issues
EOF
success "Created deployment checklist"
# Create README for production
log "Creating production README..."
cat > PRODUCTION-README.md << 'EOF'
# Sync Production Platform
## Quick Start
1. **Setup Environment**
```bash
./setup-production.sh
```
2. **Configure Environment Variables**
- Edit `.env` with your production values
- Set secure encryption keys and secrets
- Add your OpenAI API key
3. **Start Production Server**
```bash
./start-production.sh
```
4. **Run Tests**
```bash
./test-production.sh
```
## Features
### π Crypto Health Monitoring
- Real-time encryption system health
- KMS connection monitoring
- DEK key status tracking
- Test vector validation
### π― Turn-Taking Locks
- Prevents message flooding
- Ensures proper conversation flow
- Configurable lock duration
- Admin monitoring
### β‘ Rate Limiting
- Multi-tier rate limiting
- Dynamic adjustment based on safety violations
- Per-endpoint configuration
- Admin controls
### π‘οΈ Safety & Boundaries
- Content safety validation
- EU compliance resources
- Escalating restrictions
- Audit logging
### βΏ Accessibility
- WCAG 2.1 AA compliant
- Screen reader support
- Keyboard navigation
- High contrast support
### π Security
- Plaintext logging prevention
- Secure headers
- Staging controls
- Audit trails
## API Endpoints
### Health & Status
- `GET /health` - Basic health check
- `GET /health/crypto` - Crypto health status
- `GET /rate-limit/status` - Rate limit status
- `GET /turn-taking/status` - Turn lock status
### Authentication
- `POST /auth/request-code` - Request verification code
- `POST /auth/verify-code` - Verify code and get token
- `GET /auth/me` - Get user info
### Couples & Sessions
- `POST /couples` - Create couple
- `POST /invites` - Create invite
- `POST /invites/:code/accept` - Accept invite
- `POST /sessions` - Create session
- `POST /sessions/:id/messages` - Send message
- `GET /sessions/:id/messages` - Get messages (with long-polling)
- `DELETE /sessions/:id` - Delete session
### Safety & Survey
- `GET /safety/status` - Safety status
- `GET /safety/eu-resources` - EU resources
- `POST /sessions/:id/survey` - Submit survey
- `GET /survey/analytics` - Survey analytics
### Delete & Privacy
- `POST /delete/request` - Request data deletion
- `POST /delete/:id/confirm` - Confirm deletion
- `POST /delete/:id/execute` - Execute deletion
- `GET /delete/:id/status` - Deletion status
### Admin
- `GET /admin/turn-locks` - Active turn locks
- `GET /admin/logging-security` - Logging security status
- `POST /admin/rate-limit/reset` - Reset rate limits
## Environment Variables
| Variable | Description | Required |
|----------|-------------|----------|
| `PORT` | Server port | Yes |
| `HOST` | Server host | Yes |
| `DATABASE_URL` | PostgreSQL connection string | Yes |
| `ENCRYPTION_KEY` | Encryption key (32+ chars) | Yes |
| `JWT_SECRET` | JWT secret (32+ chars) | Yes |
| `OPENAI_API_KEY` | OpenAI API key | Yes |
| `AI_SERVICE_URL` | AI service URL | Yes |
| `STAGING` | Enable staging mode | No |
| `STAGING_USERNAME` | Staging basic auth username | No |
| `STAGING_PASSWORD` | Staging basic auth password | No |
| `LOG_LEVEL` | Logging level | No |
## Security Considerations
1. **Encryption Keys**: Use cryptographically secure random keys
2. **JWT Secrets**: Use long, random secrets
3. **Database**: Use connection pooling and SSL
4. **Rate Limiting**: Configure appropriate limits for your traffic
5. **Monitoring**: Set up comprehensive monitoring and alerting
6. **Backups**: Regular database and configuration backups
## Monitoring
The platform includes comprehensive monitoring:
- Health check endpoints
- Crypto health monitoring
- Rate limit tracking
- Turn lock monitoring
- Safety violation tracking
- Audit logging
## Support
For production support and issues:
1. Check the health endpoints
2. Review audit logs
3. Monitor rate limit status
4. Check crypto health
5. Review error logs
## License
This is a production-ready implementation of the Sync platform.
EOF
success "Created production README"
# Final summary
echo ""
echo "π Sync Production Setup Complete!"
echo "=================================="
echo ""
echo "Next steps:"
echo "1. Edit .env with your production values"
echo "2. Run: ./start-production.sh"
echo "3. Test: ./test-production.sh"
echo "4. Review: DEPLOYMENT-CHECKLIST.md"
echo ""
echo "Files created:"
echo "β
.env (production environment)"
echo "β
.env.staging (staging environment)"
echo "β
start-production.sh (production start script)"
echo "β
start-staging.sh (staging start script)"
echo "β
test-production.sh (test script)"
echo "β
DEPLOYMENT-CHECKLIST.md (deployment guide)"
echo "β
PRODUCTION-README.md (production documentation)"
echo ""
echo "Your Sync platform is ready for production deployment! π"