Module: ActiveJob::Temporal::PayloadEncryption

Extended by:
PayloadEncryption
Included in:
PayloadEncryption
Defined in:
lib/activejob/temporal/payload_encryption.rb

Defined Under Namespace

Classes: KeyEntry

Constant Summary collapse

CIPHER =
"aes-256-gcm"
LEGACY_VERSION =
1
VERSION =
2
DEFAULT_KEY_ID =
"primary"
KEY_ID_PATTERN =
/\A[A-Za-z0-9_.:-]{1,128}\z/
V2_IV_BYTES =
12

Instance Method Summary collapse

Instance Method Details

#decode_key(value) ⇒ Object



53
54
55
56
57
# File 'lib/activejob/temporal/payload_encryption.rb', line 53

def decode_key(value)
  Base64.strict_decode64(value.to_s)
rescue ArgumentError
  nil
end

#decrypt(payload, config, context: nil) ⇒ Object

Raises:

  • (ActiveJob::SerializationError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/activejob/temporal/payload_encryption.rb', line 33

def decrypt(payload, config, context: nil)
  version = payload[:encrypted_payload_version] || payload["encrypted_payload_version"]
  if version == LEGACY_VERSION
    unless config.allow_legacy_encrypted_payloads
      raise ActiveJob::SerializationError,
            "Version 1 encrypted payloads are rejected; set allow_legacy_encrypted_payloads " \
            "to accept payloads enqueued before context-bound encryption"
    end

    return decrypt_legacy(payload, config)
  end
  return decrypt_v2(payload, config, context) if version == VERSION

  raise ActiveJob::SerializationError, "Unsupported encrypted payload version: #{version.inspect}"
end

#encrypt(payload, config, context: nil) ⇒ Object



27
28
29
30
31
# File 'lib/activejob/temporal/payload_encryption.rb', line 27

def encrypt(payload, config, context: nil)
  return encrypt_legacy(payload, config) unless context

  encrypt_v2(payload, config, context)
end

#encrypted?(payload) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/activejob/temporal/payload_encryption.rb', line 23

def encrypted?(payload)
  payload[:encrypted_payload] == true || payload["encrypted_payload"] == true
end

#key_lengthObject



49
50
51
# File 'lib/activejob/temporal/payload_encryption.rb', line 49

def key_length
  ActiveSupport::MessageEncryptor.key_len(CIPHER)
end

#valid_key?(value) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/activejob/temporal/payload_encryption.rb', line 59

def valid_key?(value)
  !key_entry(value, fallback_id: DEFAULT_KEY_ID).nil?
end