Skip to content

Commit 23f20a4

Browse files
committed
Add partial index on keyAltNames for uniqueness
Also put the key vault inside the test database for easy tear down, tested and working local.
1 parent ff84902 commit 23f20a4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

.github/workflows/encrypted_settings.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
DATABASES["encrypted"] = { # noqa: F405
77
"ENGINE": "django_mongodb_backend",
8-
"NAME": "djangotests-encrypted",
8+
"NAME": "djangotests_encrypted",
99
"OPTIONS": {
1010
"auto_encryption_opts": AutoEncryptionOpts(
11-
key_vault_namespace="my_encrypted_database.keyvault",
11+
key_vault_namespace="test_djangotests_encrypted.__keyVault",
1212
kms_providers={"local": {"key": os.urandom(96)}},
13-
# crypt_shared_lib_path="lib/mongo_crypt_v1.so",
1413
),
1514
"directConnection": True,
1615
},

django_mongodb_backend/schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ def _get_encrypted_fields(self, model, key_alt_name=None, path_prefix=None):
505505
key_vault_db, key_vault_coll = auto_encryption_opts._key_vault_namespace.split(".", 1)
506506
key_vault_collection = client[key_vault_db][key_vault_coll]
507507

508+
# Create partial unique index on keyAltNames
509+
key_vault_collection.create_index(
510+
"keyAltNames", unique=True, partialFilterExpression={"keyAltNames": {"$exists": True}}
511+
)
512+
508513
kms_provider = router.kms_provider(model)
509514
master_key = connection.settings_dict.get("KMS_CREDENTIALS", {}).get(kms_provider)
510515
client_encryption = self.connection.client_encryption

0 commit comments

Comments
 (0)