Skip to content

Commit 0393c81

Browse files
committed
Address review
- avoid encryption tasks on non-encrypted builds - retrieve configured provider from conn settings - fail loudly & avoid uppercased values in settings
1 parent 964c20c commit 0393c81

File tree

7 files changed

+76
-52
lines changed

7 files changed

+76
-52
lines changed

.evergreen/config.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ functions:
5959
args:
6060
- ./.evergreen/run-tests.sh
6161

62+
"run encryption tests":
63+
- command: subprocess.exec
64+
type: test
65+
params:
66+
binary: bash
67+
working_dir: "src"
68+
include_expansions_in_env: ["DRIVERS_TOOLS", "MONGODB_URI", "DJANGO_SETTINGS_MODULE", "CRYPT_SHARED_LIB_PATH"]
69+
args:
70+
- ./.evergreen/run-tests.sh
71+
- encryption
72+
6273
"teardown":
6374
- command: subprocess.exec
6475
params:
@@ -80,6 +91,10 @@ tasks:
8091
commands:
8192
- func: "run unit tests"
8293

94+
- name: run-encryption-tests
95+
commands:
96+
- func: "run encryption tests"
97+
8398
buildvariants:
8499
- name: tests-6-noauth-nossl
85100
display_name: Run Tests 6.0 NoAuth NoSSL
@@ -125,12 +140,22 @@ buildvariants:
125140
tasks:
126141
- name: run-tests
127142

128-
- name: tests-8-qe
129-
display_name: Run Tests 8.2 QE
143+
- name: tests-8-qe-local
144+
display_name: Run Tests 8.2 QE local KMS
130145
run_on: rhel87-small
131146
expansions:
132147
MONGODB_VERSION: "8.2"
133148
TOPOLOGY: replica_set
134-
DJANGO_SETTINGS_MODULE: "encrypted_settings"
149+
DJANGO_SETTINGS_MODULE: "local_kms_encrypted_settings"
135150
tasks:
136-
- name: run-tests
151+
- name: run-encryption-tests
152+
153+
- name: tests-8-qe-aws
154+
display_name: Run Tests 8.2 QE aws KMS
155+
run_on: rhel87-small
156+
expansions:
157+
MONGODB_VERSION: "8.2"
158+
TOPOLOGY: replica_set
159+
DJANGO_SETTINGS_MODULE: "aws_kms_encrypted_settings"
160+
tasks:
161+
- name: run-encryption-tests

.evergreen/run-tests.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
set -eux
44

55
# Export secrets as environment variables
6-
. ../secrets-export.sh
6+
if [[ "${1:-}" == "encryption" ]]; then
7+
. ../secrets-export.sh
8+
fi
79

8-
# Install django-mongodb-backend
10+
# Set up virtual environment
911
/opt/python/3.10/bin/python3 -m venv venv
1012
. venv/bin/activate
1113
python -m pip install -U pip
12-
pip install -e '.[encryption]'
14+
15+
# Conditionally install encryption extra if "encryption" arg is passed
16+
if [[ "${1:-}" == "encryption" ]]; then
17+
pip install -e '.[encryption]'
18+
else
19+
pip install -e .
20+
fi
1321

1422
# Install django and test dependencies
1523
git clone --branch mongodb-5.2.x https://github.com/mongodb-forks/django django_repo
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from local_kms_encrypted_settings import * # noqa: F403
2+
3+
DATABASES["encrypted"] = { # noqa: F405
4+
"ENGINE": "django_mongodb_backend",
5+
"NAME": "djangotests_encrypted",
6+
"OPTIONS": {
7+
"auto_encryption_opts": AutoEncryptionOpts( # noqa: F405
8+
key_vault_namespace="djangotests_encrypted.__keyVault",
9+
kms_providers={
10+
"aws": {
11+
"accessKeyId": os.environ.get("FLE_AWS_KEY"), # noqa: F405
12+
"secretAccessKey": os.environ.get("FLE_AWS_SECRET"), # noqa: F405
13+
}
14+
},
15+
crypt_shared_lib_path=os.environ["CRYPT_SHARED_LIB_PATH"], # noqa: F405
16+
crypt_shared_lib_required=True,
17+
),
18+
"directConnection": True,
19+
},
20+
"KMS_CREDENTIALS": {
21+
"aws": {
22+
"key": "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0",
23+
"region": "us-east-1",
24+
}
25+
},
26+
}

.github/workflows/encrypted_settings.py renamed to .github/workflows/local_kms_encrypted_settings.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,18 @@
77

88
os.environ["LD_LIBRARY_PATH"] = str(Path(os.environ["CRYPT_SHARED_LIB_PATH"]).parent)
99

10-
AWS_CREDS = {
11-
"accessKeyId": os.environ.get("FLE_AWS_KEY", ""),
12-
"secretAccessKey": os.environ.get("FLE_AWS_SECRET", ""),
13-
}
14-
15-
_USE_AWS_KMS = any(AWS_CREDS.values())
16-
17-
if _USE_AWS_KMS:
18-
_AWS_REGION = os.environ.get("FLE_AWS_KMS_REGION", "us-east-1")
19-
_AWS_KEY_ARN = os.environ.get(
20-
"FLE_AWS_KMS_KEY_ARN",
21-
"arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0",
22-
)
23-
KMS_PROVIDERS = {"aws": AWS_CREDS}
24-
KMS_CREDENTIALS = {"aws": {"key": _AWS_KEY_ARN, "region": _AWS_REGION}}
25-
else:
26-
KMS_PROVIDERS = {"local": {"key": os.urandom(96)}}
27-
KMS_CREDENTIALS = {"local": {}}
28-
2910
DATABASES["encrypted"] = { # noqa: F405
3011
"ENGINE": "django_mongodb_backend",
3112
"NAME": "djangotests_encrypted",
3213
"OPTIONS": {
3314
"auto_encryption_opts": AutoEncryptionOpts(
3415
key_vault_namespace="djangotests_encrypted.__keyVault",
35-
kms_providers=KMS_PROVIDERS,
16+
kms_providers={"local": {"key": os.urandom(96)}},
3617
crypt_shared_lib_path=os.environ["CRYPT_SHARED_LIB_PATH"],
37-
crypt_shared_lib_required=True,
3818
),
3919
"directConnection": True,
4020
},
41-
"KMS_CREDENTIALS": KMS_CREDENTIALS,
21+
"KMS_CREDENTIALS": {"local": {}},
4222
}
4323

4424

.github/workflows/test-python-atlas.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ jobs:
6161
permissions:
6262
contents: read
6363
env:
64-
DJANGO_SETTINGS_MODULE: "encrypted_settings"
64+
DJANGO_SETTINGS_MODULE: "local_kms_encrypted_settings"
6565
CRYPT_SHARED_LIB_PATH: "${{ github.workspace }}/lib/mongo_crypt_v1.so"

tests/encryption_/test_base.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
import pymongo
42
from bson.binary import Binary
53
from django.conf import settings
@@ -21,18 +19,3 @@ def assertEncrypted(self, model, field):
2119
collection = db[model._meta.db_table]
2220
data = collection.find_one({}, {field: 1, "_id": 0})
2321
self.assertIsInstance(data[field], Binary)
24-
25-
def __init__(self, *args, **kwargs):
26-
super().__init__(*args, **kwargs)
27-
28-
AWS_CREDS = {
29-
"accessKeyId": os.environ.get("FLE_AWS_KEY", ""),
30-
"secretAccessKey": os.environ.get("FLE_AWS_SECRET", ""),
31-
}
32-
_USE_AWS_KMS = any(AWS_CREDS.values())
33-
34-
if _USE_AWS_KMS:
35-
self.DEFAULT_KMS_PROVIDER = "aws"
36-
else:
37-
# Local-only fallback
38-
self.DEFAULT_KMS_PROVIDER = "local"

tests/encryption_/test_management.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def test_show_encrypted_fields_map(self):
114114
self._compare_output(expected, command_output[model_key])
115115

116116
def test_missing_key(self):
117+
connection = connections["encrypted"]
118+
auto_encryption_opts = connection.connection._options.auto_encryption_opts
119+
kms_providers = auto_encryption_opts._kms_providers
117120
test_key = "encryption__patient.patient_record.ssn"
118121
msg = (
119122
f"Encryption key {test_key} not found. Have migrated the "
@@ -125,11 +128,10 @@ def test_missing_key(self):
125128
call_command("showencryptedfieldsmap", "--database", "encrypted", verbosity=0)
126129
finally:
127130
# Replace the deleted key.
128-
master_key = connections["encrypted"].settings_dict["KMS_CREDENTIALS"][
129-
self.DEFAULT_KMS_PROVIDER
130-
]
131-
connections["encrypted"].client_encryption.create_data_key(
132-
kms_provider=self.DEFAULT_KMS_PROVIDER,
131+
kms_provider = next(iter(kms_providers.keys()))
132+
master_key = connection.settings_dict["KMS_CREDENTIALS"][kms_provider]
133+
connection.client_encryption.create_data_key(
134+
kms_provider=kms_provider,
133135
master_key=master_key,
134136
key_alt_names=[test_key],
135137
)

0 commit comments

Comments
 (0)