Class: KmsRails::KmsClientMock

Inherits:
Object
  • Object
show all
Defined in:
lib/kms_rails/kms_client_mock.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(ciphertext_blob:, encryption_context: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kms_rails/kms_client_mock.rb', line 18

def decrypt(ciphertext_blob:, encryption_context: nil)
  key_id, decoded_context, plaintext = MessagePack.unpack(ciphertext_blob.reverse)
  raise ::Aws::KMS::Errors::InvalidCiphertextException.new(nil, nil) unless decoded_context == encryption_context

  ::Aws::KMS::Types::DecryptResponse.new(
    key_id: key_id,
    plaintext: plaintext,
  )
rescue MessagePack::MalformedFormatError
  raise ::Aws::KMS::Errors::InvalidCiphertextException.new(nil, nil)
end

#generate_data_key(key_id:, key_spec:, encryption_context: nil) ⇒ Object

Raises:

  • (RuntimeError)


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/kms_rails/kms_client_mock.rb', line 6

def generate_data_key(key_id:, key_spec:, encryption_context: nil)
  raise RuntimeError, 'Unsupported key_spec in test mode' unless key_spec == 'AES_256'

  plaintext = SecureRandom.random_bytes(256/8)

  ::Aws::KMS::Types::GenerateDataKeyResponse.new(
    key_id: key_id,
    plaintext: plaintext,
    ciphertext_blob: [key_id, encryption_context, plaintext].to_msgpack.reverse,
  )
end

#inspectObject



30
31
32
# File 'lib/kms_rails/kms_client_mock.rb', line 30

def inspect
  "#<Aws::KMS::Client (mocked)>"
end