Connecting to Karta

Karta is a mail platform (SMTP in and out, an HTTP API, encrypted message storage). It is optional on its side: it exposes the small /kantoor/v1 interface only when it is built with that flag.

Kantoor keeps the mailbox index (folders, flags, UIDs, users) and Karta keeps the message bodies and the transport. Karta learns nothing about the index. The written interface is the Karta interface contract; where this page and the contract differ, Karta's code is the authority.

Today Karta is the only mail source Kantoor can use. A plain IMAP, JMAP or Maildir source would need a second implementation of the mail-source layer; see Mail sources.

What Kantoor uses

  • Signed inbound push. Karta posts new mail to POST /karta/inbound as a Standard Webhooks request, with the sender-authentication result in data.authentication. Kantoor acknowledges only after its index transaction commits. A push without body bytes (a notify-only endpoint) is fetched separately with a ranged raw read.
  • Raw body reads. GET /kantoor/v1/messages/:id/raw serves a whole message or one byte range. Bodies are encrypted in chunks in Karta, so a range read is cheap.
  • Delivery delete. DELETE /kantoor/v1/deliveries/:id removes one recipient's copy. A body is removed when every delivery of it is gone.
  • Domain list. GET /kantoor/v1/domains lists the domains this Kantoor may serve.
  • Sending. POST /kantoor/v1/messages/raw sends a finished MIME message, with a retention and source: "kantoor".

Configuration

Kantoor reads the connection from environment variables:

VariableMeaning
KARTA_BASE_URLbase URL of the Karta instance
KARTA_API_KEYa Karta API key marked as a Kantoor key: the instance's organisation. A domain whose mail lives in another Karta organisation brings that organisation's key with its attach call (POST /domains/{ref}/mail-source, api_key); it is stored encrypted on the domain
KARTA_INBOUND_SECRETScomma-separated whsec_ signing secrets of the inbound endpoints (optional: secrets of endpoints Kantoor registered itself are stored, see below)
KANTOOR_VAULT_KEYkey that encrypts the secrets Kantoor stores: base64 of 32 bytes, openssl rand -base64 32
KANTOOR_VAULT_KEY_IDlabel of that key, default v1
KANTOOR_VAULT_RETIRED_KEYSid:base64 pairs, comma separated: old keys that still decrypt after a rotation
KARTA_INBOUND_HEADERoptional extra shared header, as Name: value
KARTA_MAX_BODY_BYTESlargest inbound request body, default 40 MiB

The API key is never logged. Several inbound secrets may be configured at once, so a secret can be rotated without dropping pushes.

What the contract provides

The contract's operation table is the detailed reference. In summary:

OperationState
Inbound push and raw readsimplemented on both sides
Delivery delete, send raw MIME, health, domain listimplemented on both sides
Route retentionimplemented in Karta (PATCH /routes/:id, retention only); not called by Kantoor
Register endpoint, attach domain, create and take over routes, list a domain's routes, message metadataimplemented in Karta and called by Kantoor; checked against Karta's code and tests, not against a running Karta
Null envelope sender for automatic replies (auto_reply)implemented in Karta (PR #64); used for vacation replies, with a fallback for an older Karta
Listing messages (cursor)implemented in Karta; Kantoor's index rebuild from it is not built
Structured send and outbound eventsimplemented in Karta; not used by Kantoor

Attaching a domain in Mail administration → Domains registers the HTTP endpoint (format karta, one per domain) and the domain's routes in Karta. Karta shows the endpoint's signing secret once, in that answer.

Kantoor keeps one exact route per mailbox and alias (so a known address is received even when the catch-all is off) and the domain's * route as the catch-all: delivering when the domain has a catch-all mailbox, refusing (550, while the sender is connected) when it does not. A disabled mailbox's exact route refuses too; a disabled domain puts every route on hold. The domain detail screen shows whether the routes were last synced (the list has a Routes column); Re-sync routes sets them again. Domains attached by a release before exact routes existed still have the single * route that accepts everything: run mix kantoor.routes.sync (or bin/kantoor eval 'Kantoor.Release.sync_mail_routes()') once after that release, or click Re-sync routes per domain. The contract's "The routes Kantoor keeps" describes the details.

The signing secret

With KANTOOR_VAULT_KEY set, Kantoor stores that secret itself, encrypted (AES-256-GCM, a fresh IV per value, the key id and a context string authenticated), in the database next to the domain. The inbound receiver verifies a push against the stored secrets and against KARTA_INBOUND_SECRETS, which stays supported for endpoints made by hand. A stored secret is never shown, logged or written to the audit trail again; the audit entry says only that one was stored. Nothing needs to be configured after attaching.

Without the key nothing is stored: the secret is shown once on the page, and you add it to KARTA_INBOUND_SECRETS and restart, as before. Set the key before you attach.

  • Registering again ("Register again" in the domain's danger zone) asks Karta for the same endpoint and route; both calls are idempotent. When Karta makes a new endpoint (for example after it was deleted in Karta) it shows a new secret, which replaces the stored one at once: from the moment the page answers, pushes signed with the old secret are refused. An endpoint Karta already had shows no secret, and the stored one stays.
  • Boot check. If secrets are stored and KANTOOR_VAULT_KEY is missing, or does not decrypt one of them (the wrong key, or a retired key that was dropped), Kantoor refuses to start and says which. It does not start and then answer every push from Karta with an error. Keep a copy of the key outside the server; without it the stored secrets are lost and the endpoints have to be registered again.
  • Rotating the key. Move the old key to KANTOOR_VAULT_RETIRED_KEYS as v1:<base64>, set a new KANTOOR_VAULT_KEY and KANTOOR_VAULT_KEY_ID=v2, restart, run bin/kantoor rpc "Kantoor.Vault.reencrypt()", and remove the retired key at the next restart.

If Karta refuses (another key owns the endpoint, a route already exists, the domain is not verified, the key is restricted), the page says which; the contract lists them. Read the contract before relying on any one operation.

Retention and quotas

Retention is decided by Kantoor and enforced by Karta. A policy is "keep", a number of days, or none (the server's own rule). It lives on the delivery: copied from the route, replaced by the answer to the inbound push, or taken from the send request. With several deliveries the longest retention wins, and keep beats any number of days. keep is exempt from Karta's size cap.

Quotas live in Kantoor, are computed from the index and are set per domain with a per-mailbox override. Mail over quota is accepted, stored, and held out of sight until space is freed. Deleting mail in Kantoor deletes the delivery in Karta.

When something fails

  • Karta unreachable on send: the send fails visibly. Kantoor does not queue it.
  • Duplicate pushes: ignored on the webhook id.
  • Missed pushes: if Kantoor is down longer than Karta's retry window, the message listing could recover them, but Kantoor's recovery job is not built.
  • Loss of Kantoor's database: the listing and raw reads can rebuild messages, but not flags or folders, so back up the database.
  • Revoked or rotated key: Kantoor loses access until the new key is configured.
  • Bounces and delivery status notifications: not handled in Kantoor.