Skip to content

Commit 8c293d2

Browse files
authored
PYTHON-3963 Migrate CSFLE Tests to AWS Secrets Vault (#1446)
1 parent 1412afe commit 8c293d2

File tree

8 files changed

+98
-60
lines changed

8 files changed

+98
-60
lines changed

.evergreen/config.yml

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -365,78 +365,28 @@ functions:
365365
PYTHON_BINARY=${PYTHON_BINARY} bash ${PROJECT_DIRECTORY}/.evergreen/tox.sh -m doc-test
366366
367367
"run tests":
368-
# If testing FLE, start the KMS mock servers, first create the virtualenv.
369368
- command: shell.exec
370369
params:
371-
script: |
372-
if [ -n "${test_encryption}" ]; then
373-
${PREPARE_SHELL}
374-
cd ${DRIVERS_TOOLS}/.evergreen/csfle
375-
. ./activate-kmstlsvenv.sh
376-
fi
377-
# Run in the background so the mock servers don't block the EVG task.
378-
- command: shell.exec
379-
params:
380-
background: true
381-
script: |
382-
if [ -n "${test_encryption}" ]; then
383-
${PREPARE_SHELL}
384-
cd ${DRIVERS_TOOLS}/.evergreen/csfle
385-
. ./activate-kmstlsvenv.sh
386-
# The -u options forces the stdout and stderr streams to be unbuffered.
387-
# TMPDIR is required to avoid "AF_UNIX path too long" errors.
388-
TMPDIR="$(dirname $DRIVERS_TOOLS)" python -u kms_kmip_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/server.pem --port 5698 &
389-
python -u kms_http_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/expired.pem --port 8000 &
390-
python -u kms_http_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/wrong-host.pem --port 8001 &
391-
python -u kms_http_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/server.pem --port 8002 --require_client_cert &
392-
fi
393-
# Wait up to 10 seconds for the KMIP server to start.
394-
- command: shell.exec
395-
params:
396-
script: |
397-
if [ -n "${test_encryption}" ]; then
398-
${PREPARE_SHELL}
399-
cd ${DRIVERS_TOOLS}/.evergreen/csfle
400-
. ./activate-kmstlsvenv.sh
401-
for i in $(seq 1 1 10); do
402-
sleep 1
403-
if python -u kms_kmip_client.py; then
404-
echo 'KMS KMIP server started!'
405-
exit 0
406-
fi
407-
done
408-
echo 'Failed to start KMIP server!'
409-
exit 1
410-
fi
411-
- command: shell.exec
412-
type: test
413-
params:
414-
silent: true
415370
working_dir: "src"
371+
shell: bash
372+
background: true
416373
script: |
374+
${PREPARE_SHELL}
417375
if [ -n "${test_encryption}" ]; then
418-
cat <<EOT > fle_creds.sh
419-
export FLE_AWS_KEY="${fle_aws_key}"
420-
export FLE_AWS_SECRET="${fle_aws_secret}"
421-
export FLE_AZURE_CLIENTID="${fle_azure_clientid}"
422-
export FLE_AZURE_TENANTID="${fle_azure_tenantid}"
423-
export FLE_AZURE_CLIENTSECRET="${fle_azure_clientsecret}"
424-
export FLE_GCP_EMAIL="${fle_gcp_email}"
425-
export FLE_GCP_PRIVATEKEY="${fle_gcp_privatekey}"
426-
# Needed for generating temporary aws credentials.
427-
export AWS_ACCESS_KEY_ID="${fle_aws_key}"
428-
export AWS_SECRET_ACCESS_KEY="${fle_aws_secret}"
429-
export AWS_DEFAULT_REGION=us-east-1
430-
EOT
376+
bash ./.evergreen/start-kmip-server.sh
431377
fi
432378
- command: shell.exec
433379
type: test
434380
params:
435381
working_dir: "src"
382+
shell: bash
383+
include_expansions_in_env: ["test_encryption", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"]
436384
script: |
437385
# Disable xtrace
438386
set +x
439387
${PREPARE_SHELL}
388+
# Fetch vault secrets if needed
389+
bash .evergreen/fetch-aws-vault-secrets.sh
440390
if [ -n "${MONGODB_STARTED}" ]; then
441391
export PYMONGO_MUST_CONNECT=true
442392
fi
@@ -446,10 +396,9 @@ functions:
446396
if [ -n "${test_encryption}" ]; then
447397
# Disable xtrace (just in case it was accidentally set).
448398
set +x
449-
. ./fle_creds.sh
450-
rm -f ./fle_creds.sh
451399
export LIBMONGOCRYPT_URL="${libmongocrypt_url}"
452400
export TEST_ENCRYPTION=1
401+
. .evergreen/setup-encryption-variables.sh
453402
if [ -n "${test_encryption_pyopenssl}" ]; then
454403
export TEST_ENCRYPTION_PYOPENSSL=1
455404
fi
@@ -667,8 +616,10 @@ functions:
667616
"cleanup":
668617
- command: shell.exec
669618
params:
619+
working_dir: "src"
670620
script: |
671621
${PREPARE_SHELL}
622+
. .evergreen/stop-kmip-server.sh
672623
rm -rf $DRIVERS_TOOLS || true
673624
rm -f ./secrets-export.sh || true
674625
@@ -724,6 +675,11 @@ functions:
724675
# Don't use ${file} syntax here because evergreen treats it as an empty expansion.
725676
[ -f "$file" ] && bash $file || echo "$file not available, skipping"
726677
678+
"assume ec2 role":
679+
- command: ec2.assume_role
680+
params:
681+
role_arn: ${aws_test_secrets_role}
682+
727683
"run-ocsp-test":
728684
- command: shell.exec
729685
type: test
@@ -939,6 +895,7 @@ pre:
939895
- func: "init test-results"
940896
- func: "make files executable"
941897
- func: "install dependencies"
898+
- func: "assume ec2 role"
942899

943900
post:
944901
# Disabled, causing timeouts

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
AWS_SECRETS_VAULTS=""
4+
5+
if [ -n "${test_encryption}" ]; then
6+
AWS_SECRETS_VAULTS+="drivers/csfle"
7+
fi
8+
9+
if [ -n "${AWS_SECRETS_VAULTS}" ]; then
10+
"$DRIVERS_TOOLS"/.evergreen/auth_aws/setup_secrets.sh ${AWS_SECRETS_VAULTS}
11+
fi

.evergreen/run-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ if [ -n "$TEST_ENCRYPTION" ]; then
172172

173173
# Get access to the AWS temporary credentials:
174174
# CSFLE_AWS_TEMP_ACCESS_KEY_ID, CSFLE_AWS_TEMP_SECRET_ACCESS_KEY, CSFLE_AWS_TEMP_SESSION_TOKEN
175+
export AWS_SESSION_TOKEN=
175176
. $DRIVERS_TOOLS/.evergreen/csfle/set-temp-creds.sh
176177

177178
if [ -n "$TEST_CRYPT_SHARED" ]; then
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
source ./secrets-export.sh
4+
5+
{
6+
echo "export AWS_ACCESS_KEY_ID=$FLE_AWS_KEY";
7+
echo "export AWS_SECRET_ACCESS_KEY=$FLE_AWS_SECRET";
8+
echo "export AWS_DEFAULT_REGION=us-east-1";
9+
echo "export AWS_SESSION_TOKEN=";
10+
} >> ./secrets-export.sh

.evergreen/start-kmip-server.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
cd "$DRIVERS_TOOLS"/.evergreen/csfle || exit
4+
. ./activate-kmstlsvenv.sh
5+
6+
# The -u options forces the stdout and stderr streams to be unbuffered.
7+
# TMPDIR is required to avoid "AF_UNIX path too long" errors.
8+
TMPDIR="$(dirname "$DRIVERS_TOOLS")" python -u kms_kmip_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/server.pem --port 5698 &
9+
python -u kms_http_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/expired.pem --port 8000 &
10+
python -u kms_http_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/wrong-host.pem --port 8001 &
11+
python -u kms_http_server.py --ca_file ../x509gen/ca.pem --cert_file ../x509gen/server.pem --port 8002 --require_client_cert &
12+
13+
pgrep -f "kms_...._server.py" > kmip_pids.pid
14+
15+
for _ in $(seq 1 1 10); do
16+
sleep 1
17+
if python -u kms_kmip_client.py; then
18+
echo 'KMS KMIP server started!'
19+
exit 0
20+
fi
21+
done
22+
echo 'Failed to start KMIP server!'
23+
exit 1

.evergreen/stop-kmip-server.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# Clean up CSFLE kmip servers
4+
cd "$DRIVERS_TOOLS"/.evergreen/csfle || exit
5+
6+
if [ -f "kmip_pids.pid" ]; then
7+
< kmip_pids.pid xargs kill -9
8+
rm kmip_pids.pid
9+
fi

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ tox -e doc
204204
- Run the tests from the `pymongo` checkout directory using:
205205
`TEST_LOADBALANCER=1 tox -m test-eg`.
206206

207+
## Running Encryption Tests Locally
208+
- Run `AWS_PROFILE=<profile> tox -e setup-encryption` after setting up your AWS profile with `aws configure sso`
209+
- Run the tests with `TEST_ENCRYPTION=1 LIBMONGOCRYPT_URL=<url> tox -e test-eg`.
210+
Look in `.evergreen/config.yml` for the correct `LIBMONGOCRYPT_URL` for your platform.
211+
- When done, run `tox -e teardown-encryption` to clean up.
212+
207213
## Re-sync Spec Tests
208214

209215
If you would like to re-sync the copy of the specification tests in the

tox.ini

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,24 @@ deps =
177177
passenv = *
178178
commands =
179179
python {env:DRIVERS_TOOLS}/.evergreen/auth_aws/setup_secrets.py {posargs}
180+
181+
[testenv:setup-encryption]
182+
deps =
183+
{[testenv:aws-secrets]deps}
184+
pykmip
185+
passenv = *
186+
allowlist_externals =
187+
bash
188+
commands =
189+
{[testenv:aws-secrets]commands} drivers/csfle
190+
bash .evergreen/start-kmip-server.sh
191+
bash .evergreen/setup-encryption-variables.sh
192+
193+
[testenv:teardown-encryption]
194+
passenv = *
195+
allowlist_externals =
196+
bash
197+
rm
198+
commands =
199+
bash .evergreen/stop-kmip-server.sh
200+
rm -rf libmongocrypt/ libmongocrypt_git/ libmongocrypt.tar.gz

0 commit comments

Comments
 (0)