Skip to content

fix(identity): skip DKIM keygen on a domain reclaim - #906

Merged
jiashuoz merged 1 commit into
tokencanopy:mainfrom
AmirF194:fix/826-dkim-keygen-skip-on-reclaim
Aug 17, 2026
Merged

fix(identity): skip DKIM keygen on a domain reclaim#906
jiashuoz merged 1 commit into
tokencanopy:mainfrom
AmirF194:fix/826-dkim-keygen-skip-on-reclaim

Conversation

@AmirF194

Copy link
Copy Markdown
Contributor

Summary

claimOrCreateDomain generated a fresh RSA-2048 DKIM keypair (plus the AEAD
seal over its private key, #144) before checking whether the domain row
already existed. On a reclaim, the common idempotent path the function's own
docstring describes ("the DKIM keypair are minted on first INSERT and remain
stable across re-claims"), the SELECT branch returns the existing row and the
freshly generated keypair is discarded, unused. Every re-register of an
already-owned domain paid for a full RSA-2048 keygen for nothing.

The fix moves the keygen/seal block into the pgx.ErrNoRows branch, right
before the INSERT that is its only consumer, so it runs once per domain
instead of once per call. Stored data is byte-for-byte identical either way;
this only removes the wasted work.

Operational risk

None. No stored data changes, no API response changes. The keygen/seal now
runs while holding the per-domain advisory lock (previously it ran before the
transaction started), which only extends lock hold time on the once-per-domain
new-row path; the existing TestClaimOrCreateDomain_ConcurrentParentChildClaimsDoNotSplitOwnership
and TestClaimOrCreateDomain_HierarchicalClaimsAreExclusiveAcrossAccounts
tests still pass unmodified.

Test plan

  • New regression test TestClaimOrCreateDomain_ReclaimSkipsDKIMKeygen
    (internal/identity/store_test.go): swaps crypto/rand.Reader for a
    byte-counter around a reclaim call only. Fails on unmodified main
    (120977 bytes read, a real RSA-2048 keygen) and passes on the branch
    (well under the 256-byte threshold, which is comfortably above
    generateID's own unrelated 16-byte read on every call).
  • go vet ./internal/identity/... and gofmt -l on both changed files:
    clean.
  • Full go test ./internal/identity/... ./internal/dkim/...: all
    passing (505s/1s), including the pre-existing
    TestClaimOrCreateDomain_StableOnReclaim (DKIM key/verification token
    stability across reclaim, unaffected) and the two concurrency tests named
    above.
  • Docker, Go 1.26 (matching CI's go-version: "1.26") against a
    postgres:16-alpine service on :5433 with the CI's own credentials, the
    same shape go-coverage/go-e2e use.
  • Not run: make test-e2e (the separately tagged integration suite) and the
    web/TS/Python/CLI/MCP jobs, none of which touch this file.

Fixes #826

claimOrCreateDomain generated an RSA-2048 keypair and AEAD-sealed the
private key before checking whether the domain row already existed.
On a reclaim (the common idempotent path documented right above the
function: "the DKIM keypair are minted on first INSERT and remain
stable across re-claims") the SELECT branch returns the existing row
and the freshly generated keypair is discarded, unused, on every call.

Move the keygen/seal block into the pgx.ErrNoRows branch, right before
the INSERT that is its only consumer, so it runs once per domain
instead of once per claim call. The stored data is unchanged either
way; only the wasted work on a reclaim goes away.

Fixes tokencanopy#826

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
@AmirF194
AmirF194 requested a review from jiashuoz as a code owner August 17, 2026 16:30
@jiashuoz
jiashuoz merged commit 36409f0 into tokencanopy:main Aug 17, 2026
47 of 48 checks passed
@AmirF194

Copy link
Copy Markdown
Contributor Author

Thanks for merging!

@AmirF194
AmirF194 deleted the fix/826-dkim-keygen-skip-on-reclaim branch August 18, 2026 00:08
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.

identity: ClaimOrCreateDomain generates an RSA-2048 DKIM keypair before checking whether the row already exists

2 participants