Skip to content

Commit 68d22b2

Browse files
authored
PYTHON-4151 Use Centralized Scripts for CSFLE Tests (#1477)
1 parent 85b0c0e commit 68d22b2

12 files changed

+83
-142
lines changed

.evergreen/config.yml

+3-19
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,21 @@ functions:
369369
params:
370370
working_dir: "src"
371371
shell: bash
372-
background: true
372+
include_expansions_in_env: ["test_encryption", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"]
373373
script: |
374374
${PREPARE_SHELL}
375375
if [ -n "${test_encryption}" ]; then
376-
bash ./.evergreen/start-kmip-server.sh
376+
. .evergreen/tox.sh -m setup-encryption
377377
fi
378378
- command: shell.exec
379379
type: test
380380
params:
381381
working_dir: "src"
382382
shell: bash
383-
include_expansions_in_env: ["test_encryption", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"]
384383
script: |
385384
# Disable xtrace
386385
set +x
387386
${PREPARE_SHELL}
388-
# Fetch vault secrets if needed
389-
bash .evergreen/fetch-aws-vault-secrets.sh
390387
if [ -n "${MONGODB_STARTED}" ]; then
391388
export PYMONGO_MUST_CONNECT=true
392389
fi
@@ -396,9 +393,7 @@ functions:
396393
if [ -n "${test_encryption}" ]; then
397394
# Disable xtrace (just in case it was accidentally set).
398395
set +x
399-
export LIBMONGOCRYPT_URL="${libmongocrypt_url}"
400396
export TEST_ENCRYPTION=1
401-
. .evergreen/setup-encryption-variables.sh
402397
if [ -n "${test_encryption_pyopenssl}" ]; then
403398
export TEST_ENCRYPTION_PYOPENSSL=1
404399
fi
@@ -619,7 +614,7 @@ functions:
619614
working_dir: "src"
620615
script: |
621616
${PREPARE_SHELL}
622-
. .evergreen/stop-kmip-server.sh
617+
. .evergreen/tox.sh -m teardown-encryption
623618
rm -rf $DRIVERS_TOOLS || true
624619
rm -f ./secrets-export.sh || true
625620
@@ -2164,41 +2159,32 @@ axes:
21642159
skip_EC2_auth_test: true
21652160
skip_ECS_auth_test: true
21662161
skip_web_identity_auth_test: true
2167-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/macos/master/latest/libmongocrypt.tar.gz
21682162
- id: macos-1100
21692163
display_name: "macOS 11.00"
21702164
run_on: macos-1100
21712165
variables:
21722166
skip_EC2_auth_test: true
21732167
skip_ECS_auth_test: true
21742168
skip_web_identity_auth_test: true
2175-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/macos/master/latest/libmongocrypt.tar.gz
21762169
- id: macos-1100-arm64
21772170
display_name: "macOS 11.00 Arm64"
21782171
run_on: macos-1100-arm64
21792172
variables:
21802173
skip_EC2_auth_test: true
21812174
skip_ECS_auth_test: true
21822175
skip_web_identity_auth_test: true
2183-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/macos/master/latest/libmongocrypt.tar.gz
21842176
- id: rhel7
21852177
display_name: "RHEL 7.x"
21862178
run_on: rhel79-small
21872179
batchtime: 10080 # 7 days
2188-
variables:
2189-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/rhel-70-64-bit/master/latest/libmongocrypt.tar.gz
21902180
- id: rhel8
21912181
display_name: "RHEL 8.x"
21922182
run_on: rhel87-small
21932183
batchtime: 10080 # 7 days
2194-
variables:
2195-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/rhel-80-64-bit/master/latest/libmongocrypt.tar.gz
21962184
- id: rhel80-fips
21972185
display_name: "RHEL 8.0 FIPS"
21982186
run_on: rhel80-fips
21992187
batchtime: 10080 # 7 days
2200-
variables:
2201-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/rhel-80-64-bit/master/latest/libmongocrypt.tar.gz
22022188
- id: ubuntu-22.04
22032189
display_name: "Ubuntu 22.04"
22042190
run_on: ubuntu2204-small
@@ -2220,7 +2206,6 @@ axes:
22202206
run_on: rhel82-arm64-small
22212207
batchtime: 10080 # 7 days
22222208
variables:
2223-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/rhel-82-arm64/master/latest/libmongocrypt.tar.gz
22242209
- id: windows-64-vsMulti-small
22252210
display_name: "Windows 64"
22262211
run_on: windows-64-vsMulti-small
@@ -2230,7 +2215,6 @@ axes:
22302215
skip_EC2_auth_test: true
22312216
skip_web_identity_auth_test: true
22322217
venv_bin_dir: "Scripts"
2233-
libmongocrypt_url: https://s3.amazonaws.com/mciuploads/libmongocrypt/windows-test/master/latest/libmongocrypt.tar.gz
22342218

22352219
# Test with authentication?
22362220
- id: auth

.evergreen/fetch-aws-vault-secrets.sh

-11
This file was deleted.

.evergreen/run-tests.sh

+3-42
Original file line numberDiff line numberDiff line change
@@ -111,32 +111,13 @@ fi
111111

112112
if [ -n "$TEST_ENCRYPTION" ] || [ -n "$TEST_FLE_AZURE_AUTO" ] || [ -n "$TEST_FLE_GCP_AUTO" ]; then
113113

114-
# Work around for root certifi not being installed.
115-
# TODO: Remove after PYTHON-3952 is deployed.
116-
if [ "$(uname -s)" = "Darwin" ]; then
117-
python -m pip install certifi
118-
CERT_PATH=$(python -c "import certifi; print(certifi.where())")
119-
export SSL_CERT_FILE=${CERT_PATH}
120-
export REQUESTS_CA_BUNDLE=${CERT_PATH}
121-
export AWS_CA_BUNDLE=${CERT_PATH}
122-
fi
123-
124114
python -m pip install '.[encryption]'
125115

126-
if [ "Windows_NT" = "$OS" ]; then # Magic variable in cygwin
127-
# PYTHON-2808 Ensure this machine has the CA cert for google KMS.
128-
powershell.exe "Invoke-WebRequest -URI https://oauth2.googleapis.com/" > /dev/null || true
116+
# Install libmongocrypt if necessary.
117+
if [ ! -d "libmongocrypt" ]; then
118+
bash ./.evergreen/setup-libmongocrypt.sh
129119
fi
130120

131-
if [ -z "$LIBMONGOCRYPT_URL" ]; then
132-
echo "Cannot test client side encryption without LIBMONGOCRYPT_URL!"
133-
exit 1
134-
fi
135-
curl -O "$LIBMONGOCRYPT_URL"
136-
mkdir libmongocrypt
137-
tar xzf libmongocrypt.tar.gz -C ./libmongocrypt
138-
ls -la libmongocrypt
139-
ls -la libmongocrypt/nocrypto
140121
# Use the nocrypto build to avoid dependency issues with older windows/python versions.
141122
BASE=$(pwd)/libmongocrypt/nocrypto
142123
if [ -f "${BASE}/lib/libmongocrypt.so" ]; then
@@ -155,11 +136,6 @@ if [ -n "$TEST_ENCRYPTION" ] || [ -n "$TEST_FLE_AZURE_AUTO" ] || [ -n "$TEST_FLE
155136
exit 1
156137
fi
157138
export PYMONGOCRYPT_LIB
158-
159-
# TODO: Test with 'pip install pymongocrypt'
160-
git clone https://github.com/mongodb/libmongocrypt.git libmongocrypt_git
161-
python -m pip install --prefer-binary -r .evergreen/test-encryption-requirements.txt
162-
python -m pip install ./libmongocrypt_git/bindings/python
163139
python -c "import pymongocrypt; print('pymongocrypt version: '+pymongocrypt.__version__)"
164140
python -c "import pymongocrypt; print('libmongocrypt version: '+pymongocrypt.libmongocrypt_version())"
165141
# PATH is updated by PREPARE_SHELL for access to mongocryptd.
@@ -170,11 +146,6 @@ if [ -n "$TEST_ENCRYPTION" ]; then
170146
python -m pip install '.[ocsp]'
171147
fi
172148

173-
# Get access to the AWS temporary credentials:
174-
# CSFLE_AWS_TEMP_ACCESS_KEY_ID, CSFLE_AWS_TEMP_SECRET_ACCESS_KEY, CSFLE_AWS_TEMP_SESSION_TOKEN
175-
export AWS_SESSION_TOKEN=
176-
. $DRIVERS_TOOLS/.evergreen/csfle/set-temp-creds.sh
177-
178149
if [ -n "$TEST_CRYPT_SHARED" ]; then
179150
CRYPT_SHARED_DIR=`dirname $CRYPT_SHARED_LIB_PATH`
180151
echo "using crypt_shared_dir $CRYPT_SHARED_DIR"
@@ -229,16 +200,6 @@ fi
229200
if [ -n "$TEST_AUTH_OIDC" ]; then
230201
python -m pip install ".[aws]"
231202

232-
# Work around for root certifi not being installed.
233-
# TODO: Remove after PYTHON-3952 is deployed.
234-
if [ "$(uname -s)" = "Darwin" ]; then
235-
python -m pip install certifi
236-
CERT_PATH=$(python -c "import certifi; print(certifi.where())")
237-
export SSL_CERT_FILE=${CERT_PATH}
238-
export REQUESTS_CA_BUNDLE=${CERT_PATH}
239-
export AWS_CA_BUNDLE=${CERT_PATH}
240-
fi
241-
242203
TEST_ARGS="test/auth_oidc/test_auth_oidc.py"
243204
fi
244205

.evergreen/setup-encryption-variables.sh

-10
This file was deleted.

.evergreen/setup-libmongocrypt.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
set -o errexit # Exit the script with error if any of the commands fail
3+
set -o xtrace
4+
5+
TARGET=""
6+
7+
if [ "Windows_NT" = "${OS:-''}" ]; then # Magic variable in cygwin
8+
# PYTHON-2808 Ensure this machine has the CA cert for google KMS.
9+
powershell.exe "Invoke-WebRequest -URI https://oauth2.googleapis.com/" > /dev/null || true
10+
TARGET="windows-test"
11+
fi
12+
13+
if [ "$(uname -s)" = "Darwin" ]; then
14+
TARGET="macos"
15+
fi
16+
17+
if [ "$(uname -s)" = "Linux" ]; then
18+
rhel_ver=$(awk -F'=' '/VERSION_ID/{ gsub(/"/,""); print $2}' /etc/os-release)
19+
arch=$(uname -m)
20+
echo "RHEL $rhel_ver $arch"
21+
if [[ $rhel_ver =~ 7 ]]; then
22+
TARGET="rhel-70-64-bit"
23+
elif [[ $rhel_ver =~ 8 ]]; then
24+
if [ "$arch" = "x86_64" ]; then
25+
TARGET="rhel-80-64-bit"
26+
elif [ "$arch" = "arm" ]; then
27+
TARGET="rhel-82-arm64"
28+
fi
29+
fi
30+
fi
31+
32+
if [ -z "$LIBMONGOCRYPT_URL" ] && [ -n "$TARGET" ]; then
33+
LIBMONGOCRYPT_URL="https://s3.amazonaws.com/mciuploads/libmongocrypt/$TARGET/master/latest/libmongocrypt.tar.gz"
34+
fi
35+
36+
if [ -z "$LIBMONGOCRYPT_URL" ]; then
37+
echo "Cannot test client side encryption without LIBMONGOCRYPT_URL!"
38+
exit 1
39+
fi
40+
rm -rf libmongocrypt libmongocrypt.tar.gz
41+
echo "Fetching $LIBMONGOCRYPT_URL..."
42+
curl -O "$LIBMONGOCRYPT_URL"
43+
echo "Fetching $LIBMONGOCRYPT_URL...done"
44+
mkdir libmongocrypt
45+
tar xzf libmongocrypt.tar.gz -C ./libmongocrypt
46+
ls -la libmongocrypt
47+
ls -la libmongocrypt/nocrypto

.evergreen/start-kmip-server.sh

-23
This file was deleted.

.evergreen/stop-kmip-server.sh

-9
This file was deleted.

.evergreen/test-encryption-requirements.txt

-3
This file was deleted.

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ mongocryptd.pid
2020
.nova/
2121
venv/
2222
secrets-export.sh
23+
libmongocrypt.tar.gz
24+
libmongocrypt/
25+
libmongocrypt_git/
2326

2427
# Lambda temp files
2528
test/lambda/.aws-sam

CONTRIBUTING.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,9 @@ the pages will re-render and the browser will automatically refresh.
216216
`TEST_LOADBALANCER=1 tox -m test-eg`.
217217

218218
## Running Encryption Tests Locally
219-
- Run `AWS_PROFILE=<profile> tox -e setup-encryption` after setting up your AWS profile with `aws configure sso`
220-
- Run the tests with `TEST_ENCRYPTION=1 LIBMONGOCRYPT_URL=<url> tox -e test-eg`.
221-
Look in `.evergreen/config.yml` for the correct `LIBMONGOCRYPT_URL` for your platform.
222-
- When done, run `tox -e teardown-encryption` to clean up.
219+
- Run `AWS_PROFILE=<profile> tox -m setup-encryption` after setting up your AWS profile with `aws configure sso`.
220+
- Run the tests with `TEST_ENCRYPTION=1 tox -e test-eg`.
221+
- When done, run `tox -m teardown-encryption` to clean up.
223222

224223
## Re-sync Spec Tests
225224

test/test_encryption.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,7 @@ def test_invalid_kms_certificate_expired(self):
20072007
key = {
20082008
"region": "us-east-1",
20092009
"key": "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0",
2010-
"endpoint": "mongodb://127.0.0.1:8000",
2010+
"endpoint": "mongodb://127.0.0.1:9000",
20112011
}
20122012
# Some examples:
20132013
# certificate verify failed: certificate has expired (_ssl.c:1129)
@@ -2019,7 +2019,7 @@ def test_invalid_hostname_in_kms_certificate(self):
20192019
key = {
20202020
"region": "us-east-1",
20212021
"key": "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0",
2022-
"endpoint": "mongodb://127.0.0.1:8001",
2022+
"endpoint": "mongodb://127.0.0.1:9001",
20232023
}
20242024
# Some examples:
20252025
# certificate verify failed: IP address mismatch, certificate is not valid for '127.0.0.1'. (_ssl.c:1129)"
@@ -2038,8 +2038,8 @@ def setUp(self):
20382038
super().setUp()
20392039
# 1, create client with only tlsCAFile.
20402040
providers: dict = copy.deepcopy(ALL_KMS_PROVIDERS)
2041-
providers["azure"]["identityPlatformEndpoint"] = "127.0.0.1:8002"
2042-
providers["gcp"]["endpoint"] = "127.0.0.1:8002"
2041+
providers["azure"]["identityPlatformEndpoint"] = "127.0.0.1:9002"
2042+
providers["gcp"]["endpoint"] = "127.0.0.1:9002"
20432043
kms_tls_opts_ca_only = {
20442044
"aws": {"tlsCAFile": CA_PEM},
20452045
"azure": {"tlsCAFile": CA_PEM},
@@ -2060,18 +2060,18 @@ def setUp(self):
20602060
self.addCleanup(self.client_encryption_with_tls.close)
20612061
# 3, update endpoints to expired host.
20622062
providers: dict = copy.deepcopy(providers)
2063-
providers["azure"]["identityPlatformEndpoint"] = "127.0.0.1:8000"
2064-
providers["gcp"]["endpoint"] = "127.0.0.1:8000"
2065-
providers["kmip"]["endpoint"] = "127.0.0.1:8000"
2063+
providers["azure"]["identityPlatformEndpoint"] = "127.0.0.1:9000"
2064+
providers["gcp"]["endpoint"] = "127.0.0.1:9000"
2065+
providers["kmip"]["endpoint"] = "127.0.0.1:9000"
20662066
self.client_encryption_expired = ClientEncryption(
20672067
providers, "keyvault.datakeys", self.client, OPTS, kms_tls_options=kms_tls_opts_ca_only
20682068
)
20692069
self.addCleanup(self.client_encryption_expired.close)
20702070
# 3, update endpoints to invalid host.
20712071
providers: dict = copy.deepcopy(providers)
2072-
providers["azure"]["identityPlatformEndpoint"] = "127.0.0.1:8001"
2073-
providers["gcp"]["endpoint"] = "127.0.0.1:8001"
2074-
providers["kmip"]["endpoint"] = "127.0.0.1:8001"
2072+
providers["azure"]["identityPlatformEndpoint"] = "127.0.0.1:9001"
2073+
providers["gcp"]["endpoint"] = "127.0.0.1:9001"
2074+
providers["kmip"]["endpoint"] = "127.0.0.1:9001"
20752075
self.client_encryption_invalid_hostname = ClientEncryption(
20762076
providers, "keyvault.datakeys", self.client, OPTS, kms_tls_options=kms_tls_opts_ca_only
20772077
)
@@ -2095,7 +2095,7 @@ def test_01_aws(self):
20952095
key = {
20962096
"region": "us-east-1",
20972097
"key": "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0",
2098-
"endpoint": "127.0.0.1:8002",
2098+
"endpoint": "127.0.0.1:9002",
20992099
}
21002100
with self.assertRaisesRegex(EncryptionError, self.cert_error):
21012101
self.client_encryption_no_client_cert.create_data_key("aws", key)
@@ -2105,14 +2105,14 @@ def test_01_aws(self):
21052105
# Some examples:
21062106
# certificate verify failed: certificate has expired (_ssl.c:1129)
21072107
# amazon1-2018 Python 3.6: certificate verify failed (_ssl.c:852)
2108-
key["endpoint"] = "127.0.0.1:8000"
2108+
key["endpoint"] = "127.0.0.1:9000"
21092109
with self.assertRaisesRegex(EncryptionError, "expired|certificate verify failed"):
21102110
self.client_encryption_expired.create_data_key("aws", key)
21112111
# Some examples:
21122112
# certificate verify failed: IP address mismatch, certificate is not valid for '127.0.0.1'. (_ssl.c:1129)"
21132113
# hostname '127.0.0.1' doesn't match 'wronghost.com'
21142114
# 127.0.0.1:8001: ('Certificate does not contain any `subjectAltName`s.',)
2115-
key["endpoint"] = "127.0.0.1:8001"
2115+
key["endpoint"] = "127.0.0.1:9001"
21162116
with self.assertRaisesRegex(
21172117
EncryptionError, "IP address mismatch|wronghost|IPAddressMismatch|Certificate"
21182118
):

0 commit comments

Comments
 (0)