autocert/manager

Certificates from an ACME certificate authority, such as Let’s Encrypt, validated through the TLS-ALPN-01 challenge of the TLS server that presents them, and renewed before they expire. See the autocert Erlang module for the details.

With a TrevRPC server, whose TCP listener answers the challenge:

let assert Ok(manager) =
  manager.start(
    manager.Config(
      ..manager.config(["api.example.com"]),
      contact: ["mailto:ops@example.com"],
      cache_dir: Some("/var/lib/myapp/acme"),
      accept_tos: True,
    ),
  )
let resolve = fn(hello: server.Hello) {
  manager.certificate(manager, hello.server_name, hello.acme_challenge)
  |> result.try(fn(certificate) {
    server.certificate_from_dynamic(certificate) |> result.replace_error(Nil)
  })
}
server.Config(
  ..server.config(443, "", "", routes),
  certificate: Some(resolve),
  acme_tls_alpn: True,
)

Types

pub type Config {
  Config(
    domains: List(String),
    contact: List(String),
    directory: String,
    cache_dir: option.Option(String),
    directory_ca_file: option.Option(String),
    renew_before: Int,
    accept_tos: Bool,
  )
}

Constructors

  • Config(
      domains: List(String),
      contact: List(String),
      directory: String,
      cache_dir: option.Option(String),
      directory_ca_file: option.Option(String),
      renew_before: Int,
      accept_tos: Bool,
    )

    Arguments

    domains

    The DNS names the certificate covers, which the certificate authority validates over TCP on port 443 of each.

    contact

    The account’s contact URIs, such as mailto:ops@example.com.

    directory

    The certificate authority’s ACME directory URL.

    cache_dir

    Where the account key and the certificate are kept across restarts. Without one, a restart registers a new account and orders a new certificate, which counts against the certificate authority’s rate limits.

    directory_ca_file

    A PEM file of the certificates that verify the directory, in place of the OS’s trusted certificates, as for a test certificate authority.

    renew_before

    The seconds before expiry a certificate is renewed when the certificate authority suggests no renewal window; a third of its lifetime if that is less.

    accept_tos

    Must be True, which agrees to the certificate authority’s terms of service.

A running manager, which orders, keeps and renews one certificate for its domains.

pub type Manager

Values

pub fn certificate(
  manager: Manager,
  server_name: option.Option(String),
  acme_challenge: Bool,
) -> Result(dynamic.Dynamic, Nil)

The certificate of a handshake for server_name, or the challenge certificate of a TLS-ALPN-01 one, as an Erlang map of the certificate, its chain and its key, which TrevRPC’s server.certificate_from_dynamic takes. It reads the manager’s table without a call. Error(Nil) until the first certificate is issued, for another name than the domains, and once the manager has stopped.

pub fn config(domains: List(String)) -> Config

A configuration for domains with Let’s Encrypt, renewing 30 days before expiry, which still has to accept the terms of service.

pub const lets_encrypt: String

Let’s Encrypt’s production ACME directory, the default.

pub const lets_encrypt_staging: String

Let’s Encrypt’s staging ACME directory, whose certificates no client trusts, for trying a deployment out without its production rate limits.

pub fn start(config: Config) -> Result(Manager, String)

Starts a manager, linked to the calling process. It loads the account key and the certificate from the cache, and orders a certificate at once if it has none. An error says what is wrong, such as an option or a cache directory it cannot write.

pub fn stop(manager: Manager) -> Nil

Stops the manager. Stopping one that is already stopped does nothing.

✨ Search Document