Class: KmsEncrypted::Clients::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/kms_encrypted/clients/google.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#key_id

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from KmsEncrypted::Clients::Base

Instance Attribute Details

#last_key_versionObject (readonly)

Returns the value of attribute last_key_version.



4
5
6
# File 'lib/kms_encrypted/clients/google.rb', line 4

def last_key_version
  @last_key_version
end

Instance Method Details

#decrypt(ciphertext, context: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kms_encrypted/clients/google.rb', line 22

def decrypt(ciphertext, context: nil)
  options = {
    ciphertext: ciphertext
  }
  options[:additional_authenticated_data] = generate_context(context) if context

  # ensure namespace gets loaded
  client = KmsEncrypted.google_client
  request = ::Google::Apis::CloudkmsV1::DecryptRequest.new(**options)
  begin
    client.decrypt_crypto_key(key_id, request).plaintext
  rescue ::Google::Apis::ClientError => e
    decryption_failed! if e.message.include?("Decryption failed")
    raise e
  end
end

#encrypt(plaintext, context: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kms_encrypted/clients/google.rb', line 6

def encrypt(plaintext, context: nil)
  options = {
    plaintext: plaintext
  }
  options[:additional_authenticated_data] = generate_context(context) if context

  # ensure namespace gets loaded
  client = KmsEncrypted.google_client
  request = ::Google::Apis::CloudkmsV1::EncryptRequest.new(**options)
  response = client.encrypt_crypto_key(key_id, request)

  @last_key_version = response.name

  response.ciphertext
end