Skip to content

Commit e988c55

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 f32d62b commit e988c55

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
@@ -429,7 +429,7 @@ def _create_collection(self, model):
429429
Create a collection for the model with the encrypted fields. If
430430
provided, use the `_encrypted_fields_map` in the client's
431431
`auto_encryption_opts`. Otherwise, create the encrypted fields map
432-
with `_get_encrypted_fields_map`.
432+
with `_get_encrypted_fields`.
433433
"""
434434
db = self.get_database()
435435
db_table = model._meta.db_table
@@ -443,18 +443,16 @@ def _create_collection(self, model):
443443
)
444444
encrypted_fields_map = getattr(auto_encryption_opts, "_encrypted_fields_map", None)
445445
if not encrypted_fields_map:
446-
encrypted_fields_map = self._get_encrypted_fields_map(
447-
model, client, create_data_keys=True
448-
)
446+
encrypted_fields = self._get_encrypted_fields(model, client, create_data_keys=True)
449447
else:
450-
# If the encrypted fields map is provided, get the map for the
448+
# If the encrypted fields map is provided, get the encrypted fields for the
451449
# specific collection.
452-
encrypted_fields_map = encrypted_fields_map.get(db_table)
453-
db.create_collection(db_table, encryptedFields=encrypted_fields_map)
450+
encrypted_fields = encrypted_fields_map.get(db_table)
451+
db.create_collection(db_table, encryptedFields=encrypted_fields)
454452
else:
455453
db.create_collection(db_table)
456454

457-
def _get_encrypted_fields_map(self, model, client, create_data_keys=False):
455+
def _get_encrypted_fields(self, model, client, create_data_keys=False):
458456
connection = self.connection
459457
fields = model._meta.fields
460458
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)