Skip to content

Commit 76200b1

Browse files
committed
Configure AWS KMS for testing on evergreen
Fall back to local if no env vars set
1 parent c760b5a commit 76200b1

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

.evergreen/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ buildvariants:
126126
- name: run-tests
127127

128128
- name: tests-8-qe
129-
display_name: Run Tests 8.0 QE
130-
run_on: rhel94-perf-atlas
129+
display_name: Run Tests 8.2 QE
130+
run_on: rhel87-small
131131
expansions:
132132
MONGODB_VERSION: "8.2"
133133
TOPOLOGY: replica_set

.evergreen/run-tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
set -eux
44

5+
# Export secrets as environment variables
6+
. ../secrets-export.sh
7+
58
# Install django-mongodb-backend
69
/opt/python/3.10/bin/python3 -m venv venv
710
. venv/bin/activate

.evergreen/setup.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ DRIVERS_TOOLS="$(dirname "$(pwd)")/drivers-tools"
1616
PROJECT_DIRECTORY="$(pwd)"
1717

1818
if [ "Windows_NT" = "${OS:-}" ]; then
19-
DRIVERS_TOOLS=$(cygpath -m $DRIVERS_TOOLS)
20-
PROJECT_DIRECTORY=$(cygpath -m $PROJECT_DIRECTORY)
19+
DRIVERS_TOOLS=$(cygpath -m "$DRIVERS_TOOLS")
20+
PROJECT_DIRECTORY=$(cygpath -m "$PROJECT_DIRECTORY")
2121
fi
2222
export PROJECT_DIRECTORY
2323
export DRIVERS_TOOLS
@@ -37,8 +37,8 @@ PROJECT_DIRECTORY: "$PROJECT_DIRECTORY"
3737
EOT
3838

3939
# Set up drivers-tools with a .env file.
40-
git clone https://github.com/mongodb-labs/drivers-evergreen-tools.git ${DRIVERS_TOOLS}
41-
cat <<EOT > ${DRIVERS_TOOLS}/.env
40+
git clone https://github.com/mongodb-labs/drivers-evergreen-tools.git "${DRIVERS_TOOLS}"
41+
cat <<EOT > "${DRIVERS_TOOLS}/.env"
4242
CURRENT_VERSION="$CURRENT_VERSION"
4343
DRIVERS_TOOLS="$DRIVERS_TOOLS"
4444
MONGO_ORCHESTRATION_HOME="$MONGO_ORCHESTRATION_HOME"

.github/workflows/encrypted_settings.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,38 @@
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+
1029
DATABASES["encrypted"] = { # noqa: F405
1130
"ENGINE": "django_mongodb_backend",
1231
"NAME": "djangotests_encrypted",
1332
"OPTIONS": {
1433
"auto_encryption_opts": AutoEncryptionOpts(
1534
key_vault_namespace="djangotests_encrypted.__keyVault",
16-
kms_providers={"local": {"key": os.urandom(96)}},
35+
kms_providers=KMS_PROVIDERS,
1736
crypt_shared_lib_path=os.environ["CRYPT_SHARED_LIB_PATH"],
37+
crypt_shared_lib_required=True,
1838
),
1939
"directConnection": True,
2040
},
21-
"KMS_CREDENTIALS": {},
41+
"KMS_CREDENTIALS": KMS_CREDENTIALS,
2242
}
2343

2444

tests/encryption_/test_base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pymongo
24
from bson.binary import Binary
35
from django.conf import settings
@@ -19,3 +21,18 @@ def assertEncrypted(self, model, field):
1921
collection = db[model._meta.db_table]
2022
data = collection.find_one({}, {field: 1, "_id": 0})
2123
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ def test_missing_key(self):
125125
call_command("showencryptedfieldsmap", "--database", "encrypted", verbosity=0)
126126
finally:
127127
# Replace the deleted key.
128+
master_key = connections["encrypted"].settings_dict["KMS_CREDENTIALS"][
129+
self.DEFAULT_KMS_PROVIDER
130+
]
128131
connections["encrypted"].client_encryption.create_data_key(
129-
kms_provider="local",
132+
kms_provider=self.DEFAULT_KMS_PROVIDER,
133+
master_key=master_key,
130134
key_alt_names=[test_key],
131135
)

0 commit comments

Comments
 (0)