Development

How to work on Karta itself. The rules for contributions are in Contributing; this page covers the tools. Work on the admin pages follows Admin interface, Admin design system and Live updates.

Toolchain

ToolVersionSource
Erlang/OTP29.1.2.tool-versions
Elixir1.20.5 (1.20.5-otp-29).tool-versions
Postgres17 (development stack and CI)dev/compose.yml, .woodpecker.yml
Docker with Composethe development stack and some opt-in tests

With mise or asdf, mise install in the repository installs Erlang and Elixir. Start the development stack and set up as in Local development:

docker compose -f dev/compose.yml up -d
dev/garage-bootstrap.sh
mix setup

The quality gate

One command must pass before every commit, and CI runs the same:

mix precommit

It runs, in the test environment and in this order:

  1. hex.audit (retired packages; it must run first, before anything compiles);
  2. format --check-formatted;
  3. compile --warnings-as-errors;
  4. deps.unlock --check-unused;
  5. credo --strict;
  6. sobelow --config --exit low (Phoenix security checks; .sobelow-conf);
  7. deps.audit (known advisories);
  8. test --warnings-as-errors.

An advisory that does not affect Karta may be acknowledged in mix.exs only with a comment that says why (security policy).

Tests

mix test                              # the default suite
mix test test/karta/outbound          # a directory
mix coveralls.html                    # with a coverage report in cover/

mix test creates and migrates the test database first. Tests use the development Postgres on localhost:55433 (DATABASE_HOST and DATABASE_PORT change that) and the database karta_test<MIX_TEST_PARTITION>. Set MIX_TEST_PARTITION to run several suites at once on one machine, each with its own database:

MIX_TEST_PARTITION=2 mix test

Each run also gets its own spool and blob directories (by OS process id), and the SMTP listeners in tests use ephemeral ports.

The default suite needs only Postgres: DNS is answered by a fake resolver, message bodies go to a local directory, and SMTP peers are started inside the test. Tests tagged :ipv6 are skipped where there is no IPv6 loopback.

Opt-in test suites

Tests that need another service are excluded unless their variable is 1:

TagVariableNeedsRun
:s3KARTA_TEST_S3Garage from the stack, bootstrapped (dev/garage-bootstrap.sh); KARTA_TEST_S3_ENDPOINT (default http://localhost:53900)KARTA_TEST_S3=1 mix test --only s3
:unboundKARTA_TEST_DNSUnbound from the stack; KARTA_TEST_DNS_PORT (default 55353)KARTA_TEST_DNS=1 mix test --only unbound
:dkimpyKARTA_TEST_DKIMPYthe image karta-dev-dkimpy:2 (docker build -t karta-dev-dkimpy:2 dev/dkimpy)KARTA_TEST_DKIMPY=1 mix test --only dkimpy
:mailpitKARTA_TEST_MAILPITMailpit from the stack; KARTA_TEST_MAILPIT_SMTP_PORT (default 51025), KARTA_TEST_MAILPIT_API (default http://localhost:58025)KARTA_TEST_MAILPIT=1 mix test --only mailpit
:provider_clientsKARTA_TEST_PROVIDER_CLIENTSthe image karta-dev-provider-clients:5 (docker build -t karta-dev-provider-clients:5 dev/provider-clients)KARTA_TEST_PROVIDER_CLIENTS=1 mix test --only provider_clients
:powerdnsKARTA_TEST_PDNSa throwaway PowerDNS server (dev/powerdns.sh start, then stop); KARTA_TEST_PDNS_PORT (default 58081)KARTA_TEST_PDNS=1 mix test --only powerdns
:rfc2136KARTA_TEST_RFC2136a throwaway BIND 9 server that accepts dynamic update with TSIG (dev/rfc2136.sh start, then stop); KARTA_TEST_RFC2136_PORT (default 58053)KARTA_TEST_RFC2136=1 mix test --only rfc2136
:two_nodeKARTA_TEST_TWO_NODEa test database of its own and a few free PostgreSQL connections: the test starts two Karta nodes as separate OS processes and kills one during a delivery and a webhook requestKARTA_TEST_TWO_NODE=1 KARTA_DB_POOL_SIZE=2 MIX_TEST_PARTITION=<unique> mix test --only two_node
  • two_node starts two Karta nodes as separate OS processes against the test database, sends a message and a webhook request to local receivers that do not answer, kills the node that took the job, and checks that the other node delivers it once more and not at all twice. Give it a test database of its own (MIX_TEST_PARTITION), keep KARTA_DB_POOL_SIZE small so two nodes and the test share a handful of connections, and run it alone: it truncates the jobs, messages and webhook deliveries of that database. It needs no Docker and takes about a minute.
  • dkimpy signs and seals (ARC) with Karta and verifies with dkimpy, and the other way round, in a container without a network.
  • provider_clients runs the official mailgun-ruby, sendgrid-ruby and postmark gems against a Karta listener on the gateway address of a Docker network created with --internal (no route anywhere else) and removed afterwards. It also runs inbound_check.rb with no network, on a request the test built: the script does what an application written for Postal, Mailgun, SendGrid or Postmark does with a received message (checks the signature where there is one, reads the field the provider's ingress requires, and decodes the message).
  • mailpit delivers through Karta's outbound path to Mailpit and reads the message back through Mailpit's API; each test deletes what it delivered.
  • powerdns publishes Karta's records through the API of a PowerDNS server in a container of its own (API on 127.0.0.1 only, a test-only key, nothing asked of the outside world) and reads the zone back: exactly the expected RRsets, and a second run that changes nothing. Each test creates a zone and removes it.
  • rfc2136 publishes Karta's records through RFC 2136 dynamic update with TSIG to a BIND server in a container of its own (on 127.0.0.1 only, a test-only key, one zone, nothing asked of the outside world) and reads the zone back: the expected RRsets with the provider's owner tags, a second run that changes nothing, another domain's records left alone, and records no longer needed removed.
  • After a change to dev/unbound, recreate the container (docker compose -f dev/compose.yml up -d --force-recreate unbound); it reads the zones only at start. The test zones and how to re-sign them are described in dev/unbound/README.md.

TLS scan

dev/tls-scan.sh

starts SMTP listeners with test certificates (dev/tls_scan_server.exs) and scans them with testssl.sh in Docker. CI runs the same scan for information; the gate is test/karta/smtp/listener/tls_policy_test.exs.

Documentation

mix docs

builds this site into doc/ (git-ignored) with ExDoc: the guides in guides/, the changelog and policies, the module reference, and the OpenAPI document of the Karta API (exported first by mix openapi.spec.json). Links between pages are relative, so the site works from any path. mix docs --warnings-as-errors fails on broken references; CI builds the documentation on every pull request.

When you change behaviour, update the guide that describes it. The page Status and roadmap lists what does not exist yet.

Continuous integration

.woodpecker.yml runs on every pull request and on master:

StepWhat
testmix precommit against a Postgres 17 service
docsmix docs --warnings-as-errors
tls-scan-server, tls-scanthe TLS scan (informational)
dependency-scantrivy fs for critical vulnerabilities

The pipeline needs no secrets and publishes nothing.

Project layout

PathContents
lib/karta/accounts, servers, domains, credentials, suppressionstenants, servers, domains and DKIM keys, credentials, suppression lists
lib/karta/smtpthe SMTP server (session state machine, listener, handler, limits, TLS, PROXY protocol, spool) and client
lib/karta/messages, storage, vault.ex, crypto.exmessage intake, storage protocol, retention, blob stores, encryption
lib/karta/sending.exsending through the HTTP APIs
lib/karta/outbounddelivery jobs, MX resolution, transfer, retries, return paths, DSNs, signing
lib/karta/trackingtracking tokens, link rewriting, events
lib/karta/webhookswebhooks: events, signatures, formats (Postal's, Mailgun's), the sending job, replays
lib/karta/net_guard.ex, net_guardwhere Karta may connect when someone else names the host; HTTP through it
lib/karta/changes.exchanges announced after their transaction commits, for live admin pages
lib/karta/mailthe mail format: addresses, headers, MIME, encodings, Punycode, Received:
lib/karta/auth/dkimDKIM signing and verification
lib/karta/dnsthe DNS client, wire format, cache, resolver
lib/karta_webendpoints, routers, the four APIs, tracking, health, metrics; the admin pages (live/, live_updates.ex)
lib/mix/tasksmix karta.gen.dev_ca
priv/repo/migrationsdatabase migrations
config/configuration; runtime.exs reads the environment
dev/the development stack and test images
test/tests; test/support helpers; test/fixtures RFC vectors, keys and provider examples
rel/overlays/binserver and migrate scripts of the release
guides/this documentation

Conventions

  • Tests first. New behaviour comes with tests; a bug fix with a test that failed before.
  • Protocol code (SMTP, DKIM, DNS, MIME) is pure functions without sockets or processes, tested against RFC vectors and with property tests (stream_data); side effects sit behind behaviours (policies, sinks, blob stores, resolvers, key sources) so tests use fakes.
  • Tests never send mail to real addresses; the stack in dev/ is the only network tests touch, and DNS in tests is a fake resolver or the development Unbound.
  • No real secrets in the repository: test keys are generated in tests or marked as fixtures.
  • English names in code, comments and documentation; user-facing strings through Gettext.
  • Every security fix gets a Security entry in CHANGELOG.md.
  • One topic per pull request; rebase on master rather than merging it in.
  • AGENTS.md holds the Phoenix, Elixir and Ecto conventions the code follows.

Useful mix tasks

TaskWhat
mix setupdependencies, database, assets
mix precommitthe quality gate
mix karta.gen.dev_ca [--dir priv/dev_ca] [--host NAME]... [--force]a development CA and SMTP certificates (RSA and ECDSA) for localhost, 127.0.0.1, ::1 and the given names
mix ecto.resetdrop and recreate the development database
mix docsthis documentation
mix openapi.spec.json --spec KartaWeb.API.V2.Spec --start-app=falsethe OpenAPI document of /api/v2