Skip to content

Commit c095f6a

Browse files
committed
Make router.kms_provider() unneeded if only a single provider is configured
1 parent 5d4dc6b commit c095f6a

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

django_mongodb_backend/schema.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,15 @@ 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.
502+
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)
509+
# Providing master_key raises an error for the local provider.
503510
master_key = connection.settings_dict.get("KMS_CREDENTIALS").get(kms_provider)
504511
client_encryption = self.connection.client_encryption
505512

docs/howto/queryable-encryption.rst

Lines changed: 5 additions & 6 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
@@ -194,10 +191,12 @@ Example of KMS configuration with ``aws`` in your :class:`kms_providers
194191
},
195192
}
196193
197-
In your :ref:`custom database router <qe-configuring-database-routers-setting>`,
198-
specify the KMS provider to use for the models in your application:
194+
(TODO: If there's a use case for multiple providers, motivate with a use case
195+
and add a test.)
199196

200-
.. code-block:: python
197+
If you've configured multiple KMS providers, you must define logic to determine
198+
the provider for each model in your :ref:`database router
199+
<qe-configuring-database-routers-setting>`::
201200

202201
class EncryptedRouter:
203202
# ...

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)