SaltDns.Certificates (Salt v1.0.0)

The optional certificate module: Salt DNS as an ACME client that obtains certificates for a zone (the zone and *.zone, the default) or for one host at any depth inside a zone of the organisation, with or without the wildcard below it (custom certificates). It answers the DNS-01 challenge in its own zones, watches and renews the certificates and keeps them, encrypted with a key of its own, in S3-compatible storage. Off unless SALT_DNS_CERTIFICATES=on; an installation without it is unchanged. See docs/certificates.md.

For the delivery stage

  • current_bundle/1 - names, serial, validity, chain PEM and key PEM of a certificate's current version (the key is decrypted from the store);
  • subscribe/0 - PubSub topic "certificates:current"; a message {:certificate_current_version, info} is broadcast whenever a certificate gets a new current version (info has :certificate_id, :version_id, :serial, :zone_name, :names, :not_after, :organisation_id; it carries no key).

Structure

SaltDns.Certificates.Runner is the order state machine (run by SaltDns.Certificates.Worker, an Oban job that is unique per certificate); SaltDns.Certificates.Watcher is the light process that notices what is due. The pure rules are in Names, Renewal, Caa and Capabilities.

Summary

Types

What plan_external/2 found out about a request for an external certificate.

What plan_custom/3 found out about a request for a custom certificate.

Functions

Changeset for the provider form.

Orders the certificate FOR a zone: the zone and *.zone, any other list is refused (:not_wildcard_pair). Custom certificates (one host, with or without the wildcard) are ordered with create_custom_certificate/2. The zone must be verified (active). attrs: provider_ids (required, tried in this order, each visible to the zone's organisation and registered), profile, publish_caa (default true), names (optional; checked).

Orders a custom certificate: attrs has host, wildcard (boolean; default false), provider_ids, optional profile and publish_caa. The certificate belongs to the most specific zone of the organisation that contains the host. Errors as for plan_custom/3 and create_certificate/2.

Orders an external certificate (plan_external/2 has the attributes and the refusals). It belongs to the organisation and to the proxy group, which serves it without being told to. No CAA record is written: Salt does not serve the host's DNS.

Adds a provider to an organisation (nil for a provider shared by the whole installation) and reads what its directory supports. Reading can fail (the address is wrong, the provider is down); the provider is saved anyway with the error in last_error, and refresh_capabilities/1 tries again.

The current version of a certificate with its key: the names, serial, validity, chain and private key (PEM), decrypted from the store. For the delivery to node agents; nothing else hands out key material, and no API endpoint returns it.

The current version of a certificate, or nil.

Deletes a provider that no certificate lists.

Disables a certificate: nothing is ordered or renewed, its CAA records are removed. The stored versions stay. A disabled certificate never starts or continues an order: jobs that are waiting or scheduled are cancelled, open orders end as cancelled and their challenge records are removed at once, and a job that is running stops at its next step (SaltDns.Certificates.Runner reads the certificate again at every step). The metrics of the certificate disappear with the next measurement.

The names of a certificate for display: [%{name:, unicode:}], the A-label form that is ordered and stored, and the Unicode form (equal when the name is plain ASCII).

Enables a disabled certificate again (a zone can have one live certificate).

True when the module is switched on (SALT_DNS_CERTIFICATES=on).

A sentence for an error of the functions that request or change a certificate.

A certificate of the organisation, or nil.

The live (not disabled) certificate FOR the zone (the zone and its wildcard), or nil.

A provider the organisation may use, or nil.

A provider by id, without an organisation check (internal).

The most specific zone of the organisation that contains the host (Unicode accepted): where a custom certificate for it would belong, so a caller can check who may order there before it looks at anything else.

The kind of a certificate: :zone (the zone and its wildcard), :custom (one host inside a zone, with or without its wildcard) or :external (one host in no zone of Salt, validated by HTTP-01 through a proxy group).

The certificates of an organisation, by zone name and then by names.

The most recent events of a certificate, newest first.

The most recent orders of a certificate, newest first.

The providers an organisation may use: its own and the shared ones.

Versions of a certificate, newest first.

The label a certificate has in the metrics: its host (the wildcard pair) or host (no wildcard).

The names of a certificate as one line: example.org, *.example.org; a name that is internationalised shows its Unicode form first and the stored A-label form in brackets.

Checks a request for a custom certificate without making it: the host (Unicode is accepted, the A-label form is used) and whether the wildcard below it is included. Returns the names, the most specific zone of the organisation that contains the host, and warnings that do not stop the request, or {:error, {reason, message}} with a message a person understands. Used by the console to show the result before the request.

Checks a request for an external certificate without making it. attrs: host, proxy_group_id and optionally provider_ids (when given it must be exactly the provider the group answers for). Returns the plan with the result of the pre-check (the host may not point at the group yet: that does not stop the request, it stops each order until it does) and the DNS record the customer must create, or {:error, {reason, message}}

Reads the provider's directory again. Returns {:ok | :error, provider}.

Registers the account at the provider (once): a new key, the binding key if the provider wants one, agreement to its terms of service. The account key is stored encrypted, the account address in account_url.

Asks for a new version now. It still obeys the daily limit on orders.

Number of certificates per status (every status present).

Subscribes the caller to new-current-version messages.

Changes the provider list (the order of providers), the profile and the CAA option of a certificate. The checks are those of the request (create_certificate/2): at least one provider, each visible to the organisation and registered. A different provider list resets the fall-back position to the first provider. The change is an event of the certificate; the CAA records follow at once. attrs keys: provider_ids, profile, publish_caa; keys that are absent stay as they are.

Changes a provider's name, contact, binding key or preferred profile.

Types

external_plan()

@type external_plan() :: %{
  host: String.t(),
  unicode: String.t(),
  names: [String.t()],
  group: SaltDns.Proxy.Group.t(),
  provider: SaltDns.Certificates.Provider.t(),
  precheck: {:ok, map()} | {:error, {:precheck, String.t()}},
  dns: map()
}

What plan_external/2 found out about a request for an external certificate.

plan()

@type plan() :: %{
  host: String.t(),
  unicode: String.t(),
  names: [String.t()],
  unicode_names: [String.t()],
  zone: SaltDns.Zones.Zone.t(),
  warnings: [String.t()]
}

What plan_custom/3 found out about a request for a custom certificate.

Functions

change_provider(provider, attrs \\ %{})

@spec change_provider(SaltDns.Certificates.Provider.t(), map()) :: Ecto.Changeset.t()

Changeset for the provider form.

create_certificate(zone, attrs)

@spec create_certificate(SaltDns.Zones.Zone.t(), map()) ::
  {:ok, SaltDns.Certificates.Certificate.t()}
  | {:error,
     :zone_not_verified
     | :already_exists
     | SaltDns.Certificates.Names.error()
     | {:providers, String.t()}
     | {:budget, String.t()}}

Orders the certificate FOR a zone: the zone and *.zone, any other list is refused (:not_wildcard_pair). Custom certificates (one host, with or without the wildcard) are ordered with create_custom_certificate/2. The zone must be verified (active). attrs: provider_ids (required, tried in this order, each visible to the zone's organisation and registered), profile, publish_caa (default true), names (optional; checked).

create_custom_certificate(organisation, attrs)

@spec create_custom_certificate(SaltDns.Accounts.Organisation.t(), map()) ::
  {:ok, SaltDns.Certificates.Certificate.t()} | {:error, term()}

Orders a custom certificate: attrs has host, wildcard (boolean; default false), provider_ids, optional profile and publish_caa. The certificate belongs to the most specific zone of the organisation that contains the host. Errors as for plan_custom/3 and create_certificate/2.

create_external_certificate(organisation, attrs)

@spec create_external_certificate(SaltDns.Accounts.Organisation.t(), map()) ::
  {:ok, SaltDns.Certificates.Certificate.t()} | {:error, term()}

Orders an external certificate (plan_external/2 has the attributes and the refusals). It belongs to the organisation and to the proxy group, which serves it without being told to. No CAA record is written: Salt does not serve the host's DNS.

create_provider(organisation, attrs)

@spec create_provider(SaltDns.Accounts.Organisation.t() | nil, map()) ::
  {:ok, SaltDns.Certificates.Provider.t()} | {:error, Ecto.Changeset.t()}

Adds a provider to an organisation (nil for a provider shared by the whole installation) and reads what its directory supports. Reading can fail (the address is wrong, the provider is down); the provider is saved anyway with the error in last_error, and refresh_capabilities/1 tries again.

current_bundle(certificate_id)

@spec current_bundle(integer()) ::
  {:ok,
   %{
     certificate_id: integer(),
     version_id: integer(),
     names: [String.t()],
     serial: String.t(),
     not_before: DateTime.t(),
     not_after: DateTime.t(),
     chain_pem: String.t(),
     key_pem: String.t()
   }}
  | {:error, :not_found | :no_version | term()}

The current version of a certificate with its key: the names, serial, validity, chain and private key (PEM), decrypted from the store. For the delivery to node agents; nothing else hands out key material, and no API endpoint returns it.

current_version(certificate)

The current version of a certificate, or nil.

delete_provider(provider)

@spec delete_provider(SaltDns.Certificates.Provider.t()) ::
  {:ok, SaltDns.Certificates.Provider.t()} | {:error, :in_use}

Deletes a provider that no certificate lists.

disable_certificate(certificate)

Disables a certificate: nothing is ordered or renewed, its CAA records are removed. The stored versions stay. A disabled certificate never starts or continues an order: jobs that are waiting or scheduled are cancelled, open orders end as cancelled and their challenge records are removed at once, and a job that is running stops at its next step (SaltDns.Certificates.Runner reads the certificate again at every step). The metrics of the certificate disappear with the next measurement.

display_names(certificate)

@spec display_names(SaltDns.Certificates.Certificate.t()) :: [
  %{name: String.t(), unicode: String.t()}
]

The names of a certificate for display: [%{name:, unicode:}], the A-label form that is ordered and stored, and the Unicode form (equal when the name is plain ASCII).

enable_certificate(certificate)

@spec enable_certificate(SaltDns.Certificates.Certificate.t()) ::
  {:ok, SaltDns.Certificates.Certificate.t()} | {:error, :already_exists}

Enables a disabled certificate again (a zone can have one live certificate).

enabled?()

@spec enabled?() :: boolean()

True when the module is switched on (SALT_DNS_CERTIFICATES=on).

error_message(code)

@spec error_message(term()) :: String.t()

A sentence for an error of the functions that request or change a certificate.

get_certificate(organisation, id)

A certificate of the organisation, or nil.

get_for_zone(zone)

@spec get_for_zone(SaltDns.Zones.Zone.t()) ::
  SaltDns.Certificates.Certificate.t() | nil

The live (not disabled) certificate FOR the zone (the zone and its wildcard), or nil.

get_provider(organisation, id)

A provider the organisation may use, or nil.

get_provider!(id)

@spec get_provider!(integer()) :: SaltDns.Certificates.Provider.t()

A provider by id, without an organisation check (internal).

host_zone(organisation, host_input)

@spec host_zone(SaltDns.Accounts.Organisation.t(), term()) ::
  {:ok, SaltDns.Zones.Zone.t()} | {:error, {atom(), String.t()}}

The most specific zone of the organisation that contains the host (Unicode accepted): where a custom certificate for it would belong, so a caller can check who may order there before it looks at anything else.

kind(certificate)

@spec kind(SaltDns.Certificates.Certificate.t()) :: :zone | :custom | :external

The kind of a certificate: :zone (the zone and its wildcard), :custom (one host inside a zone, with or without its wildcard) or :external (one host in no zone of Salt, validated by HTTP-01 through a proxy group).

list_certificates(organisation)

The certificates of an organisation, by zone name and then by names.

list_events(certificate, limit \\ 50)

The most recent events of a certificate, newest first.

list_orders(certificate, limit \\ 10)

The most recent orders of a certificate, newest first.

list_providers(organisation)

The providers an organisation may use: its own and the shared ones.

list_versions(certificate)

Versions of a certificate, newest first.

metric_label(certificate)

@spec metric_label(SaltDns.Certificates.Certificate.t()) :: String.t()

The label a certificate has in the metrics: its host (the wildcard pair) or host (no wildcard).

names_text(certificate)

@spec names_text(SaltDns.Certificates.Certificate.t()) :: String.t()

The names of a certificate as one line: example.org, *.example.org; a name that is internationalised shows its Unicode form first and the stored A-label form in brackets.

plan_custom(organisation, host_input, wildcard)

@spec plan_custom(SaltDns.Accounts.Organisation.t(), term(), term()) ::
  {:ok, plan()} | {:error, {atom(), String.t()} | :already_exists}

Checks a request for a custom certificate without making it: the host (Unicode is accepted, the A-label form is used) and whether the wildcard below it is included. Returns the names, the most specific zone of the organisation that contains the host, and warnings that do not stop the request, or {:error, {reason, message}} with a message a person understands. Used by the console to show the result before the request.

Refused: a host that is not valid (:bad_host), inside no zone of the organisation (:no_zone), in a zone that is not verified (:zone_not_verified), under a delegation (:delegated), with a CNAME at its challenge name (:challenge_cname), the same names as a certificate the organisation has already (:already_exists), or over the weekly budget of new certificates for the registered domain (:budget).

plan_external(organisation, attrs)

@spec plan_external(SaltDns.Accounts.Organisation.t(), map()) ::
  {:ok, external_plan()} | {:error, {atom(), String.t()} | :already_exists}

Checks a request for an external certificate without making it. attrs: host, proxy_group_id and optionally provider_ids (when given it must be exactly the provider the group answers for). Returns the plan with the result of the pre-check (the host may not point at the group yet: that does not stop the request, it stops each order until it does) and the DNS record the customer must create, or {:error, {reason, message}}:

refresh_capabilities(provider)

@spec refresh_capabilities(SaltDns.Certificates.Provider.t()) ::
  {:ok | :error, SaltDns.Certificates.Provider.t()}

Reads the provider's directory again. Returns {:ok | :error, provider}.

register_provider(provider)

@spec register_provider(SaltDns.Certificates.Provider.t()) ::
  {:ok, SaltDns.Certificates.Provider.t()} | {:error, String.t()}

Registers the account at the provider (once): a new key, the binding key if the provider wants one, agreement to its terms of service. The account key is stored encrypted, the account address in account_url.

request_renewal(certificate)

@spec request_renewal(SaltDns.Certificates.Certificate.t()) ::
  {:ok, SaltDns.Certificates.Certificate.t()} | {:error, :disabled}

Asks for a new version now. It still obeys the daily limit on orders.

status_counts()

@spec status_counts() :: %{required(atom()) => non_neg_integer()}

Number of certificates per status (every status present).

subscribe()

@spec subscribe() :: :ok | {:error, term()}

Subscribes the caller to new-current-version messages.

update_certificate(certificate, attrs)

@spec update_certificate(SaltDns.Certificates.Certificate.t(), map()) ::
  {:ok, SaltDns.Certificates.Certificate.t()}
  | {:error, {:providers, String.t()}}

Changes the provider list (the order of providers), the profile and the CAA option of a certificate. The checks are those of the request (create_certificate/2): at least one provider, each visible to the organisation and registered. A different provider list resets the fall-back position to the first provider. The change is an event of the certificate; the CAA records follow at once. attrs keys: provider_ids, profile, publish_caa; keys that are absent stay as they are.

update_provider(provider, attrs)

@spec update_provider(SaltDns.Certificates.Provider.t(), map()) ::
  {:ok, SaltDns.Certificates.Provider.t()} | {:error, Ecto.Changeset.t()}

Changes a provider's name, contact, binding key or preferred profile.