Class: Mongo::Crypt::EncryptionIO Private
- Inherits:
-
Object
- Object
- Mongo::Crypt::EncryptionIO
- Defined in:
- lib/mongo/crypt/encryption_io.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class that implements I/O methods between the driver and the MongoDB server or mongocryptd.
Constant Summary collapse
- SOCKET_TIMEOUT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Timeout used for TLS socket connection, reading, and writing. There is no specific timeout written in the spec. See SPEC-1394 for a discussion and updates on what this timeout should be.
10
Instance Method Summary collapse
-
#add_key_alt_name(id, key_alt_name, timeout_ms: nil) ⇒ Object
private
Adds a key_alt_name to the key_alt_names array of the key document in the key vault collection with the given id.
-
#collection_info(db_name, filter, timeout_ms: nil) ⇒ Hash
private
Get collection info for a collection matching the provided filter.
-
#delete_key(id, timeout_ms: nil) ⇒ Object
private
Removes the key document with the given id from the key vault collection.
-
#feed_kms(kms_context, tls_options, timeout_ms: nil) ⇒ Object
private
Get information about the remote KMS encryption key and feed it to the the KmsContext object.
-
#find_keys(filter, timeout_ms: nil) ⇒ Array<BSON::Document>
private
Query for keys in the key vault collection using the provided filter.
-
#get_key(id, timeout_ms: nil) ⇒ Object
private
Finds a single key document with the given id.
-
#get_key_by_alt_name(key_alt_name, timeout_ms: nil) ⇒ Object
private
Returns a key document in the key vault collection with the given key_alt_name.
-
#get_keys(timeout_ms: nil) ⇒ Object
private
Finds all documents in the key vault collection.
-
#initialize(key_vault_namespace:, key_vault_client:, metadata_client:, client: nil, mongocryptd_client: nil, mongocryptd_options: {}) ⇒ EncryptionIO
constructor
private
Creates a new EncryptionIO object with information about how to connect to the key vault.
-
#insert_data_key(document, timeout_ms: nil) ⇒ Mongo::Operation::Insert::Result
private
Insert a document into the key vault collection.
-
#mark_command(cmd, timeout_ms: nil) ⇒ Hash
private
Send the command to mongocryptd to be marked with intent-to-encrypt markings.
-
#remove_key_alt_name(id, key_alt_name, timeout_ms: nil) ⇒ Object
private
Removes a key_alt_name from the key_alt_names array of the key document in the key vault collection with the given id.
-
#update_data_keys(updates, timeout_ms: nil) ⇒ BulkWrite::Result
private
Apply given requests to the key vault collection using bulk write.
Constructor Details
#initialize(key_vault_namespace:, key_vault_client:, metadata_client:, client: nil, mongocryptd_client: nil, mongocryptd_options: {}) ⇒ EncryptionIO
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
When being used for auto encryption, all arguments are required. When being used for explicit encryption, only the key_vault_namespace and key_vault_client arguments are required.
This class expects that the key_vault_client and key_vault_namespace options are not nil and are in the correct format.
Creates a new EncryptionIO object with information about how to connect to the key vault.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo/crypt/encryption_io.rb', line 54 def initialize( key_vault_namespace:, key_vault_client:, metadata_client:, client: nil, mongocryptd_client: nil, mongocryptd_options: {} ) validate_key_vault_client!(key_vault_client) validate_key_vault_namespace!(key_vault_namespace) @client = client @mongocryptd_client = mongocryptd_client @key_vault_db_name, @key_vault_collection_name = key_vault_namespace.split('.') @key_vault_client = key_vault_client @metadata_client = @options = end |
Instance Method Details
#add_key_alt_name(id, key_alt_name, timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a key_alt_name to the key_alt_names array of the key document in the key vault collection with the given id.
181 182 183 184 185 186 187 |
# File 'lib/mongo/crypt/encryption_io.rb', line 181 def add_key_alt_name(id, key_alt_name, timeout_ms: nil) key_vault_collection.find_one_and_update( { _id: id }, { '$addToSet' => { keyAltNames: key_alt_name } }, timeout_ms: timeout_ms ) end |
#collection_info(db_name, filter, timeout_ms: nil) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get collection info for a collection matching the provided filter
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mongo/crypt/encryption_io.rb', line 101 def collection_info(db_name, filter, timeout_ms: nil) unless @metadata_client raise ArgumentError, 'collection_info requires metadata_client to have been passed to the constructor, but it was not' end @metadata_client .use(db_name) .database .list_collections(filter: filter, deserialize_as_bson: true, timeout_ms: timeout_ms) .first end |
#delete_key(id, timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes the key document with the given id from the key vault collection.
191 192 193 |
# File 'lib/mongo/crypt/encryption_io.rb', line 191 def delete_key(id, timeout_ms: nil) key_vault_collection.delete_one(_id: id, timeout_ms: timeout_ms) end |
#feed_kms(kms_context, tls_options, timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get information about the remote KMS encryption key and feed it to the the KmsContext object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/mongo/crypt/encryption_io.rb', line 159 def feed_kms(kms_context, , timeout_ms: nil) with_ssl_socket(kms_context.endpoint, ) do |ssl_socket| Timeout.timeout(timeout_ms || SOCKET_TIMEOUT, Error::SocketTimeoutError, 'Socket write operation timed out') do ssl_socket.syswrite(kms_context.) end bytes_needed = kms_context.bytes_needed while bytes_needed > 0 bytes = Timeout.timeout(timeout_ms || SOCKET_TIMEOUT, Error::SocketTimeoutError, 'Socket read operation timed out') do ssl_socket.sysread(bytes_needed) end kms_context.feed(bytes) bytes_needed = kms_context.bytes_needed end end end |
#find_keys(filter, timeout_ms: nil) ⇒ Array<BSON::Document>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Query for keys in the key vault collection using the provided filter
77 78 79 |
# File 'lib/mongo/crypt/encryption_io.rb', line 77 def find_keys(filter, timeout_ms: nil) key_vault_collection.find(filter, timeout_ms: timeout_ms).to_a end |
#get_key(id, timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Finds a single key document with the given id.
196 197 198 |
# File 'lib/mongo/crypt/encryption_io.rb', line 196 def get_key(id, timeout_ms: nil) key_vault_collection.find(_id: id, timeout_ms: timeout_ms).first end |
#get_key_by_alt_name(key_alt_name, timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a key document in the key vault collection with the given key_alt_name.
202 203 204 |
# File 'lib/mongo/crypt/encryption_io.rb', line 202 def get_key_by_alt_name(key_alt_name, timeout_ms: nil) key_vault_collection.find(keyAltNames: key_alt_name, timeout_ms: timeout_ms).first end |
#get_keys(timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Finds all documents in the key vault collection.
207 208 209 |
# File 'lib/mongo/crypt/encryption_io.rb', line 207 def get_keys(timeout_ms: nil) key_vault_collection.find(nil, timeout_ms: timeout_ms) end |
#insert_data_key(document, timeout_ms: nil) ⇒ Mongo::Operation::Insert::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert a document into the key vault collection
89 90 91 |
# File 'lib/mongo/crypt/encryption_io.rb', line 89 def insert_data_key(document, timeout_ms: nil) key_vault_collection.insert_one(document, timeout_ms: timeout_ms) end |
#mark_command(cmd, timeout_ms: nil) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Send the command to mongocryptd to be marked with intent-to-encrypt markings
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mongo/crypt/encryption_io.rb', line 122 def mark_command(cmd, timeout_ms: nil) unless @mongocryptd_client raise ArgumentError, 'mark_command requires mongocryptd_client to have been passed to the constructor, but it was not' end # Ensure the response from mongocryptd is deserialized with { mode: :bson } # to prevent losing type information in commands = { execution_options: { deserialize_as_bson: true }, timeout_ms: timeout_ms } begin response = @mongocryptd_client.database.command(cmd, ) rescue Error::NoServerAvailable => e raise e if @options[:mongocryptd_bypass_spawn] spawn_mongocryptd response = @mongocryptd_client.database.command(cmd, ) end response.first end |
#remove_key_alt_name(id, key_alt_name, timeout_ms: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes a key_alt_name from the key_alt_names array of the key document in the key vault collection with the given id.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/mongo/crypt/encryption_io.rb', line 213 def remove_key_alt_name(id, key_alt_name, timeout_ms: nil) key_vault_collection.find_one_and_update( { _id: id }, [ { '$set' => { keyAltNames: { '$cond' => [ { '$eq' => [ '$keyAltNames', [ key_alt_name ] ] }, '$$REMOVE', { '$filter' => { input: '$keyAltNames', cond: { '$ne' => [ '$$this', key_alt_name ] } } } ] } } } ], timeout_ms: timeout_ms ) end |
#update_data_keys(updates, timeout_ms: nil) ⇒ BulkWrite::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Apply given requests to the key vault collection using bulk write.
243 244 245 |
# File 'lib/mongo/crypt/encryption_io.rb', line 243 def update_data_keys(updates, timeout_ms: nil) key_vault_collection.bulk_write(updates, timeout_ms: timeout_ms) end |