# Mutual TLS with Cloudflare

Run Charter behind Cloudflare Authenticated Origin Pulls so only Cloudflare can talk to your origin over HTTPS.

You may put Cloudflare in front of Charter and require Authenticated Origin Pulls so only Cloudflare can talk to your origin over HTTPS. This guide assumes Charter is already installed on Ubuntu as in [Install on Ubuntu](/docs/install-on-ubuntu/).

## What you need

- An Ubuntu host with Charter installed and supervised, as in [Install on Ubuntu](/docs/install-on-ubuntu/)
- A domain name you control, and a Cloudflare zone for that domain with Domain Name System (DNS) proxied through Cloudflare (the orange cloud)
- A Cloudflare Origin Certificate, or another server certificate Cloudflare accepts in Full or Full (strict) mode, for `--tls-cert` and `--tls-key`
- Cloudflare's zone-level Authenticated Origin Pull client certificate authority (CA) file for `--tls-client-ca`
- A plain Hypertext Transfer Protocol (HTTP) `--health-addr` on a port Cloudflare does not publish, so health checks can call `/healthz` without a client certificate

Transport Layer Security (TLS) is the encryption that turns HTTP into HTTPS. Without TLS flags, `charter serve` listens in plain HTTP only. `charter dev` has no TLS flags. Mutual TLS (mTLS) means both sides present a certificate: your origin presents its server certificate, and Cloudflare presents a client certificate that Charter verifies.

> [!WARNING]
> The account-level mTLS CA is for client certificates that browsers present to Cloudflare. That is a different file. Do not pass it as `--tls-client-ca` for Authenticated Origin Pulls.

See [Cloudflare's guide to Authenticated Origin Pulls](https://developers.cloudflare.com/ssl/origin-configuration/authenticated-origin-pull/).

Addresses and firewall rules for this guide:

- Application Programming Interface (API) listener: `0.0.0.0:443`, HTTPS with Authenticated Origin Pulls
- Health listener: `127.0.0.1:3001`, plain HTTP `GET /healthz` only, not published through Cloudflare
- Host firewall: allow Transmission Control Protocol (TCP) 22 for Secure Shell (SSH) and 443 for HTTPS. Do not open the health port on the public network interface.

Port 443 is a privileged port. The systemd unit below grants `CAP_NET_BIND_SERVICE` so the `charter` user can bind it without running as root. See [systemd's execution options](https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html).

## Point the domain through Cloudflare

- Add an A or AAAA DNS record for the hostname that points at the Ubuntu host
- Proxy the record (orange cloud)
- Set SSL/TLS encryption mode to Full or Full (strict)

Cloudflare documents [encryption modes](https://developers.cloudflare.com/ssl/origin-configuration/ssl-modes/).

## Enable Authenticated Origin Pulls

Authenticated Origin Pulls make Cloudflare present a client certificate when it connects to your origin. Charter can require that certificate so random clients on the internet cannot talk to the origin directly.

- Under SSL/TLS → Origin Server, create or download an Origin Certificate. Put the certificate and private key where the Charter process can read them, or pass them as inline Privacy-Enhanced Mail (PEM) text. See [Environment variables](/docs/environment-variables/) for the PEM rules.
- Download Cloudflare's [Authenticated Origin Pull CA PEM](https://developers.cloudflare.com/ssl/static/authenticated_origin_pull_ca.pem). The same page under SSL/TLS → Origin Server → Authenticated Origin Pulls also links it.
- On SSL/TLS → Origin Server → Authenticated Origin Pulls, turn on the zone-level toggle so Cloudflare presents its client certificate to your origin.

Example file layout on the host from [Install on Ubuntu](/docs/install-on-ubuntu/):

```sh
sudo install -d -o charter -g charter -m 750 /etc/charter
sudo install -o charter -g charter -m 644 origin.pem /etc/charter/origin.pem
sudo install -o charter -g charter -m 600 origin.key /etc/charter/origin.key
sudo install -o charter -g charter -m 644 authenticated_origin_pull_ca.pem \
  /etc/charter/authenticated_origin_pull_ca.pem
```

## Supervise with systemd

If you already created `charter.service` in [Install on Ubuntu](/docs/install-on-ubuntu/), replace the `ExecStart` block (and add the capability lines) so Charter listens on 443 with TLS. Keep `EnvironmentFile=/etc/charter/charter.env` for `DATABASE_URL` and other settings. You can keep TLS paths in the unit file, or set `CHARTER_TLS_*` and `CHARTER_HEALTH_ADDR` in the env file instead.

`/etc/systemd/system/charter.service`:

```
[Unit]
Description=Charter API
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=charter
Group=charter
WorkingDirectory=/var/lib/charter
EnvironmentFile=/etc/charter/charter.env
ExecStart=/usr/local/bin/charter serve \
  --host 0.0.0.0 \
  --port 443 \
  --tls-cert /etc/charter/origin.pem \
  --tls-key /etc/charter/origin.key \
  --tls-client-ca /etc/charter/authenticated_origin_pull_ca.pem \
  --health-addr 127.0.0.1:3001
Restart=on-failure
RestartSec=5
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
```

Reload and restart:

```sh
sudo systemctl daemon-reload
sudo systemctl restart charter.service
sudo systemctl status charter.service
```

## Run charter serve

These flags match the unit above if you run Charter by hand:

```sh
charter serve \
  --host 0.0.0.0 \
  --port 443 \
  --tls-cert /etc/charter/origin.pem \
  --tls-key /etc/charter/origin.key \
  --tls-client-ca /etc/charter/authenticated_origin_pull_ca.pem \
  --health-addr 127.0.0.1:3001
```

When the flags are absent, the same settings come from the environment: `CHARTER_HOST`, `CHARTER_PORT`, `CHARTER_TLS_CERT`, `CHARTER_TLS_KEY`, `CHARTER_TLS_CLIENT_CA`, `CHARTER_HEALTH_ADDR`. A value is PEM when it contains `-----BEGIN`. Otherwise it is a file path. Real newlines and the two-character sequence `\n` both work in `.env` files.

With certificate, key, and client CA set:

- The API listener (`--host` / `--port`) speaks HTTPS and rejects handshakes that omit a client certificate chaining to that CA. Callers that skip Cloudflare fail the handshake.
- `--health-addr` serves only `GET /healthz` in plain HTTP. Point local probes there. Every other path on that listener returns 404. `/readyz` stays on the API listener.

If you omit `--tls-client-ca`, the listener still encrypts traffic, but it accepts clients that present no certificate. That is enough for Full (strict) TLS to the origin. It does not enforce Authenticated Origin Pulls.

## Check it

From a machine that can reach the origin IP address directly, not through Cloudflare:

```sh
# Should fail the TLS handshake (no client cert).
curl -vk https://ORIGIN_IP/

# On the Ubuntu host: should return 200 ok.
curl -sS http://127.0.0.1:3001/healthz
```

Through Cloudflare, requests succeed once the zone toggle is on and DNS is orange-clouded.
