@@ -9,11 +9,15 @@ const { dropCollection, APMEventCollector } = require('../shared');
9
9
10
10
const { EJSON } = BSON ;
11
11
const { LEGACY_HELLO_COMMAND } = require ( '../../mongodb' ) ;
12
- const { MongoServerError } = require ( '../../mongodb' ) ;
12
+ const { MongoServerError, MongoServerSelectionError , MongoClient } = require ( '../../mongodb' ) ;
13
13
const { getEncryptExtraOptions } = require ( '../../tools/utils' ) ;
14
14
const { installNodeDNSWorkaroundHooks } = require ( '../../tools/runner/hooks/configuration' ) ;
15
15
const { coerce, gte } = require ( 'semver' ) ;
16
16
17
+ const {
18
+ externalSchema
19
+ } = require ( '../../spec/client-side-encryption/external/external-schema.json' ) ;
20
+
17
21
const getKmsProviders = ( localKey , kmipEndpoint , azureEndpoint , gcpEndpoint ) => {
18
22
const result = BSON . EJSON . parse ( process . env . CSFLE_KMS_PROVIDERS || '{}' ) ;
19
23
if ( localKey ) {
@@ -1106,6 +1110,80 @@ describe('Client Side Encryption Prose Tests', metadata, function () {
1106
1110
1107
1111
it . skip ( 'Via bypassAutoEncryption' , ( ) => { } ) . skipReason =
1108
1112
'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 , / ' S e r v e r s e l e c t i o n t i m e d o u t ' / i) ;
1185
+ } ) ;
1186
+ } ) ;
1109
1187
} ) ;
1110
1188
1111
1189
describe ( 'Deadlock tests' , ( ) => {
0 commit comments