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
@type result(t) :: {:ok, t} | {:error, Ecto.Changeset.t() | atom() | tuple()}
Functions
@spec all_groups() :: [SaltDns.Proxy.Group.t()]
Every group of every organisation, by organisation and name (metrics).
@spec attach_certificate( SaltDns.Proxy.Group.t(), SaltDns.Certificates.Certificate.t(), SaltDns.Zones.Actor.t() ) :: result(SaltDns.Proxy.GroupCertificate.t())
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.
@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.
@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).
@spec attached_ids(SaltDns.Proxy.Group.t()) :: [integer()]
The ids of the certificates attached by hand.
@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).
@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.
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.
@spec certificate_id(SaltDns.Certificates.Certificate.t()) :: String.t()
The id a certificate has on the wire.
@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.
@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.
@spec change_backend(SaltDns.Proxy.Backend.t(), map()) :: Ecto.Changeset.t()
Changeset for the backend form.
@spec change_group(SaltDns.Proxy.Group.t(), map()) :: Ecto.Changeset.t()
Changeset for the group form.
@spec change_route(SaltDns.Proxy.RouteEntry.t(), map()) :: Ecto.Changeset.t()
Changeset for the route form.
@spec create_group(SaltDns.Accounts.Organisation.t(), map(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.Group.t())
Creates a group. The generation starts at 1.
@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.
@spec delete_backend(SaltDns.Proxy.Group.t(), String.t(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.Backend.t())
Deletes a backend that no route and no default points at ({:error, :in_use}).
@spec delete_group(SaltDns.Proxy.Group.t(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.Group.t())
Deletes a group with its backends, routes and attachments. Refused while a
node that is not retired still belongs to it ({:error, :in_use}).
@spec delete_route(SaltDns.Proxy.Group.t(), String.t(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.RouteEntry.t())
Deletes the route for host.
@spec detach_certificate( SaltDns.Proxy.Group.t(), SaltDns.Certificates.Certificate.t(), SaltDns.Zones.Actor.t() ) :: result(:ok)
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.
@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.
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).
@spec enabled?() :: boolean()
True when routing and the Shaker channel are switched on (SALT_DNS_SHAKER=on).
@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.
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.
@spec get_backend(SaltDns.Proxy.Group.t(), String.t()) :: SaltDns.Proxy.Backend.t() | nil
A backend of the group by name, or nil.
@spec get_group(term()) :: SaltDns.Proxy.Group.t() | nil
A group by id, without an organisation check (internal).
@spec get_group(SaltDns.Accounts.Organisation.t(), term()) :: SaltDns.Proxy.Group.t() | nil
A group of the organisation by id, or nil.
@spec get_node(SaltDns.Accounts.Organisation.t(), term()) :: SaltDns.Shaker.Node.t() | nil
A node of the organisation by id, or nil.
@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.
@spec group_of(SaltDns.Shaker.Node.t()) :: SaltDns.Proxy.Group.t() | nil
The group a node belongs to, or nil.
@spec list_backends(SaltDns.Proxy.Group.t()) :: [SaltDns.Proxy.Backend.t()]
The backends of a group, by name.
@spec list_changes(SaltDns.Proxy.Group.t(), pos_integer()) :: [ SaltDns.Proxy.Change.t() ]
The latest audit entries of a group, newest first.
@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.
@spec list_groups(SaltDns.Accounts.Organisation.t()) :: [SaltDns.Proxy.Group.t()]
The groups of an organisation, by name.
@spec list_nodes(SaltDns.Proxy.Group.t()) :: [SaltDns.Shaker.Node.t()]
The nodes of a group, by hostname.
@spec list_nodes(SaltDns.Accounts.Organisation.t(), :organisation) :: [ SaltDns.Shaker.Node.t() ]
The nodes of every group of an organisation, by hostname.
@spec list_routes(SaltDns.Proxy.Group.t()) :: [SaltDns.Proxy.RouteEntry.t()]
The routes of a group, exact hosts first, then wildcards, each by host.
@spec notify(SaltDns.Proxy.Group.t() | integer()) :: :ok
Asks the connected nodes of a group to fetch its state.
@spec notify_groups([integer()]) :: :ok
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).
@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).
@spec put_backend(SaltDns.Proxy.Group.t(), String.t(), map(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.Backend.t())
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).
@spec put_route(SaltDns.Proxy.Group.t(), String.t(), map(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.RouteEntry.t())
Creates the route for host, or changes the existing one (backend,
note, reference). The backend must exist in the group.
@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.
@spec retire_node( SaltDns.Proxy.Group.t(), SaltDns.Shaker.Node.t(), SaltDns.Zones.Actor.t() ) :: result(SaltDns.Shaker.Node.t())
Retires a node of the group (audited).
@spec route_coverage(SaltDns.Proxy.Group.t()) :: [ {SaltDns.Proxy.RouteEntry.t(), SaltDns.Certificates.Certificate.t() | nil} ]
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}].
@spec routed_elsewhere?(SaltDns.Proxy.Group.t(), String.t()) :: boolean()
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.
@spec routes_without_certificate(SaltDns.Proxy.Group.t()) :: non_neg_integer()
The number of routes of the group that no current certificate covers.
Serial equality as Shaker compares it: ignoring case and leading zeros.
@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.
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.
@spec status_errors(SaltDns.Shaker.Node.t()) :: [map()]
The errors in a node's last verified status: [%{"what", "id", "message"}].
@spec update_group(SaltDns.Proxy.Group.t(), map(), SaltDns.Zones.Actor.t()) :: result(SaltDns.Proxy.Group.t())
Changes a group's settings.
@spec version_label(SaltDns.Certificates.Version.t()) :: String.t()
The wire version of a certificate version: its serial in lower-case hex.