Class: EventStoreClient::Mapper::Encrypted

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store_client/mapper/encrypted.rb

Overview

Transforms given event’s data and encrypts/decrypts selected subset of data based on encryption schema stored in the event itself.

Constant Summary collapse

MissingEncryptionKey =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(key_repository, config:, serializer: Serializer::Json) ⇒ Encrypted

Returns a new instance of Encrypted.

Parameters:

  • key_repository (#find, #create, #encrypt, #decrypt)

    See spec/support/dummy_repository.rb for the example of simple in-memory implementation

  • config (EventStoreClient::Config)
  • serializer (#serialize, #deserialize) (defaults to: Serializer::Json)


23
24
25
26
27
# File 'lib/event_store_client/mapper/encrypted.rb', line 23

def initialize(key_repository, config:, serializer: Serializer::Json)
  @key_repository = key_repository
  @config = config
  @serializer = serializer
end

Instance Method Details

#deserialize(event_or_raw_event, skip_decryption: false) ⇒ Object

Decrypts the given event’s subset of data.

Parameters:

  • event_or_raw_event (EventStoreClient::DeserializedEvent, EventStore::Client::Streams::ReadResp::ReadEvent::RecordedEvent, EventStore::Client::PersistentSubscriptions::ReadResp::ReadEvent::RecordedEvent)
  • skip_decryption (Boolean) (defaults to: false)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/event_store_client/mapper/encrypted.rb', line 56

def deserialize(event_or_raw_event, skip_decryption: false)
  if skip_decryption
    return Default.new(serializer: serializer, config: config).deserialize(event_or_raw_event)
  end

  event =
    if event_or_raw_event.is_a?(EventStoreClient::DeserializedEvent)
      event_or_raw_event
    else
      Serializer::EventDeserializer.call(
        event_or_raw_event, config: config, serializer: serializer
      )
    end

  decrypted_data =
    EventStoreClient::DataDecryptor.new(
      data: event.data,
      schema: event.['encryption'],
      repository: key_repository
    ).call
  event.class.new(**event.to_h.merge(data: decrypted_data, skip_validation: true))
end

#serialize(event) ⇒ Hash

Parameters:

Returns:

  • (Hash)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/event_store_client/mapper/encrypted.rb', line 31

def serialize(event)
  # Links don't need to be encrypted
  return Default.new(serializer: serializer, config: config).serialize(event) if event.link?

  serialized = Serializer::EventSerializer.call(event, serializer: serializer, config: config)
  encryption_schema =
    if event.class.respond_to?(:encryption_schema)
      event.class.encryption_schema
    end

  encryptor = EventStoreClient::DataEncryptor.new(
    data: serialized.data,
    schema: encryption_schema,
    repository: key_repository
  )
  encryptor.call
  serialized.data = encryptor.encrypted_data
  serialized.['encryption'] = encryptor.
  serialized
end