SaltDns.Credentials (Salt v1.0.0)

API tokens and TSIG keys of an organisation.

Creating a credential returns its secret exactly once (the API token's plaintext, the TSIG key's base64 secret); only a hash (token) or the encrypted secret (TSIG key) is stored. What a credential may do is its grants (SaltDns.Auth.Grants), enforced by SaltDns.Access; this module only authenticates.

authenticate_token/2 answers {:error, :invalid} for every failure: malformed token, unknown id, wrong secret, revoked, expired, a credential of the wrong kind or one with unusable grants. The secret comparison runs (against a fixed dummy hash when the id is unknown) before any state is looked at, so neither the answer nor the work done tells a caller which ids exist.

TSIG key names are generated as <random>.<organisation id>.keys.salt-dns.invalid and never chosen by a user, so a name from one organisation cannot collide with another's.

Summary

Functions

Authenticates an API token. {:ok, credential} or {:error, :invalid}, with no distinction between the reasons. Updates last_used_at at most once a minute. A provisioning token (provisioning: true when it was created) authenticates only when the caller says provisioning: true as well; the PowerDNS API does not, so such a token is plain invalid there.

An ACME DNS-01 API token for zone (see SaltDns.Auth.Presets.acme/2). The zone must belong to the organisation.

An ACME TSIG key (for RFC 2136 clients such as certbot) for zone.

Creates an API token. Returns the credential and the plaintext token, which is not stored and cannot be shown again.

A mail-platform TSIG key (for Karta's RFC 2136 client) for zone.

Creates a TSIG key pinned to algorithm (SaltDns.Auth.TsigPolicy). The key name is generated. Returns the credential and the base64 secret, which is stored only encrypted and not shown again.

Gets a credential of the organisation by id, or nil.

The organisation's credentials, newest first.

Looks up a TSIG key by its (wire) name for the RFC 2136 listener.

A map of credential id to name, for the organisation's credentials among ids.

Revokes a credential. Idempotent: an already revoked credential keeps its first revocation time. Option: :now (for tests).

Sets the registrar level of an API token: :none, :read or :purchase (which includes :read). Only an owner or admin may do this (the console and SaltDns.Release check that); a TSIG key has no registrar level.

Records that a credential was used, at most once a minute (the update only happens when last_used_at is empty or older than a minute, so concurrent requests write at most once).

Types

opts()

@type opts() :: [{:expires_at, DateTime.t() | nil}]

Functions

authenticate_token(plaintext, opts \\ [])

@spec authenticate_token(term(), keyword()) ::
  {:ok, SaltDns.Credentials.Credential.t()} | {:error, :invalid}

Authenticates an API token. {:ok, credential} or {:error, :invalid}, with no distinction between the reasons. Updates last_used_at at most once a minute. A provisioning token (provisioning: true when it was created) authenticates only when the caller says provisioning: true as well; the PowerDNS API does not, so such a token is plain invalid there.

create_acme_token(org, user, name, zone, relative_names, opts \\ [])

An ACME DNS-01 API token for zone (see SaltDns.Auth.Presets.acme/2). The zone must belong to the organisation.

create_acme_tsig_key(org, user, name, algorithm, zone, relative_names, opts \\ [])

An ACME TSIG key (for RFC 2136 clients such as certbot) for zone.

create_api_token(org, user, name, grants, opts \\ [])

Creates an API token. Returns the credential and the plaintext token, which is not stored and cannot be shown again.

grants must be a non-empty list of valid grants (atom form); options: :expires_at, :provisioning (a provisioning token: it acts in the child organisations of org, see SaltDns.Provisioning), :registrar (the level of access to the registrar module: :none, the default, :read or :purchase; see set_registrar_level/2).

create_mail_token(org, user, name, zone, preset_opts \\ [])

A mail-platform API token for zone (see SaltDns.Auth.Presets.mail/2).

create_mail_tsig_key(org, user, name, algorithm, zone, preset_opts \\ [])

A mail-platform TSIG key (for Karta's RFC 2136 client) for zone.

create_tsig_key(org, user, name, algorithm, grants, opts \\ [])

Creates a TSIG key pinned to algorithm (SaltDns.Auth.TsigPolicy). The key name is generated. Returns the credential and the base64 secret, which is stored only encrypted and not shown again.

get(organisation, id)

Gets a credential of the organisation by id, or nil.

list(organisation)

The organisation's credentials, newest first.

lookup_tsig_key(key_name, opts \\ [])

@spec lookup_tsig_key(term(), keyword()) ::
  {:ok,
   %{
     key_name: String.t(),
     algorithm: String.t(),
     secret: binary(),
     grants: list(),
     organisation_id: integer(),
     credential_id: integer()
   }}
  | :error

Looks up a TSIG key by its (wire) name for the RFC 2136 listener.

Returns the key name, algorithm, decoded secret, grants, organisation id and credential id, or :error for an unknown, revoked or expired key, a key with unusable grants, or an algorithm that is no longer allowed. This does not update last_used_at: the lookup happens before the request's signature is verified, so call touch_last_used/2 after verification.

names_by_ids(arg1, ids)

@spec names_by_ids(SaltDns.Accounts.Organisation.t(), [integer()]) :: %{
  required(integer()) => String.t()
}

A map of credential id to name, for the organisation's credentials among ids.

revoke(credential, opts \\ [])

Revokes a credential. Idempotent: an already revoked credential keeps its first revocation time. Option: :now (for tests).

set_registrar_level(credential, level)

@spec set_registrar_level(SaltDns.Credentials.Credential.t(), atom()) ::
  {:ok, SaltDns.Credentials.Credential.t()} | {:error, :invalid}

Sets the registrar level of an API token: :none, :read or :purchase (which includes :read). Only an owner or admin may do this (the console and SaltDns.Release check that); a TSIG key has no registrar level.

touch_last_used(credential_or_id, now \\ DateTime.utc_now())

@spec touch_last_used(SaltDns.Credentials.Credential.t() | integer(), DateTime.t()) ::
  :ok

Records that a credential was used, at most once a minute (the update only happens when last_used_at is empty or older than a minute, so concurrent requests write at most once).