Skip to content

Commit ed476a7

Browse files
committed
remove support for multiple kms providers
1 parent 301e1b4 commit ed476a7

File tree

5 files changed

+28
-40
lines changed

5 files changed

+28
-40
lines changed

django_mongodb_backend/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from .indexes import register_indexes # noqa: E402
1515
from .lookups import register_lookups # noqa: E402
1616
from .query import register_nodes # noqa: E402
17-
from .routers import register_routers # noqa: E402
1817

1918
__all__ = ["parse_uri"]
2019

@@ -26,4 +25,3 @@
2625
register_indexes()
2726
register_lookups()
2827
register_nodes()
29-
register_routers()

django_mongodb_backend/routers.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
from django.apps import apps
2-
from django.core.exceptions import ImproperlyConfigured
3-
from django.db.utils import ConnectionRouter
42

53

64
class MongoRouter:
@@ -18,22 +16,3 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
1816
except LookupError:
1917
return None
2018
return False if issubclass(model, EmbeddedModel) else None
21-
22-
23-
# This function is intended to be monkey-patched as a method of ConnectionRouter.
24-
def kms_provider(self, model, *args, **kwargs):
25-
"""
26-
Return the Key Management Service (KMS) provider for a given model.
27-
28-
Call each router's kms_provider() method (if present), and return the
29-
first non-None result. Raise ImproperlyConfigured if no provider is found.
30-
"""
31-
for router in self.routers:
32-
func = getattr(router, "kms_provider", None)
33-
if func and callable(func) and (result := func(model, *args, **kwargs)):
34-
return result
35-
raise ImproperlyConfigured("No kms_provider found in database routers.")
36-
37-
38-
def register_routers():
39-
ConnectionRouter.kms_provider = kms_provider

django_mongodb_backend/schema.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from time import monotonic, sleep
22

33
from django.core.exceptions import ImproperlyConfigured
4-
from django.db import router
4+
from django.db import NotSupportedError
55
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
66
from django.db.models import Index, UniqueConstraint
77
from pymongo.operations import SearchIndexModel
@@ -508,9 +508,12 @@ def _get_encrypted_fields(
508508
if len(kms_providers) == 1:
509509
# If one provider is configured, no need to consult the router.
510510
kms_provider = next(iter(kms_providers.keys()))
511-
else:
512-
# Otherwise, call the user-defined router.kms_provider().
513-
kms_provider = router.kms_provider(model)
511+
else: # (Since PyMongo requires at least one KMS provider.)
512+
raise NotSupportedError(
513+
"Multiple KMS providers per database aren't supported. "
514+
"Please create a feature request with details about your "
515+
"use case."
516+
)
514517
if kms_provider == "local":
515518
master_key = None
516519
else:

docs/howto/queryable-encryption.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,6 @@ Here's an example of KMS configuration with ``aws``::
263263
},
264264
}
265265

266-
(TODO: If there's a use case for multiple providers, motivate with a use case
267-
and add a test.)
268-
269-
If you've configured multiple KMS providers, you must define logic to determine
270-
the provider for each model in your :ref:`database router
271-
<qe-configuring-database-routers-setting>`::
272-
273-
class EncryptedRouter:
274-
# ...
275-
def kms_provider(self, model, **hints):
276-
return "aws"
277-
278266
.. _qe-configuring-encrypted-fields-map:
279267

280268
Configuring the ``encrypted_fields_map`` option

tests/encryption_/test_schema.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from bson.binary import Binary
22
from django.core.exceptions import ImproperlyConfigured
3-
from django.db import connections
3+
from django.db import NotSupportedError, connections
44

55
from . import models
66
from .models import EncryptionKey
@@ -150,3 +150,23 @@ def test_missing_auto_encryption_opts(self):
150150
connection.schema_editor() as editor,
151151
):
152152
editor.create_model(models.Patient)
153+
154+
def test_multiple_kms_providers(self):
155+
connection = connections["encrypted"]
156+
auto_encryption_opts = connection.connection._options.auto_encryption_opts
157+
kms_providers = auto_encryption_opts._kms_providers
158+
# Mock multiple kms_providers by using a list of length > 1.
159+
auto_encryption_opts._kms_providers = [{}, {}]
160+
msg = (
161+
"Multiple KMS providers per database aren't supported. Please "
162+
"create a feature request with details about your use case."
163+
)
164+
try:
165+
with (
166+
self.assertRaisesMessage(NotSupportedError, msg),
167+
connection.schema_editor() as editor,
168+
):
169+
editor.create_model(models.Patient)
170+
finally:
171+
# Restore the original value.
172+
auto_encryption_opts._kms_providers = kms_providers

0 commit comments

Comments
 (0)