Current section
Files
Jump to
Current section
Files
README.md
# autocert
Certificates from an ACME certificate authority, such as [Let's Encrypt](https://letsencrypt.org), for TLS servers on the BEAM, validated through the TLS-ALPN-01 challenge ([RFC 8737](https://www.rfc-editor.org/rfc/rfc8737)) of the server that presents them and renewed before they expire, as Go's [`autocert`](https://pkg.go.dev/golang.org/x/crypto/acme/autocert) and Rust's [`rustls-acme`](https://crates.io/crates/rustls-acme) do. It needs nothing but OTP 29 or later (`ssl`, `public_key`, `crypto`, `inets` and `json`); `gleam_stdlib` is for its Gleam binding. It lives in the [TrevRPC](https://trev.zip/llc/TrevRPC) repository but does not depend on TrevRPC.
> **Versioning:** this package follows TrevRPC's release numbers, but depends on no TrevRPC package.
```erlang
%% rebar.config
{deps, [autocert]}.
```
```erlang
{ok, Manager} = autocert:start_link(#{
domains => [<<"api.example.com">>],
contact => [<<"mailto:ops@example.com">>],
cache_dir => "/var/lib/myapp/acme",
accept_tos => true
}).
```
A manager orders one certificate for all its `domains`, keeps it and the account key in its `cache_dir`, and renews it in the window the certificate authority suggests through ACME Renewal Information ([RFC 9773](https://www.rfc-editor.org/rfc/rfc9773)), or `renew_before` (30 days) before it expires where the authority suggests none. A failed order is tried again after 10 seconds, then after twice as long each time, up to an hour. A TLS server asks it for the certificate of each handshake, and for the challenge certificate of the certificate authority's TLS-ALPN-01 handshakes, which offer only the ALPN protocol `acme-tls/1`:
- **TrevRPC.** `autocert:resolver(Manager)` is what the server's `certificate` option takes: a fun that reads the manager's ETS table without a call, and answers with errors once the manager has stopped, and `acme_tls_alpn => true` makes its TCP listener answer the challenge:
```erlang
trevrpc_server:start_link(#{
port => 443, certificate => autocert:resolver(Manager), acme_tls_alpn => true, routes => Routes
}).
```
- **An `ssl` listener.** Listen with `{handshake, hello}` and `acme-tls/1` last in `alpn_preferred_protocols`, and complete each accepted socket's handshake with `autocert:ssl_handshake(Resolve, Socket, Timeout)`, `Resolve` being one `autocert:resolver(Manager)` for all of them, which returns `{error, acme_challenge}` for the certificate authority's handshakes, after closing them. It takes the manager too, which it then calls for each handshake.
- **Anything else.** `autocert:certificate(Manager, ServerName)` and `autocert:challenge_certificate(Manager, ServerName)` return a map of the DER certificate, its chain and its decoded key, each with a call to the manager.
What the deployment needs:
- **Port 443 over TCP.** The certificate authority validates over TCP on port 443 of each name, with nothing that terminates TLS in front of the server; a load balancer that passes TLS through is fine.
- **One manager per name.** With several replicas behind a name, the certificate authority may reach any of them, so one must get the certificate and share it: managers do not share their pending challenges.
- **A cache.** Without `cache_dir`, every restart registers a new account and orders a new certificate, which counts against the certificate authority's [rate limits](https://letsencrypt.org/docs/rate-limits/). Until the first certificate is issued or loaded, handshakes other than the challenge's fail.
- **The right directory.** Let's Encrypt's production directory is the default; `directory` names another, such as its staging one (`https://acme-staging-v02.api.letsencrypt.org/directory`), whose certificates no client trusts. `ssl` sets the options of the connections to the directory, which verify it against the OS's trusted certificates by default.
The Gleam module `autocert/manager` starts a manager from a `manager.Config` and returns certificates as `Dynamic` maps, which TrevRPC's `server.certificate_from_dynamic` takes. See [Certificate resolvers and ACME](https://trev.zip/llc/TrevRPC/wiki/Erlang-Guide#certificate-resolvers-and-acme) in TrevRPC's Erlang guide.
## Development
Gleam builds and tests the package; there is no rebar3 build. `gleam test` runs the Erlang tests in `test/` and the Gleam ones. `autocert_pebble_tests` gets a certificate from [Pebble](https://github.com/letsencrypt/pebble), Let's Encrypt's ACME test server, with its mock DNS server, so `pebble` and `pebble-challtestsrv` must be on the `PATH`, as they are in the repository's development shell. `nix build .#trevrpc-erlang-autocert` builds the package as `gleam publish` does and runs the tests and dialyzer, and the `trevrpc-erlang-autocert-lint` check runs erlfmt and `gleam format`.