Skip to content

Commit 1169c04

Browse files
committed
encrypted fields map != encrypted fields
The spec says: "encryptedFieldsMap maps a collection namespace to an encryptedFields." In this commit, we clarify the distinction between encrypted fields map and encrypted fields.
1 parent 774df35 commit 1169c04

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

django_mongodb_backend/management/commands/showencryptedfieldsmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def handle(self, *args, **options):
4141
for app_config in apps.get_app_configs():
4242
for model in router.get_migratable_models(app_config, db):
4343
if model_has_encrypted_fields(model):
44-
fields = editor._get_encrypted_fields_map(
44+
fields = editor._get_encrypted_fields(
4545
model, client, create_data_keys=create_data_keys
4646
)
4747
encrypted_fields_map[model._meta.db_table] = fields

django_mongodb_backend/schema.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def _create_collection(self, model):
461461
Create a collection for the model with the encrypted fields. If
462462
provided, use the `_encrypted_fields_map` in the client's
463463
`auto_encryption_opts`. Otherwise, create the encrypted fields map
464-
with `_get_encrypted_fields_map`.
464+
with `_get_encrypted_fields`.
465465
"""
466466
db = self.get_database()
467467
db_table = model._meta.db_table
@@ -475,18 +475,16 @@ def _create_collection(self, model):
475475
)
476476
encrypted_fields_map = getattr(auto_encryption_opts, "_encrypted_fields_map", None)
477477
if not encrypted_fields_map:
478-
encrypted_fields_map = self._get_encrypted_fields_map(
479-
model, client, create_data_keys=True
480-
)
478+
encrypted_fields = self._get_encrypted_fields(model, client, create_data_keys=True)
481479
else:
482-
# If the encrypted fields map is provided, get the map for the
480+
# If the encrypted fields map is provided, get the encrypted fields for the
483481
# specific collection.
484-
encrypted_fields_map = encrypted_fields_map.get(db_table)
485-
db.create_collection(db_table, encryptedFields=encrypted_fields_map)
482+
encrypted_fields = encrypted_fields_map.get(db_table)
483+
db.create_collection(db_table, encryptedFields=encrypted_fields)
486484
else:
487485
db.create_collection(db_table)
488486

489-
def _get_encrypted_fields_map(self, model, client, create_data_keys=False):
487+
def _get_encrypted_fields(self, model, client, create_data_keys=False):
490488
connection = self.connection
491489
fields = model._meta.fields
492490
options = client._options

tests/encryption_/test_schema.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
class SchemaTests(QueryableEncryptionTestCase):
88
maxDiff = None
99

10-
def test_get_encrypted_fields_map(self):
10+
def test_get_encrypted_fields(self):
1111
"""
1212
Test class method called by schema editor and management command to get
13-
encrypted fields map for `create_collection` and `auto_encryption_opts`
14-
respectively. There are no data keys in the results.
13+
encrypted fields for `create_collection` and `auto_encryption_opts`
14+
respectively.
1515
16-
Data keys for the schema editor are created by
17-
`create_encrypted_collection` and data keys for the management command
18-
are created by the management command using code similar to the code in
19-
create_encrypted_collection` in Pymongo.
16+
This method is called per collection when creating a new collection and
17+
per database when setting up auto encryption options.
18+
19+
Data keys are not tested here as they are expected to differ each time.
2020
"""
21-
expected_encrypted_fields_map = {
21+
expected_encrypted_fields = {
2222
"fields": [
2323
{
2424
"bsonType": "long",
@@ -54,11 +54,11 @@ def test_get_encrypted_fields_map(self):
5454
connection = connections["encrypted"]
5555
with connection.schema_editor() as editor:
5656
client = connection.connection
57-
encrypted_fields_map = editor._get_encrypted_fields_map(Patient, client)
58-
for field in encrypted_fields_map["fields"]:
57+
encrypted_fields = editor._get_encrypted_fields(Patient, client)
58+
for field in encrypted_fields["fields"]:
5959
# Remove data keys from the output; they are expected to differ
6060
field.pop("keyId", None)
6161
self.assertEqual(
62-
encrypted_fields_map,
63-
expected_encrypted_fields_map,
62+
encrypted_fields,
63+
expected_encrypted_fields,
6464
)

0 commit comments

Comments
 (0)