Writing a DNS provider
Karta publishes its own records of verified sending domains: the
ownership record, the DKIM keys' records, the tracking host's CNAME, and
the records of the MTA-STS policy, the TLS reports' address and the BIMI
logo Karta serves. It writes them through a DNS provider, a module
that implements the Karta.DNS.Provider behaviour. Karta's own
PowerDNS client (Karta.PowerDNS) is the default; RFC 2136 dynamic
update (Karta.RFC2136) is built in for BIND, Knot, PowerDNS and other
authoritative servers that accept it. Another DNS service can be reached
with a provider of your own, in a Hex package that is compiled into the
image.
The three calls
A provider answers five functions: name/0, config/0, and the three
that do the work:
| Call | Gives back |
|---|---|
list_zones(config) | The zones the server holds, each %{id: ..., name: ..., kind: ...}. id is whatever your calls later use; name is absolute, lower case, with the final dot. kind is your word for the zone's type, or nil. |
read_zone(config, zone_id, names) | A %Karta.DNS.Zone{id:, name:, kind:, rrsets: [...]} with the RRsets at names and every RRset in the zone that Karta wrote. names is a hint: a provider that reads a whole zone at once ignores it. |
apply(config, zone, changes) | {:ok, warnings} after the change set was written whole, or {:error, text} when it was not. A warning is something that did not finish after the records were written (a failed notification, say). |
config/0 reads your settings from the environment and returns whatever
your other calls need, or nil when nothing is configured (Karta then
publishes nothing and hides the publishing panel). It runs at start and
whenever Karta needs the configuration; read System.get_env/1 there and
nowhere else. Keep the settings in a struct with an Inspect
implementation that redacts secrets, as Karta.PowerDNS.Config does.
The record model
Records are in zone-file presentation form: absolute names in lower
case with the final dot, and a TXT RR's content as quoted strings of at
most 255 octets ("v=DKIM1; ..." "..."). Karta.DNS.Records builds and
parses them. Karta publishes only TXT and CNAME records.
Every RRset Karta wrote carries an owner tag in rrset.owner:
"karta:<domain id>". Your provider must keep it, because it is what
lets Karta change and remove its own records without touching anyone
else's:
Karta.PowerDNSstores it as a PowerDNS comment of the accountkarta.Karta.RFC2136keeps an inventory TXT record at_karta-records.<zone>.- A provider whose backend has no metadata can keep the same inventory
record, or a table of its own; what matters is that
read_zone/3reports the tag only for RRsets Karta wrote and thatapply/3writes it with the records it creates.
read_zone/3 reports an RRset without an owner tag when someone else
wrote it. Karta then leaves it alone, or takes it over when it already
holds exactly Karta's records.
A %Karta.DNS.Change{} has an action (:create, :update, :adopt,
:delete, :conflict), the zone id, the owner domain's id, the
name, type, ttl and records Karta wants, and current, the
records the zone holds now. :conflict changes are never passed to
apply/3 (Karta reports them instead), and :adopt asks you to record
the owner tag; the records themselves are already right. What you write
must be idempotent: with nothing to change, Karta sends no change set at
all.
An example
This provider talks to a made-up JSON API; the shape is what matters.
It keeps the owner tag in a comment field the API has room for, reads
the whole zone, and writes one request per change set.
defmodule MyDNS do
@moduledoc "Karta's DNS provider for MyDNS."
@behaviour Karta.DNS.Provider
alias Karta.DNS.{Change, RRSet, Zone}
alias Karta.DNS.Records
defmodule Config do
@enforce_keys [:url, :token]
defstruct [:url, :token, zones: []]
defimpl Inspect do
def inspect(config, _opts) do
"#MyDNS.Config<url: #{inspect(config.url)}, token: \"[redacted]\", zones: #{inspect(config.zones)}>"
end
end
end
@impl true
def name, do: "MyDNS"
@impl true
def config do
with url when is_binary(url) <- System.get_env("MYDNS_URL"),
token when is_binary(token) <- System.get_env("MYDNS_TOKEN") do
%Config{
url: url,
token: token,
zones: Karta.DNS.Provider.normalize_zones(String.split(System.get_env("MYDNS_ZONES", ""), ","))
}
else
_unset -> nil
end
end
@impl true
def list_zones(%Config{} = config) do
with {:ok, zones} <- request(config, :get, "/zones") do
{:ok, for(zone <- zones, do: %{id: zone["id"], name: zone["name"], kind: zone["kind"]})}
end
end
@impl true
def read_zone(%Config{} = config, id, _names) do
with {:ok, zone} <- request(config, :get, "/zones/" <> id) do
{:ok,
%Zone{
id: zone["id"],
name: zone["name"],
kind: zone["kind"],
rrsets: for(rrset <- zone["rrsets"], do: rrset(rrset))
}}
end
end
@impl true
def apply(%Config{} = config, %Zone{id: id}, changes) do
body = %{rrsets: Enum.map(changes, &change/1)}
case request(config, :patch, "/zones/" <> id, json: body) do
{:ok, _answer} -> {:ok, []}
{:error, text} -> {:error, text}
end
end
defp rrset(rrset) do
%RRSet{
name: String.downcase(rrset["name"]),
type: rrset["type"],
ttl: rrset["ttl"],
records: rrset["records"],
owner: owner(rrset["comment"])
}
end
# Your API's way of storing "karta:<domain id>", if it has one.
defp owner(nil), do: nil
defp owner("karta:" <> _id = tag), do: tag
defp owner(_comment), do: nil
defp change(%Change{action: :delete} = change),
do: %{name: change.name, type: change.type, delete: true}
defp change(change) do
%{
name: change.name,
type: change.type,
ttl: change.ttl,
records: change.records,
comment: RRSet.owner(change.owner)
}
end
defp request(config, method, path, options \\ []) do
[
method: method,
base_url: config.url,
url: path,
headers: [{"authorization", "Bearer " <> config.token}],
redirect: false,
retry: false,
receive_timeout: 15_000
]
|> Keyword.merge(options)
|> Req.request()
|> case do
{:ok, %{status: status, body: body}} when status in 200..299 -> {:ok, body}
{:ok, %{status: 401}} -> {:error, "MyDNS refused the token (401)."}
{:ok, %{status: status}} -> {:error, "MyDNS answered #{status}."}
{:error, exception} -> {:error, "MyDNS could not be reached (#{Exception.message(exception)})."}
end
end
endKarta.DNS.Records.absolute/1, txt/1, txt_strings/1 and
txt_value/1 are the presentation helpers; Karta.DNS.Wire encodes and
decodes DNS messages if your API speaks them.
Adding it to the build
A provider is compiled into the image; Karta never loads code into a
running release. Add the package to config/extra_deps.exs:
[{:my_dns, "~> 1.0"}]or name another file with KARTA_EXTRA_DEPS at build time (a path). The
file is Elixir and runs while mix.exs reads the dependencies, so build
only what you trust: it is part of your image. Then build as usual
(mix deps.get, mix compile, mix release, or the Dockerfile). The
default file adds nothing.
Selecting it
Set KARTA_DNS_PROVIDER to the provider's module name:
KARTA_DNS_PROVIDER=MyDNSpowerdns (the default) and rfc2136 select the built-in providers. At
start Karta checks that the module is in the build and exports the
behaviour's functions; anything else stops the release with an error that
names KARTA_DNS_PROVIDER. The provider's own variables are then read by
its config/0.
Testing a provider
The suite's fakes show the shape:
test/support/power_dns/fake_server.ex (the PowerDNS API in memory) and
test/support/rfc2136/fake_server.ex (an authoritative server that
checks the prerequisites and applies updates). A provider's own tests
should cover:
config/0:nilwithout its settings, and no secret ininspect/1;list_zones/1andread_zone/3, including a record someone else wrote (no owner tag);apply/3: a create, an update, a delete, a second run that changes nothing, and an error whose text carries no credential;- the zones the operator allows: a name outside them is never written.
Run the real thing too: an opt-in test against a throwaway server, as
:powerdns and :rfc2136 do (dev/powerdns.sh, dev/rfc2136.sh).
Never point a test at a server that serves real zones.