Skip to content

Commit ae765eb

Browse files
committed
Update test_create_new_keys to test production flow
We need to ensure that the "production flow" for QE works as expected which includes: - Run `showencryptedfieldsmap --create-new-keys` for new encrypted_fields_map. - Update client settings to include new encrypted_fields_map. - Check the key vault to make sure the keys in the map match the keys in the key vault.
1 parent f5c34a2 commit ae765eb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

tests/encryption_/test_management.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import os
12
from io import StringIO
23

4+
import pymongo
35
from bson import json_util
46
from django.core.management import call_command
7+
from django.db import connections
58
from django.test import TransactionTestCase, modify_settings, override_settings
9+
from pymongo.encryption import AutoEncryptionOpts
610

711
from .routers import TestEncryptedRouter
812

@@ -83,5 +87,20 @@ def test_create_new_keys(self):
8387
command_output["encryption__patientrecord"],
8488
)
8589

86-
# TODO: Create a new connection to verify that the keys can be used
87-
# in a client-side configuration to migrate the encrypted fields.
90+
# Create a new connection to verify that the keys can be used in a
91+
# client-side configuration to migrate the encrypted fields.
92+
conn_params = connections["encrypted"].get_connection_params()
93+
auto_encryption_opts = AutoEncryptionOpts(
94+
key_vault_namespace="encryption.__keyvault",
95+
kms_providers={"local": {"key": os.urandom(96)}},
96+
encrypted_fields_map=command_output,
97+
)
98+
if conn_params.pop("auto_encryption_opts", False):
99+
# Call MongoClient instead of get_new_connection because
100+
# get_new_connection will return the encrypted connection from the
101+
# connection pool.
102+
with pymongo.MongoClient(**conn_params, auto_encryption_opts=auto_encryption_opts):
103+
call_command("migrate", "--database", "encrypted", verbosity=0)
104+
105+
# TODO: Check the key vault to ensure that the keys created by
106+
# `showencryptedfieldsmap --create-new-keys` are in the key vault.

0 commit comments

Comments
 (0)