Class: Karafka::Pro::Encryption::Messages::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/pro/encryption/messages/middleware.rb

Overview

Middleware for WaterDrop. It automatically encrypts messages payload. It is injected only if encryption is enabled. It also fingerprints the payload for verification if fingerprinting was enabled

Instance Method Summary collapse

Instance Method Details

#call(message) ⇒ Hash

Returns hash with encrypted payload and encryption version indicator.

Parameters:

  • message (Hash)

    WaterDrop message hash

Returns:

  • (Hash)

    hash with encrypted payload and encryption version indicator



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/karafka/pro/encryption/messages/middleware.rb', line 48

def call(message)
  payload = message[:payload]

  # Tombstones (records with a nil payload) carry no data to encrypt. We pass them
  # through untouched so they remain valid tombstones (for example for log compaction)
  # instead of crashing on `cipher.encrypt(nil)`.
  return message if payload.nil?

  message[:headers] ||= {}
  message[:headers]["encryption"] = version
  message[:payload] = cipher.encrypt(payload)

  return message unless fingerprinter

  message[:headers]["encryption_fingerprint"] = fingerprinter.hexdigest(payload)

  message
end