Skip to content

Commit

Permalink
fix: restore createAutoEncrypter() functionality (#2710)
Browse files Browse the repository at this point in the history
Corrects the import name of the AutoEncrypter class. 
Passes through client constructor options to CSFLE lib.

NODE-3042
  • Loading branch information
addaleax authored and ljhaywar committed Nov 9, 2021
1 parent 3e4f76a commit 24b52c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export function parseOptions(

checkTLSOptions(mongoOptions);
if (mongoClient && options.autoEncryption) {
mongoOptions.autoEncrypter = createAutoEncrypter(mongoClient);
mongoOptions.autoEncrypter = createAutoEncrypter(mongoClient, options);
}
if (options.promiseLibrary) PromiseProvider.set(options.promiseLibrary);

Expand Down
17 changes: 11 additions & 6 deletions src/operations/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolveSRVRecord } from '../connection_string';
import { emitDeprecationWarning, Callback } from '../utils';
import { CMAP_EVENT_NAMES } from '../cmap/events';
import * as BSON from '../bson';
import type { MongoClient, MongoOptions } from '../mongo_client';
import type { MongoClient, MongoOptions, MongoClientOptions } from '../mongo_client';
import { Connection } from '../cmap/connection';
import { Server } from '../sdam/server';
import type { AutoEncrypter } from '../deps';
Expand Down Expand Up @@ -114,8 +114,11 @@ function registerDeprecatedEventNotifiers(client: MongoClient) {
* returns undefined if CSFLE is not enabled.
* @throws if optional 'mongodb-client-encryption' dependency missing
*/
export function createAutoEncrypter(client: MongoClient): AutoEncrypter | undefined {
if (!client.options.autoEncryption) {
export function createAutoEncrypter(
client: MongoClient,
options: MongoClientOptions
): AutoEncrypter | undefined {
if (!options.autoEncryption) {
return;
}
try {
Expand All @@ -135,10 +138,12 @@ export function createAutoEncrypter(client: MongoClient): AutoEncrypter | undefi
'Please make sure you are loading the correct version of `mongodb-client-encryption`'
);
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { AutoEncrypterClass } = mongodbClientEncryption.extension(require('../../lib/index'));
const { AutoEncrypter: AutoEncrypterClass } = mongodbClientEncryption.extension(
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../../lib/index')
);

const mongoCryptOptions = Object.assign({ bson: BSON }, client.options.autoEncryption);
const mongoCryptOptions = Object.assign({ bson: BSON }, options.autoEncryption);
return new AutoEncrypterClass(client, mongoCryptOptions);
}

Expand Down

0 comments on commit 24b52c2

Please sign in to comment.