ADR-00020 — Shared Core-Lib Encryption/Audit/CRUD Pattern vs. In-Service Implementation

By Engineering Team•October 1, 2026•5 min read
  • Status: Approved / Implemented
  • Date: 2026-10-01
  • Authors: Engineering Team

Context

pii-service and pci-service both need the same thing at their core: a gRPC-only, encryption-at-rest attribute store keyed by (tenant_id, entity_type, entity_id, attribute_key), built on ADR-00018’s row-based model, with an append-only audit log of every read/write/delete. Building and maintaining that twice — once per service, in the same language — would mean every future bug fix, audit-log field addition, or cipher change has to land in two places and stay in sync by discipline alone.


Decision Drivers (FRs & NFRs)

See SR-010 for the full requirement list.

  • NFR-02 (Shared Implementation, Isolated Schema): pii-service and pci-service must share identical business logic while keeping their data fully isolated.
  • FR-12 (Per-Call Audit Logging): the audit-log write behavior must be identical and co-evolve across both services.
  • FR-02 / FR-03 (EAV Keying, Encryption At Rest): both services persist the same shape of row under ADR-00018’s model — this ADR is about where the code implementing that model lives, not the model itself.

Options Evaluated

Option 1 — Shared domain::encrypt/service::encrypt/repository::encrypt modules in rust-lang-core-lib, consumed by both services (CHOSEN)

rust-lang-core-lib owns RowAttributeService/RowAttributeServiceTrait (validate → encrypt/decrypt via AesGcmCipher → call the repo → write one AuditLogEntry), RowAttributeRepositoryTrait/RowAttributeAuditRepositoryTrait, and the RowAttribute* DTOs — all named generically (RowAttribute, never Pii/Pci). Each service owns only its concrete SeaORM entity (table/schema name are compile-time macro attributes, so they can’t be parameterized at runtime), its own composition root, and its own gRPC handler.

  • Pros:
    • One place to fix a bug or extend the audit log for both services at once (NFR-02).
    • The compile-time/runtime boundary is clean: anything that must be a compile-time constant for one service (table/schema name) stays local; everything expressible as generic input/output types moves to the core lib.
    • Each service still keeps full data isolation — pci-service never shares a schema, a table, or a database connection with pii-service, only source code.
    • A future third encrypted-attribute consumer (if one ever needs the same shape) inherits the identical, already-audited implementation for free, rather than needing its own ADR-00019-style build-vs-duplicate decision repeated from scratch.
  • Cons & Accepted Trade-offs:
    • A bug in the shared RowAttributeService logic affects both services simultaneously — there is no independent blast-radius containment between pii-service and pci-service for a defect in the shared path.
    • Any new per-service behavior that doesn’t fit the generic RowAttribute* shape has to either be added as a new generic capability (raising the core lib’s surface area for both consumers) or built entirely locally, with no middle ground.

Option 2 — Independent implementation per service, no shared encryption/audit code

pii-service and pci-service each hand-write their own RowAttributeService-equivalent, repository traits, and audit-log writer.

  • Pros:
    • Fully independent blast radius — a defect in one service’s encryption/audit logic cannot affect the other.
    • Each service’s implementation could diverge freely if one’s requirements ever needed to (e.g. a different audit granularity).
  • Cons (Why Rejected):
    • Duplicates the exact same AES-256-GCM encrypt/decrypt, validation, and audit-log-write logic in two codebases that have no actual reason to diverge today — any future fix (e.g. a cipher migration) has to be applied, tested, and reviewed twice.
    • No current requirement calls for divergence between the two services’ core attribute-storage behavior — this is speculative flexibility, not a need either service has stated today.
    • Reintroduces per-service encryption-practice drift as a real risk — the same concern ADR-00019 raises against duplicating PII handling across consumers, here one level down, between the two data-protection services themselves.

Final Decision

Option 1 is selected. The shared core-lib implementation keeps pii-service and pci-service’s identical encryption/validation/audit logic in one place while preserving full data isolation — the compile-time/runtime split (table/schema names stay local; everything else is generic) is the same test applied wherever this monorepo decides what belongs in a shared library versus a single service, see the platform’s layered architecture standard.