Auth, Performance & Debugging

JWT, RBAC and license gating, rate-limit behavior, the in-memory migration-state gotcha, resource tuning, and the log/inspection workflow across Docker and Kubernetes.

Auth, RBAC, and License Gating

401 vs 403 — they mean different things

A 401 is an authentication failure: no valid JWT in the Authorization header or the sdbs_session cookie. extractUser attaches req.user from a HS256 token that lasts JWT_EXPIRES_IN (8h in .env.example); an expired, missing, or wrong-signature token yields 401. A 403 is authorization/licensing: the user is authenticated but lacks the role (requireRole) for the route, or no license is active (requireLicense). Do not chase a 403 as a login bug. A 403 CSRF_REFUSED means a change was sent with only the session cookie and without X-Requested-With: SimchaDBStudio, or from another site's origin: scripts should use an API token instead of a browser cookie, and a reverse proxy must pass the Origin and Host headers through unchanged (or APP_PUBLIC_URL must be the address people use).

Rotating JWT_SECRET locks everyone out

Changing JWT_SECRET invalidates every previously issued token — signatures no longer verify, so all sessions 401 at once. This is expected; plan rotation as a coordinated re-login event. Symmetrically, if two replicas run with different JWT_SECRET values behind a load balancer, sessions flap 401 depending on which replica serves the request. Ensure the secret is identical across all instances.

License 403s map to the license, not the account

Every licensed route needs an active license, nothing more: the IDE, DB DevOps, AI and the Audit Log all open with any valid, unexpired key. A 403 LICENSE_REQUIRED from a valid admin means no license is active or it has expired — check Settings or /api/license/status. A container that saw no license reads it again before refusing, so a license just activated on another container is honoured at once.

Rate-Limit 429s

Symptom: Bursts of 429 Too Many Requests, often during scripted or automated access. Cause: three limits apply — every route at GENERAL_RATE_LIMIT_MAX per IP per 15 minutes, the sign-in, onboarding, DB DevOps and notification routes at DB_DEVOPS_RATE_LIMIT_MAX per IP per minute, and IDE queries at QUERY_RATE_LIMIT_PER_MIN per user per minute (2000, 5000 and 60 in .env.example; all three are required, with no default). Resolution: throttle the client, or raise the relevant knob for known-good high-volume automation. If a load balancer collapses client IPs, the limiter sees one aggregate source and trips early — have it send X-Forwarded-For and list it in TRUST_PROXY so limits apply per client.

Performance

Query fast at the engine, slow in the IDE

Confirm the plan at the source (EXPLAIN ANALYZE) first. If the engine is fast but the IDE is slow, the cost is result-set materialization: the browser renders rows, so 100k+ row results are render-bound — add a LIMIT. Remote engines add round-trip latency on top of execution. This is a client-side rendering cost, not backend slowness.

OOM under concurrency or large results

Symptom: Reached heap limit — JavaScript heap out of memory or the container is OOM-killed. Cause: concurrent live pools plus large materialized result sets. Resolution: raise the container memory limit (docker run -m 4g ..., or resources.limits.memory in k8s) and cap result sizes.

bcrypt CPU under login bursts

Password hashing is bcrypt at 12 rounds (~200–300ms/hash by design). Simultaneous logins queue on CPU. Give the container at least one full core (resources.requests.cpu: "1000m"). This is intentional work-factor cost, not a regression.

Logs and Direct API Probing

The backend logs structured JSON to stdout only (no log files) via Pino; JSON when NODE_ENV=production or LOG_STDOUT_ONLY=true, pretty-printed otherwise. Level is LOG_LEVEL (required, e.g. info). Correlate a browser request to a backend line by its X-Request-Id (forwarded by the proxy).

docker logs -f simcha-db-studio
docker logs simcha-db-studio 2>&1 | grep -i error | tail -20

Bypass the frontend to isolate proxy-vs-Express faults:

# Unauthenticated signals
curl -s http://localhost:5002/api/health | python3 -m json.tool
curl -s http://localhost:5002/api/health/migrations | python3 -m json.tool
curl -s http://localhost:5002/api/license/status | python3 -m json.tool

# Authenticate, then exercise a gated route
TOKEN=$(curl -s -X POST http://localhost:5002/api/auth/login \\
  -H "Content-Type: application/json" \\
  -d '{"email":"admin@example.com","password":"..."}' \\
  | python3 -c "import sys,json;print(json.load(sys.stdin)['token'])")
curl -s -H "Authorization: Bearer $TOKEN" http://localhost:5002/api/db-devops/scripts | python3 -m json.tool

X-QA-Bypass-Key signs in as the qa@internal admin only when QA_BYPASS_KEY is set; a production deployment must leave it unset (the server refuses to start with it under NODE_ENV=production). X-Agent-Key never grants access; it only exempts internal automation from rate limits.

Docker Inspection

# Resolved env (confirm the target the process actually uses)
docker exec simcha-db-studio env | grep -E "DB_DEVOPS_DB_|JWT_|ENCRYPTION_KEY|CLIENT_URL|PORT|NODE_ENV"

# Health from inside the container
docker exec simcha-db-studio wget -qO- http://localhost:5002/api/health

# Container-reported health and recent probe output
docker inspect --format='{{.State.Health.Status}}' simcha-db-studio
docker inspect --format='{{range .State.Health.Log}}{{.ExitCode}} {{.Output}}{{end}}' simcha-db-studio

If ENCRYPTION_KEY or another required setting is missing or too short, the server exits at startup (code 78) and its log lists every setting that needs attention.

Kubernetes Inspection

kubectl get pods -l app=simcha-db-studio
kubectl logs -f -l app=simcha-db-studio --tail=100
kubectl logs -l app=simcha-db-studio --previous   # crash-loop: prior container

kubectl describe pod -l app=simcha-db-studio
# ImagePullBackOff = image/registry auth; CrashLoopBackOff = check logs;
# Pending with no events = unschedulable (resources); OOMKilled = raise memory limit

With the Helm chart, select the pods with -l app.kubernetes.io/name=simcha-db-studio; app=simcha-db-studio is the label of the k8s/ manifests. A pod that is Running but whose readiness probe (wired to /api/health) never passes is in STARTING: it cannot reach the metadata database — debug it as a DB-reachability problem, not a k8s one.