Class: KmsRails::Aws::KMS::Client

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



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

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)


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

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



32
33
34
# File 'lib/kms_rails/kms_client_mock.rb', line 32

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