Skip to content

Isolate teams on a shared organization ESP (#220) - #226

Merged
s3rot0n1n merged 10 commits into
mainfrom
t3/school-team-provisioning-isolation
Nov 26, 2026
Merged

s3rot0n1n merged 10 commits into
mainfrom
t3/school-team-provisioning-isolation

Conversation

@s3rot0n1n

@s3rot0n1n s3rot0n1n commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes the work in #220, except domain support for providers other than Amazon SES. SendGrid, Resend, Postmark and Mailgun come in a follow-up PR.

What changes

1. Guard the shared ESP account (3beb013)

  • A rolling 7-day bounce and complaint check for each organization-owned ESP, summed across its teams from the outbound ledger. The thresholds come from config and sit below ESP limits.
  • Teams over the line have marketing paused on that mailbox, worst first. The whole mailbox pauses only if the remaining teams are still over. Transactional email keeps sending.
  • Small teams are judged on absolute counts.
  • Self-hosters enable it with SHARED_ESP_GUARD=on; it's on by default in cloud mode. Operators release a pause with the billing CLI.
  • The dashboard explains a shared-mailbox pause when a broadcast or sequence is refused. ADR 0013 records the design.
  • Also fixes a deadlock in usage-bucket locking: reserving and committing a send locked the buckets in different orders.

2. Per-team health for the organization (ef9cd5b)

  • GET /provisioning/teams/:id and /usage return health: status, reason, scope, and 7-day sent, bounced and complained counts.
  • GET /organizations/:id/mail-activity now reads the outbound ledger, so it covers campaigns as well as transactional email, plus complaints. It also returns each team's health and quota, so one call covers every team. The Organizations → Activity dashboard shows both.

3. Hold only the team that made a flagged import (08081f9)

  • Each team is judged on its own imports, and a flag holds only that team.
  • A list split across teams still holds the whole organization, and flags without a team keep their old behaviour.

4. A team's own sending domain on a shared Amazon SES mailbox (697007a, docs a9fd80a)

  • The organization stores a separate, encrypted SES API key on the shared mailbox, set in the dashboard or with PUT /organizations/:id/esps/:espId/management-credentials.
  • A team's From address is set in the dashboard or with PUT/GET/DELETE /provisioning/teams/:teamId/sending-domain. SendLit registers the domain in SES (Easy DKIM) and returns the DNS records to add.
  • The team sends from its own domain only once DKIM and DMARC pass. If they later fail, there is a 72-hour grace period, then the team's marketing email pauses. SendLit never silently falls back to the shared domain.
  • Organization policy: requireTeamSendingDomain (off, marketing or all; default off) and an optional monthly cap on marketing sent through the shared domain.
  • Team domains are added to the Postmaster Tools sync. The dashboard has a team sending-domain dialog and shows the domain's status in the team list. Self-hosting docs explain how to create the IAM key.
  • SendLit never takes over or deletes an SES identity it didn't create (domain_exists_at_provider, 409). A team can't use the shared mailbox's own domain.

Fixes found during the QA smoke

  • 04b323a: concurrent sends through a shared mailbox deadlocked on the grant row. The lock is now FOR NO KEY UPDATE. On real Postgres, 10 concurrent senders went from 199 deadlocks in 200 sends to none in 4,000.
  • 2345618: team domains no longer appear in the organization's own Sending domains list, and the organization's TXT-challenge verify and revoke can't reach them.

Review pass (60268f0)

  • Removed duplicate and unused code:
    • one 7-day ledger count query instead of three;
    • reuse of the existing rate, rank, day, access, scheduler, DialogSection, STATUS_STYLE and DNS-records-table code;
    • dead exports and test hooks;
    • two unused sending_domains columns, dropped from the unreleased migration 0036.
  • Docs now explain each topic once and link to it.
  • Fixed:
    • with several API replicas, each due domain is now claimed first, so it's checked once;
    • when a team switches or removes its domain, the old SES identity is deleted only after the change is saved;
    • provider errors return a declared 422 instead of a 502;
    • an operator's mailbox-wide release now applies to a team that was released earlier;
    • the sequence list shows the same send-refusal messages as the editor.

Code review fixes (41513fb)

  • Cancelling a team's grant (which also happens when the team is archived or the organization closed) or retiring the mailbox removes the team's domain from the ESP. Retiring a mailbox and deleting an organization also wipe the SES API key.
  • A team moved to another mailbox registers its domain there. A domain registered through the old mailbox is never used to send.
  • The SES API key can't be removed while team domains use it (409 esp_management_credentials_in_use).
  • The shared-domain monthly cap counts only broadcast and sequence sends from the shared domain, queued ones included.
  • The pause message for a required team domain is now accurate.
  • Import flags raised by the previous release keep holding the whole organization.
  • GET on a team domain re-checks the provider at most every 30 seconds.
  • The organization can add a domain a team used before; one a team still uses returns domain_in_use.

Migrations 0035 and 0036 are additive.

Testing

  • API suite: 1145 pass, 0 fail. Web suite: 124 pass. Typecheck, Biome, drift, conformance, openapi:check, drizzle-kit generate (no changes) and the FrontLit docs check all pass.

  • A local browser smoke passed for each subtask. For subtask 4 it used an SESv2 mock and covered both refusals, switching domains, verification, sending from the team domain, the provisioning routes, and removal.

  • QA, with a real Amazon SES account (ap-southeast-1), arm64 images:

    • pr-226-697007a: storing the API key; registering a domain and verifying it once DNS was published; transactional email and a 20-recipient broadcast sent From the team domain; the organization policy refusing a team without its own domain; removing the domain, which deleted the SES identity.
    • pr-226-41513fb (deployed now): registering a domain, the SES API key refused for removal while it's in use, the organization refused that domain, then cancelling the grant deleted the SES identity, the organization could add the domain, and after a re-grant the team sends from the shared domain.
    • pr-226-60268f0: refusing the shared mailbox's domain; registering and verifying a new domain; transactional email sent From it; removing it, which deleted the SES identity.

    The mailbox's SMTP host was QA Mailpit, so no email went through SES. Subtasks 1–3 were smoked on QA earlier. Details are in the comments on Isolate provisioned teams that share an organization ESP #220.

🤖 Generated with Claude Code

Rajat and others added 4 commits November 25, 2026 21:00
Each organization-owned ESP gets a rolling 7-day bounce and complaint
check summed across its teams. Teams over the line pause marketing on that
mailbox, worst first; the whole mailbox pauses only if the rest is still
over. Transactional email keeps sending. Small teams are judged on absolute
bounce and complaint counts. Self-hosted operators turn it on with
SHARED_ESP_GUARD=on; cloud runs it by default.

Operators release a pause with the billing CLI; for 7 days after, only
sends made after the release count. The dashboard explains a shared-mailbox
pause when a broadcast or sequence is refused.

Also fixes a deadlock in shared-mailbox quota locking: reserving and
committing a send locked the usage buckets in different orders, so API
sends and the mail worker could deadlock each other. Every path now locks
them in one order.

ADR 0013 records the design.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Provisioned-team and usage responses now include the team's health: its
strictest sending control (its own reputation or the shared mailbox's
guard), the reason and where it comes from, and its last 7 days of sent,
bounced and spam-complaint counts.

Organization mail activity now reads the outbound ledger, so it covers
broadcasts and sequences as well as transactional email and counts spam
complaints. Each team also carries its health and shared-mailbox quota, so
one call shows every team. The dashboard's Activity tab shows both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new account's large import used to hold campaigns for the whole
organization. Each team is now judged on its own imports, and the flag
holds only that team, so other teams keep sending. Import flags are kept
per team, so a second team's large import still gets its own flag.

A list split across teams is still caught: the imports of teams that
aren't over on their own are judged together, and that flag holds the
whole organization, as before. Flags raised before this change have no
team and also still hold the whole organization.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each send inserts its outbound row, whose foreign key takes FOR KEY SHARE
on the team's grant, and then reserves quota with SELECT ... FOR UPDATE on
that same grant in the same transaction. FOR UPDATE conflicts with the
other sends' key-share locks, so concurrent sends through a shared mailbox
(any broadcast) deadlocked and the broadcast paused itself. Found in the
QA smoke for #220; it predates this branch.

Lock the grant FOR NO KEY UPDATE instead, as reserveSend already does for
the team row. Reservations still serialise on the grant. On real Postgres,
10 concurrent senders went from 199 deadlocks in 200 sends to none in
4,000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T18:29:06.054784Z 04b323a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 04b323ac2e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Rajat and others added 3 commits November 26, 2026 00:31
A team on an organization's Amazon SES mailbox can now register its own
From domain. SendLit creates the SES identity with Easy DKIM through a
management API key stored encrypted on the mailbox (separate from the SMTP
secret) and returns the DKIM CNAMEs and a DMARC record to publish. Once SES
verifies DKIM and DMARC is published, the team's mail goes out from its own
address, signed for its own domain, so its sender reputation is its own.

If the domain later fails, the team keeps it for a grace period
(TEAM_SENDING_DOMAIN_GRACE_HOURS, 72 by default), then its marketing
pauses; it never falls back to the shared domain on its own. The delivery
policy gains requireTeamSendingDomain (off/marketing/all, default off) and
an optional monthly cap on marketing through the shared domain.

- API: PUT/GET/DELETE /provisioning/teams/:teamId/sending-domain and the
  same under /organizations/:id/teams/:teamId, plus PUT/DELETE
  /organizations/:id/esps/:espId/management-credentials.
- Team domains are re-checked hourly and included in the Google Postmaster
  Tools sync.
- Dashboard: a sending-domain dialog per team, an SES API key dialog per
  mailbox, the team's domain status in the Teams tab, and the new policy
  fields.
- Migration 0036 only adds columns, foreign keys and an index.

Other providers (SendGrid, Resend, Postmark, Mailgun) plug into the same
adapter later. ADR 0013 is updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A step-by-step page for self-hosters and organization owners: the scoped
IAM policy, the user and access key, where the key goes (dashboard or API),
the region, and the related settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Team sending domains are proven by the ESP's DKIM and managed per team,
so the organization's Sending domains list no longer shows them with
Verify and Revoke, and the TXT-challenge verify and revoke cannot reach
them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes:
- Never adopt or delete an SES identity SendLit didn't create: register
  refuses an existing identity (domain_exists_at_provider, 409), and a
  team can't take the shared mailbox's own domain (domain_in_use).
- Switching or removing a team domain removes the old identity only after
  the change is committed.
- Claim due team domains before checking them, so replicas don't double
  check.
- Provider errors map to 422 esp_provider_error on every route, instead
  of an undeclared 502.
- A team released before a later ESP-wide release is counted from the
  later release.
- Sequence list actions show the same send-refusal messages as the editor.

Cuts, no behaviour change: one ledger count query for the guard, team
health and reputation; reused rate, rank, day and deferral helpers; drop
the unused sending_domains columns authenticated_at and provider from the
unreleased 0036; drop dead exports and test hooks; reuse the existing
access, actor, scheduler, DialogSection, STATUS_STYLE and DNS records
table; state each docs topic once and link to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@s3rot0n1n s3rot0n1n changed the title Isolate teams on a shared organization ESP: guard, health, per-team import hold (#220) Isolate teams on a shared organization ESP (#220) Nov 26, 2026
Rajat and others added 2 commits November 26, 2026 12:59
- Cancelling a team's grant (team archived, organization closed) or
  retiring a shared mailbox removes the team's own domain from the ESP,
  so its identity isn't left in the customer's account and checked
  forever. Retiring a mailbox and deleting an organization also wipe the
  provider API key.
- A team moved to another mailbox registers its domain there; a domain
  row on another ESP is never used to send.
- The SES API key can't be removed while team domains need it
  (esp_management_credentials_in_use, 409).
- The shared-domain monthly cap counts only broadcast and sequence sends
  from the shared domain, queued ones included.
- A sequence paused because the organization requires a team domain says
  so, instead of pointing at organization domain verification.
- Import flags raised before flags were per team keep holding the whole
  organization.
- Reading a team domain re-checks the provider at most every 30 seconds.
- The organization can add a domain a team used before; one a team still
  uses returns domain_in_use.
- The provisioned-team contract comment now says what health reveals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The guard read release state and metrics before taking the row lock, so
an operator release that committed while an evaluation waited for the
lock was re-paused straight away on the older numbers. It now reads the
locked rows and computes the metrics inside the same transaction.

Reproduced on Postgres: release committed while an evaluation waited on
the lock; before, the team was paused again; now it stays released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@s3rot0n1n
s3rot0n1n merged commit 4bb0b23 into main Nov 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant