Skip to content

Commit 41d4f0d

Browse files
test(NODE-4772): mongocryptd is not spawned when shared library is loaded (#3661)
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
1 parent c085cf0 commit 41d4f0d

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

.evergreen/run-kms-servers.sh

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/bin/bash
2+
13
cd ${DRIVERS_TOOLS}/.evergreen/csfle
24
. ./activate-kmstlsvenv.sh
35
# by default it always runs on port 5698

test/integration/client-side-encryption/client_side_encryption.prose.test.js

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ const { dropCollection, APMEventCollector } = require('../shared');
99

1010
const { EJSON } = BSON;
1111
const { LEGACY_HELLO_COMMAND } = require('../../mongodb');
12-
const { MongoServerError } = require('../../mongodb');
12+
const { MongoServerError, MongoServerSelectionError, MongoClient } = require('../../mongodb');
1313
const { getEncryptExtraOptions } = require('../../tools/utils');
1414
const { installNodeDNSWorkaroundHooks } = require('../../tools/runner/hooks/configuration');
1515
const { coerce, gte } = require('semver');
1616

17+
const {
18+
externalSchema
19+
} = require('../../spec/client-side-encryption/external/external-schema.json');
20+
1721
const getKmsProviders = (localKey, kmipEndpoint, azureEndpoint, gcpEndpoint) => {
1822
const result = BSON.EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}');
1923
if (localKey) {
@@ -1106,6 +1110,80 @@ describe('Client Side Encryption Prose Tests', metadata, function () {
11061110

11071111
it.skip('Via bypassAutoEncryption', () => {}).skipReason =
11081112
'TODO(NODE-2422): Implement "Bypass spawning mongocryptd" tests';
1113+
1114+
describe('via loading shared library', function () {
1115+
let clientEncrypted;
1116+
let client;
1117+
beforeEach(function () {
1118+
const { cryptSharedLibPath } = getEncryptExtraOptions();
1119+
if (!cryptSharedLibPath) {
1120+
this.currentTest.skipReason =
1121+
'test requires that the shared library is present, but CRYPT_SHARED_LIB_PATH is unset.';
1122+
this.skip();
1123+
}
1124+
});
1125+
1126+
// Setup
1127+
beforeEach(async function () {
1128+
const { cryptSharedLibPath } = getEncryptExtraOptions();
1129+
// 1. Create a MongoClient configured with auto encryption (referred to as `client_encrypted`)
1130+
clientEncrypted = this.configuration.newClient(
1131+
{},
1132+
{
1133+
// 2. Configure the required options. use the `local` KMS provider as follows:
1134+
// ```javascript
1135+
// { "local" : {"key": <base64 decoding of LOCAL_MASTERKEY>} }
1136+
// ```
1137+
// configure with the `keyVaultNamespace` set to `keyvault.datakeys`
1138+
// configure with `client_encrypted` to use the schema `external/external-schema.json` for
1139+
// `db.coll` by setting a schema map like `{"db.coll": <contents of external-schema.json }`
1140+
autoEncryption: {
1141+
keyVaultNamespace,
1142+
kmsProviders: { local: { key: LOCAL_KEY } },
1143+
// Configure the following `extraOptions`
1144+
// {
1145+
// "mongocryptdURI": "mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000",
1146+
// "mongocryptdSpawnArgs": [ "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021"],
1147+
// "cryptSharedLibPath": "<path to shared library>",
1148+
// "cryptSharedRequired": true
1149+
// }
1150+
extraOptions: {
1151+
mongocryptdURI: 'mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000',
1152+
mongocryptdSpawnArgs: [
1153+
'--pidfilepath=bypass-spawning-mongocryptd.pid',
1154+
'--port=27021'
1155+
],
1156+
cryptdSharedLibRequired: true,
1157+
cryptSharedLibPath
1158+
},
1159+
schemaMap: externalSchema
1160+
}
1161+
}
1162+
);
1163+
// 3. Use `client_encrypted` to insert the document `{"unencrypted": "test"}` into `db.coll`
1164+
// expect this to succeed
1165+
await clientEncrypted.connect();
1166+
const insertResult = await clientEncrypted
1167+
.db(dataDbName)
1168+
.collection(dataCollName)
1169+
.insertOne({ unencrypted: 'test' });
1170+
expect(insertResult).to.have.property('insertedId');
1171+
});
1172+
1173+
afterEach(async function () {
1174+
await clientEncrypted?.close();
1175+
await client?.close();
1176+
});
1177+
1178+
// 4. Validate that mongocryptd was not spawned. Create a MongoClient to localhost:27021 (or
1179+
// whatever was passed via `--port` with serverSelectionTimeoutMS=1000.) Run a handshake
1180+
// command and ensure it fails with a server selection timeout
1181+
it('should not spawn mongocryptd', metadata, async function () {
1182+
client = new MongoClient('mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000');
1183+
const error = await client.connect().catch(e => e);
1184+
expect(error).to.be.instanceOf(MongoServerSelectionError, /'Server selection timed out'/i);
1185+
});
1186+
});
11091187
});
11101188

11111189
describe('Deadlock tests', () => {

test/tools/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,13 @@ export class EventCollector {
9393
}
9494
}
9595

96-
export function getEncryptExtraOptions() {
97-
if (process.env.CRYPT_SHARED_LIB_PATH) {
96+
export function getEncryptExtraOptions(): {
97+
cryptSharedLibPath?: string;
98+
} {
99+
if (
100+
typeof process.env.CRYPT_SHARED_LIB_PATH === 'string' &&
101+
process.env.CRYPT_SHARED_LIB_PATH.length > 0
102+
) {
98103
return { cryptSharedLibPath: process.env.CRYPT_SHARED_LIB_PATH };
99104
}
100105
return {};

0 commit comments

Comments
 (0)