Files
sbiertiandClaude Fable 5.1 b0dde55d65 Aggiunge CLAUDE.md con guida per Claude Code
Documenta comandi di validazione, architettura dello stack (blog,
postgresql, nginx, certbot), il bootstrap dei certificati e le convenzioni
facili da rompere (header di sicurezza, resolver, pin delle immagini).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 11:59:27 +02:00

116 lines
7.3 KiB
Markdown

# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## What this repo is
This is **not an application codebase**. It is a Docker Compose deployment for exposing a JHipster
(Spring Boot) web app behind an Nginx reverse proxy with Let's Encrypt/Certbot TLS. There is no
build step, no test suite, and no application source here. The real site is `www.elenabaracchini.it`,
but every committed file uses the placeholder `example.com`.
Comments, scripts and the README are written in **Italian**. Match that language when editing them,
and keep `README.md` in sync with any behavioural change (it is the primary user-facing doc).
## Commands
```bash
# Validate changes (the only "tests" that exist)
docker-compose config # compose file parses, env guards (:?) satisfied
docker-compose exec nginx nginx -t # nginx syntax (needs the nginx container running)
# First-time certificate bootstrap (staging=1 first, to avoid LE rate limits)
./init-letsencrypt.sh <domain> <email> [0|1]
# Run / stop the stack
docker-compose up -d
docker-compose down
# Reload nginx without downtime (always run nginx -t first: reload exits 0 on invalid config)
docker-compose exec nginx nginx -t && docker-compose exec nginx nginx -s reload
# Certificates (certbot entrypoint passes args straight to certbot)
docker-compose run --rm certbot certificates
docker-compose run --rm certbot renew --dry-run
docker-compose run --rm certbot delete --cert-name <domain> # needed before staging -> prod
./renew-certificates.sh # interactive manual renewal
# Logs
docker-compose logs -f nginx | blog | postgresql | certbot
```
`.env` (gitignored) must define `BLOG_IMAGE` and `POSTGRES_PASSWORD`; `docker-compose.yml` uses
`${VAR:?}` guards so any compose command fails loudly without them. `.env.example` deliberately leaves
`POSTGRES_PASSWORD` empty so the guard cannot be satisfied by a placeholder.
## Architecture
Four services on one internal bridge network (`app-network`). Only `nginx` publishes host ports
(80/443); `blog:8080` and `postgresql:5432` are intentionally **not** exposed.
- **`blog`** — the JHipster app, image from `${BLOG_IMAGE}`, `prod` profile, datasource pointed at
`postgresql`. Healthcheck hits `/management/health` with `wget` (slim JRE images lack curl).
Starts only after `postgresql` is healthy; `nginx` depends on `blog`.
- **`postgresql`** — `postgres:17.11`, data in the `postgresql-data` volume, explicit `POSTGRES_DB`.
- **`nginx`** — `nginx:1.30-alpine`, config bind-mounted read-only, cert volumes read-only. Its
`command` wraps `nginx -g 'daemon off;'` in a loop that runs `nginx -s reload` every 6h: nginx
loads certs into memory once, so without this it would serve an expired cert after renewal.
- **`certbot`** — `certbot/certbot:v5.4.0` with `certbot-entrypoint.sh` as entrypoint. **No args**
→ infinite `certbot renew` loop every 12h (what `up -d` runs). **With args** → `exec certbot "$@"`
(what `docker-compose run --rm certbot ...` uses). Shares `certbot-webroot` (ACME challenge
files) and `certbot-certs` (`/etc/letsencrypt`) with nginx via named volumes.
### Nginx layout
- `nginx/nginx.conf` — http-level settings, logging, gzip, `server_tokens off`, and the
`$connection_upgrade` map used for WebSocket upgrade. Includes `conf.d/*.conf`.
- `nginx/conf.d/app.conf` — HTTP→HTTPS redirect server + HTTPS reverse-proxy server.
- `nginx/conf.d/security-headers.inc` — HSTS / nosniff / `X-XSS-Protection: 0`. The `.inc`
extension is deliberate so the `*.conf` glob does not load it as a standalone file.
### The certificate bootstrap (why `init-letsencrypt.sh` is shaped the way it is)
On a fresh host the HTTPS `server` block in `app.conf` cannot load (no cert files yet), so nginx would
refuse to start. The script therefore: backs up `app.conf` to `app.conf.bak`, writes a temporary
HTTP-only config serving `/.well-known/acme-challenge/`, starts nginx with `--no-deps` (so `blog`/
`postgresql` and a populated `.env` are not required for the ACME phase), waits for nginx to answer,
runs `certbot certonly --webroot --non-interactive`, then restores the backup via an `EXIT` trap,
`sed`s `example.com` → real domain, brings up the full stack, and runs `nginx -t` **before**
`nginx -s reload`. `nginx/conf.d/*.bak` is gitignored; the script also recovers from a leftover
`.bak` of an interrupted run. Because `certonly` is `--non-interactive`, an existing (e.g. staging)
lineage makes it fail rather than prompt: delete the lineage or use `--force-renewal` first.
## Conventions and things that are easy to break
- **Keep `example.com` in committed config.** The real domain is substituted at deploy time by the
script; never commit a domain-specific `app.conf`. When changing domain handling, both `app.conf`
server blocks and the `/etc/letsencrypt/live/<domain>/` paths must agree.
- **Image pinning:** no `:latest` anywhere (compose uses exact tags; `BLOG_IMAGE` should be a tag or
digest). A silent certbot/nginx upgrade can break auto-renewal.
- **Security header ownership.** JHipster already emits `Content-Security-Policy`, `X-Frame-Options`,
`Referrer-Policy` and `Permissions-Policy`; nginx `add_header` does not replace upstream headers, so
adding them in nginx produces duplicates (two CSPs = most restrictive intersection, can break the
SPA bundle). Nginx only sets HSTS (without `includeSubDomains`/`preload`, by design) and the two
non-conflicting headers. Use `proxy_hide_header` if nginx must take ownership.
- **`add_header` inside a `location` discards all inherited `add_header`s.** Any new location with
its own `add_header` must re-`include security-headers.inc`.
- **`/management` is 404'd at nginx** (`location ^~ /management`) so actuator/prometheus endpoints
are never public; the healthcheck reaches it from inside the container. Do not enable the JHipster
`api-docs` profile in prod for the same reason (nginx proxies all of `/`).
- **`proxy_pass` uses a variable plus `resolver 127.0.0.11`** so nginx re-resolves `blog` at
runtime; a literal hostname is resolved once at startup and yields 502s after `blog` gets a new
IP. Keep that pattern for any new upstream.
- **No per-vhost `access_log`/`error_log` in `app.conf`**: only the default paths are linked to
stdout/stderr in the official image, so dedicated files would be invisible to `docker-compose logs`
and never rotated.
- **OCSP stapling and DHE ciphers were removed on purpose** (Let's Encrypt dropped OCSP; no
`ssl_dhparam` means DHE would never be negotiated). Don't reintroduce them without the matching
config. Preserve TLS 1.2/1.3 only and the Mozilla-intermediate cipher list.
- **Docker does not restart `unhealthy` containers**; healthchecks here only drive start order
(`depends_on: condition: service_healthy`) and `ps` status.
- Shell scripts: `init-letsencrypt.sh` and `renew-certificates.sh` are `#!/bin/bash` with `set -e`;
`certbot-entrypoint.sh` runs inside Alpine and must stay POSIX `#!/bin/sh`. All three must remain
executable (`chmod +x`), since the entrypoint is bind-mounted into the container.
- `.junie/guidelines.md` is an older agent-guidance file that predates the `blog`/`postgresql` split
and the OCSP removal; prefer this file and the README where they disagree.