Class: Mongo::ClientEncryption
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/mongo/client_encryption.rb |
Overview
ClientEncryption encapsulates explicit operations on a key vault
collection that cannot be done directly on a MongoClient. It
provides an API for explicitly encrypting and decrypting values,
and creating data keys.
Class Method Summary
-
.new(key_vault_client, options = {}) ⇒ ClientEncryption
constructor
Create a new
ClientEncryptionobject with the provided options.
Instance Method Summary
-
#add_key_alt_name(id, key_alt_name) ⇒ BSON::Document | nil
Adds a key_alt_name for the key in the key vault collection with the given id.
-
#create_data_key(kms_provider, options = {}) ⇒ BSON::Binary
Generates a data key used for encryption/decryption and stores that key in the KMS collection.
-
#create_encrypted_collection(database, coll_name, coll_opts, kms_provider, master_key) ⇒ Array<Operation::Result, Hash>
Create collection with encrypted fields.
-
#decrypt(value) ⇒ Object
Decrypts a value that has already been encrypted.
-
#delete_key(id) ⇒ Operation::Result
Removes the key with the given id from the key vault collection.
-
#encrypt(value, options = {}) ⇒ BSON::Binary
Encrypts a value using the specified encryption key and algorithm.
-
#encrypt_expression(expression, options = {}) ⇒ BSON::Binary
Encrypts a Match Expression or Aggregate Expression to query a range index.
-
#get_key(id) ⇒ BSON::Document | nil
Finds a single key with the given id.
-
#get_key_by_alt_name(key_alt_name) ⇒ BSON::Document | nil
Returns a key in the key vault collection with the given key_alt_name.
-
#get_keys ⇒ Collection::View
(also: #keys)
Returns all keys in the key vault collection.
-
#keys
Alias for #get_keys.
-
#remove_key_alt_name(id, key_alt_name) ⇒ BSON::Document | nil
Removes a key_alt_name from a key in the key vault collection with the given id.
-
#rewrap_many_data_key(filter, opts = {}) ⇒ Crypt::RewrapManyDataKeyResult
Decrypts multiple data keys and (re-)encrypts them with a new master_key,.
-
#create_data_keys(encrypted_fields, kms_provider, master_key) ⇒ Hash
private
Create data keys for fields in encrypted_fields that has
:keyIdkey, but the value is nil.
Constructor Details
.new(key_vault_client, options = {}) ⇒ ClientEncryption
Create a new ClientEncryption object with the provided options.
# File 'lib/mongo/client_encryption.rb', line 52
def initialize(key_vault_client, = {}) @encrypter = Crypt::ExplicitEncrypter.new( key_vault_client, [:key_vault_namespace], Crypt::KMS::Credentials.new([:kms_providers]), Crypt::KMS::Validations.([:]), [:timeout_ms], [:key_expiration_ms] ) end
Instance Method Details
#add_key_alt_name(id, key_alt_name) ⇒ BSON::Document | nil
Adds a key_alt_name for the key in the key vault collection with the given id.
# File 'lib/mongo/client_encryption.rb', line 195
def add_key_alt_name(id, key_alt_name) @encrypter.add_key_alt_name(id, key_alt_name) end
#create_data_key(kms_provider, options = {}) ⇒ BSON::Binary
Generates a data key used for encryption/decryption and stores that key in the KMS collection. The generated key is encrypted with the KMS master key.
# File 'lib/mongo/client_encryption.rb', line 89
def create_data_key(kms_provider, = {}) key_document = Crypt::KMS::MasterKeyDocument.new(kms_provider, ) key_alt_names = [:key_alt_names] key_material = [:key_material] @encrypter.create_and_insert_data_key(key_document, key_alt_names, key_material) end
#create_data_keys(encrypted_fields, kms_provider, master_key) ⇒ Hash (private)
Create data keys for fields in encrypted_fields that has :keyId key,
but the value is nil.
# File 'lib/mongo/client_encryption.rb', line 306
def create_data_keys(encrypted_fields, kms_provider, master_key) encrypted_fields = encrypted_fields.dup # We must return the partially formed encrypted_fields hash if an error # occurs - https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.md#create-encrypted-collection-helper # Thefore, we do this in a loop instead of using #map. encrypted_fields[:fields].size.times do |i| field = encrypted_fields[:fields][i] next unless field.is_a?(Hash) && field.fetch(:keyId, false).nil? begin encrypted_fields[:fields][i][:keyId] = create_data_key(kms_provider, master_key: master_key) rescue Error::CryptError => e raise Error::CryptError, "Error creating data key for field #{field[:path]} \ with encrypted fields #{encrypted_fields}: #{e.class}: #{e.}" end end encrypted_fields end
#create_encrypted_collection(database, coll_name, coll_opts, kms_provider, master_key) ⇒ Array<Operation::Result, Hash>
This method does not update the :encrypted_fields_map in the client's
:auto_encryption_options. Therefore, in order to use the collection
created by this method with automatic encryption, the user must create
a new client after calling this function with the :encrypted_fields returned.
Create collection with encrypted fields.
If :encryption_fields contains a keyId with a null value, a data key
will be automatically generated and assigned to keyId value.
# File 'lib/mongo/client_encryption.rb', line 282
def create_encrypted_collection(database, coll_name, coll_opts, kms_provider, master_key) raise ArgumentError, 'coll_opts must contain :encrypted_fields' unless coll_opts[:encrypted_fields] encrypted_fields = create_data_keys(coll_opts[:encrypted_fields], kms_provider, master_key) begin new_coll_opts = coll_opts.dup.merge(encrypted_fields: encrypted_fields) [ database[coll_name].create(new_coll_opts), encrypted_fields ] rescue Mongo::Error => e raise Error::CryptError, "Error creating collection with encrypted fields \ #{encrypted_fields}: #{e.class}: #{e.}" end end
#decrypt(value) ⇒ Object
Decrypts a value that has already been encrypted.
# File 'lib/mongo/client_encryption.rb', line 184
def decrypt(value) @encrypter.decrypt(value) end
#delete_key(id) ⇒ Operation::Result
Removes the key with the given id from the key vault collection.
# File 'lib/mongo/client_encryption.rb', line 205
def delete_key(id) @encrypter.delete_key(id) end
#encrypt(value, options = {}) ⇒ BSON::Binary
The "substring" query type is unstable and subject to backwards breaking changes.
The :key_id and :key_alt_name options are mutually exclusive. Only
one is required to perform explicit encryption.
Encrypts a value using the specified encryption key and algorithm.
# File 'lib/mongo/client_encryption.rb', line 134
def encrypt(value, = {}) @encrypter.encrypt(value, ) end
#encrypt_expression(expression, options = {}) ⇒ BSON::Binary
The :key_id and :key_alt_name options are mutually exclusive. Only
one is required to perform explicit encryption.
Encrypts a Match Expression or Aggregate Expression to query a range index.
Only supported when queryType is "range" and algorithm is "Range". @note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.
@param [ Hash ] options
# File 'lib/mongo/client_encryption.rb', line 174
def encrypt_expression(expression, = {}) @encrypter.encrypt_expression(expression, ) end
#get_key(id) ⇒ BSON::Document | nil
Finds a single key with the given id.
# File 'lib/mongo/client_encryption.rb', line 215
def get_key(id) @encrypter.get_key(id) end
#get_key_by_alt_name(key_alt_name) ⇒ BSON::Document | nil
Returns a key in the key vault collection with the given key_alt_name.
# File 'lib/mongo/client_encryption.rb', line 225
def get_key_by_alt_name(key_alt_name) @encrypter.get_key_by_alt_name(key_alt_name) end
#get_keys ⇒ Collection::View Also known as: #keys
Returns all keys in the key vault collection.
# File 'lib/mongo/client_encryption.rb', line 232
def get_keys @encrypter.get_keys end
#keys
Alias for #get_keys.
# File 'lib/mongo/client_encryption.rb', line 235
alias keys get_keys
#remove_key_alt_name(id, key_alt_name) ⇒ BSON::Document | nil
Removes a key_alt_name from a key in the key vault collection with the given id.
# File 'lib/mongo/client_encryption.rb', line 244
def remove_key_alt_name(id, key_alt_name) @encrypter.remove_key_alt_name(id, key_alt_name) end
#rewrap_many_data_key(filter, opts = {}) ⇒ Crypt::RewrapManyDataKeyResult
Decrypts multiple data keys and (re-)encrypts them with a new master_key, or with their current master_key if a new one is not given.
# File 'lib/mongo/client_encryption.rb', line 259
def rewrap_many_data_key(filter, opts = {}) @encrypter.rewrap_many_data_key(filter, opts) end