Class: Karafka::Pro::Encryption::Messages::Parser

Inherits:
Messages::Parser show all
Defined in:
lib/karafka/pro/encryption/messages/parser.rb

Overview

Note:

There may be a case where someone decides not to encrypt data and we start getting unencrypted payloads. That is why we always rely on message headers for encryption indication.

Pro parser that takes into consideration encryption usage

Instance Method Summary collapse

Instance Method Details

#call(message) ⇒ Object

Returns deserialized payload.

Parameters:

Returns:

  • (Object)

    deserialized payload

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/karafka/pro/encryption/messages/parser.rb', line 31

def call(message)
  headers = message.headers
  encryption = headers['encryption']
  fingerprint = headers['encryption_fingerprint']

  return super(message) unless active && encryption

  # Decrypt raw payload so it can be handled by the default parser logic
  decrypted_payload = cipher.decrypt(
    encryption,
    message.raw_payload
  )

  message.raw_payload = decrypted_payload

  return super(message) unless fingerprint && fingerprinter

  message_fingerprint = fingerprinter.hexdigest(decrypted_payload)

  return super(message) if message_fingerprint == fingerprint

  raise(Errors::FingerprintVerificationError, message.to_s)
end