Karta. Domains. Rotation
(Karta v1.0.0)
DKIM key rotation with dual publication: a new key's record is in DNS before the key signs, and an old key's record stays while mail it signed may still be checked.
- An owner or admin rotates the active key
(
Karta.Domains.rotate_dkim_key/1): a pending key of the same algorithm with a new selector. Its record is published next to the active key's: by hand, or by Karta where it publishes the domain's zone (Karta.DNS.Publish). - Every hour (
advance/2, run byKarta.Domains.Check) Karta looks the pending key's record up, without the DNS cache. Once checks have found it forpublication_wait/0(an hour, so that every name server of the zone has it, not only the one that answered), the key is activated and the key it replaces starts retiring. A check that does not find it, or finds another key there, starts the wait again; a lookup that fails changes nothing. - A retiring key is retired
retirement_wait/0(7 days) after it started retiring. Karta signs every delivery attempt with the keys active at that moment, so no mail leaves signed with a retiring key; the week is for receivers that check a signature again later (forwarding, mailing lists, mail that waits further down the way). Its record can then be removed (where Karta publishes the zone, it removes it).
Activating a key by hand (activate/2, the domain page) looks its
record up first, and refuses while the record is not in DNS.
On a schedule. With KARTA_DKIM_ROTATE_AFTER_DAYS set, schedule/2
(run by the hourly check) rotates an active key that has signed for that
many days, in a domain whose zone Karta publishes
(Karta.DNS.Publish.allowed?/1): the new key's record is published by
Karta, seen for publication_wait/0 (never less than the records' TTL,
so every resolver that cached the absence has had time to forget it),
and only then signs; the old key stops signing at that moment and its
record stays for retirement_wait/0, far beyond a record's TTL. In a
domain whose DNS Karta does not write, a rotation could not finish by
itself, so none is started: its owner rotates it by hand.
Summary
Functions
Activates a key once its record is in DNS: looks it up (without the
cache) and activates the key (Karta.Domains.activate_dkim_key/2) when
the record holds the key. Options: dns (options for
Karta.DNS.lookup/3) and now.
Takes the domain's rotations a step further (see the module doc): looks
up the record of every pending key that replaces another, activates
those found for publication_wait/0 (the domain must be verified), and
retires the keys that started retiring retirement_wait/0 ago. Returns
the keys activated and retired. Options: dns (options for
Karta.DNS.lookup/3; the cache is never used) and now.
The days a key signs before it is rotated, from the setting
KARTA_DKIM_ROTATE_AFTER_DAYS: nil when it is not set (Karta rotates
no key by itself), else a whole number from 30 to 3650.
A value outside that range or not a number raises, so that a typo is
found when Karta starts.
How long a rotation's key must have been found in DNS before it is
activated: an hour, which is the TTL of the records Karta publishes
(Karta.DNS.Publish.ttl/0; a test keeps the wait from falling below it).
How long a key retires before it is retired.
Starts the rotation of the domain's active keys that have signed for the
configured number of days (rotate_after_days; option, else
config :karta, Karta.Domains.Rotation, rotate_after_days:), when the
domain is verified and Karta publishes its zone. A key already being
replaced is left to that rotation. Returns the pending keys started.
Options: now, rotate_after_days and publishes? (a function from the
domain to a boolean; default Karta.DNS.Publish.allowed?/1).
Functions
@spec activate(Karta.Domains.DKIMKey.t(), keyword()) :: {:ok, Karta.Domains.DKIMKey.t()} | {:error, :retired | :domain_not_verified | :record_missing | :record_different | :lookup_failed}
Activates a key once its record is in DNS: looks it up (without the
cache) and activates the key (Karta.Domains.activate_dkim_key/2) when
the record holds the key. Options: dns (options for
Karta.DNS.lookup/3) and now.
@spec advance(Karta.Domains.Domain.t(), keyword()) :: {:ok, %{ activated: [Karta.Domains.DKIMKey.t()], retired: [Karta.Domains.DKIMKey.t()] }}
Takes the domain's rotations a step further (see the module doc): looks
up the record of every pending key that replaces another, activates
those found for publication_wait/0 (the domain must be verified), and
retires the keys that started retiring retirement_wait/0 ago. Returns
the keys activated and retired. Options: dns (options for
Karta.DNS.lookup/3; the cache is never used) and now.
@spec parse_rotate_after_days!(String.t() | nil) :: pos_integer() | nil
The days a key signs before it is rotated, from the setting
KARTA_DKIM_ROTATE_AFTER_DAYS: nil when it is not set (Karta rotates
no key by itself), else a whole number from 30 to 3650.
A value outside that range or not a number raises, so that a typo is
found when Karta starts.
@spec publication_wait() :: Duration.t()
How long a rotation's key must have been found in DNS before it is
activated: an hour, which is the TTL of the records Karta publishes
(Karta.DNS.Publish.ttl/0; a test keeps the wait from falling below it).
@spec retirement_wait() :: Duration.t()
How long a key retires before it is retired.
@spec schedule(Karta.Domains.Domain.t(), keyword()) :: [Karta.Domains.DKIMKey.t()]
Starts the rotation of the domain's active keys that have signed for the
configured number of days (rotate_after_days; option, else
config :karta, Karta.Domains.Rotation, rotate_after_days:), when the
domain is verified and Karta publishes its zone. A key already being
replaced is left to that rotation. Returns the pending keys started.
Options: now, rotate_after_days and publishes? (a function from the
domain to a boolean; default Karta.DNS.Publish.allowed?/1).