Skip to content

Commit 0307650

Browse files
committed
CR comments
1 parent d410602 commit 0307650

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

packages/service-provider-server/src/cli-service-provider.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,27 @@ describe('CliServiceProvider', () => {
100100
try {
101101
await connectMongoClient(uri, opts, mClientType);
102102
} catch (e) {
103-
return expect(e.message).to.include('automatic encryption');
103+
return expect(e.message.toLowerCase()).to.include('automatic encryption');
104+
}
105+
expect.fail('Failed to throw expected error');
106+
});
107+
it('errors when bypassAutoEncryption is falsy, missing modules', async() => {
108+
const uri = 'localhost:27017';
109+
const opts = { autoEncryption: {} };
110+
const mClientType = stubInterface<typeof MongoClient>();
111+
const mClientFirst = stubInterface<MongoClient>();
112+
const commandSpy = sinon.spy();
113+
mClientFirst.db.returns({ admin: () => ({ command: (...args) => {
114+
commandSpy(...args);
115+
return {};
116+
} } as any) } as any);
117+
const mClientSecond = stubInterface<MongoClient>();
118+
mClientType.connect.onFirstCall().resolves(mClientFirst);
119+
mClientType.connect.onSecondCall().resolves(mClientSecond);
120+
try {
121+
await connectMongoClient(uri, opts, mClientType);
122+
} catch (e) {
123+
return expect(e.message.toLowerCase()).to.include('automatic encryption');
104124
}
105125
expect.fail('Failed to throw expected error');
106126
});

packages/service-provider-server/src/cli-service-provider.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const DEFAULT_BASE_OPTIONS = Object.freeze({
126126
* @param clientOptions {MongoClientOptions}
127127
* @param mClient {MongoClient}
128128
*/
129-
export async function connectMongoClient(uri: string, clientOptions: MongoClientOptions, mClient = MongoClient): Promise<MongoClient | void> {
129+
export async function connectMongoClient(uri: string, clientOptions: MongoClientOptions, mClient = MongoClient): Promise<MongoClient> {
130130
if (clientOptions.autoEncryption !== undefined &&
131131
!clientOptions.autoEncryption.bypassAutoEncryption) {
132132
// connect first without autoEncryptionOptions
@@ -135,11 +135,11 @@ export async function connectMongoClient(uri: string, clientOptions: MongoClient
135135
const client = await mClient.connect(uri, optionsWithoutFLE);
136136
const buildInfo = await client.db('admin').admin().command({ buildInfo: 1 });
137137
if (
138-
!(buildInfo.gitVersion && buildInfo.gitVersion.match(/enterprise/)) &&
139-
!(buildInfo.modules && buildInfo.modules.indexOf('enterprise') !== -1)
138+
!(buildInfo.modules?.includes('enterprise')) &&
139+
!(buildInfo.gitVersion?.match(/enterprise/))
140140
) {
141141
await client.close();
142-
throw new MongoshRuntimeError('Cannot turn on automatic encryption for connections to non-enterprise hosts');
142+
throw new MongoshRuntimeError('Automatic encryption is only available with Atlas and MongoDB Enterprise');
143143
}
144144
await client.close();
145145
}
@@ -173,7 +173,7 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
173173
await connectMongoClient(
174174
uri,
175175
clientOptions
176-
) as MongoClient :
176+
) :
177177
new MongoClient(uri || 'mongodb://nodb/', clientOptions);
178178

179179
return new CliServiceProvider(mongoClient, clientOptions, uri);
@@ -223,7 +223,7 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
223223
const mongoClient = await connectMongoClient(
224224
uri,
225225
clientOptions
226-
) as MongoClient;
226+
);
227227
return new CliServiceProvider(mongoClient, uri);
228228
}
229229

@@ -1054,7 +1054,7 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
10541054
const mc = await connectMongoClient(
10551055
this.uri as string,
10561056
clientOptions
1057-
) as MongoClient;
1057+
);
10581058
try {
10591059
await this.mongoClient.close();
10601060
// eslint-disable-next-line no-empty
@@ -1123,7 +1123,7 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
11231123
const mc = await connectMongoClient(
11241124
this.uri as string,
11251125
clientOptions
1126-
) as MongoClient;
1126+
);
11271127
try {
11281128
await this.mongoClient.close();
11291129
// eslint-disable-next-line no-empty

0 commit comments

Comments
 (0)