[Feature] A PII-protecting message transform, backed by a pluggable key store
Summary
Brighter needs a message transform that protects personally identifiable information (PII) in a message. The send side encrypts the sensitive fields, or the whole body, with a key from a key store and writes in a header which key it used. The receive side reads that header, fetches the same key from the store, and decrypts. The key store is an abstraction, so that Brighter can provide several implementations.
Why
The documentation's Message Transforms page showed a [RemovePII] / [RestorePII] pair as though Brighter shipped it. Brighter doesn't: 10.8.1 ships ClaimCheck/RetrieveClaim, Compress/Decompress, CloudEvents and the JustSaying and MassTransit pairs. The docs have been corrected (BrighterCommand/Docs, spec 018 task 6.3, ledger 72). Asking for the pair is a reasonable thing for a reader to expect, though:
- Messages leave the process boundary. They sit in an Outbox table, on a broker, in a dead-letter queue, in a claim-check store, and in logs and traces. Each of these has its own retention and its own access rules. Unless it is protected, PII is copied into all of them.
- Regulation (GDPR, HIPAA, PCI-DSS) requires data minimisation, and the right to erasure. Erasing a record from a stream of immutable messages is hard. Destroying a key is easy (crypto-shredding: see below).
- Teams solve this today inside each mapper, by hand and inconsistently. It is a cross-cutting concern, which is exactly what the transform pipeline is for.
Proposed shape
It follows the existing claim-check design, which already separates a transform from a pluggable store (IAmAStorageProvider / IAmAStorageProviderAsync).
Transform pair
[Encrypt(step: 0, keyId: "orders")] // wrap: encrypt, stamp the key reference in a header
public Message MapToMessage(SensitiveOrder request, Publication publication) { ... }
[Decrypt(step: 0)] // unwrap: read the header, fetch the key, decrypt
public SensitiveOrder MapToRequest(Message message) { ... }
The names are open. ProtectPII / UnprotectPII would match the intent better than "remove/restore", because the data is still there, just unreadable without the key.
Key store abstraction
public interface IAmAKeyStore // + IAmAKeyStoreAsync
{
// The current key for a key id, used to encrypt; returns the key and its version
DataKey GetCurrentKey(string keyId);
// A specific version, used to decrypt whatever version the message was written with
DataKey GetKey(string keyId, string version);
}
Implementations, in their own packages, as the claim-check stores are: Azure Key Vault, AWS KMS, GCP KMS and HashiCorp Vault. An in-memory store would serve tests.
Headers
The headers must carry enough for the receive side to decrypt, and never the key itself:
- the key id and key version, so that rotation does not break messages already in flight or held in an Outbox
- the algorithm (for example
AES-256-GCM), so the cipher can change later - which fields were encrypted, if field-level encryption is used (see below)
The IV/nonce and the authentication tag travel with the ciphertext.
Design points to decide
- Envelope encryption. Calling KMS for every message is slow, rate-limited and expensive. The usual pattern is envelope encryption: generate a data encryption key (DEK) locally, encrypt the payload with it, encrypt the DEK with a key-encryption key held in KMS, and put the wrapped DEK in a header. The receiver asks KMS only to unwrap the DEK. A cached DEK can serve many messages. This probably means the store abstraction is "wrap / unwrap a DEK" rather than "give me a key".
- Whole body or selected fields. Encrypting the whole body is simple, but it hides routing-relevant or diagnostic fields. Field-level encryption keeps the message inspectable. Candidates for marking the fields are an attribute on the request property (
[PersonalData], or reuseSystem.ComponentModel.DataAnnotations), or a JSON path list on the transform. Field-level encryption has to operate on the serialised body, so it is tied to the body's format (JSON first). - Crypto-shredding as a feature. With a key per data subject (for example per customer), deleting that key makes every copy of their data unreadable, in every Outbox, DLQ and archive. That is a strong answer to the right to erasure. The key id could come from the request through a resolver (
Func<IRequest, string>), not only from a constant in an attribute. - Ordering with the other transforms. Encrypt before compressing and the compression gains nothing, because ciphertext does not compress. So the documented order should be compress, then encrypt, then claim check on wrap, and the reverse on unwrap. If a later step is in the wrong order, the transform might warn.
- Failure semantics. When the store is unreachable on send, the post should fail rather than send plaintext. On receive, a missing key or a key that has been destroyed (shredded) is a permanent failure. It should reject or dead-letter, not requeue forever. This should map onto the existing
DeferMessageAction/RejectMessageActionsemantics. - Leakage elsewhere. The span attributes and log scopes in observability must not record the plaintext of the protected fields. This overlaps with #299 (obfuscating logs of sensitive data).
- CloudEvents. Whether the key-reference headers map to CloudEvents extension attributes in binary mode, and how they travel in structured mode.
Out of scope for a first cut
- Tokenisation or format-preserving encryption
- Searching encrypted fields
Related
- #299, Obfuscate logs of sensitive data (closed)
- The claim-check transform and
IAmAStorageProvider, the model for a transform with a pluggable store
Raised from the Brighter documentation review (spec 018, task 6.3).
I'd like to work on this. I've opened a draft PR with an ADR for a first cut (whole-body envelope encryption, a wrap/unwrap key store contract, in-memory store): #4776. It leaves field-level encryption, per-subject keys and the cloud key stores to later ADRs, and lists the open questions I'd need your view on before writing code.