Class: HatiConfig::Encryption::AwsKmsKeyProvider

Inherits:
KeyProvider
  • Object
show all
Defined in:
lib/hati_config/encryption.rb

Overview

AwsKmsKeyProvider gets the encryption key from AWS KMS.

Instance Method Summary collapse

Methods inherited from KeyProvider

create

Constructor Details

#initialize(options = {}) ⇒ AwsKmsKeyProvider

Returns a new instance of AwsKmsKeyProvider.

Raises:



221
222
223
224
225
226
227
228
# File 'lib/hati_config/encryption.rb', line 221

def initialize(options = {})
  super()
  require 'aws-sdk-kms'
  @key_id = options[:key_id]
  @region = options[:region]
  @client = nil
  raise EncryptionError, 'KMS key ID not provided' unless @key_id
end

Instance Method Details

#key ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/hati_config/encryption.rb', line 230

def key
  @key ||= begin
    client = Aws::KMS::Client.new(region: @region)
    response = client.generate_data_key(
      key_id: @key_id,
      key_spec: 'AES_256'
    )
    response.plaintext
  rescue Aws::KMS::Errors::ServiceError => e
    raise EncryptionError, "Failed to get key from KMS: #{e.message}"
  end
end