Repository navigation
[M2.1b] Envelope encryption, domain events and audit log - #28
Merged
Merged
Conversation
…L-applied migrations
…K script, tick M2.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task
[M2.1b] Shared foundations of the control plane (second half of M2.1). Closes #20
What changed
Domain (
packages/domain)DomainEventwithtype(<aggregate>.<verb>),occurredAt, optionalorganizationId,actor(user, api_token, system, node with id and ip) andtarget;createEvent()validates the type name and target.Envelope encryption (
apps/api/src/platform/crypto, SPEC section 9)0x01 | nonce | tag | ciphertext, associated data on every message.wrapDataKey/unwrapDataKeybind a DEK to its scope and KEK version.loadKek(path): 32 raw bytes or base64, refuses files readable by group or others, missing files and non-files. The key is aSecret.EnvelopeService.encryptFor(scope, purpose, bytes)/decryptFor: one DEK per scope, generated on first use, wrapped by the KEK and stored indata_encryption_keys(unique scope; a concurrent insert is read back instead of failing). Unwrapped DEKs are cached in memory asSecrets. A stored key with another KEK version is refused with a message pointing to rotation.Domain events and audit (
platform/events,modules/audit)DomainEventBus: in-process, ordered delivery, subscriber errors propagate to the publisher so a failed audit write fails the command (CLAUDE.md rule 6).auditmodule laid out per CLAUDE.md:domain/audit-record.ts(pure mapping event to record),application/(AuditEventStoreport withappendonly,AuditRecordersubscribed for the app lifetime, UUIDv7 ids),infrastructure/PrismaAuditEventStore. No update or delete path exists.Database
AuditEvent(audit_events, indexes on organization + time and on target) andDataEncryptionKey(data_encryption_keys), initial migration20261103123622_init.pnpm devnow runsprisma migrate deployand creates the development KEK (tools/dev/ensure-kek.mjs, gitignored path, mode 0400).Tests
EnvelopeService(one DEK per scope, scope and purpose binding, foreign KEK, concurrent insert), bus (ordering, propagation, unsubscribe), audit mapping and recorder (UUIDv7, ordering, store failure).pg(no process spawning), readiness probe, two published events become two audit rows with the expected tenant, actor and metadata, a value encrypted through the service round-trips and the stored key row contains neither the plaintext nor the unwrapped key.DATABASE_URLand the KEK path (Nx loads the developer.envintoprocess.env), so they never touch the development database or key.Why
Every later module needs secrets at rest (M3.7, identity secrets in M2.2) and emits audit events from its first command. Putting both in place now means no module ships without them.
How it was tested
pnpm nx run-many -t lint typecheck test build: all 7 projects green (api: 9 test files).pnpm test:integrationwith a clean environment: 3 tests against PostgreSQL 17.pnpm gentwice: no drift. Semgrep custom rules: no findings.prettier --check: clean.Security considerations
Secret). The development KEK lives under the gitignoreddeploy/dev/secrets/.shipkeep:data:<scope>:<purpose>, so a blob copied to another environment or column fails to decrypt. GCM failures are reported uniformly to avoid oracle behaviour.EnvelopeService.REVOKE UPDATE, DELETEis task M2.4 as planned.DataEncryptionKeytable added to SPEC section 4 as the persistence of the DEKs the spec already required; no change to the agent action list or to invariants of existing entities.Checklist
docs/PLAN.mdare met and M2.1b (and M2.1) are tickedbuf lintpass