/
connection_string.test.ts
413 lines (352 loc) · 15.6 KB
/
connection_string.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { expect } from 'chai';
import * as dns from 'dns';
import * as sinon from 'sinon';
import { promisify } from 'util';
import { MongoCredentials } from '../../src/cmap/auth/mongo_credentials';
import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from '../../src/cmap/auth/providers';
import { parseOptions, resolveSRVRecord } from '../../src/connection_string';
import {
MongoAPIError,
MongoDriverError,
MongoInvalidArgumentError,
MongoParseError
} from '../../src/error';
import { MongoClient, MongoOptions } from '../../src/mongo_client';
describe('Connection String', function () {
it('should not support auth passed with user', function () {
const optionsWithUser = {
authMechanism: 'SCRAM-SHA-1',
auth: { user: 'testing', password: 'llamas' }
};
expect(() => parseOptions('mongodb://localhost', optionsWithUser as any)).to.throw(
MongoParseError
);
});
it('should support auth passed with username', function () {
const optionsWithUsername = {
authMechanism: 'SCRAM-SHA-1',
auth: { username: 'testing', password: 'llamas' }
};
const options = parseOptions('mongodb://localhost', optionsWithUsername as any);
expect(options.credentials).to.containSubset({
source: 'admin',
username: 'testing',
password: 'llamas'
});
});
it('should provide a default port if one is not provided', function () {
const options = parseOptions('mongodb://hostname');
expect(options.hosts[0].socketPath).to.be.undefined;
expect(options.hosts[0].host).to.be.a('string');
expect(options.hosts[0].port).to.equal(27017);
});
context('readPreferenceTags', function () {
it('should parse multiple readPreferenceTags when passed in the uri', () => {
const options = parseOptions(
'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=baz:bar'
);
expect(options.readPreference.tags).to.deep.equal([{ bar: 'foo' }, { baz: 'bar' }]);
});
it('should parse multiple readPreferenceTags when passed in options object', () => {
const options = parseOptions('mongodb://hostname?', {
readPreferenceTags: [{ bar: 'foo' }, { baz: 'bar' }]
});
expect(options.readPreference.tags).to.deep.equal([{ bar: 'foo' }, { baz: 'bar' }]);
});
});
it('should parse boolean values', function () {
let options = parseOptions('mongodb://hostname?retryWrites=1');
expect(options.retryWrites).to.equal(true);
options = parseOptions('mongodb://hostname?retryWrites=false');
expect(options.retryWrites).to.equal(false);
options = parseOptions('mongodb://hostname?retryWrites=t');
expect(options.retryWrites).to.equal(true);
});
it('should parse compression options', function () {
const options = parseOptions('mongodb://localhost/?compressors=zlib&zlibCompressionLevel=4');
expect(options).to.have.property('compressors');
expect(options.compressors).to.include('zlib');
expect(options.zlibCompressionLevel).to.equal(4);
});
it('should parse `readConcernLevel`', function () {
const options = parseOptions('mongodb://localhost/?readConcernLevel=local');
expect(options).to.have.property('readConcern');
expect(options.readConcern.level).to.equal('local');
});
it('should parse `authMechanismProperties`', function () {
const options = parseOptions(
'mongodb://user%40EXAMPLE.COM:secret@localhost/?authMechanismProperties=SERVICE_NAME:other,SERVICE_REALM:blah,CANONICALIZE_HOST_NAME:true,SERVICE_HOST:example.com&authMechanism=GSSAPI'
);
expect(options.credentials.mechanismProperties).to.deep.include({
SERVICE_HOST: 'example.com',
SERVICE_NAME: 'other',
SERVICE_REALM: 'blah',
CANONICALIZE_HOST_NAME: true
});
expect(options.credentials.mechanism).to.equal(AuthMechanism.MONGODB_GSSAPI);
});
it('should provide default authSource when valid AuthMechanism provided', function () {
const options = parseOptions(
'mongodb+srv://jira-sync.pw0q4.mongodb.net/testDB?authMechanism=MONGODB-AWS&retryWrites=true&w=majority'
);
expect(options.credentials.source).to.equal('$external');
});
it('should omit credentials option when the only authSource is provided', function () {
let options = parseOptions(`mongodb://a/?authSource=someDb`);
expect(options).to.not.have.property('credentials');
options = parseOptions(`mongodb+srv://a/?authSource=someDb`);
expect(options).to.not.have.property('credentials');
});
it('should omit credentials and not throw a MongoAPIError if the only auth related option is authSource', async () => {
// The error we're looking to **not** see is
// `new MongoInvalidArgumentError('No AuthProvider for ${credentials.mechanism} defined.')`
// in `prepareHandshakeDocument` and/or `performInitialHandshake`.
// Neither function is exported currently but if I did export them because of the inlined callbacks
// I think I would need to mock quite a bit of internals to get down to that layer.
// My thinking is I can lean on server selection failing for th unit tests to assert we at least don't get an error related to auth.
const client = new MongoClient('mongodb://localhost:123/?authSource=someDb', {
serverSelectionTimeoutMS: 500
});
let thrownError: Error;
try {
// relies on us not running a mongod on port 123, fairly likely assumption
await client.connect();
} catch (error) {
thrownError = error;
}
// We should fail to connect, not fail to find an auth provider thus we should not find a MongoAPIError
expect(thrownError).to.not.be.instanceOf(MongoAPIError);
expect(client.options).to.not.have.a.property('credentials');
});
it('should parse a numeric authSource with variable width', function () {
const options = parseOptions('mongodb://test@localhost/?authSource=0001');
expect(options.credentials.source).to.equal('0001');
});
it('should not remove dbName from the options if authSource is provided', function () {
const dbName = 'my-db-name';
const authSource = 'admin';
const options = parseOptions(
`mongodb://myName:myPassword@localhost:27017/${dbName}?authSource=${authSource}`
);
expect(options).has.property('dbName', dbName);
expect(options.credentials).to.have.property('source', authSource);
});
it('should parse a replicaSet with a leading number', function () {
const options = parseOptions('mongodb://localhost/?replicaSet=123abc');
expect(options).to.have.property('replicaSet');
expect(options.replicaSet).to.equal('123abc');
});
context('when directionConnection is set', () => {
it('sets directConnection successfully when there is one host', () => {
const options = parseOptions('mongodb://localhost:27027/?directConnection=true');
expect(options.directConnection).to.be.true;
});
it('throws when directConnection is true and there is more than one host', () => {
expect(() =>
parseOptions('mongodb://localhost:27027,localhost:27018/?directConnection=true')
).to.throw(MongoParseError, 'directConnection option requires exactly one host');
});
});
context('when both tls and ssl options are provided', function () {
context('when the options are provided in the URI', function () {
context('when the options are equal', function () {
context('when both options are true', function () {
const options = parseOptions('mongodb://localhost/?tls=true&ssl=true');
it('sets the tls option', function () {
expect(options.tls).to.be.true;
});
it('does not set the ssl option', function () {
expect(options).to.not.have.property('ssl');
});
});
context('when both options are false', function () {
const options = parseOptions('mongodb://localhost/?tls=false&ssl=false');
it('sets the tls option', function () {
expect(options.tls).to.be.false;
});
it('does not set the ssl option', function () {
expect(options).to.not.have.property('ssl');
});
});
});
context('when the options are not equal', function () {
it('raises an error', function () {
expect(() => {
parseOptions('mongodb://localhost/?tls=true&ssl=false');
}).to.throw(MongoParseError, 'All values of tls/ssl must be the same.');
});
});
});
context('when the options are provided in the options', function () {
context('when the options are equal', function () {
context('when both options are true', function () {
const options = parseOptions('mongodb://localhost/', { tls: true, ssl: true });
it('sets the tls option', function () {
expect(options.tls).to.be.true;
});
it('does not set the ssl option', function () {
expect(options).to.not.have.property('ssl');
});
});
context('when both options are false', function () {
context('when the URI is an SRV URI', function () {
const options = parseOptions('mongodb+srv://localhost/', { tls: false, ssl: false });
it('overrides the tls option', function () {
expect(options.tls).to.be.false;
});
it('does not set the ssl option', function () {
expect(options).to.not.have.property('ssl');
});
});
context('when the URI is not SRV', function () {
const options = parseOptions('mongodb://localhost/', { tls: false, ssl: false });
it('sets the tls option', function () {
expect(options.tls).to.be.false;
});
it('does not set the ssl option', function () {
expect(options).to.not.have.property('ssl');
});
});
});
});
context('when the options are not equal', function () {
it('raises an error', function () {
expect(() => {
parseOptions('mongodb://localhost/', { tls: true, ssl: false });
}).to.throw(MongoParseError, 'All values of tls/ssl must be the same.');
});
});
});
});
describe('validation', function () {
it('should validate compressors options', function () {
expect(() => parseOptions('mongodb://localhost/?compressors=bunnies')).to.throw(
MongoInvalidArgumentError,
'bunnies is not a valid compression mechanism'
);
});
it('should validate authMechanism', function () {
expect(() => parseOptions('mongodb://localhost/?authMechanism=DOGS')).to.throw(
MongoParseError,
'authMechanism one of MONGODB-AWS,MONGODB-CR,DEFAULT,GSSAPI,PLAIN,SCRAM-SHA-1,SCRAM-SHA-256,MONGODB-X509, got DOGS'
);
});
it('should validate readPreference', function () {
expect(() => parseOptions('mongodb://localhost/?readPreference=llamasPreferred')).to.throw(
MongoDriverError, // not parse Error b/c thrown from ReadPreference construction
'Invalid read preference mode "llamasPreferred"'
);
});
});
describe('mongodb+srv', function () {
it('should parse a default database', function () {
const options = parseOptions('mongodb+srv://test1.test.build.10gen.cc/somedb');
expect(options.dbName).to.equal('somedb');
expect(options.srvHost).to.equal('test1.test.build.10gen.cc');
});
});
describe('resolveSRVRecord()', () => {
const resolveSRVRecordAsync = promisify(resolveSRVRecord);
afterEach(async () => {
sinon.restore();
});
function makeStub(txtRecord: string) {
const mockAddress = [
{
name: 'localhost.test.mock.test.build.10gen.cc',
port: 2017,
weight: 0,
priority: 0
}
];
const mockRecord: string[][] = [[txtRecord]];
// first call is for stubbing resolveSrv
// second call is for stubbing resolveTxt
sinon.stub(dns, 'resolveSrv').callsFake((address, callback) => {
return process.nextTick(callback, null, mockAddress);
});
sinon.stub(dns, 'resolveTxt').callsFake((address, whatWeTest) => {
whatWeTest(null, mockRecord);
});
}
for (const mechanism of AUTH_MECHS_AUTH_SRC_EXTERNAL) {
it(`should set authSource to $external for ${mechanism} external mechanism`, async function () {
makeStub('authSource=thisShouldNotBeAuthSource');
const credentials = new MongoCredentials({
source: '$external',
mechanism,
username: 'username',
password: mechanism === AuthMechanism.MONGODB_X509 ? undefined : 'password',
mechanismProperties: {}
});
credentials.validate();
const options = {
credentials,
srvHost: 'test.mock.test.build.10gen.cc',
srvServiceName: 'mongodb',
userSpecifiedAuthSource: false
} as MongoOptions;
await resolveSRVRecordAsync(options);
// check MongoCredentials instance (i.e. whether or not merge on options.credentials was called)
expect(options).property('credentials').to.equal(credentials);
expect(options).to.have.nested.property('credentials.source', '$external');
});
}
it('should set a default authSource for non-external mechanisms with no user-specified source', async function () {
makeStub('authSource=thisShouldBeAuthSource');
const credentials = new MongoCredentials({
source: 'admin',
mechanism: AuthMechanism.MONGODB_SCRAM_SHA256,
username: 'username',
password: 'password',
mechanismProperties: {}
});
credentials.validate();
const options = {
credentials,
srvHost: 'test.mock.test.build.10gen.cc',
srvServiceName: 'mongodb',
userSpecifiedAuthSource: false
} as MongoOptions;
await resolveSRVRecordAsync(options);
// check MongoCredentials instance (i.e. whether or not merge on options.credentials was called)
expect(options).property('credentials').to.not.equal(credentials);
expect(options).to.have.nested.property('credentials.source', 'thisShouldBeAuthSource');
});
it('should retain credentials for any mechanism with no user-sepcificed source and no source in DNS', async function () {
makeStub('');
const credentials = new MongoCredentials({
source: 'admin',
mechanism: AuthMechanism.MONGODB_SCRAM_SHA256,
username: 'username',
password: 'password',
mechanismProperties: {}
});
credentials.validate();
const options = {
credentials,
srvHost: 'test.mock.test.build.10gen.cc',
srvServiceName: 'mongodb',
userSpecifiedAuthSource: false
} as MongoOptions;
await resolveSRVRecordAsync(options as any);
// check MongoCredentials instance (i.e. whether or not merge on options.credentials was called)
expect(options).property('credentials').to.equal(credentials);
expect(options).to.have.nested.property('credentials.source', 'admin');
});
it('should retain specified authSource with no provided credentials', async function () {
makeStub('authSource=thisShouldBeAuthSource');
const credentials = {};
const options = {
credentials,
srvHost: 'test.mock.test.build.10gen.cc',
srvServiceName: 'mongodb',
userSpecifiedAuthSource: false
} as MongoOptions;
await resolveSRVRecordAsync(options as any);
expect(options).to.have.nested.property('credentials.username', '');
expect(options).to.have.nested.property('credentials.mechanism', 'DEFAULT');
expect(options).to.have.nested.property('credentials.source', 'thisShouldBeAuthSource');
});
});
});