Cratis Chronicle event subjects for PII encryption
An event’s subject in Cratis Chronicle identifies who the event is about and selects the key that protects its personally identifiable information (PII). Set it explicitly when the EventSourceId identifies something else, such as an order. The subject is not the actor who caused the event; that actor is recorded by identity.
For a ShippingAddressChanged event, the EventSourceId can be the order identifier while the subject is the customer whose address changed. Using the customer as the subject groups their protected data under that person’s key, even when events belong to different orders. Keys are scoped by event store, namespace and subject.
Default — the EventSourceId
Section titled “Default — the EventSourceId”When you provide neither an explicit subject nor an event-property subject, Chronicle uses the EventSourceId. Keep this default when the event source already identifies the person the event is about:
using Cratis.Chronicle;using Cratis.Chronicle.Events;
[EventType]public record SubjectAuthorRegistered(string Name);
public class SubjectAuthorService(IEventStore eventStore){ public Task Register(EventSourceId authorId, string name) => // Subject defaults to authorId; encryption keys for any PII on SubjectAuthorRegistered // are keyed by authorId. eventStore.EventLog.Append(authorId, new SubjectAuthorRegistered(name));}import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.events.EventType
@EventTypedata class SubjectAuthorRegistered(val name: String)
class SubjectAuthorService(private val eventStore: IEventStore) { suspend fun register(authorId: String, name: String) { // Subject defaults to authorId, so encryption keys for any PII on // SubjectAuthorRegistered are keyed by authorId. eventStore.eventLog.append(authorId, SubjectAuthorRegistered(name)) }}import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.java.EventLogJavaBridge;
@EventTyperecord SubjectAuthorRegistered(String name) {}
class SubjectAuthorService { private final IEventStore eventStore;
SubjectAuthorService(IEventStore eventStore) { this.eventStore = eventStore; }
void register(String authorId, String name) { // Subject defaults to authorId, so encryption keys for any PII on // SubjectAuthorRegistered are keyed by authorId. EventLogJavaBridge.append( eventStore.getEventLog(), authorId, new SubjectAuthorRegistered(name), null); }}defmodule MyApp.Events.SubjectAuthorRegistered do use Cratis.Chronicle.Events.EventType, id: "subject-author-registered"
defstruct [:name]end
defmodule MyApp.SubjectAuthorService do alias MyApp.Events.SubjectAuthorRegistered
def register(author_id, name) do # Subject defaults to author_id; encryption keys for any PII on SubjectAuthorRegistered # are keyed by author_id. Cratis.Chronicle.append(author_id, %SubjectAuthorRegistered{name: name}) endendimport { eventType, IEventStore } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class SubjectAuthorRegistered { @field(String) name: string;
constructor(name: string) { this.name = name; }}
class SubjectAuthorService { constructor(private readonly store: IEventStore) {}
register(authorId: string, name: string) { // Subject defaults to authorId; encryption keys for any PII on SubjectAuthorRegistered // are keyed by authorId. return this.store.eventLog.append(authorId, new SubjectAuthorRegistered(name)); }}Specifying an explicit subject
Section titled “Specifying an explicit subject”Pass a subject explicitly when the event source identifies something else, such as an order. In .NET, Subject converts implicitly from EventSourceId, so you can supply an existing identifier without constructing a wrapper:
using Cratis.Chronicle;using Cratis.Chronicle.Events;
[EventType]public record SubjectShippingAddressChanged(string Street);
public class SubjectShippingService(IEventStore eventStore){ public Task ChangeAddress(EventSourceId orderId, Subject customerId, string street) => eventStore.EventLog.Append( eventSourceId: orderId, @event: new SubjectShippingAddressChanged(street), subject: customerId);}import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.eventSequences.AppendOptionsimport io.cratis.chronicle.events.EventType
@EventTypedata class SubjectShippingAddressChanged(val street: String)
class SubjectShippingService(private val eventStore: IEventStore) { suspend fun changeAddress(orderId: String, customerId: String, street: String) { // The event happens to the order, but the address is the customer's data - so the // subject is the customer, not the event source. eventStore.eventLog.append( orderId, SubjectShippingAddressChanged(street), AppendOptions(subject = customerId) ) }}import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.java.AppendOptionsBuilder;import io.cratis.chronicle.java.EventLogJavaBridge;
@EventTyperecord SubjectShippingAddressChanged(String street) {}
class SubjectShippingService { private final IEventStore eventStore;
SubjectShippingService(IEventStore eventStore) { this.eventStore = eventStore; }
void changeAddress(String orderId, String customerId, String street) { // The event happens to the order, but the address is the customer's data - so the // subject is the customer, not the event source. EventLogJavaBridge.append( eventStore.getEventLog(), orderId, new SubjectShippingAddressChanged(street), new AppendOptionsBuilder().subject(customerId).build()); }}defmodule MyApp.Events.SubjectShippingAddressChanged do use Cratis.Chronicle.Events.EventType, id: "subject-shipping-address-changed"
defstruct [:street]end
defmodule MyApp.SubjectShippingService do alias MyApp.Events.SubjectShippingAddressChanged
def change_address(order_id, customer_id, street) do Cratis.Chronicle.append(order_id, %SubjectShippingAddressChanged{street: street}, subject: customer_id ) endendimport { eventType, IEventStore } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class SubjectShippingAddressChanged { @field(String) street: string;
constructor(street: string) { this.street = street; }}
class SubjectShippingService { constructor(private readonly store: IEventStore) {}
changeAddress(orderId: string, customerId: string, street: string) { return this.store.eventLog.append( orderId, new SubjectShippingAddressChanged(street), { subject: customerId } ); }}The .NET Subject type also converts implicitly from string and Guid. Kotlin, Java and TypeScript accept a subject through AppendOptions; TypeScript also accepts a subject on each entry passed to appendMany.
Implicit subject with [Subject]
Section titled “Implicit subject with [Subject]”In .NET, mark the event property that identifies the person with [Subject]. The client reads its value when no explicit subject is provided. This keeps the choice with the event type instead of repeating it at every append:
using Cratis.Chronicle;using Cratis.Chronicle.Compliance.GDPR;using Cratis.Chronicle.Events;
public record SubjectOrderId(Guid Value);public record SubjectCustomerId(Guid Value);
[EventType]public record SubjectShippingAddressChangedWithImplicitSubject( SubjectOrderId Order, [Subject] SubjectCustomerId Customer, [PII] string Street, [PII] string City);
public class SubjectImplicitSubjectService(IEventStore eventStore){ // Subject is derived from the Customer property — no explicit subject needed. public Task ChangeAddress(SubjectOrderId orderId, SubjectCustomerId customerId, string street, string city) => eventStore.EventLog.Append( orderId.Value, new SubjectShippingAddressChangedWithImplicitSubject(orderId, customerId, street, city));}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.SubjectShippingAddressChangedWithImplicitSubject do # `subject:` names the field that holds the data subject, so the subject is # derived from `customer_id` and no explicit subject is needed on append. use Cratis.Chronicle.Events.EventType, id: "subject-shipping-address-changed-with-implicit-subject", subject: :customer_id
defstruct [:order_id, :customer_id, :street, :city]
pii(:street) pii(:city)end
defmodule MyApp.SubjectImplicitSubjectService do alias MyApp.Events.SubjectShippingAddressChangedWithImplicitSubject
def change_address(order_id, customer_id, street, city) do Cratis.Chronicle.append( order_id, %SubjectShippingAddressChangedWithImplicitSubject{ order_id: order_id, customer_id: customer_id, street: street, city: city } ) endendimport { eventType, IEventStore, subject } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class SubjectBillingAddressChanged { @field(String) @subject() customerId: string; @field(String) street: string;
constructor(customerId: string, street: string) { this.customerId = customerId; this.street = street; }}
class SubjectBillingService { constructor(private readonly store: IEventStore) {}
changeAddress(orderId: string, customerId: string, street: string) { // No subject option: the subject is the event's @subject() property, customerId. // An explicit { subject } option would still take precedence. return this.store.eventLog.append(orderId, new SubjectBillingAddressChanged(customerId, street)); }}An explicit subject argument takes precedence over a [Subject] property. Elixir can derive an append subject with subject: :field on the event type, and TypeScript can derive it from an event property marked @subject(). Kotlin and Java pass the append subject explicitly.
Read-model subject markers select the key used to decrypt protected values when a read model is returned. These are @Subject in Kotlin and Java, @subject() in TypeScript, and subject :field in Elixir. A Kotlin or Java read-model marker does not supply an event’s append subject.
Reading the subject back
Section titled “Reading the subject back”The subject resolved at append time is returned as EventContext.Subject when you read the event sequence or receive the event in an observer, such as a reactor or reducer. If neither an explicit value nor an event property supplied a subject, the returned value is the EventSourceId.
When an observer forwards an event, pass context.Subject to preserve who the event is about. Otherwise, the destination can fall back to its own event source identifier and select a different compliance subject.
Compliance
Section titled “Compliance”Properties marked as PII, for example with .NET’s [PII], are encrypted at append time under the subject’s key. Chronicle provisions the key on first use and stores it in the encryption key store. Adding a PII marker later does not encrypt values already stored as plaintext.
Erasure deletes the subject’s key revisions across event stores within the addressed namespace. Repeat the operation for each namespace holding that person’s data. Events remain in the log, but their protected values cannot be decrypted without the deleted keys. This does not erase plaintext, exported copies or keys retained in backups.
Follow Erasing a subject for the erasure procedure, including stopping in-flight PII writes and handling subsequent appends. PII compliance explains how to mark the data that needs this protection.