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
| Tool | Version | Source |
|---|---|---|
| Erlang/OTP | 29.1.2 | .tool-versions |
| Elixir | 1.20.5 (1.20.5-otp-29) | .tool-versions |
| Postgres | 17 (development stack and CI) | dev/compose.yml, .woodpecker.yml |
| Docker with Compose | the 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:
hex.audit(retired packages; it must run first, before anything compiles);format --check-formatted;compile --warnings-as-errors;deps.unlock --check-unused;credo --strict;sobelow --config --exit low(Phoenix security checks;.sobelow-conf);deps.audit(known advisories);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:
| Tag | Variable | Needs | Run |
|---|---|---|---|
:s3 | KARTA_TEST_S3 | Garage from the stack, bootstrapped (dev/garage-bootstrap.sh); KARTA_TEST_S3_ENDPOINT (default http://localhost:53900) | KARTA_TEST_S3=1 mix test --only s3 |
:unbound | KARTA_TEST_DNS | Unbound from the stack; KARTA_TEST_DNS_PORT (default 55353) | KARTA_TEST_DNS=1 mix test --only unbound |
:dkimpy | KARTA_TEST_DKIMPY | the image karta-dev-dkimpy:2 (docker build -t karta-dev-dkimpy:2 dev/dkimpy) | KARTA_TEST_DKIMPY=1 mix test --only dkimpy |
:mailpit | KARTA_TEST_MAILPIT | Mailpit 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_clients | KARTA_TEST_PROVIDER_CLIENTS | the 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 |
:powerdns | KARTA_TEST_PDNS | a throwaway PowerDNS server (dev/powerdns.sh start, then stop); KARTA_TEST_PDNS_PORT (default 58081) | KARTA_TEST_PDNS=1 mix test --only powerdns |
:rfc2136 | KARTA_TEST_RFC2136 | a 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_node | KARTA_TEST_TWO_NODE | a 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 request | KARTA_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), keepKARTA_DB_POOL_SIZEsmall 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-rubyandpostmarkgems against a Karta listener on the gateway address of a Docker network created with--internal(no route anywhere else) and removed afterwards. It also runsinbound_check.rbwith 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 indev/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:
| Step | What |
|---|---|
test | mix precommit against a Postgres 17 service |
docs | mix docs --warnings-as-errors |
tls-scan-server, tls-scan | the TLS scan (informational) |
dependency-scan | trivy fs for critical vulnerabilities |
The pipeline needs no secrets and publishes nothing.
Project layout
| Path | Contents |
|---|---|
lib/karta/accounts, servers, domains, credentials, suppressions | tenants, servers, domains and DKIM keys, credentials, suppression lists |
lib/karta/smtp | the SMTP server (session state machine, listener, handler, limits, TLS, PROXY protocol, spool) and client |
lib/karta/messages, storage, vault.ex, crypto.ex | message intake, storage protocol, retention, blob stores, encryption |
lib/karta/sending.ex | sending through the HTTP APIs |
lib/karta/outbound | delivery jobs, MX resolution, transfer, retries, return paths, DSNs, signing |
lib/karta/tracking | tracking tokens, link rewriting, events |
lib/karta/webhooks | webhooks: events, signatures, formats (Postal's, Mailgun's), the sending job, replays |
lib/karta/net_guard.ex, net_guard | where Karta may connect when someone else names the host; HTTP through it |
lib/karta/changes.ex | changes announced after their transaction commits, for live admin pages |
lib/karta/mail | the mail format: addresses, headers, MIME, encodings, Punycode, Received: |
lib/karta/auth/dkim | DKIM signing and verification |
lib/karta/dns | the DNS client, wire format, cache, resolver |
lib/karta_web | endpoints, routers, the four APIs, tracking, health, metrics; the admin pages (live/, live_updates.ex) |
lib/mix/tasks | mix karta.gen.dev_ca |
priv/repo/migrations | database 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/bin | server 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
masterrather than merging it in. AGENTS.mdholds the Phoenix, Elixir and Ecto conventions the code follows.
Useful mix tasks
| Task | What |
|---|---|
mix setup | dependencies, database, assets |
mix precommit | the 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.reset | drop and recreate the development database |
mix docs | this documentation |
mix openapi.spec.json --spec KartaWeb.API.V2.Spec --start-app=false | the OpenAPI document of /api/v2 |