Skip to content

Commit 05a7610

Browse files
committed
Add EncryptedRouter
Support configurable options like db name and encrypted apps.
1 parent a2342e2 commit 05a7610

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

django_mongodb_backend/encryption.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,49 @@
55

66
from bson.binary import STANDARD
77
from bson.codec_options import CodecOptions
8+
from django.conf import settings
89
from pymongo.encryption import AutoEncryptionOpts, ClientEncryption
910

11+
# Default settings for MongoDB Client-Side Field Level Encryption (CSFLE)
12+
# which can be imported into user settings and customized as needed. E.g.
13+
#
14+
# import os
15+
# from django_mongodb_backend import encryption, parse_uri
16+
# KEY_VAULT_NAMESPACE = encryption.get_key_vault_namespace()
17+
# KMS_PROVIDERS = encryption.get_kms_providers()
18+
# KMS_PROVIDER = encryption.KMS_PROVIDER
19+
# AUTO_ENCRYPTION_OPTS = encryption.get_auto_encryption_opts(
20+
# key_vault_namespace=KEY_VAULT_NAMESPACE,
21+
# kms_providers=KMS_PROVIDERS,
22+
# )
23+
# ENCRYPTED_DATABASE_NAME = encryption.ENCRYPTED_DATABASE_NAME
24+
# ENCRYPTED_APPS = encryption.ENCRYPTED_APPS
25+
# DATABASE_URL = os.environ.get("MONGODB_URI", "mongodb://localhost:27017")
26+
# DATABASES = {
27+
# "default": parse_uri(
28+
# DATABASE_URL,
29+
# db_name="test",
30+
# ),
31+
# ENCRYPTED_DATABASE_NAME: parse_uri(
32+
# DATABASE_URL,
33+
# options={"auto_encryption_opts": AUTO_ENCRYPTION_OPTS},
34+
# db_name=ENCRYPTED_DATABASE_NAME,
35+
# ),
36+
# }
37+
# DATABASE_ROUTERS = [encryption.EncryptedRouter()]
38+
1039
KEY_VAULT_DATABASE_NAME = "keyvault"
1140
KEY_VAULT_COLLECTION_NAME = "__keyVault"
1241
KMS_PROVIDER = "local" # e.g., "aws", "azure", "gcp", "kmip", or "local"
42+
ENCRYPTED_DATABASE_NAME = "encrypted"
43+
ENCRYPTED_APPS = ["encryption_"]
44+
45+
46+
class EncryptedRouter:
47+
def allow_migrate(self, db, app_label, model_name=None, **hints):
48+
if db == settings.ENCRYPTED_DATABASE_NAME and app_label not in settings.ENCRYPTED_APPS:
49+
return False
50+
return None
1351

1452

1553
class EqualityQuery:

tests/encryption_/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class EncryptedModelTests(TestCase):
1111
@classmethod
1212
def setUpTestData(cls):
1313
cls.person = Person(ssn="123-45-6789")
14+
cls.person.save()
1415

1516
def test_encrypted_fields_map(self):
1617
""" """

0 commit comments

Comments
 (0)