class Mongo::Crypt::DataKeyContext

A Context object initialized specifically for the purpose of creating a data key in the key management system.

@api private

Public Class Methods

new(mongocrypt, io, master_key_document, key_alt_names, key_material) click to toggle source

Create a new DataKeyContext object

@param [ Mongo::Crypt::Handle ] mongocrypt a Handle that

wraps a mongocrypt_t object used to create a new mongocrypt_ctx_t

@param [ Mongo::Crypt::EncryptionIO ] io An object that performs all

driver I/O on behalf of libmongocrypt

@param [ Mongo::Crypt::KMS::MasterKeyDocument ] master_key_document The master

key document that contains master encryption key parameters.

@param [ Array<String> | nil ] key_alt_names An optional array of strings specifying

alternate names for the new data key.

@param [ String | nil ] :key_material Optional

96 bytes to use as custom key material for the data key being created.
If :key_material option is given, the custom key material is used
for encrypting and decrypting data.
Calls superclass method Mongo::Crypt::Context::new
# File lib/mongo/crypt/data_key_context.rb, line 41
def initialize(mongocrypt, io, master_key_document, key_alt_names, key_material)
  super(mongocrypt, io)
  Binding.ctx_setopt_key_encryption_key(self, master_key_document.to_document)
  set_key_alt_names(key_alt_names) if key_alt_names
  Binding.ctx_setopt_key_material(self, BSON::Binary.new(key_material)) if key_material
  initialize_ctx
end

Private Instance Methods

initialize_ctx() click to toggle source

Initializes the underlying mongocrypt_ctx_t object

# File lib/mongo/crypt/data_key_context.rb, line 68
def initialize_ctx
  Binding.ctx_datakey_init(self)
end
set_key_alt_names(key_alt_names) click to toggle source

Set the alt names option on the context

# File lib/mongo/crypt/data_key_context.rb, line 52
def set_key_alt_names(key_alt_names)
  unless key_alt_names.is_a?(Array)
    raise ArgumentError.new, 'The :key_alt_names option must be an Array'
  end

  unless key_alt_names.all? { |key_alt_name| key_alt_name.is_a?(String) }
    raise ArgumentError.new(
      "#{key_alt_names} contains an invalid alternate key name. All " +
      "values of the :key_alt_names option Array must be Strings"
    )
  end

  Binding.ctx_setopt_key_alt_names(self, key_alt_names)
end