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:

What the deployment needs:

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.

✨ Search Document