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 API token for zone (see SaltDns.Auth.Presets.mail/2).
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
@type opts() :: [{:expires_at, DateTime.t() | nil}]
Functions
@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.
@spec create_acme_token( SaltDns.Accounts.Organisation.t(), SaltDns.Accounts.User.t(), String.t(), SaltDns.Zones.Zone.t(), [String.t()], opts() ) :: {:ok, SaltDns.Credentials.Credential.t(), String.t()} | {:error, term()}
An ACME DNS-01 API token for zone (see SaltDns.Auth.Presets.acme/2).
The zone must belong to the organisation.
@spec create_acme_tsig_key( SaltDns.Accounts.Organisation.t(), SaltDns.Accounts.User.t(), String.t(), String.t(), SaltDns.Zones.Zone.t(), [String.t()], opts() ) :: {:ok, SaltDns.Credentials.Credential.t(), String.t()} | {:error, term()}
An ACME TSIG key (for RFC 2136 clients such as certbot) for zone.
@spec create_api_token( SaltDns.Accounts.Organisation.t(), SaltDns.Accounts.User.t(), String.t(), list(), opts() ) :: {:ok, SaltDns.Credentials.Credential.t(), String.t()} | {:error, Ecto.Changeset.t()}
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).
@spec create_mail_token( SaltDns.Accounts.Organisation.t(), SaltDns.Accounts.User.t(), String.t(), SaltDns.Zones.Zone.t(), keyword() ) :: {:ok, SaltDns.Credentials.Credential.t(), String.t()} | {:error, term()}
A mail-platform API token for zone (see SaltDns.Auth.Presets.mail/2).
@spec create_mail_tsig_key( SaltDns.Accounts.Organisation.t(), SaltDns.Accounts.User.t(), String.t(), String.t(), SaltDns.Zones.Zone.t(), keyword() ) :: {:ok, SaltDns.Credentials.Credential.t(), String.t()} | {:error, term()}
A mail-platform TSIG key (for Karta's RFC 2136 client) for zone.
@spec create_tsig_key( SaltDns.Accounts.Organisation.t(), SaltDns.Accounts.User.t(), String.t(), String.t(), list(), opts() ) :: {:ok, SaltDns.Credentials.Credential.t(), String.t()} | {:error, Ecto.Changeset.t()}
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.
@spec get(SaltDns.Accounts.Organisation.t(), integer()) :: SaltDns.Credentials.Credential.t() | nil
Gets a credential of the organisation by id, or nil.
@spec list(SaltDns.Accounts.Organisation.t()) :: [SaltDns.Credentials.Credential.t()]
The organisation's credentials, newest first.
@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.
@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.
@spec revoke(SaltDns.Credentials.Credential.t(), keyword()) :: {:ok, SaltDns.Credentials.Credential.t()}
Revokes a credential. Idempotent: an already revoked credential keeps its
first revocation time. Option: :now (for tests).
@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.
@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).