SaltDns.Shaker (Salt v1.0.0)

Salt's side of the contract with Shaker, the node agent on load-balancer hosts (contract version 1 in docs/suite/plans/salt-certificates-build.md, section 3).

Everything here is inactive unless the switch is on:

SALT_DNS_SHAKER=on

(read in config/runtime.exs). When off, the routes answer 404 and no process for this module is started. The contract is law: the exact bytes Salt signs, the envelope numbering and the error codes below are the wire specification, and docs/shaker.md is the operator's view.

Enrolment

An operator makes a one-time token bound to a proxy group (create_token/2, or SaltDns.Release.shaker_token/1). Shaker sends the token with its public keys to POST /shaker/v1/enrol; enrol/2 spends the token, stores the node as active and answers the node id and Salt's signing key.

A host that cannot reach Salt yet is enrolled offline: shaker init prints an enrolment request, and bootstrap/2 (SaltDns.Release.shaker_bootstrap/2) registers the node as pending and returns a bootstrap file with the current desired state and every certificate it names.

Errors

enrol/2 answers {:error, :invalid_token}, {:error, :already_enrolled} or {:error, {:validation, %{field => [message]}}}; the controller maps these to the contract's 401, 409 and 422.

Settings

Configuration is the application environment of SaltDns.Shaker:

  • :enabled (default false) — the switch above.
  • :source (default SaltDns.Shaker.Source.Empty) — the module that supplies the desired state and certificates (SaltDns.Shaker.Source).
  • :url (default the endpoint URL) — Salt's public base URL, used in a bootstrap file when there is no request to take it from.
  • :enrol_rate_limit (default {10, 60}) — enrolments per address per window, through SaltDns.RateLimiter.
  • :ping_interval_ms (default 30_000) — how long the socket waits before pinging an idle Shaker.
  • :metric_max_nodes (default 500) — the most node hostnames the per-node metric is labelled with, so its cardinality stays bounded.

Summary

Functions

Registers a node from an offline enrolment request and returns the bootstrap file as a map.

The encloses of a bootstrap file: the state first, then its certificates.

Fetches a certificate for a node, but only one its current desired state names — with that exact id and version.

True when the node has an open connection, on any replica.

Creates a one-time enrolment token for group and returns its plaintext, which is shown once and not stored.

Whether the Shaker feature is switched on (SALT_DNS_SHAKER=on).

Enrols a Shaker: spends its token and stores the node as active.

The enrolment rate limit, {requests, window_seconds}.

A node by id (a UUID string), or nil.

The key of the PubSub group topic a node listens on: its proxy group id, or its group name when it has none.

The PubSub topic every connection in one proxy group subscribes to.

Counts an enrolment attempt from address. Returns the same result as SaltDns.RateLimiter.hit/4.

All nodes, or the nodes of one proxy group, by hostname.

Records a successful authentication: a pending node becomes active, and its last-seen time is now.

Records that the node reported in (hello, status or pong).

The most nodes the per-node status-age metric labels.

The PubSub topic the connection holder for one node subscribes to.

Asks the connection holder to push a new desired state: to one node, or to every node in a proxy group.

How long the socket waits before pinging an idle Shaker, in milliseconds.

Records a node's hello: the generation it says it applied, and its versions when it sent them.

Retires a node: the socket refuses it from then on, and it receives no more envelopes. Its rows stay for the record.

Salt's public base URL, without a trailing slash.

The socket URL Shaker connects to, built from Salt's base URL.

The configured SaltDns.Shaker.Source implementation.

Stores a status report after its signature verified. The report is kept without its signature, with the applied generation, the last-seen time and the first reported error.

Types

bootstrap_result()

@type bootstrap_result() :: %{
  node_id: String.t(),
  salt_url: String.t(),
  salt_signing_key: String.t(),
  envelopes: [map()]
}

enrol_error()

@type enrol_error() ::
  :invalid_token | :already_enrolled | {:validation, validation_errors()}

enrol_result()

@type enrol_result() :: %{
  node_id: String.t(),
  salt_signing_key: String.t(),
  socket_url: String.t()
}

validation_errors()

@type validation_errors() :: %{required(String.t()) => [String.t()]}

Functions

bootstrap(request_base64url, group)

@spec bootstrap(String.t(), String.t() | %{id: integer(), name: String.t()}) ::
  {:ok, bootstrap_result()} | {:error, term()}

Registers a node from an offline enrolment request and returns the bootstrap file as a map.

request_base64url is the base64url of the request shaker init prints: {"recipient","signing_key","hostname","shaker_version","strategy", "software_version"} (and optionally "socket_group"). The node is stored as pending, in group, and the answer carries the node id, Salt's base URL, Salt's signing key, and envelopes with the current desired state followed by every certificate it names.

A certificate the source cannot supply is left out; the node asks for it again over the socket once it connects.

bootstrap_envelopes(node)

@spec bootstrap_envelopes(SaltDns.Shaker.Node.t()) ::
  {:ok, [map()]} | {:error, term()}

The encloses of a bootstrap file: the state first, then its certificates.

certificate_for(node, id, version)

@spec certificate_for(SaltDns.Shaker.Node.t(), String.t(), String.t()) ::
  {:ok, map()} | {:error, :not_found | :not_allowed}

Fetches a certificate for a node, but only one its current desired state names — with that exact id and version.

{:error, :not_allowed} means the state does not name it (or the state could not be read); {:error, :not_found} means the source names it but cannot supply it.

connected?(node_id)

@spec connected?(String.t()) :: boolean()

True when the node has an open connection, on any replica.

create_token(group, opts \\ [])

@spec create_token(String.t() | %{id: integer(), name: String.t()}, keyword()) ::
  {:ok, String.t()} | {:error, Ecto.Changeset.t()}

Creates a one-time enrolment token for group and returns its plaintext, which is shown once and not stored.

Options: :expires_at (a DateTime, or nil for no expiry).

enabled?()

@spec enabled?() :: boolean()

Whether the Shaker feature is switched on (SALT_DNS_SHAKER=on).

enrol(params, opts \\ [])

@spec enrol(map(), keyword()) :: {:ok, enrol_result()} | {:error, enrol_error()}

Enrols a Shaker: spends its token and stores the node as active.

params is the JSON body of the contract. opts may carry :socket_url, which the controller builds from the request so the answer names the URL the node actually reached Salt on.

enrol_rate_limit()

@spec enrol_rate_limit() :: {pos_integer(), pos_integer()}

The enrolment rate limit, {requests, window_seconds}.

get_node(id)

@spec get_node(String.t() | nil) :: SaltDns.Shaker.Node.t() | nil

A node by id (a UUID string), or nil.

group_key(node)

@spec group_key(SaltDns.Shaker.Node.t()) :: String.t()

The key of the PubSub group topic a node listens on: its proxy group id, or its group name when it has none.

group_topic(group)

@spec group_topic(String.t()) :: String.t()

The PubSub topic every connection in one proxy group subscribes to.

hit_enrolment_limit(address, now \\ System.system_time(:second))

@spec hit_enrolment_limit(:inet.ip_address(), integer()) ::
  SaltDns.RateLimiter.result()

Counts an enrolment attempt from address. Returns the same result as SaltDns.RateLimiter.hit/4.

list_nodes(group \\ nil)

@spec list_nodes(String.t() | nil) :: [SaltDns.Shaker.Node.t()]

All nodes, or the nodes of one proxy group, by hostname.

mark_connected(node)

@spec mark_connected(SaltDns.Shaker.Node.t()) ::
  {:ok, SaltDns.Shaker.Node.t()} | {:error, Ecto.Changeset.t()}

Records a successful authentication: a pending node becomes active, and its last-seen time is now.

mark_seen(node)

@spec mark_seen(SaltDns.Shaker.Node.t()) ::
  {:ok, SaltDns.Shaker.Node.t()} | {:error, Ecto.Changeset.t()}

Records that the node reported in (hello, status or pong).

metric_max_nodes()

@spec metric_max_nodes() :: pos_integer()

The most nodes the per-node status-age metric labels.

node_topic(node_id)

@spec node_topic(String.t()) :: String.t()

The PubSub topic the connection holder for one node subscribes to.

notify(group)

@spec notify(SaltDns.Shaker.Node.t() | String.t()) :: :ok

Asks the connection holder to push a new desired state: to one node, or to every node in a proxy group.

Any replica may call this; only the process holding the WebSocket pushes. A node that is not connected catches up when it next connects, so a failed broadcast loses nothing.

ping_interval_ms()

@spec ping_interval_ms() :: pos_integer()

How long the socket waits before pinging an idle Shaker, in milliseconds.

record_hello(node, hello)

@spec record_hello(SaltDns.Shaker.Node.t(), map()) ::
  {:ok, SaltDns.Shaker.Node.t()} | {:error, Ecto.Changeset.t()}

Records a node's hello: the generation it says it applied, and its versions when it sent them.

retire_node(node)

@spec retire_node(SaltDns.Shaker.Node.t() | String.t()) ::
  {:ok, SaltDns.Shaker.Node.t()} | {:error, :not_found}

Retires a node: the socket refuses it from then on, and it receives no more envelopes. Its rows stay for the record.

salt_url()

@spec salt_url() :: String.t()

Salt's public base URL, without a trailing slash.

socket_url()

@spec socket_url() :: String.t()

The socket URL Shaker connects to, built from Salt's base URL.

source()

@spec source() :: module()

The configured SaltDns.Shaker.Source implementation.

store_status(node, report)

@spec store_status(SaltDns.Shaker.Node.t(), map()) ::
  {:ok, SaltDns.Shaker.Node.t()} | {:error, Ecto.Changeset.t()}

Stores a status report after its signature verified. The report is kept without its signature, with the applied generation, the last-seen time and the first reported error.