Skip to content

Commit bc76db3

Browse files
committed
Allow user to customize some QE settings.
Via Django settings. With this change we don't need to provide helpers for `kms_providers` and `key_vault_namespace` because they can be configured in Django settings and retrieved by the schema during `client_encryption` and `create_encrypted_collection`.
1 parent 6487086 commit bc76db3

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

django_mongodb_backend/encryption.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111
KEY_VAULT_COLLECTION_NAME = "__keyVault"
1212

1313

14+
def get_customer_master_key():
15+
"""
16+
Returns a 96-byte local master key for use with MongoDB Client-Side Field Level
17+
Encryption (CSFLE). For local testing purposes only. In production, use a secure KMS
18+
like AWS, Azure, GCP, or KMIP.
19+
Returns:
20+
bytes: A 96-byte key.
21+
"""
22+
# WARNING: This is a static key for testing only.
23+
# Generate with: os.urandom(96)
24+
return bytes.fromhex(
25+
"000102030405060708090a0b0c0d0e0f"
26+
"101112131415161718191a1b1c1d1e1f"
27+
"202122232425262728292a2b2c2d2e2f"
28+
"303132333435363738393a3b3c3d3e3f"
29+
"404142434445464748494a4b4c4d4e4f"
30+
"505152535455565758595a5b5c5d5e5f"
31+
)
32+
33+
1434
def get_kms_providers():
1535
"""
1636
Return supported KMS providers for MongoDB Client-Side Field Level Encryption (CSFLE).
@@ -22,16 +42,7 @@ def get_kms_providers():
2242
}
2343

2444

25-
def get_client_encryption(client):
26-
"""
27-
Returns a `ClientEncryption` instance for MongoDB Client-Side Field Level
28-
Encryption (CSFLE) that can be used to create an encrypted collection.
29-
"""
30-
31-
key_vault_namespace = get_key_vault_namespace()
32-
kms_providers = get_kms_providers()
33-
codec_options = CodecOptions(uuid_representation=STANDARD)
34-
return ClientEncryption(kms_providers, key_vault_namespace, client, codec_options)
45+
KMS_PROVIDERS = get_kms_providers()
3546

3647

3748
def get_key_vault_namespace(
@@ -44,6 +55,18 @@ def get_key_vault_namespace(
4455
KEY_VAULT_NAMESPACE = get_key_vault_namespace()
4556

4657

58+
def get_client_encryption(
59+
client, key_vault_namespace=KEY_VAULT_NAMESPACE, kms_providers=KMS_PROVIDERS
60+
):
61+
"""
62+
Returns a `ClientEncryption` instance for MongoDB Client-Side Field Level
63+
Encryption (CSFLE) that can be used to create an encrypted collection.
64+
"""
65+
66+
codec_options = CodecOptions(uuid_representation=STANDARD)
67+
return ClientEncryption(kms_providers, key_vault_namespace, client, codec_options)
68+
69+
4770
def get_auto_encryption_opts(
4871
key_vault_namespace=KEY_VAULT_NAMESPACE, crypt_shared_lib_path=None, kms_providers=None
4972
):
@@ -56,23 +79,3 @@ def get_auto_encryption_opts(
5679
kms_providers=kms_providers,
5780
crypt_shared_lib_path=crypt_shared_lib_path,
5881
)
59-
60-
61-
def get_customer_master_key():
62-
"""
63-
Returns a 96-byte local master key for use with MongoDB Client-Side Field Level
64-
Encryption (CSFLE). For local testing purposes only. In production, use a secure KMS
65-
like AWS, Azure, GCP, or KMIP.
66-
Returns:
67-
bytes: A 96-byte key.
68-
"""
69-
# WARNING: This is a static key for testing only.
70-
# Generate with: os.urandom(96)
71-
return bytes.fromhex(
72-
"000102030405060708090a0b0c0d0e0f"
73-
"101112131415161718191a1b1c1d1e1f"
74-
"202122232425262728292a2b2c2d2e2f"
75-
"303132333435363738393a3b3c3d3e3f"
76-
"404142434445464748494a4b4c4d4e4f"
77-
"505152535455565758595a5b5c5d5e5f"
78-
)

django_mongodb_backend/schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.conf import settings
12
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
23
from django.db.models import Index, UniqueConstraint
34
from pymongo.operations import SearchIndexModel
@@ -430,7 +431,11 @@ def _create_collection(self, model):
430431
db.create_collection(model._meta.db_table)
431432
else:
432433
client = self.connection.connection
433-
ce = get_client_encryption(client)
434+
ce = get_client_encryption(
435+
client,
436+
key_vault_namespace=settings.KEY_VAULT_NAMESPACE,
437+
kms_providers=settings.KMS_PROVIDERS,
438+
)
434439
ce.create_encrypted_collection(
435440
db,
436441
model._meta.db_table,

0 commit comments

Comments
 (0)