Skip to content

refactor(keyring): fold master-key/KMS lifecycle into the deep module#144

Merged
allisson merged 1 commit into
mainfrom
refactor/deepen-keyring-master-key-lifecycle
Jul 17, 2026
Merged

refactor(keyring): fold master-key/KMS lifecycle into the deep module#144
allisson merged 1 commit into
mainfrom
refactor/deepen-keyring-master-key-lifecycle

Conversation

@allisson

Copy link
Copy Markdown
Owner

Summary

Extends ADR-0013. The envelope-encryption data plane was already a deep module, but the master-key/KMS lifecycle around it stayed a shallow, exported control surface — it leaked raw key material and had no test seam. This folds that lifecycle into keyring.

Hide root-key material

  • Unexport the master-key type and its key bytes; only the opaque MasterKeyChain handle crosses the package boundary.
  • Add MasterKeyChain.Has(id) bool — presence check with no material leak.
  • Delete the unused internal/tokenization/testing helper.

Make bootstrap testable (Option A — narrow public interface)

  • Extract bootstrapWith behind the kekStore/dekStore seams so KEK loading is unit-tested without a database.
  • Public Bootstrap / NewKekUseCase signatures and the DI wiring are unchanged.

Honest KMS seam

  • Add Encrypt to KMSKeeper — the create/rotate-master-key CLI commands (and the integration helpers) drop their runtime *secrets.Keeper type assertions.
  • Add an exported keyring.FakeKMSService as a real second adapter; cover loadMasterKeyChainFromKMS in-package (env / base64 / key-size / decrypt-fail / active-not-found / happy multi-key).

Mockery reconciliation

.mockery.yaml still referenced deleted crypto/auth-service packages. Repointed every interface at its real home: repository mocks now generate beside their domain interfaces, KekUseCase under keyring. The 6 usecase tests import paths updated accordingly.

Testing

  • make lint — 0 issues, govulncheck clean
  • make test — pass, keyring unit coverage added for previously-untested paths
  • make test-all — pass, including the DB-backed integration suite against real Postgres

Review

Ran the two-axis code review (Standards + Spec) — Spec clean; the one hard Standards finding (table-driven tests) fixed.

🤖 Generated with Claude Code

Extends ADR-0013: the envelope-encryption data plane was already a deep
module, but the master-key/KMS lifecycle around it was a shallow,
exported control surface that leaked and could not be tested.

- Hide root-key material: unexport the master-key type and its bytes;
  only the opaque MasterKeyChain handle crosses the package boundary.
  Add MasterKeyChain.Has for presence checks without leaking material.
- Make bootstrap testable: extract bootstrapWith behind the kek/dek
  store seams so KEK loading is unit-tested without a database. Public
  Bootstrap / NewKekUseCase signatures and di wiring are unchanged.
- Honest KMS seam: add Encrypt to KMSKeeper so the create/rotate
  master-key CLI commands drop their runtime type assertions; add an
  exported keyring.FakeKMSService as a real second adapter and cover
  loadMasterKeyChainFromKMS in-package.
- Delete the unused internal/tokenization/testing helper.

Also reconcile .mockery.yaml with the current package layout (it still
referenced the deleted crypto/auth-service packages): repository mocks
now generate beside their domain interfaces, KekUseCase under keyring.

Verified: make lint, make test, and make test-all (incl. the DB-backed
integration suite) all pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@allisson
allisson merged commit b1515e9 into main Jul 17, 2026
3 checks passed
@allisson
allisson deleted the refactor/deepen-keyring-master-key-lifecycle branch July 17, 2026 20:39
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