autocert
Certificates from an ACME certificate authority, such as Let’s Encrypt, for TLS servers on the BEAM, validated through the TLS-ALPN-01 challenge (RFC 8737) of the server that presents them and renewed before they expire, as Go’s autocert and Rust’s 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 repository but does not depend on TrevRPC.
Versioning: this package follows TrevRPC’s release numbers, but depends on no TrevRPC package.
%% rebar.config
{deps, [autocert]}.
{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), 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’scertificateoption takes: a fun that reads the manager’s ETS table without a call, and answers with errors once the manager has stopped, andacme_tls_alpn => truemakes its TCP listener answer the challenge:trevrpc_server:start_link(#{ port => 443, certificate => autocert:resolver(Manager), acme_tls_alpn => true, routes => Routes }). -
An
ssllistener. Listen with{handshake, hello}andacme-tls/1last inalpn_preferred_protocols, and complete each accepted socket’s handshake withautocert:ssl_handshake(Resolve, Socket, Timeout),Resolvebeing oneautocert: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)andautocert: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. 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;
directorynames another, such as its staging one (https://acme-staging-v02.api.letsencrypt.org/directory), whose certificates no client trusts.sslsets 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 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, 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.