Class: Mongo::ClientEncryption
- Inherits:
-
Object
- Object
- Mongo::ClientEncryption
- 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.
Instance Method Summary collapse
-
#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.
-
#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.
-
#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.
-
#initialize(key_vault_client, options = {}) ⇒ ClientEncryption
constructor
Create a new ClientEncryption object with the provided options.
-
#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, or with their current master_key if a new one is not given.
Constructor Details
#initialize(key_vault_client, options = {}) ⇒ ClientEncryption
Create a new ClientEncryption object with the provided options.
46 47 48 49 50 51 52 53 |
# File 'lib/mongo/client_encryption.rb', line 46 def initialize(key_vault_client, = {}) @encrypter = Crypt::ExplicitEncrypter.new( key_vault_client, [:key_vault_namespace], Crypt::KMS::Credentials.new([:kms_providers]), Crypt::KMS::Validations.([:kms_tls_options]) ) 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.
140 141 142 |
# File 'lib/mongo/client_encryption.rb', line 140 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.
81 82 83 84 85 86 87 |
# File 'lib/mongo/client_encryption.rb', line 81 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 |
#decrypt(value) ⇒ Object
Decrypts a value that has already been encrypted.
129 130 131 |
# File 'lib/mongo/client_encryption.rb', line 129 def decrypt(value) @encrypter.decrypt(value) end |
#delete_key(id) ⇒ Operation::Result
Removes the key with the given id from the key vault collection.
150 151 152 |
# File 'lib/mongo/client_encryption.rb', line 150 def delete_key(id) @encrypter.delete_key(id) end |
#encrypt(value, options = {}) ⇒ BSON::Binary
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.
if encryption algorithm is set to “Indexed”. Query type should be set
only if encryption algorithm is set to "Indexed". The only allowed
value is "equality".
119 120 121 |
# File 'lib/mongo/client_encryption.rb', line 119 def encrypt(value, ={}) @encrypter.encrypt(value, ) end |
#get_key(id) ⇒ BSON::Document | nil
Finds a single key with the given id.
160 161 162 |
# File 'lib/mongo/client_encryption.rb', line 160 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.
170 171 172 |
# File 'lib/mongo/client_encryption.rb', line 170 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.
177 178 179 |
# File 'lib/mongo/client_encryption.rb', line 177 def get_keys @encrypter.get_keys end |
#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.
189 190 191 |
# File 'lib/mongo/client_encryption.rb', line 189 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.
204 205 206 |
# File 'lib/mongo/client_encryption.rb', line 204 def rewrap_many_data_key(filter, opts = {}) @encrypter.rewrap_many_data_key(filter, opts) end |