Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.venv/
__pycache__/
sticky.key
pebble-tls/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ is at [docs.gen0sec.com](https://docs.gen0sec.com).
- [Load-balancing methods](./use-cases/lb-methods/): one route per `lb.method`
(round robin, maglev, ring hash, least request and more) over the same three
servers. Needs Synapse 0.8.9 or later.
- [Certificates from certbot](./use-cases/certbot/): serve the certificates certbot
issues and renews, with the webroot challenge routed through Synapse. Needs
Synapse 0.8.9 or later.
- [Load balancing on a passthrough host](./use-cases/lb-passthrough/): one
passthrough host per `lb.method`, without terminating TLS. Needs Synapse
0.8.9 or later.
Expand Down
1 change: 1 addition & 0 deletions use-cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ closest to what you want and copy it. Every set uses placeholder values
| [`capture-ids-ml`](./capture-ids-ml/) | agent | yes | `CAP_NET_RAW`, `CAP_BPF`, `CAP_NET_ADMIN`, and `CAP_SYS_ADMIN` or `CAP_PERFMON` | Detect-only XDP IDS with capture and four ML models that download with the API key; needs the `-ml` package |
| [`agent-full-ml`](./agent-full-ml/) | agent | yes | `CAP_NET_RAW`, `CAP_BPF`, `CAP_NET_ADMIN`, and `CAP_SYS_ADMIN` or `CAP_PERFMON` | XDP firewall, smart firewall, blocking IDS with managed rules, capture and four ML models; needs the `-ml` package and an API key |
| [`lb-methods`](./lb-methods/) | proxy | no | none: the demo listens on port 8080 | One route per `lb.method` over the same three servers; `maglev`, `ring_hash` and `least_request` need Synapse 0.8.9 or later |
| [`certbot`](./certbot/) | proxy | no | none: the demo listens on ports 8089 and 8443 | Serve and renew certificates that certbot writes, with the webroot challenge routed through Synapse; needs Synapse 0.8.9 or later |
| [`lb-passthrough`](./lb-passthrough/) | proxy | no | none: the demo listens on port 8080 | One passthrough host per `lb.method`, without terminating TLS; `maglev` and `ring_hash` on a passthrough host need Synapse 0.8.9 or later |
| [`lb-source-ip-persistence`](./lb-source-ip-persistence/) | proxy | no | none: the demo listens on port 8080 | Pin a client address to the server it first got, whatever `lb.method` says; needs Synapse 0.8.9 or later |
| [`sticky-sessions`](./sticky-sessions/) | proxy | no | none: the demo listens on port 8088 | Keep a client on the server it first got with a signed cookie, and what happens to a cookie that does not verify; needs Synapse 0.8.9 or later |
Expand Down
189 changes: 189 additions & 0 deletions use-cases/certbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# Certificates from certbot

Let certbot get and renew the certificate, and have Synapse serve it, without
a restart. **Needs Synapse 0.8.9 or later.** Earlier releases read the same
directory, but do not pick up a renewal made by certbot's link swap.

```yaml
proxy:
listeners:
- { name: http, address: "0.0.0.0:80", mode: http }
- { name: https, address: "0.0.0.0:443", mode: tls }
certificates: "/etc/letsencrypt/live"
acme:
enabled: false
```

```yaml
hosts:
app.example.com:
tls:
terminate:
cert: app.example.com
paths:
"/":
upstream: whoami:80
force_https: true
"/.well-known/acme-challenge":
upstream: webroot:80
```

## Things to know

- `proxy.certificates` is certbot's `live` directory: one subdirectory per
domain, each with `fullchain.pem` and `privkey.pem`. The subdirectory is named
after the first `-d` name given to certbot. `cert:` on the host names it.
- The directory MUST exist when Synapse starts. It MAY be empty. A missing
directory is a start-up error (from 0.8.8). While it is empty, a TLS
handshake fails with alert 40 and the log says that no certificates were found.
- Synapse watches the directory and reads the certificate at each handshake. A
renewal needs no restart and no `SIGHUP`.
- `proxy.acme.enabled: false` turns the embedded ACME client off. It is
deprecated.
- With it off, Synapse answers nothing on `/.well-known/acme-challenge` by
itself. The request goes to the host's routes, so a second path sends it to
the directory certbot writes the challenge file to (`--webroot`). Write the
path without a trailing slash: with one, the route does not match and the
challenge goes to the `/` upstream.
- Certbot needs the plaintext listener on port 80 to reach the host from the
CA. The challenge cannot be answered on 443.
- `force_https: true` on `/` redirects every other request on port 80 to HTTPS,
so the app is not served in clear. The challenge route has no `force_https`
and stays plain HTTP. The redirect goes to the first `tls` listener's port
(443); set `proxy.https_redirect_port` if clients reach HTTPS on another
port, as the demo's `8443` mapping does from the host.
- Put the challenge route in before the first issue. Without it the challenge
falls to `/` and is redirected to HTTPS, where there is no certificate yet.
- `certbot renew` is meant to run from cron or a timer. Run it with
`--no-random-sleep-on-renew` when you test it by hand, or it waits up to
several minutes.

## Run

The demo uses [Pebble](https://github.com/letsencrypt/pebble), a test ACME
server, so nothing reaches Let's Encrypt. Docker and Compose are needed.

```sh
cd use-cases/certbot
./make-certs.sh # a test certificate for Pebble's API
docker compose up -d
docker compose run --rm client # before: no certificate yet
docker compose run --rm certbot # issue
docker compose run --rm client # after: the issued chain
```

To renew at once, and see that Synapse keeps running:

```sh
docker inspect -f '{{.State.StartedAt}}' $(docker compose ps -q synapse)
docker compose run --rm --entrypoint certbot certbot \
renew --force-renewal --no-random-sleep-on-renew
docker compose run --rm client
docker inspect -f '{{.State.StartedAt}}' $(docker compose ps -q synapse)
docker compose down -v
```

The image is the 0.8.9 release image. To use a build of your own, set
`SYNAPSE_IMAGE`.

### What it prints

Before issuing, the handshake fails, plain HTTP on `/` is redirected, and the
challenge path reaches the webroot (404, no file yet):

```
== the certificate on app.example.com:443
...SSL alert number 40
== a request on http
HTTP/1.1 301 Moved Permanently
Location: https://app.example.com:443/
== the challenge path on http, with no file behind it
HTTP/1.1 404 Not Found
```

Certbot issues through port 80:

```
Requesting a certificate for app.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/app.example.com/fullchain.pem
```

Synapse then serves the chain, and it verifies against Pebble's root:

```
serial=438B3F45B317B805
issuer=CN=Pebble Intermediate CA 7fb12e
DNS:app.example.com
Verification: OK
```

After the forced renewal the serial has changed and the container's start time
is the same as before (`2026-11-02T12:56:46.334774266Z` in both reads):

```
serial=7E3AAB75D18199E5
```

The serials, the issuer name and the dates differ on every run.

Synapse loads the first certificate when certbot creates its directory under
`/etc/letsencrypt/live`, so it is served without waiting for a renewal. Builds
before this fix, 0.8.9-rc.2 and earlier, could miss a first certificate:
certbot makes the directory and its links within a fraction of a millisecond,
and under load Synapse sometimes saw only the directory. The handshake then
failed with alert 40 until the next change in the directory (the forced renewal
loaded it). If the "after" check shows alert 40 on such a build, look for
`Certificate changed` in the log and update.

With the fixed build, the first certificate was served at the first poll in 10
of 10 runs of this demo, and in 10 of 10 runs with Synapse sharing one CPU with
two busy loops. The same starved runs on a build without the fix served it 1
time in 10.

### What goes wrong

Without the challenge path in the routes, the request goes to `/`, which
redirects it to HTTPS, where there is no certificate yet. The CA reports:

```
Type: connection
Detail: Get "https://app.example.com:443/.well-known/acme-challenge/...":
remote error: tls: handshake failure
```

Without the `live` directory, Synapse does not start:

```
proxy failed to start: listener "https" (0.0.0.0:443, mode=tls): proxy.certificates directory "/etc/letsencrypt/live" does not exist; fix or create it, or remove proxy.certificates
```

That is why `docker-compose.yml` creates it first.

## For production

- Remove the `pebble`, `client` and `init` services, `REQUESTS_CA_BUNDLE` and
the `--server` flag. Certbot then uses Let's Encrypt.
- Replace `--email admin@example.com` with your own address, or use
`--register-unsafely-without-email`. Let's Encrypt refuses `example.com`
contacts. This was not tried against Let's Encrypt.
- Use your own host name for `-d`, for the `hosts:` key and for `cert:`.
- Keep `/etc/letsencrypt` on a volume that Synapse mounts read-only and certbot
mounts read-write, and run `certbot renew` from a timer.
- Port 80 MUST reach Synapse from the internet: the CA fetches the challenge
there. Keep `force_https: true` on `/` and leave it off the challenge route.
That redirects the app to HTTPS and keeps the challenge on plain HTTP.

## Not checked here

The set was run against Pebble only. It was not run against Let's Encrypt, and
the published 0.8.9 image was not pulled: the run used a build of the 0.8.9
branch that includes the first-certificate fix.

## Files

- `config.yaml`, `upstreams.yaml`, `security_rules.yaml`: the Synapse set.
- `docker-compose.yml`: Synapse, a test back-end, the webroot, Pebble, certbot.
- `nginx.conf`: serves the webroot.
- `pebble-config.json`, `make-certs.sh`: the test ACME server.
- `demo.sh`: what the `client` service prints.
25 changes: 25 additions & 0 deletions use-cases/certbot/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Proxy mode, no firewall or capture, so the container needs no privileges.
mode: proxy

firewall:
mode: none
capture:
enabled: false

proxy:
listeners:
# The plaintext listener answers the ACME http-01 challenge. Without it
# no certificate can be issued.
- { name: http, address: "0.0.0.0:80", mode: http }
- { name: https, address: "0.0.0.0:443", mode: tls }
upstream:
conf: "/etc/synapse/upstreams.yaml"
# Certbot's directory: one subdirectory per domain, holding fullchain.pem and
# privkey.pem. The directory must exist when Synapse starts; it may be empty.
certificates: "/etc/letsencrypt/live"
# The embedded ACME client is deprecated. Certbot does the issuing.
acme:
enabled: false

logging:
level: info
28 changes: 28 additions & 0 deletions use-cases/certbot/demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
# Runs in the client container: what Synapse serves for app.example.com now.
# Pebble makes a new root on every start, so fetch the one in use, over its
# own API port, which the test certificate in /pebble-tls secures.
get() { printf 'GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n' "$2" "$1"; }
get pebble /roots/0 | openssl s_client -quiet -CAfile /pebble-tls/cert.pem \
-connect pebble:15000 2>/dev/null | sed -n '/BEGIN CERT/,/END CERT/p' > /tmp/root.pem

echo "== the certificate on app.example.com:443"
out=$(openssl s_client -connect app.example.com:443 -servername app.example.com \
-CAfile /tmp/root.pem </dev/null 2>&1)
cert=$(echo "$out" | openssl x509 2>/dev/null)
if [ -z "$cert" ]; then
echo "$out" | grep -E 'alert|error' | head -3
else
echo "$cert" | openssl x509 -noout -serial -issuer -dates -ext subjectAltName
echo "$out" | grep '^Verification'
fi

echo "== a request on https"
get app.example.com / | openssl s_client -quiet -CAfile /tmp/root.pem \
-connect app.example.com:443 -servername app.example.com 2>&1 | grep -E '^(Hostname|Host):|error'

echo "== a request on http"
get app.example.com / | nc app.example.com 80 | sed -n '1p;2p' | tr -d '\r'

echo "== the challenge path on http, with no file behind it"
wget -S -O /dev/null http://app.example.com/.well-known/acme-challenge/none 2>&1 | grep -E '^ HTTP/'
90 changes: 90 additions & 0 deletions use-cases/certbot/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Run: ./make-certs.sh && docker compose up -d
# The image is the 0.8.9 release image. To use a build of your own, set
# SYNAPSE_IMAGE (for example SYNAPSE_IMAGE=synapse:local docker compose up -d).
#
# Pebble is a test ACME server. It checks the challenge by connecting to
# app.example.com on port 80; the network alias makes that name reach Synapse.
# For a real server, drop pebble and see the README.
services:
# Certbot's directory must exist when Synapse starts. It is a named volume, so
# this one-shot service makes the empty live directory first.
init:
image: busybox:1.36
command: ["mkdir", "-p", "/etc/letsencrypt/live"]
volumes: ["letsencrypt:/etc/letsencrypt"]
synapse:
image: ${SYNAPSE_IMAGE:-registry.gen0sec.com/gen0sec/synapse:0.8.9}
command: ["-c", "/etc/synapse/config.yaml"]
ports: ["8089:80", "8443:443"]
volumes:
- ".:/etc/synapse:ro"
- "letsencrypt:/etc/letsencrypt:ro"
networks:
certbot:
ipv4_address: 172.30.8.10
aliases: [app.example.com]
depends_on:
init: { condition: service_completed_successfully }
whoami: { condition: service_started }
webroot: { condition: service_started }
whoami:
image: traefik/whoami:v1.10.3
hostname: whoami
networks: [certbot]
webroot:
image: nginx:1.27-alpine
volumes:
- "./nginx.conf:/etc/nginx/conf.d/default.conf:ro"
- "webroot:/usr/share/nginx/html:ro"
networks: [certbot]
pebble:
image: ghcr.io/letsencrypt/pebble:2.9.0
command: ["-config", "/etc/pebble/pebble-config.json"]
environment:
# Skip the pauses Pebble adds to behave like a busy CA.
PEBBLE_VA_NOSLEEP: "1"
volumes:
- "./pebble-config.json:/etc/pebble/pebble-config.json:ro"
- "./pebble-tls:/pebble-tls:ro"
networks: [certbot]
certbot:
image: certbot/certbot:v2.11.0
profiles: ["demo"]
environment:
# Trust the test server's certificate. Remove this for a real server.
REQUESTS_CA_BUNDLE: /pebble-tls/cert.pem
command:
- certonly
- --webroot
- -w
- /var/www/html
- -d
- app.example.com
- --server
- https://pebble:14000/dir
- --email
- admin@example.com
- --agree-tos
- --no-eff-email
volumes:
- "letsencrypt:/etc/letsencrypt"
- "webroot:/var/www/html"
- "./pebble-tls:/pebble-tls:ro"
networks: [certbot]
client:
image: alpine/openssl:3.3.2
profiles: ["demo"]
entrypoint: ["/bin/sh", "/demo/demo.sh"]
volumes:
- "./demo.sh:/demo/demo.sh:ro"
- "./pebble-tls:/pebble-tls:ro"
networks: [certbot]

volumes:
letsencrypt:
webroot:

networks:
certbot:
ipam:
config: [{ subnet: 172.30.8.0/24 }]
12 changes: 12 additions & 0 deletions use-cases/certbot/make-certs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
# Make the certificate Pebble, the test ACME server, serves on its own API port,
# in ./pebble-tls, again once it has under a day left. Certbot trusts exactly
# this file. Test material only: with a real ACME server there is nothing to make.
set -eu
cd "$(dirname "$0")"
mkdir -p pebble-tls
# Make it again when it is missing or has under a day left.
openssl x509 -checkend 86400 -noout -in pebble-tls/cert.pem >/dev/null 2>&1 || openssl req -x509 -newkey rsa:2048 -nodes -days 30 \
-subj "/CN=pebble" -addext "subjectAltName=DNS:pebble" \
-keyout pebble-tls/key.pem -out pebble-tls/cert.pem
chmod 644 pebble-tls/key.pem
11 changes: 11 additions & 0 deletions use-cases/certbot/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# The webroot: serves the files certbot writes for the http-01 challenge.
server {
listen 80;
root /usr/share/nginx/html;
location / {
return 404;
}
location /.well-known/acme-challenge/ {
try_files $uri =404;
}
}
12 changes: 12 additions & 0 deletions use-cases/certbot/pebble-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"pebble": {
"listenAddress": "0.0.0.0:14000",
"managementListenAddress": "0.0.0.0:15000",
"certificate": "/pebble-tls/cert.pem",
"privateKey": "/pebble-tls/key.pem",
"httpPort": 80,
"tlsPort": 443,
"ocspResponderURL": "",
"externalAccountBindingRequired": false
}
}
Loading