HTTPS
Give the server certificate files and it speaks TLS itself, with no proxy in front. It needs OpenSSL 3: Homebrew's openssl@3 on macOS (never the system LibreSSL in /usr/lib), libssl.so.3 on Linux, or wherever JOLT_OPENSSL_LIBDIR points. A server without :tls never loads it.
(http/run-server handler
{:port 443 :host "0.0.0.0"
:tls {:certs [{:cert "/etc/letsencrypt/live/example.com/fullchain.pem"
:key "/etc/letsencrypt/live/example.com/privkey.pem"}
{:cert "/etc/letsencrypt/live/other.org/fullchain.pem"
:key "/etc/letsencrypt/live/other.org/privkey.pem"}]
:reload-ms 60000 ; how often the files are checked (default)
:handshake-ms 10000} ; a client gets this long to finish its handshake (default)
:redirect-http {:port 80}}) ; optional: plain HTTP answers 301 to https
Each entry is a PEM certificate chain and its key. The server reads the name the client asks for (SNI) and answers with the first certificate that covers it, wildcards included. The first entry is the default, for a client that sends no name or one nothing covers. Names compare without regard to case, and a trailing dot is ignored. Every pair is loaded and checked before the port is bound, so a missing file or a key that doesn't match its certificate throws at start, with the file named in the ex-data.
The files are checked every :reload-ms, and a pair whose size or mtime changed is loaded again, so a renewal from certbot takes effect within a minute without a restart. If the new files don't load, say a renewal caught halfway, the old certificate stays and the failure reaches :on-error as :tls-reload-failed. The next check tries again.
Only TLS 1.2 and 1.3 are accepted, and requests carry :scheme :https. A client that connects and never finishes its handshake is closed after :handshake-ms, and one that sends plain HTTP to the TLS port just gets a closed connection. Failed handshakes reach :on-error as :tls-handshake-failed, but the default hook doesn't print them, since scanners on the open internet make a lot of them. Past :max-connections, a new TLS connection is closed without the plaintext 503.
Redirects and HSTS
:redirect-http {:port 80} starts a second, plain listener that answers every request with a 301 to the same host and path over https. :https-port sets the port in that URL and defaults to the TLS server's own, so behind port 443 the URL carries no port. A Host that isn't a plain host name gets 400. The handle from run-server gains :redirect-port, and stop-server stops both listeners.
plumeria.https/wrap-hsts adds Strict-Transport-Security to responses over https:
(https/wrap-hsts handler {:max-age 31536000 :include-subdomains? false :preload? false})
Browsers remember that header for max-age seconds and then refuse plain HTTP to the host, so start with a small value until HTTPS is known to work.
Automatic certificates
Plumeria can get and renew its own certificate from Let's Encrypt, so a site runs on 443 and 80 with no certbot, no cron job and no second program holding port 80. It uses the ACME protocol (RFC 8555) with the HTTP-01 challenge, which the :redirect-http listener answers. It works for one certificate covering one or more DNS names on one host. Wildcards, IP addresses, DNS-01 and several hosts sharing a certificate aren't supported, and :certs stays as it is for anyone who keeps certbot.
(http/run-server handler
{:port 443 :host "0.0.0.0"
:tls {:acme {:domains ["example.com" "www.example.com"]
:directory :staging ; :production, or a directory URL
:dir "/var/lib/plumeria/acme"
:agree-tos true
:email "ops@example.com"}} ; optional
:redirect-http {:port 80}
:run-as {:user "plumeria"}})
:acme goes inside :tls in place of :certs, and exactly one of the two is allowed. Every option is checked before anything binds, and a bad one throws plumeria: :acme ....
:domainsis a non-empty vector of DNS names, all on the one certificate. Names must be ASCII, so write an internationalized name in itsxn--form.:directoryis required and has no default.:stagingand:productionname Let's Encrypt's two directories, and a string is any ACME directory. Requiring it means a deploy can't end up with untrusted staging certificates or spend production rate limits.:agree-tosmust betrue. The account is created with the terms of service accepted, and plumeria won't accept them for you.:diris where the account, key and certificate live (below).:emailis the account's contact address and nothing more. Let's Encrypt doesn't send expiry warnings to it any more (see Monitoring).:ca-fileis a PEM file of roots to trust for the ACME server's own certificate. Leave it out for Let's Encrypt: the system trust store verified its staging server on macOS with no:ca-file. It is for private CAs such as pebble, the test CA used byjolt acme-check.:redirect-httpis required, since the challenge arrives on port 80.
ACME needs jolt-lang/crypto, which plumeria doesn't pull in for apps that don't use it. Add it to your deps.edn with the coordinate this repo's :test alias uses:
jolt-lang/crypto {:git/url "https://github.com/jolt-lang/crypto"
:git/tag "v0.5.0"
:git/sha "14626d0dd9638add6d3d906f4949bfdad1dca2e4"}
Without it, startup throws one message that names the dependency. OpenSSL 3 is needed as for any :tls.
What's in :dir
The directory is created with mode 0700 if it's missing. Every write goes to a temporary file with its permissions already set and is then renamed into place, so a crash never leaves a half-written key. An existing directory is never changed. One with group or other permission bits makes startup fail, naming it as "readable by others" and telling you to run chmod 700 on it.
| file | holds |
|---|---|
account.key, account.pub | the ACME account's P-256 key pair (the public half is stored because jolt-lang/crypto can't derive it from the private PEM), key 0600 |
account.edn | the account URL and the directory URL it belongs to |
cert.key | the current certificate's P-256 key, 0600 |
fullchain.pem | the current certificate and its chain |
cert.edn | the directory URL that issued fullchain.pem |
state.edn | the next attempt allowed and how many attempts failed in a row |
An account belongs to one directory. Moving from staging to production makes a second one beside the first, in files named account-<hash>.*. Every new certificate gets a new key. fullchain.pem and cert.key are ordinary PEM files, so they also work as a :certs pair, and certbot's files can be copied in to seed the directory. A key and certificate that don't match count as no certificate.
First start and renewal
With no usable certificate, the server starts anyway. Port 443 accepts and closes each connection at once, and port 80 answers the challenge path for pending tokens and 503 with a Retry-After for everything else, so the site is never served over plain HTTP. When the certificate arrives, 443 starts handshaking and port 80 goes back to the 301 redirect, with no restart and no connection disturbed. At start, when there's no usable certificate, :on-error gets one :acme-no-certificate event with the reason, and the order runs in the background. Failures arrive as :acme-failed.
A certificate that's on disk and valid is served at once. It is still replaced in the background when it doesn't cover every name in :domains, when it came from another directory (staging, then production), or when cert.edn is missing. The old one keeps serving until the new one is ready.
The task checks about every 12 hours, with up to an hour of jitter, and renews when a third of the certificate's lifetime is left, which is about 30 days of 90 and scales down for shorter lifetimes. The new certificate is swapped in without a restart, and handshakes already under way finish on the old one. One order is in flight at a time.
A failure leaves the serving certificate alone and retries later: after a minute, doubling to 4 hours. A failed authorization waits at least 15 minutes, because Let's Encrypt allows only a handful of failed validations per name per hour and the usual cause is DNS not pointing at this host yet. A Retry-After from the CA is honoured, but never below plumeria's own backoff and never above 24 hours. The next allowed attempt is written to state.edn, so a crash loop or restarting by hand can't send an order per start. Every order sends the account request again, which is idempotent, so a CA that has forgotten the account recovers on its own. A badNonce answer is retried with the fresh nonce.
Staging first, then production
Before the first start, point each name's DNS at the host and make sure port 80 reaches the process. Plumeria doesn't check this itself, since fetching its own challenge URL fails on hosts without hairpin NAT. Start with :directory :staging, confirm that a browser shows the staging chain, then change to :production. Staging certificates are untrusted on purpose and its rate limits are generous. The switch makes a new account and a background reissue, and the staging certificate keeps serving until the real one is in.
With :run-as
Every port binds as root, then the process drops to the target user, and only then does the ACME task start. A missing directory is created as root and handed to that user, so the dropped process can write it. One that already exists keeps its owner and must already be writable by that user. If the process can't write it, startup fails with a message naming the directory. Keys never leave a 0600 file inside a 0700 directory, and no private key appears in a log line or an :on-error event.
Monitoring
Let's Encrypt stopped sending expiry emails in 2025, so a renewal that keeps failing is invisible unless you look for it. Two ways to look:
- Alert on
acme-failedlines. The default:on-errorprints one per failure, with the step, the CA's problem type and detail, when the serving certificate expires and when the next attempt is. See logging.md. - Poll
(:acme-status handle), a fn answering{:not-after :next-attempt :failures :last-problem}: the serving certificate's expiry (nil when none), the time of the next attempt, the number of failed attempts in a row and the last problem.
Also check the certificate's expiry from outside, from a monitor that connects to port 443 the way a browser does. That catches a stopped process as well, which neither of the two above can.
Testing it
jolt acme-check runs the whole thing against pebble, Let's Encrypt's test CA. Install pebble (go install github.com/letsencrypt/pebble/v2/cmd/pebble@latest), set PEBBLE to its binary if it isn't on PATH, and set PEBBLE_DIR to a pebble checkout (the task reads its test CA and config from there). It starts pebble, runs a server for localhost, waits up to 60 seconds for a handshake that verifies against pebble's root, and prints ACME-CHECK OK. Without pebble it prints a SKIP line and exits 0. pebble rejects 5% of nonces on purpose, which exercises the badNonce retry. No test or task ever contacts Let's Encrypt's production server.
Running on 443 and 80
Binding ports below 1024 takes root. Started as root, plumeria can give it up with :run-as: it binds every listener (the HTTPS one and :redirect-http's), becomes that user, checks that it can't get root back, and only then accepts a connection, so no request is ever served as root.
(http/run-server handler {:port 443 :host "0.0.0.0"
:tls {:certs [{:cert "/etc/letsencrypt/live/example.com/fullchain.pem"
:key "/etc/letsencrypt/live/example.com/privkey.pem"}]}
:redirect-http {:port 80}
:run-as {:user "plumeria"}})
:user is a name or a uid, and :group (optional) a name or a gid. The process takes the user's groups from the system, so a key group the user belongs to (Debian's ssl-cert, say) still applies. A target of uid or gid 0 is refused, and so is a user who would keep group 0 (wheel or root). Already running as that user, :run-as does nothing. Started as anyone else, it throws before binding. If any step of the drop or its check fails, the listeners are closed and run-server throws.
Certificate reload keeps working only if the new user can read the PEM files. certbot keeps them root-only, so give the plumeria user's group read access, for example from a certbot deploy hook:
chgrp -R plumeria /etc/letsencrypt/live /etc/letsencrypt/archive
chmod 0750 /etc/letsencrypt/live /etc/letsencrypt/archive /etc/letsencrypt/archive/*
chmod 0640 /etc/letsencrypt/archive/*/privkey*.pem
When a file can't be read after the drop, reload turns itself off and :on-error gets one :tls-reload-disabled event naming the files (the default hook prints plumeria: tls-reload-disabled files ...). Renewed certificates then load on the next restart.
On Linux under systemd you can skip root entirely: run as the plumeria user with User=plumeria and AmbientCapabilities=CAP_NET_BIND_SERVICE, which allows binding 443 and 80, and leave :run-as out.
The test suite runs unprivileged, so it can't make the real switch. sudo env PATH="$PATH" jolt drop-check does: it starts HTTPS and the redirect with :run-as nobody and checks the ids, that setuid(0) fails, that both listeners answer, and both reload cases. On Linux it also checks that every thread of the process changed user, so run it on the host you deploy to.
Without :run-as, your handler code runs as root, and so does every library it uses. Keep the handler small and careful then, and serve files only through wrap-static, which refuses paths that leave its root.
Keep a connection's I/O inside the handler
A handler that passes a stream's write! or a streamed :body to another fiber can interleave writes or write after the connection has ended. Over HTTPS each connection's OpenSSL calls take a lock, and a write after the end answers false, so the worst case is a garbled or closed connection, the same as over plain HTTP.
Cost
Keep-alive over TLS (TLS 1.2, an ECDSA P-256 certificate) ran at 0.75-0.85 of the plain rate with 10 clients and 0.56-0.76 with 100, over two runs. A new connection costs about 0.86 ms of server CPU, handshake and request together; see performance.md.