Sha256: a38df8b264b618f8a06d054ddebed9686cb2d58cae3a661545292732583db648

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module KmsEncrypted
  module Clients
    class Google < Base
      attr_reader :last_key_version

      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

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kms_encrypted-1.2.4 lib/kms_encrypted/clients/google.rb