/home/techb158/trellopowerup.abdallabala.com/docs
Edit: /home/techb158/trellopowerup.abdallabala.com/docs/17-step-9-production-deployment-security.md (4228B)
# Step 9, Production Deployment and Security Hardening
## Purpose
Step 9 prepares the COSMIC AI-Risk Dashboard for controlled deployment on a server. This step does not change the risk model. It adds operational controls around the existing dashboard, API, OAuth connectors, reporting module, and role-based access control.
## Source alignment
The project source positions COSMIC-Risk as a software prototype and REST API for automated, reproducible AI risk analysis and project-management integration. The production layer is an implementation extension needed to operate that prototype safely.
## Scope
| Area | Implementation |
|---|---|
| Runtime configuration | Central `runtimeConfig` module using environment variables |
| Security headers | CSP, no-sniff, referrer policy, permissions policy, frame policy, optional HSTS |
| HTTPS support | Reverse-proxy aware HTTPS enforcement |
| CORS control | Explicit origin allowlist through environment variables |
| Readiness checks | `/api/ready` validates config, database access, and backup directory |
| Security status | `/api/operations/security` exposes safe configuration summary |
| Backups | Backup API and CLI script create database copy with SHA-256 manifest |
| Deployment files | Dockerfile, docker-compose, Nginx example, systemd service |
| Production scripts | Config check and backup scripts |
| Tests | Production hardening test suite |
## New API endpoints
| Method | Endpoint | Permission | Purpose |
|---|---|---|---|
| GET | `/api/ready` | public | Readiness probe for deployment monitoring |
| GET | `/api/operations/security` | `audit:read` | Review safe security configuration status |
| GET | `/api/operations/backups` | `audit:read` | List backup manifests |
| POST | `/api/operations/backups` | `user:write` | Create a database backup |
## New environment variables
| Variable | Default | Purpose |
|---|---|---|
| `NODE_ENV` | `development` | Runtime mode |
| `COSMIC_SECURITY_HEADERS_ENABLED` | `true` | Enable security response headers |
| `COSMIC_FORCE_HTTPS` | `false` in local `.env.example` | Redirect GET/HEAD traffic to HTTPS when enabled |
| `COSMIC_TRUST_PROXY` | `false` in local `.env.example` | Trust `X-Forwarded-Proto` behind Nginx or another reverse proxy |
| `COSMIC_HSTS_ENABLED` | `false` in local `.env.example` | Add HSTS when request is HTTPS |
| `COSMIC_ALLOWED_ORIGINS` | empty | Comma-separated CORS allowlist |
| `COSMIC_FRAME_ANCESTORS` | `'self'` | CSP frame ancestor list |
| `COSMIC_MAX_BODY_BYTES` | `1048576` | API JSON body limit |
| `COSMIC_BACKUP_DIR` | `./backups` | Backup output directory |
| `COSMIC_BACKUP_RETENTION` | `10` | Number of backups retained |
| `COSMIC_REQUEST_LOG_ENABLED` | `false` | Structured request logging |
## Production commands
```bash
npm run check:config
npm test
npm run backup
NODE_ENV=production npm start
```
## Docker deployment
```bash
cp .env.example .env
# edit .env with production values
mkdir -p data backups
docker compose up --build -d
curl http://localhost:8090/api/ready
```
## Nginx deployment
Use `deploy/nginx.conf` as a starting point. In production, terminate TLS at Nginx and set:
```env
NODE_ENV=production
COSMIC_TRUST_PROXY=true
COSMIC_FORCE_HTTPS=true
COSMIC_HSTS_ENABLED=true
COSMIC_PUBLIC_BASE_URL=https://your-domain.example
```
## Backup behavior
The backup module copies the JSON database to the backup directory and creates a manifest file containing:
- backup ID
- actor user ID
- source file
- backup file path
- creation timestamp
- file size
- SHA-256 digest
The backup keeps OAuth token ciphertext as stored in the database. The backup directory must therefore be protected like a production secret store.
## Acceptance criteria
| Check | Expected result |
|---|---|
| `/api/health` | Returns service version `2.0.0` |
| `/api/ready` | Returns `ok: true` when database and backup directory are available |
| Security headers | API and static responses include CSP, no-sniff, referrer policy, and permissions policy |
| Backup script | `npm run backup` creates JSON backup and manifest |
| Config script | `npm run check:config` prints safe runtime summary |
| Tests | `npm test` passes including production hardening tests |