Skip to content

Commit 0a63b01

Browse files
benjirewisBenjamin Rewis
authored andcommitted
GODRIVER-1916 Test that KMS TLS connections verify peer certificates (#619)
1 parent 797e160 commit 0a63b01

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

.evergreen/config.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,51 @@ functions:
759759
cat setup.js
760760
mongo --nodb setup.js aws_e2e_ecs.js
761761
762+
start-kms-mock-server:
763+
- command: shell.exec
764+
type: test
765+
params:
766+
working_dir: src
767+
background: true
768+
script: |
769+
${PREPARE_SHELL}
770+
cd ${DRIVERS_TOOLS}/.evergreen/csfle
771+
cat <<EOF > kms_setup.json
772+
{
773+
"kms_ca_file": "${KMS_CA_FILE}",
774+
"kms_cert_file": "${KMS_CERT_FILE}"
775+
}
776+
EOF
777+
mongo --nodb mock_kms.js
778+
779+
run-kms-tls-test:
780+
- command: shell.exec
781+
type: test
782+
params:
783+
working_dir: src/go.mongodb.org/mongo-driver
784+
script: |
785+
${PREPARE_SHELL}
786+
export KMS_TLS_TESTCASE="${KMS_TLS_TESTCASE}"
787+
788+
export GOFLAGS=-mod=vendor
789+
set +o xtrace
790+
AUTH="${AUTH}" \
791+
SSL="${SSL}" \
792+
MONGODB_URI="${MONGODB_URI}" \
793+
TOPOLOGY="${TOPOLOGY}" \
794+
MONGO_GO_DRIVER_COMPRESSOR=${MONGO_GO_DRIVER_COMPRESSOR} \
795+
BUILD_TAGS="-tags cse" \
796+
AWS_ACCESS_KEY_ID="${cse_aws_access_key_id}" \
797+
AWS_SECRET_ACCESS_KEY="${cse_aws_secret_access_key}" \
798+
AZURE_TENANT_ID="${cse_azure_tenant_id}" \
799+
AZURE_CLIENT_ID="${cse_azure_client_id}" \
800+
AZURE_CLIENT_SECRET="${cse_azure_client_secret}" \
801+
GCP_EMAIL="${cse_gcp_email}" \
802+
GCP_PRIVATE_KEY="${cse_gcp_private_key}" \
803+
make evg-test-kms \
804+
PKG_CONFIG_PATH=$PKG_CONFIG_PATH \
805+
LD_LIBRARY_PATH=$LD_LIBRARY_PATH
806+
762807
pre:
763808
- func: fetch-source
764809
- func: prepare-resources
@@ -1545,6 +1590,44 @@ tasks:
15451590
- func: run-aws-auth-test-with-aws-EC2-credentials
15461591
- func: run-aws-ECS-auth-test
15471592

1593+
- name: "test-kms-tls-invalid-cert"
1594+
tags: ["kms-tls"]
1595+
commands:
1596+
- func: bootstrap-mongo-orchestration
1597+
vars:
1598+
TOPOLOGY: "server"
1599+
AUTH: "noauth"
1600+
SSL: "nossl"
1601+
- func: start-kms-mock-server
1602+
vars:
1603+
KMS_CA_FILE: "ca.pem"
1604+
KMS_CERT_FILE: "expired.pem"
1605+
- func: run-kms-tls-test
1606+
vars:
1607+
KMS_TLS_TESTCASE: "INVALID_CERT"
1608+
TOPOLOGY: "server"
1609+
AUTH: "noauth"
1610+
SSL: "nossl"
1611+
1612+
- name: "test-kms-tls-invalid-hostname"
1613+
tags: ["kms-tls"]
1614+
commands:
1615+
- func: bootstrap-mongo-orchestration
1616+
vars:
1617+
TOPOLOGY: "server"
1618+
AUTH: "noauth"
1619+
SSL: "nossl"
1620+
- func: start-kms-mock-server
1621+
vars:
1622+
KMS_CA_FILE: "ca.pem"
1623+
KMS_CERT_FILE: "wrong-host.pem"
1624+
- func: run-kms-tls-test
1625+
vars:
1626+
KMS_TLS_TESTCASE: "INVALID_HOSTNAME"
1627+
TOPOLOGY: "server"
1628+
AUTH: "noauth"
1629+
SSL: "nossl"
1630+
15481631
axes:
15491632
- id: version
15501633
display_name: MongoDB Version
@@ -1766,3 +1849,9 @@ buildvariants:
17661849
tasks:
17671850
# macos MongoDB servers do not staple OCSP responses and only support RSA.
17681851
- name: ".ocsp-rsa !.ocsp-staple"
1852+
1853+
- matrix_name: "kms-tls-test"
1854+
matrix_spec: { version: ["latest"], os-ssl-32: ["ubuntu1604-64-go-1-15"] }
1855+
display_name: "KMS TLS ${version} ${os-ssl-32}"
1856+
tasks:
1857+
- name: ".kms-tls"

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ evg-test-atlas-data-lake:
159159
ATLAS_DATA_LAKE_INTEGRATION_TEST=true go test -v ./mongo/integration -run TestUnifiedSpecs/atlas-data-lake-testing >> spec_test.suite
160160
ATLAS_DATA_LAKE_INTEGRATION_TEST=true go test -v ./mongo/integration -run TestAtlasDataLake >> spec_test.suite
161161

162+
.PHONY: evg-test-kms
163+
evg-test-kms:
164+
go test -exec "env PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) LD_LIBRARY_PATH=$(LD_LIBRARY_PATH)" $(BUILD_TAGS) -v -timeout $(TEST_TIMEOUT)s ./mongo/integration -run TestClientSideEncryptionProse/kms_tls_tests >> test.suite
165+
162166
# benchmark specific targets and support
163167
perf:driver-test-data.tar.gz
164168
tar -zxf $< $(if $(eq $(UNAME_S),Darwin),-s , --transform=s)/data/perf/

mongo/integration/client_side_encryption_prose_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"encoding/base64"
1414
"fmt"
1515
"io/ioutil"
16+
"os"
1617
"path/filepath"
1718
"runtime"
1819
"strings"
@@ -1028,6 +1029,57 @@ func TestClientSideEncryptionProse(t *testing.T) {
10281029
})
10291030
}
10301031
})
1032+
1033+
// These tests only run when a KMS mock server is running on localhost:8000.
1034+
mt.RunOpts("kms tls tests", noClientOpts, func(mt *mtest.T) {
1035+
kmsTlsTestcase := os.Getenv("KMS_TLS_TESTCASE")
1036+
if kmsTlsTestcase == "" {
1037+
mt.Skipf("Skipping test as KMS_TLS_TESTCASE is not set")
1038+
}
1039+
1040+
testcases := []struct {
1041+
name string
1042+
envValue string
1043+
errMessage string
1044+
}{
1045+
{
1046+
"invalid certificate",
1047+
"INVALID_CERT",
1048+
"expired",
1049+
},
1050+
{
1051+
"invalid hostname",
1052+
"INVALID_HOSTNAME",
1053+
"SANs",
1054+
},
1055+
}
1056+
1057+
for _, tc := range testcases {
1058+
mt.Run(tc.name, func(mt *mtest.T) {
1059+
// Only run test if correct KMS mock server is running.
1060+
if kmsTlsTestcase != tc.envValue {
1061+
mt.Skipf("Skipping test as KMS_TLS_TESTCASE is set to %q, expected %v", kmsTlsTestcase, tc.envValue)
1062+
}
1063+
1064+
ceo := options.ClientEncryption().
1065+
SetKmsProviders(fullKmsProvidersMap).
1066+
SetKeyVaultNamespace(kvNamespace)
1067+
cpt := setup(mt, nil, nil, ceo)
1068+
defer cpt.teardown(mt)
1069+
1070+
_, err := cpt.clientEnc.CreateDataKey(context.Background(), "aws", options.DataKey().SetMasterKey(
1071+
bson.D{
1072+
{"region", "us-east-1"},
1073+
{"key", "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0"},
1074+
{"endpoint", "mongodb://127.0.0.1:8000"},
1075+
},
1076+
))
1077+
assert.NotNil(mt, err, "expected CreateDataKey error, got nil")
1078+
assert.True(mt, strings.Contains(err.Error(), tc.errMessage),
1079+
"expected CreateDataKey error to contain %v, got %v", tc.errMessage, err.Error())
1080+
})
1081+
}
1082+
})
10311083
}
10321084

10331085
func getWatcher(mt *mtest.T, streamType mongo.StreamType, cpt *cseProseTest) watcher {

0 commit comments

Comments
 (0)