Skip to content

Commit 503fba5

Browse files
Fix various FLE issues (#5669)
1 parent 276e846 commit 503fba5

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

docs/tutorials/automatic-encryption.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ dependencies in `the driver documentation. <https://www.mongodb.com/docs/ruby-dr
5050
Note the version of the Ruby driver being used in your application and select
5151
the appropriate steps below.
5252

53-
5453
Install ``libmongocrypt``
5554
~~~~~~~~~~~~~~~~~~~~~~~~~
5655

@@ -239,12 +238,29 @@ Now we can tell Mongoid what should be encrypted:
239238
# policy_number_key field.
240239
field :policy_number, type: Integer, encrypt: {
241240
deterministic: false,
242-
key_field_name: :policy_number_key
241+
key_name_field: :policy_number_key
243242
}
244243

245244
embedded_in :patient
246245
end
247246

247+
.. note::
248+
If you are developing a Rails application, it is recommended to set
249+
``preload_models`` to ``true`` in ``mongoid.yml``. This will ensure that
250+
Mongoid loads all models before the application starts, and the encryption
251+
schema is configured before any data is read or written.
252+
253+
Known Limitations
254+
~~~~~~~~~~~~~~~~~
255+
256+
* MongoDB CSFLE has some limitations that are described in
257+
`the server documentation. <https://www.mongodb.com/docs/manual/core/csfle/reference/limitations/>`_
258+
These limitations also apply to Mongoid.
259+
* Mongoid does not support encryption of ``embeds_many`` relations.
260+
* If you use ``:key_name_field`` option, the field must be encrypted using
261+
non-deterministic algorithm. To encrypt your field deterministically, you must
262+
specify ``:key_id`` option instead.
263+
248264
Working with Data
249265
=================
250266

lib/mongoid/clients.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def with_name(name)
6060
name_as_symbol = name.to_sym
6161
return clients[name_as_symbol] if clients[name_as_symbol]
6262
CREATE_LOCK.synchronize do
63+
if (key_vault_client = Mongoid.clients.dig(name_as_symbol, :options, :auto_encryption_options, :key_vault_client))
64+
clients[key_vault_client.to_sym] ||= Clients::Factory.create(key_vault_client)
65+
end
6366
clients[name_as_symbol] ||= Clients::Factory.create(name)
6467
end
6568
end

lib/mongoid/clients/factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def build_auto_encryption_options(opts, database)
9898
auto_encryption_options[:schema_map] = Mongoid.config.encryption_schema_map(database)
9999
end
100100
if auto_encryption_options.key?(:key_vault_client)
101-
auto_encryption_options[:key_vault_client] = Mongoid::Clients.with_name(
101+
auto_encryption_options[:key_vault_client] = Mongoid.client(
102102
auto_encryption_options[:key_vault_client]
103103
)
104104
end

lib/mongoid/config/encryption.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,10 @@ def properties_for_fields(model)
147147
def properties_for_relations(model, visited)
148148
model.relations.each_with_object({}) do |(name, relation), props|
149149
next if visited.include?(relation.relation_class)
150-
visited << relation.relation_class
151-
next unless relation.is_a?(Association::Embedded::EmbedsMany) ||
152-
relation.is_a?(Association::Embedded::EmbedsOne)
150+
next unless relation.is_a?(Association::Embedded::EmbedsOne)
153151
next unless relation.relation_class.encrypted?
154152

153+
visited << relation.relation_class
155154
metadata_for(
156155
relation.relation_class
157156
).merge(

lib/mongoid/encryptable.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ module ClassMethods
1818
#
1919
# @param [ Hash ] options The encryption metadata.
2020
# @option options [ String ] :key_id The base64-encoded UUID of the key
21-
# used to encrypt fields.
21+
# used to encrypt fields. Mutually exclusive with :key_name_field option.
22+
# @option options [ String ] :key_name_field The name of the field that
23+
# contains the key alt name to use for encryption. Mutually exclusive
24+
# with :key_id option.
2225
# @option options [ true | false ] :deterministic Whether the encryption
2326
# is deterministic or not.
2427
def encrypt_with(options = {})

0 commit comments

Comments
 (0)