SaltDns.Knot behaviour (Salt v1.0.0)

The bridge between Salt DNS and the hidden Knot primary.

Postgres is the system of record; this behaviour is how its state reaches Knot. Implementations:

The adapter in use is Application.get_env(:salt_dns, :knot_adapter), default SaltDns.Knot.Knotc.

Zone names are absolute, lower-case, without a trailing dot. All functions are idempotent: ensuring an existing zone, removing an absent one, adding a record that exists or removing one that does not all succeed.

Errors are tagged tuples. The ones callers should expect:

  • {:error, :no_such_zone}
  • {:error, :transaction_in_progress} (a zone or configuration transaction is open that this call did not open)
  • {:error, {:invalid_zone, name}}, {:error, {:invalid_change, change}}
  • {:error, {:change_failed, change, reason}} (the whole batch was rolled back)
  • {:error, :no_ksk_ready} (ksk_submitted/1, nothing to confirm)
  • {:error, {:knotc, exit_status, output}}, {:error, {:timeout, ms}}, {:error, {:executable_not_found, binary}}

Summary

Types

One key from Knot's key store. state is :created | :pre_active | :published | :ready | :active | :retiring | :retired | :removed; a KSK in :ready is waiting for its DS to be confirmed (ksk_submitted/1). timers holds the key's timer names as atoms mapped to unix seconds.

DNSSEC mode of a zone: unsigned, or signed with NSEC or NSEC3 denial of existence.

One public edge as Knot's configuration knows it: the id of the remote, its addresses without port (IPv4 and IPv6 literals) and the port.

SOA values for a new zone. Names have no trailing dot.

What ensure_zone/2 needs: the SOA, the apex NS target names (no trailing dot), and the name of the catalog member template (default "member"). Apex records are only seeded when the zone is empty.

Callbacks

Aborts the zone's open transaction, if any (idempotent). For the zone's single writer to clear a transaction a crashed predecessor left open.

Applies the changes, in order, in one zone transaction (zone-begin, zone-set/zone-unset, zone-commit). On any error the transaction is aborted and nothing is applied. The SOA serial rises on every commit.

Whether the zone is signed, its mode, keys with their states, and pending events.

The DS (SHA-256), CDS and CDNSKEY that Knot publishes in the zone for the registrar. Empty lists while the zone is unsigned.

The edges this primary notifies: the remote sections that belong to the notify groups (SALT_DNS_KNOT_BASE_NOTIFY, default knot-edge), read from the configuration database. Read only; placeholder addresses are left out.

Makes sure the zone is configured on the primary (one configuration transaction) and has an SOA and apex NS records. Does nothing to a zone that already has data.

Makes this node's Knot a secondary of its peer for exactly zones (customer zones, the organisations' catalogs and the edge catalog): every listed zone gets the follower template (master: the peer) and loses any signing, ACL and NOTIFY settings of its own; zones not listed are removed (except :keep). Zones that were primaries here are retransferred, so divergent data of a former leader is replaced. One configuration transaction. Runs even when this node is not the leader: it is the demotion step.

Confirms that the parent now has the DS for a KSK in the :ready state (knotc zone-ksk-submitted). Returns {:error, :no_ksk_ready} when no key is waiting.

The zones this Knot has loaded (a serial), as lower-case names without the final dot.

Points this Knot's peer remote (the primary a secondary transfers from) at address (SaltDns.Knot.PeerAddress, validated again here). One configuration transaction, none when the address is already the only one.

Reads the committed zone: every record, as :add changes.

Asks this Knot (a secondary) to check its primary for these zones now (knotc zone-refresh) instead of at its next poll. Only a speed-up; the poll makes it correct anyway.

Removes the zone from the primary's configuration and deletes its stored data.

As remove_zone/1; with keys: true the zone's DNSSEC keys are purged too (for a deleted zone, not a disabled one).

Switches DNSSEC signing for the zone: :off, or {:on, :nsec | :nsec3} (one configuration transaction; Knot generates keys and signs on its own). Switching on returns once DNSKEY is published. Switching on again with a different mode changes the denial of existence and re-signs. Switching off also removes the DNSSEC records (SaltDns.Knot.Protocol.dnssec_types/0) from the zone; do that only after the DS is gone from the parent (SaltDns.Dnssec.Lifecycle).

How many zones this Knot has configured to sign.

Status of the zone: role and current serial.

Makes Knot's distribution configuration for one organisation match spec (SaltDns.Knot.OrgConfig): its catalog zone and member template, and a transfer key, remote and ACL per secondary. One configuration transaction if anything differs, none otherwise. Stale secondaries of the organisation are removed.

The template the zone is configured with ({:ok, nil} when the zone is not configured). The template names the organisation that owns the zone in Knot.

Types

cdnskey()

@type cdnskey() :: %{
  flags: non_neg_integer(),
  protocol: non_neg_integer(),
  algorithm: non_neg_integer(),
  public_key: String.t()
}

dnssec_event()

@type dnssec_event() :: %{
  event: String.t(),
  key_tag: non_neg_integer() | nil,
  in_seconds: non_neg_integer()
}

dnssec_key()

@type dnssec_key() :: %{
  id: String.t(),
  tag: non_neg_integer(),
  role: :ksk | :zsk | :csk,
  algorithm: non_neg_integer(),
  state: atom(),
  timers: %{optional(atom()) => non_neg_integer()}
}

One key from Knot's key store. state is :created | :pre_active | :published | :ready | :active | :retiring | :retired | :removed; a KSK in :ready is waiting for its DS to be confirmed (ksk_submitted/1). timers holds the key's timer names as atoms mapped to unix seconds.

dnssec_mode()

@type dnssec_mode() :: :off | {:on, :nsec | :nsec3}

DNSSEC mode of a zone: unsigned, or signed with NSEC or NSEC3 denial of existence.

dnssec_status()

@type dnssec_status() :: %{
  signed: boolean(),
  mode: :nsec | :nsec3 | nil,
  policy: String.t() | nil,
  dnskey_published: boolean(),
  keys: [dnssec_key()],
  next_events: [dnssec_event()]
}

ds()

@type ds() :: %{
  key_tag: non_neg_integer(),
  algorithm: non_neg_integer(),
  digest_type: non_neg_integer(),
  digest: String.t()
}

ds_info()

@type ds_info() :: %{ds: [ds()], cds: [ds()], cdnskey: [cdnskey()]}

edge()

@type edge() :: %{id: String.t(), addresses: [String.t()], port: non_neg_integer()}

One public edge as Knot's configuration knows it: the id of the remote, its addresses without port (IPv4 and IPv6 literals) and the port.

error()

@type error() :: {:error, term()}

soa()

@type soa() :: %{
  :mname => String.t(),
  :rname => String.t(),
  :refresh => non_neg_integer(),
  :retry => non_neg_integer(),
  :expire => non_neg_integer(),
  :minimum => non_neg_integer(),
  optional(:ttl) => pos_integer()
}

SOA values for a new zone. Names have no trailing dot.

zone_opts()

@type zone_opts() :: %{
  :soa => soa(),
  :ns => [String.t(), ...],
  optional(:template) => String.t(),
  optional(:ns_ttl) => pos_integer()
}

What ensure_zone/2 needs: the SOA, the apex NS target names (no trailing dot), and the name of the catalog member template (default "member"). Apex records are only seeded when the zone is empty.

zone_record()

@type zone_record() :: %SaltDns.Zones.Change{
  name: term(),
  op: :add,
  rdata: term(),
  ttl: term(),
  type: term()
}

zone_status()

@type zone_status() :: %{
  role: String.t(),
  serial: non_neg_integer() | nil,
  raw: String.t()
}

Callbacks

abort_zone(zone)

@callback abort_zone(zone :: String.t()) :: :ok | error()

Aborts the zone's open transaction, if any (idempotent). For the zone's single writer to clear a transaction a crashed predecessor left open.

apply_changes(zone, list)

@callback apply_changes(zone :: String.t(), [SaltDns.Zones.Change.t()]) :: :ok | error()

Applies the changes, in order, in one zone transaction (zone-begin, zone-set/zone-unset, zone-commit). On any error the transaction is aborted and nothing is applied. The SOA serial rises on every commit.

dnssec_status(zone)

@callback dnssec_status(zone :: String.t()) :: {:ok, dnssec_status()} | error()

Whether the zone is signed, its mode, keys with their states, and pending events.

ds_records(zone)

@callback ds_records(zone :: String.t()) :: {:ok, ds_info()} | error()

The DS (SHA-256), CDS and CDNSKEY that Knot publishes in the zone for the registrar. Empty lists while the zone is unsigned.

edges()

@callback edges() :: {:ok, [edge()]} | error()

The edges this primary notifies: the remote sections that belong to the notify groups (SALT_DNS_KNOT_BASE_NOTIFY, default knot-edge), read from the configuration database. Read only; placeholder addresses are left out.

ensure_zone(zone, zone_opts)

@callback ensure_zone(zone :: String.t(), zone_opts()) :: :ok | error()

Makes sure the zone is configured on the primary (one configuration transaction) and has an SOA and apex NS records. Does nothing to a zone that already has data.

follow(zones, opts)

@callback follow(zones :: [String.t()] | :current, opts :: keyword()) ::
  {:ok, %{changed: non_neg_integer(), removed: non_neg_integer()}} | error()

Makes this node's Knot a secondary of its peer for exactly zones (customer zones, the organisations' catalogs and the edge catalog): every listed zone gets the follower template (master: the peer) and loses any signing, ACL and NOTIFY settings of its own; zones not listed are removed (except :keep). Zones that were primaries here are retransferred, so divergent data of a former leader is replaced. One configuration transaction. Runs even when this node is not the leader: it is the demotion step.

ksk_submitted(zone)

@callback ksk_submitted(zone :: String.t()) :: :ok | error()

Confirms that the parent now has the DS for a KSK in the :ready state (knotc zone-ksk-submitted). Returns {:error, :no_ksk_ready} when no key is waiting.

loaded_zones()

@callback loaded_zones() :: {:ok, [String.t()]} | error()

The zones this Knot has loaded (a serial), as lower-case names without the final dot.

point_peer(address)

@callback point_peer(address :: String.t()) :: {:ok, :changed | :unchanged} | error()

Points this Knot's peer remote (the primary a secondary transfers from) at address (SaltDns.Knot.PeerAddress, validated again here). One configuration transaction, none when the address is already the only one.

read_zone(zone)

@callback read_zone(zone :: String.t()) :: {:ok, [zone_record()]} | error()

Reads the committed zone: every record, as :add changes.

refresh_zones(zones)

@callback refresh_zones(zones :: [String.t()]) :: :ok | error()

Asks this Knot (a secondary) to check its primary for these zones now (knotc zone-refresh) instead of at its next poll. Only a speed-up; the poll makes it correct anyway.

remove_zone(zone)

@callback remove_zone(zone :: String.t()) :: :ok | error()

Removes the zone from the primary's configuration and deletes its stored data.

remove_zone(zone, opts)

@callback remove_zone(zone :: String.t(), opts :: keyword()) :: :ok | error()

As remove_zone/1; with keys: true the zone's DNSSEC keys are purged too (for a deleted zone, not a disabled one).

set_dnssec(zone, dnssec_mode)

@callback set_dnssec(zone :: String.t(), dnssec_mode()) :: :ok | error()

Switches DNSSEC signing for the zone: :off, or {:on, :nsec | :nsec3} (one configuration transaction; Knot generates keys and signs on its own). Switching on returns once DNSKEY is published. Switching on again with a different mode changes the denial of existence and re-signs. Switching off also removes the DNSSEC records (SaltDns.Knot.Protocol.dnssec_types/0) from the zone; do that only after the DS is gone from the parent (SaltDns.Dnssec.Lifecycle).

signing_zones()

@callback signing_zones() :: {:ok, non_neg_integer()} | error()

How many zones this Knot has configured to sign.

status(zone)

@callback status(zone :: String.t()) :: {:ok, zone_status()} | error()

Status of the zone: role and current serial.

sync_organisation(spec)

@callback sync_organisation(SaltDns.Knot.OrgConfig.spec()) :: :ok | error()

Makes Knot's distribution configuration for one organisation match spec (SaltDns.Knot.OrgConfig): its catalog zone and member template, and a transfer key, remote and ACL per secondary. One configuration transaction if anything differs, none otherwise. Stale secondaries of the organisation are removed.

zone_template(zone)

@callback zone_template(zone :: String.t()) :: {:ok, String.t() | nil} | error()

The template the zone is configured with ({:ok, nil} when the zone is not configured). The template names the organisation that owns the zone in Knot.

Functions

abort_zone(zone)

@spec abort_zone(String.t()) :: :ok | error()

See abort_zone/1.

apply_changes(zone, changes)

@spec apply_changes(String.t(), [SaltDns.Zones.Change.t()]) :: :ok | error()

See apply_changes/2.

dnssec_status(zone)

@spec dnssec_status(String.t()) :: {:ok, dnssec_status()} | error()

See dnssec_status/1.

ds_records(zone)

@spec ds_records(String.t()) :: {:ok, ds_info()} | error()

See ds_records/1.

edges()

@spec edges() :: {:ok, [edge()]} | error()

See edges/0.

ensure_zone(zone, opts)

@spec ensure_zone(String.t(), zone_opts()) :: :ok | error()

See ensure_zone/2.

follow(zones, opts \\ [])

@spec follow([String.t()] | :current, keyword()) :: {:ok, map()} | error()

See follow/2.

ksk_submitted(zone)

@spec ksk_submitted(String.t()) :: :ok | error()

See ksk_submitted/1.

loaded_zones()

@spec loaded_zones() :: {:ok, [String.t()]} | error()

See loaded_zones/0.

point_peer(address)

@spec point_peer(String.t()) :: {:ok, :changed | :unchanged} | error()

See point_peer/1.

read_zone(zone)

@spec read_zone(String.t()) :: {:ok, [zone_record()]} | error()

See read_zone/1.

refresh_zones(zones)

@spec refresh_zones([String.t()]) :: :ok | error()

See refresh_zones/1.

remove_zone(zone, opts \\ [])

@spec remove_zone(String.t(), keyword()) :: :ok | error()

See remove_zone/1.

set_dnssec(zone, mode)

@spec set_dnssec(String.t(), dnssec_mode()) :: :ok | error()

See set_dnssec/2.

signing_zones()

@spec signing_zones() :: {:ok, non_neg_integer()} | error()

See signing_zones/0.

status(zone)

@spec status(String.t()) :: {:ok, zone_status()} | error()

See status/1.

sync_organisation(spec)

@spec sync_organisation(SaltDns.Knot.OrgConfig.spec()) :: :ok | error()

See sync_organisation/1.

zone_template(zone)

@spec zone_template(String.t()) :: {:ok, String.t() | nil} | error()

See zone_template/1.