Skip to content

Commit ff84902

Browse files
committed
Remove create_data_keys until use case manifests
In addition to removing the arg from the showencryptedfields command, reduces complexity in schema editor with removal of the `create_data_keys` boolean. Previous logic may have been flawed in looking up existing keys.
1 parent 25e7da1 commit ff84902

File tree

6 files changed

+11
-66
lines changed

6 files changed

+11
-66
lines changed

django_mongodb_backend/management/commands/showencryptedfieldsmap.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,16 @@ def add_arguments(self, parser):
2222
help="""
2323
Specifies the database to use. Defaults to ``default``.""",
2424
)
25-
parser.add_argument(
26-
"--create-data-keys",
27-
action="store_true",
28-
help="""
29-
If specified, this option will create and show new encryption
30-
keys instead of showing existing keys from the configured key vault.
31-
""",
32-
)
3325

3426
def handle(self, *args, **options):
3527
db = options["database"]
36-
create_data_keys = options.get("create_data_keys", False)
3728
connection = connections[db]
3829
connection.ensure_connection()
3930
encrypted_fields_map = {}
4031
with connection.schema_editor() as editor:
4132
for app_config in apps.get_app_configs():
4233
for model in router.get_migratable_models(app_config, db):
4334
if model_has_encrypted_fields(model):
44-
fields = editor._get_encrypted_fields(
45-
model, create_data_keys=create_data_keys
46-
)
35+
fields = editor._get_encrypted_fields(model)
4736
encrypted_fields_map[model._meta.db_table] = fields
4837
self.stdout.write(json_util.dumps(encrypted_fields_map, indent=2))

django_mongodb_backend/schema.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def _create_collection(self, model):
475475
encrypted_fields_map = getattr(auto_encryption_opts, "_encrypted_fields_map", None)
476476

477477
if not encrypted_fields_map:
478-
encrypted_fields = self._get_encrypted_fields(model, create_data_keys=True)
478+
encrypted_fields = self._get_encrypted_fields(model)
479479
else:
480480
encrypted_fields = encrypted_fields_map.get(db_table)
481481

@@ -488,9 +488,7 @@ def _create_collection(self, model):
488488
# Unencrypted path
489489
db.create_collection(db_table)
490490

491-
def _get_encrypted_fields(
492-
self, model, create_data_keys=False, key_alt_name=None, path_prefix=None
493-
):
491+
def _get_encrypted_fields(self, model, key_alt_name=None, path_prefix=None):
494492
"""
495493
Recursively collect encryption schema data for only encrypted fields in a model.
496494
Returns None if no encrypted fields are found anywhere in the model hierarchy.
@@ -520,7 +518,6 @@ def _get_encrypted_fields(
520518
if isinstance(field, EmbeddedModelField) and not getattr(field, "encrypted", False):
521519
embedded_result = self._get_encrypted_fields(
522520
field.embedded_model,
523-
create_data_keys=create_data_keys,
524521
key_alt_name=new_key_alt_name,
525522
path_prefix=path,
526523
)
@@ -530,15 +527,15 @@ def _get_encrypted_fields(
530527

531528
if getattr(field, "encrypted", False):
532529
bson_type = field.db_type(connection)
533-
if create_data_keys:
530+
data_key = key_vault_collection.find_one({"keyAltNames": new_key_alt_name})
531+
if data_key:
532+
data_key = data_key["_id"]
533+
else:
534534
data_key = client_encryption.create_data_key(
535535
kms_provider=kms_provider,
536536
master_key=master_key,
537537
key_alt_names=[new_key_alt_name],
538538
)
539-
else:
540-
key = key_vault_collection.find_one({"keyAltNames": new_key_alt_name})
541-
data_key = key["_id"]
542539
field_dict = {
543540
"bsonType": bson_type,
544541
"path": path,

docs/howto/queryable-encryption.rst

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,7 @@ run the following command::
211211

212212
You can then use the output of the :djadmin:`showencryptedfieldsmap` command
213213
to set the ``encrypted_fields_map`` in
214-
:class:`pymongo.encryption_options.AutoEncryptionOpts` in your Django settings
215-
if you want to use a pre-defined encrypted fields map in the client instead of
216-
letting Django MongoDB Backend create them for you.
217-
218-
.. try to explain the chicken/egg scenario here
219-
220-
Of course, if you do this after Django MongoDB Backend has already created the
221-
collections, you will need to drop the collections first before using the
222-
pre-defined encrypted fields map.
223-
224-
If you do not want to use the data keys created by Django MongoDB Backend (when
225-
``python manage.py migrate`` is run), you can generate new data keys with::
226-
227-
$ python manage.py showencryptedfieldsmap --database encrypted \
228-
--create-data-keys
229-
230-
In this scenario, Django MongoDB Backend will use the newly created data keys
231-
to create collections for models with encrypted fields.
214+
:class:`pymongo.encryption_options.AutoEncryptionOpts` in your Django settings.
232215

233216
Here is an example of how to configure the
234217
``encrypted_fields_map`` in your Django settings:

docs/ref/django-admin.rst

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,8 @@ Available commands
2323

2424
This command shows the mapping of encrypted fields to attributes including
2525
data type, data keys and query types. It can be used to set the
26-
``encrypted_fields_map`` in ``AutoEncryptionOpts``. Defaults to showing
27-
existing keys from the configured key vault.
26+
``encrypted_fields_map`` in ``AutoEncryptionOpts``.
2827

2928
.. django-admin-option:: --database DATABASE
3029

3130
Specifies the database to use. Defaults to ``default``.
32-
33-
.. django-admin-option:: --create-data-keys
34-
35-
If specified, this option will create and show new encryption keys
36-
instead of showing existing keys from the configured key vault.

tests/encryption_/test_management.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,3 @@ def test_show_encrypted_fields_map(self):
109109
with self.subTest(model=model_key):
110110
self.assertIn(model_key, command_output)
111111
self._compare_output(expected, command_output[model_key])
112-
113-
def test_create_new_keys(self):
114-
out = StringIO()
115-
call_command(
116-
"showencryptedfieldsmap",
117-
"--database",
118-
"encrypted",
119-
"--create-data-keys",
120-
verbosity=0,
121-
stdout=out,
122-
)
123-
command_output = json_util.loads(out.getvalue())
124-
125-
for model_key, expected in self.expected_maps.items():
126-
with self.subTest(model=model_key):
127-
self.assertIn(model_key, command_output)
128-
self._compare_output(expected, command_output[model_key])

tests/encryption_/test_schema.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_get_encrypted_fields_all_models(self):
109109

110110
def test_key_creation_and_lookup(self):
111111
"""
112-
Use _get_encrypted_fields(create_data_keys=True) to
112+
Use _get_encrypted_fields to
113113
generate and store a data key in the vault, then
114114
query the vault with the keyAltName.
115115
"""
@@ -124,9 +124,8 @@ def test_key_creation_and_lookup(self):
124124
test_key_alt_name = f"{model_class._meta.db_table}.value"
125125
vault_coll.delete_many({"keyAltNames": test_key_alt_name})
126126

127-
# Call _get_encrypted_fields with create_data_keys=True
128127
with connection.schema_editor() as editor:
129-
encrypted_fields = editor._get_encrypted_fields(model_class, create_data_keys=True)
128+
encrypted_fields = editor._get_encrypted_fields(model_class)
130129

131130
# Validate schema contains a keyId for our field
132131
self.assertTrue(encrypted_fields["fields"])

0 commit comments

Comments
 (0)