Skip to content

Commit 7f0d08d

Browse files
committed
Make router.kms_provider() unneeded if only a single provider is configured
1 parent 9cc9614 commit 7f0d08d

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

django_mongodb_backend/schema.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,14 @@ def _get_encrypted_fields(self, model, key_alt_name=None, path_prefix=None):
498498
key_vault_collection.create_index(
499499
"keyAltNames", unique=True, partialFilterExpression={"keyAltNames": {"$exists": True}}
500500
)
501-
502-
kms_provider = router.kms_provider(model)
501+
# Select the KMS provider.
503502
kms_providers = auto_encryption_opts._kms_providers
503+
if len(kms_providers) == 1:
504+
# If one provider is configured, no need to consult the router.
505+
kms_provider = next(iter(kms_providers.keys()))
506+
else:
507+
# Otherwise, call the user-defined router.kms_provider().
508+
kms_provider = router.kms_provider(model)
504509
# Providing master_key raises an error for the local provider.
505510
master_key = kms_providers[kms_provider] if kms_provider != "local" else None
506511
client_encryption = self.connection.client_encryption

docs/howto/queryable-encryption.rst

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ models in that application. The router also specifies the :ref:`KMS provider
124124
return "encrypted"
125125
return None
126126
127-
def kms_provider(self, model, **hints):
128-
return "local"
129-
130127
db_for_write = db_for_read
131128
132129
Then in your Django settings, add the custom database router to the
@@ -184,11 +181,12 @@ Example of KMS configuration with ``aws`` in your :class:`kms_providers
184181
},
185182
}
186183
184+
(TODO: If there's a use case for multiple providers, motivate with a use case
185+
and add a test.)
187186

188-
In your :ref:`custom database router <qe-configuring-database-routers-setting>`,
189-
specify the KMS provider to use for the models in your application:
190-
191-
.. code-block:: python
187+
If you've configured multiple KMS providers, you must define logic to determine
188+
the provider for each model in your :ref:`database router
189+
<qe-configuring-database-routers-setting>`::
192190

193191
class EncryptedRouter:
194192
# ...

docs/ref/utils.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,3 @@ following parts can be considered stable.
8383
else:
8484
return db == "default"
8585
return None
86-
87-
def kms_provider(self, model):
88-
if model_has_encrypted_fields(model):
89-
return "local"
90-
return None

0 commit comments

Comments
 (0)