SaltDns.Proxy (Salt v1.0.0)

Routing for the load balancers Shaker manages: proxy groups, their backends and routes, and which certificates a group serves. Active only with SALT_DNS_SHAKER=on (which needs SALT_DNS_CERTIFICATES=on); see docs/shaker.md.

An organisation owns its groups. Every change goes through a function here, which validates (SaltDns.Proxy.Routes), writes, raises the group's generation in the database, records an audit row (SaltDns.Proxy.Change, with the SaltDns.Zones.Actor that made it) and, after the commit, asks the connected nodes of the group to fetch the new state (SaltDns.Shaker.notify/1). Exactly one replica does all of that for a given change: the one that handled it.

Which certificates a group serves

Every live certificate of the organisation (not disabled, with a current version) that covers at least one route host of the group (SaltDns.Proxy.Coverage, from the certificate's names), plus every certificate attached by hand, used by a route or not. A route no current certificate covers is allowed; route_coverage/1 shows it without one, because visitors would get a TLS error.

Certificate events

A new current version changes what nodes must hold. certificate_changed/1 (called by SaltDns.Proxy.Watcher for each PubSub event of the certificate module) raises the generation of each group that serves the certificate with one conditional UPDATE that also records the version, so two replicas seeing the same event raise it once. It returns the groups it changed.

Summary

Functions

Every group of every organisation, by organisation and name (metrics).

Attaches a certificate to the group (it is served even when no route needs it). The certificate belongs to the group's organisation or to one of its direct children (parent_organisation_id): one level down, never upward (a child's group cannot serve the parent's certificates), never a sibling's or a grandchild's. Anything else is {:error, :not_found}. Whether the actor may do it is decided before, by attachable_certificate/3; this function checks only the shape.

The certificate id as the actor may attach it to the group, or {:error, :not_found}.

The live certificates the actor could attach to the group and that are not attached yet: [%{certificate:, organisation:}], those of the group's organisation first, then those of its direct children the actor may attach (attachable_certificate/3). organisation is the owning organisation (the console labels the children's).

The ids of the certificates attached by hand.

The state plaintext (contract 3.4) for one node of a group. The configuration is rendered with what the node reported: its socket group (a number renders gid) and whether it runs in a container (outside one, HAProxy drops privileges to the group's run_user and run_group).

Raises the generation of the groups (inside the caller's transaction, if any), so their nodes fetch a state without a certificate that was disabled or deleted. Call notify_groups/1 after the commit.

Handles a {:certificate_current_version, info} event: raises the generation of every group of the organisation (or of its parent) that serves the certificate, once per (certificate, version) however many replicas see the event. Returns the ids of the groups it raised.

The id a certificate has on the wire.

The plaintext of a certificate envelope for a node of the group: only for a certificate the group serves, only its current version.

For each certificate the group serves: the serial the node should serve and the serial it reported, %{id, zone, names, expected, served, loaded, match}. match is true/false once the node reported the certificate (case and leading zeros ignored) and nil when it has not.

Changeset for the backend form.

Changeset for the group form.

Changeset for the route form.

Creates a group. The generation starts at 1.

Creates a one-time enrolment token for the group. The plaintext is returned once and not stored.

Deletes a backend that no route and no default points at ({:error, :in_use}).

Deletes a group with its backends, routes and attachments. Refused while a node that is not retired still belongs to it ({:error, :in_use}).

Deletes the route for host.

Removes an attachment (a certificate that covers a route is still served). Works for a certificate of a child organisation as well, and writes the second audit entry in the child's organisation.

The certificate id to detach: one attached to the group, or any certificate of the group's organisation. {:error, :not_found} for the rest.

The exact hosts in no active zone that more than one proxy group routes: [%{host:, group_ids: [id]}]. Such duplicates can only come from before the first-come rule existed or from a zone that stopped being active; no certificate is ordered for them (SaltDns.Certificates.Http01.before_order/2). The operator resolves them by removing all but one route (SaltDns.Release.duplicate_outside_routes/0 prints them).

True when routing and the Shaker channel are switched on (SALT_DNS_SHAKER=on).

A group by name across all organisations, for the release commands that only know a name. {:error, :ambiguous} when two organisations use it.

A public key's fingerprint as the contract shows it (3.8): SHA-256 of the key text as exchanged, hex, eight groups of eight digits.

A backend of the group by name, or nil.

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

A group of the organisation by id, or nil.

A node of the organisation by id, or nil.

A route of the group by host (normalised first), or nil.

The group a node belongs to, or nil.

The backends of a group, by name.

The latest audit entries of a group, newest first.

The entries recorded in organisation's audit trail about its certificates being attached to, or detached from, a proxy group of another organisation (its parent's), newest first: [%{id:, at:, action:, group_name:, group_organisation:, certificate_id:, zone:, names:}]. It shows the group's name and the parent organisation's name and nothing else of the parent.

The groups of an organisation, by name.

The nodes of a group, by hostname.

The nodes of every group of an organisation, by hostname.

The routes of a group, exact hosts first, then wildcards, each by host.

Asks the connected nodes of a group to fetch its state.

Asks the nodes of the groups to fetch their state.

The names of the organisations with these ids, as %{id => name}.

The organisations whose zones count as a group's own for the rules about hosts: the group's organisation and its direct children (parent_organisation_id): one level, never grandchildren, never the other direction (a child's group may not route the parent's zones, and siblings are strangers). The group of a provider organisation may route the hosts of its customers' zones. This is only about who may route and order for a host; the children's certificates are served only when attached (attach_certificate/3).

Which backend a request for host would get, as the generated frontend decides (SaltDns.Proxy.Routes.match/2): {:ok, route} or {:error, :no_route} (the default backend, if any, would answer).

Creates the backend name, or replaces its servers and mode when it exists. attrs may hold servers (a list of name, address, port, check), mode and local (true: the backend is defined in the operator's own HAProxy file, has no servers and is not rendered; saving it as local clears its servers).

Creates the route for host, or changes the existing one (backend, note, reference). The backend must exist in the group.

Makes the routes with the given reference exactly routes (a list of maps with host and backend, and optionally note): creates, changes and deletes in one transaction, one generation. A host already used by a route of another reference (or none) is refused with {:error, {:host_taken, host}} and nothing changes. An empty list removes all routes of that reference.

Retires a node of the group (audited).

Each route with the certificate that covers its host, or nil: the console and the API show nil as "no certificate". Returns [{route, certificate | nil}].

True when a group other than group has a route for exactly host.

Routes that break the rule that a group may not route a host inside another organisation's active zone (made before the rule, or by a zone created later): [%{group_id:, host:}]. Nothing is deleted; the operator decides.

The number of routes of the group that no current certificate covers.

Serial equality as Shaker compares it: ignoring case and leading zeros.

The certificates the group serves, each as %{certificate:, version:, attached?:, covers: [hosts]}: live certificates of the organisation with a current version that cover a route host or are attached (every certificate attached to the group is delivered, whether or not a route uses it: contract 3.9). A certificate covers a host by its own names (the exact name, or a wildcard for exactly one label below its base), never by its zone. Ordered by zone name and names.

The ids of the groups that have an attachment for a certificate ({:certificate, id}), for every certificate of a zone ({:zone, id}) or of an organisation ({:organisation, id}). Ask before a delete: the attachments go with the certificate.

The certificate id and version as the state names them.

The errors in a node's last verified status: [%{"what", "id", "message"}].

Changes a group's settings.

The wire version of a certificate version: its serial in lower-case hex.

Types

result(t)

@type result(t) :: {:ok, t} | {:error, Ecto.Changeset.t() | atom() | tuple()}

Functions

all_groups()

@spec all_groups() :: [SaltDns.Proxy.Group.t()]

Every group of every organisation, by organisation and name (metrics).

attach_certificate(group, certificate, actor)

Attaches a certificate to the group (it is served even when no route needs it). The certificate belongs to the group's organisation or to one of its direct children (parent_organisation_id): one level down, never upward (a child's group cannot serve the parent's certificates), never a sibling's or a grandchild's. Anything else is {:error, :not_found}. Whether the actor may do it is decided before, by attachable_certificate/3; this function checks only the shape.

Attaching a certificate of a child writes the audit entry in the group's organisation and a second one in the child's, so both organisations see it.

attachable_certificate(group, access_actor, id)

@spec attachable_certificate(SaltDns.Proxy.Group.t(), SaltDns.Access.actor(), term()) ::
  {:ok, SaltDns.Certificates.Certificate.t()} | {:error, :not_found}

The certificate id as the actor may attach it to the group, or {:error, :not_found}.

A certificate of the group's own organisation: the caller has already checked the actor's right to manage routing there. A certificate of a direct child: on top of that, the actor must manage certificates in the group's organisation (an owner or admin; a credential with a certificates grant for the certificate's zone). The child organisation is not asked. A routing grant alone is not enough: it must not reach a customer's private keys. A refusal looks like a missing certificate, so ids of other organisations are not confirmed.

attachable_certificates(group, access_actor)

@spec attachable_certificates(SaltDns.Proxy.Group.t(), SaltDns.Access.actor()) :: [
  map()
]

The live certificates the actor could attach to the group and that are not attached yet: [%{certificate:, organisation:}], those of the group's organisation first, then those of its direct children the actor may attach (attachable_certificate/3). organisation is the owning organisation (the console labels the children's).

attached_ids(group)

@spec attached_ids(SaltDns.Proxy.Group.t()) :: [integer()]

The ids of the certificates attached by hand.

build_state(group, node)

@spec build_state(SaltDns.Proxy.Group.t(), SaltDns.Shaker.Node.t()) ::
  {:ok, map()} | {:error, term()}

The state plaintext (contract 3.4) for one node of a group. The configuration is rendered with what the node reported: its socket group (a number renders gid) and whether it runs in a container (outside one, HAProxy drops privileges to the group's run_user and run_group).

bump_groups(group_ids)

@spec bump_groups([integer()]) :: :ok

Raises the generation of the groups (inside the caller's transaction, if any), so their nodes fetch a state without a certificate that was disabled or deleted. Call notify_groups/1 after the commit.

certificate_changed(info)

@spec certificate_changed(map()) :: [integer()]

Handles a {:certificate_current_version, info} event: raises the generation of every group of the organisation (or of its parent) that serves the certificate, once per (certificate, version) however many replicas see the event. Returns the ids of the groups it raised.

certificate_id(certificate)

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

The id a certificate has on the wire.

certificate_payload(group, id, version)

@spec certificate_payload(SaltDns.Proxy.Group.t(), String.t(), String.t()) ::
  {:ok, map()} | {:error, :not_found | :not_allowed}

The plaintext of a certificate envelope for a node of the group: only for a certificate the group serves, only its current version.

certificate_report(node, group)

@spec certificate_report(SaltDns.Shaker.Node.t(), SaltDns.Proxy.Group.t() | nil) :: [
  map()
]

For each certificate the group serves: the serial the node should serve and the serial it reported, %{id, zone, names, expected, served, loaded, match}. match is true/false once the node reported the certificate (case and leading zeros ignored) and nil when it has not.

change_backend(backend, attrs \\ %{})

@spec change_backend(SaltDns.Proxy.Backend.t(), map()) :: Ecto.Changeset.t()

Changeset for the backend form.

change_group(group, attrs \\ %{})

@spec change_group(SaltDns.Proxy.Group.t(), map()) :: Ecto.Changeset.t()

Changeset for the group form.

change_route(route, attrs \\ %{})

@spec change_route(SaltDns.Proxy.RouteEntry.t(), map()) :: Ecto.Changeset.t()

Changeset for the route form.

create_group(org, attrs, actor)

Creates a group. The generation starts at 1.

create_token(group, actor, opts \\ [])

@spec create_token(SaltDns.Proxy.Group.t(), SaltDns.Zones.Actor.t(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Creates a one-time enrolment token for the group. The plaintext is returned once and not stored.

delete_backend(group, name, actor)

Deletes a backend that no route and no default points at ({:error, :in_use}).

delete_group(group, actor)

Deletes a group with its backends, routes and attachments. Refused while a node that is not retired still belongs to it ({:error, :in_use}).

delete_route(group, host, actor)

Deletes the route for host.

detach_certificate(group, certificate, actor)

Removes an attachment (a certificate that covers a route is still served). Works for a certificate of a child organisation as well, and writes the second audit entry in the child's organisation.

detachable_certificate(group, id)

@spec detachable_certificate(SaltDns.Proxy.Group.t(), term()) ::
  {:ok, SaltDns.Certificates.Certificate.t()} | {:error, :not_found}

The certificate id to detach: one attached to the group, or any certificate of the group's organisation. {:error, :not_found} for the rest.

duplicate_outside_routes()

@spec duplicate_outside_routes() :: [%{host: String.t(), group_ids: [integer()]}]

The exact hosts in no active zone that more than one proxy group routes: [%{host:, group_ids: [id]}]. Such duplicates can only come from before the first-come rule existed or from a zone that stopped being active; no certificate is ordered for them (SaltDns.Certificates.Http01.before_order/2). The operator resolves them by removing all but one route (SaltDns.Release.duplicate_outside_routes/0 prints them).

enabled?()

@spec enabled?() :: boolean()

True when routing and the Shaker channel are switched on (SALT_DNS_SHAKER=on).

find_group(name)

@spec find_group(String.t()) ::
  {:ok, SaltDns.Proxy.Group.t()} | {:error, :not_found | :ambiguous}

A group by name across all organisations, for the release commands that only know a name. {:error, :ambiguous} when two organisations use it.

fingerprint(text)

@spec fingerprint(String.t() | nil) :: String.t() | nil

A public key's fingerprint as the contract shows it (3.8): SHA-256 of the key text as exchanged, hex, eight groups of eight digits.

get_backend(group, name)

@spec get_backend(SaltDns.Proxy.Group.t(), String.t()) ::
  SaltDns.Proxy.Backend.t() | nil

A backend of the group by name, or nil.

get_group(id)

@spec get_group(term()) :: SaltDns.Proxy.Group.t() | nil

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

get_group(organisation, id)

A group of the organisation by id, or nil.

get_node(organisation, node_id)

A node of the organisation by id, or nil.

get_route(group, host)

@spec get_route(SaltDns.Proxy.Group.t(), String.t()) ::
  SaltDns.Proxy.RouteEntry.t() | nil

A route of the group by host (normalised first), or nil.

group_of(node)

@spec group_of(SaltDns.Shaker.Node.t()) :: SaltDns.Proxy.Group.t() | nil

The group a node belongs to, or nil.

list_backends(group)

@spec list_backends(SaltDns.Proxy.Group.t()) :: [SaltDns.Proxy.Backend.t()]

The backends of a group, by name.

list_changes(group, limit \\ 50)

@spec list_changes(SaltDns.Proxy.Group.t(), pos_integer()) :: [
  SaltDns.Proxy.Change.t()
]

The latest audit entries of a group, newest first.

list_foreign_changes(organisation, limit \\ 50)

@spec list_foreign_changes(SaltDns.Accounts.Organisation.t(), pos_integer()) :: [
  map()
]

The entries recorded in organisation's audit trail about its certificates being attached to, or detached from, a proxy group of another organisation (its parent's), newest first: [%{id:, at:, action:, group_name:, group_organisation:, certificate_id:, zone:, names:}]. It shows the group's name and the parent organisation's name and nothing else of the parent.

list_groups(organisation)

The groups of an organisation, by name.

list_nodes(group)

@spec list_nodes(SaltDns.Proxy.Group.t()) :: [SaltDns.Shaker.Node.t()]

The nodes of a group, by hostname.

list_nodes(organisation, atom)

@spec list_nodes(SaltDns.Accounts.Organisation.t(), :organisation) :: [
  SaltDns.Shaker.Node.t()
]

The nodes of every group of an organisation, by hostname.

list_routes(group)

The routes of a group, exact hosts first, then wildcards, each by host.

notify(id)

@spec notify(SaltDns.Proxy.Group.t() | integer()) :: :ok

Asks the connected nodes of a group to fetch its state.

notify_groups(group_ids)

@spec notify_groups([integer()]) :: :ok

Asks the nodes of the groups to fetch their state.

organisation_names(ids)

@spec organisation_names([integer()]) :: %{required(integer()) => String.t()}

The names of the organisations with these ids, as %{id => name}.

own_organisation_ids(organisation_id)

@spec own_organisation_ids(integer()) :: [integer()]

The organisations whose zones count as a group's own for the rules about hosts: the group's organisation and its direct children (parent_organisation_id): one level, never grandchildren, never the other direction (a child's group may not route the parent's zones, and siblings are strangers). The group of a provider organisation may route the hosts of its customers' zones. This is only about who may route and order for a host; the children's certificates are served only when attached (attach_certificate/3).

preview(group, host)

@spec preview(SaltDns.Proxy.Group.t(), String.t()) ::
  {:ok, SaltDns.Proxy.RouteEntry.t()} | {:error, :no_route}

Which backend a request for host would get, as the generated frontend decides (SaltDns.Proxy.Routes.match/2): {:ok, route} or {:error, :no_route} (the default backend, if any, would answer).

put_backend(group, name, attrs, actor)

Creates the backend name, or replaces its servers and mode when it exists. attrs may hold servers (a list of name, address, port, check), mode and local (true: the backend is defined in the operator's own HAProxy file, has no servers and is not rendered; saving it as local clears its servers).

put_route(group, host, attrs, actor)

Creates the route for host, or changes the existing one (backend, note, reference). The backend must exist in the group.

replace_routes(group, reference, routes, actor)

@spec replace_routes(
  SaltDns.Proxy.Group.t(),
  String.t(),
  [map()],
  SaltDns.Zones.Actor.t()
) ::
  result(%{
    created: non_neg_integer(),
    updated: non_neg_integer(),
    deleted: non_neg_integer()
  })

Makes the routes with the given reference exactly routes (a list of maps with host and backend, and optionally note): creates, changes and deletes in one transaction, one generation. A host already used by a route of another reference (or none) is refused with {:error, {:host_taken, host}} and nothing changes. An empty list removes all routes of that reference.

retire_node(group, node, actor)

Retires a node of the group (audited).

route_coverage(group)

Each route with the certificate that covers its host, or nil: the console and the API show nil as "no certificate". Returns [{route, certificate | nil}].

routed_elsewhere?(group, host)

@spec routed_elsewhere?(SaltDns.Proxy.Group.t(), String.t()) :: boolean()

True when a group other than group has a route for exactly host.

routes_in_foreign_zones()

@spec routes_in_foreign_zones() :: [%{group_id: integer(), host: String.t()}]

Routes that break the rule that a group may not route a host inside another organisation's active zone (made before the rule, or by a zone created later): [%{group_id:, host:}]. Nothing is deleted; the operator decides.

routes_without_certificate(group)

@spec routes_without_certificate(SaltDns.Proxy.Group.t()) :: non_neg_integer()

The number of routes of the group that no current certificate covers.

same_serial?(a, b)

@spec same_serial?(String.t(), String.t()) :: boolean()

Serial equality as Shaker compares it: ignoring case and leading zeros.

served(group)

@spec served(SaltDns.Proxy.Group.t()) :: [map()]

The certificates the group serves, each as %{certificate:, version:, attached?:, covers: [hosts]}: live certificates of the organisation with a current version that cover a route host or are attached (every certificate attached to the group is delivered, whether or not a route uses it: contract 3.9). A certificate covers a host by its own names (the exact name, or a wildcard for exactly one label below its base), never by its zone. Ordered by zone name and names.

serving_groups(arg)

@spec serving_groups({:certificate | :zone | :organisation, integer()}) :: [integer()]

The ids of the groups that have an attachment for a certificate ({:certificate, id}), for every certificate of a zone ({:zone, id}) or of an organisation ({:organisation, id}). Ask before a delete: the attachments go with the certificate.

state_certificate(map)

@spec state_certificate(map()) :: map()

The certificate id and version as the state names them.

status_errors(node)

@spec status_errors(SaltDns.Shaker.Node.t()) :: [map()]

The errors in a node's last verified status: [%{"what", "id", "message"}].

update_group(group, attrs, actor)

Changes a group's settings.

version_label(version)

@spec version_label(SaltDns.Certificates.Version.t()) :: String.t()

The wire version of a certificate version: its serial in lower-case hex.