-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
config.ts
374 lines (320 loc) · 11.1 KB
/
config.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
import { expect } from 'chai';
import ConnectionString from 'mongodb-connection-string-url';
import * as qs from 'querystring';
import * as url from 'url';
import { AuthMechanism, WriteConcernSettings } from '../../../src';
import { MongoClient } from '../../../src/mongo_client';
import { TopologyType } from '../../../src/sdam/common';
import { Topology } from '../../../src/sdam/topology';
import { HostAddress } from '../../../src/utils';
import { getEnvironmentalOptions } from '../utils';
interface ProxyParams {
proxyHost?: string;
proxyPort?: number;
proxyUsername?: string;
proxyPassword?: string;
}
interface UrlOptions {
/** name of the default db */
db?: string;
/** replSet name */
replicaSet?: string;
/** Username to authenticate with */
username?: string;
/** Password to authenticate with */
password?: string;
/** Name of the auth mechanism to use */
authMechanism?: AuthMechanism;
/** Additional properties used by the mechanism */
authMechanismProperties?: Record<string, any>;
/** The database to specify as the authentication source */
authSource?: string;
/** If set will use concatenate all known HostAddresses in URI */
useMultipleMongoses?: boolean;
/** Parameters for configuring a proxy connection */
proxyURIParams?: ProxyParams;
}
function convertToConnStringMap(obj: Record<string, any>) {
const result = [];
Object.keys(obj).forEach(key => {
result.push(`${key}:${obj[key]}`);
});
return result.join(',');
}
export class TestConfiguration {
version: string;
clientSideEncryption: Record<string, any>;
parameters: Record<string, any>;
singleMongosLoadBalancerUri: string;
multiMongosLoadBalancerUri: string;
isServerless: boolean;
topologyType: TopologyType;
buildInfo: Record<string, any>;
options: {
hosts?: string[];
hostAddresses: HostAddress[];
hostAddress?: HostAddress;
host?: string;
port?: number;
db?: string;
replicaSet?: string;
authMechanism?: string;
authMechanismProperties?: Record<string, any>;
auth?: { username: string; password: string; authSource?: string };
proxyURIParams?: ProxyParams;
};
constructor(uri: string, context: Record<string, any>) {
const url = new ConnectionString(uri);
const { hosts } = url;
const hostAddresses = hosts.map(HostAddress.fromString);
this.version = context.version;
this.clientSideEncryption = context.clientSideEncryption;
this.parameters = { ...context.parameters };
this.singleMongosLoadBalancerUri = context.singleMongosLoadBalancerUri;
this.multiMongosLoadBalancerUri = context.multiMongosLoadBalancerUri;
this.isServerless = !!process.env.SERVERLESS;
this.topologyType = this.isLoadBalanced ? TopologyType.LoadBalanced : context.topologyType;
this.buildInfo = context.buildInfo;
this.options = {
hosts,
hostAddresses,
hostAddress: hostAddresses[0],
host: hostAddresses[0].host,
port:
typeof hostAddresses[0].host === 'string' && !this.isServerless
? hostAddresses[0].port
: undefined,
db: url.pathname.slice(1) ? url.pathname.slice(1) : 'integration_tests',
replicaSet: url.searchParams.get('replicaSet'),
proxyURIParams: url.searchParams.get('proxyHost')
? {
proxyHost: url.searchParams.get('proxyHost'),
proxyPort: Number(url.searchParams.get('proxyPort')),
proxyUsername: url.searchParams.get('proxyUsername'),
proxyPassword: url.searchParams.get('proxyPassword')
}
: undefined
};
if (url.username) {
this.options.auth = {
username: url.username,
password: url.password
};
}
if (context.serverlessCredentials) {
const { username, password } = context.serverlessCredentials;
this.options.auth = { username, password, authSource: 'admin' };
}
}
get isLoadBalanced() {
return (
!!this.singleMongosLoadBalancerUri && !!this.multiMongosLoadBalancerUri && !this.isServerless
);
}
writeConcern() {
return { writeConcern: { w: 1 } };
}
get host() {
return this.options.host;
}
get port() {
return this.options.port;
}
set db(_db) {
this.options.db = _db;
}
get db() {
return this.options.db;
}
// legacy accessors, consider for removal
get replicasetName() {
return this.options.replicaSet;
}
get setName() {
return this.options.replicaSet;
}
newClient(dbOptions?: string | Record<string, any>, serverOptions?: Record<string, any>) {
serverOptions = Object.assign(
{ minHeartbeatFrequencyMS: 100 },
getEnvironmentalOptions(),
serverOptions
);
// support MongoClient constructor form (url, options) for `newClient`
if (typeof dbOptions === 'string') {
return new MongoClient(dbOptions, serverOptions);
}
dbOptions = dbOptions || {};
// Fall back
let dbHost = (serverOptions && serverOptions.host) || this.options.host;
const dbPort = (serverOptions && serverOptions.port) || this.options.port;
if (dbHost.indexOf('.sock') !== -1) {
dbHost = qs.escape(dbHost);
}
if (this.options.authMechanism) {
Object.assign(dbOptions, {
authMechanism: this.options.authMechanism
});
}
if (this.options.authMechanismProperties) {
Object.assign(dbOptions, {
authMechanismProperties: convertToConnStringMap(this.options.authMechanismProperties)
});
}
if (this.options.replicaSet) {
Object.assign(dbOptions, { replicaSet: this.options.replicaSet });
}
if (this.options.proxyURIParams) {
for (const [name, value] of Object.entries(this.options.proxyURIParams)) {
if (value) {
dbOptions[name] = value;
}
}
}
// Flatten any options nested under `writeConcern` before we make the connection string
if (dbOptions.writeConcern) {
Object.assign(dbOptions, dbOptions.writeConcern);
delete dbOptions.writeConcern;
}
if (this.topologyType === TopologyType.LoadBalanced && !this.isServerless) {
dbOptions.loadBalanced = true;
}
if (process.env.MONGODB_API_VERSION) {
dbOptions.apiVersion = process.env.MONGODB_API_VERSION;
}
const urlOptions: url.UrlObject = {
protocol: this.isServerless ? 'mongodb+srv' : 'mongodb',
slashes: true,
hostname: dbHost,
port: this.isServerless ? null : dbPort,
query: dbOptions,
pathname: '/'
};
if (this.options.auth) {
const { username, password } = this.options.auth;
if (username) {
urlOptions.auth = `${encodeURIComponent(username)}:${encodeURIComponent(password)}`;
}
}
if (dbOptions.auth) {
const { username, password } = dbOptions.auth;
if (username) {
urlOptions.auth = `${encodeURIComponent(username)}:${encodeURIComponent(password)}`;
}
}
if (typeof urlOptions.query === 'object') {
// Auth goes at the top of the uri, not in the searchParams
delete urlOptions.query.auth;
}
const connectionString = url.format(urlOptions);
if (Reflect.has(serverOptions, 'host') || Reflect.has(serverOptions, 'port')) {
throw new Error(`Cannot use options to specify host/port, must be in ${connectionString}`);
}
return new MongoClient(connectionString, serverOptions);
}
newTopology(host, port, options) {
if (typeof host === 'object') {
options = host;
host = null;
port = null;
}
options = Object.assign({}, options);
const hosts =
host == null ? [].concat(this.options.hostAddresses) : [new HostAddress(`${host}:${port}`)];
return new Topology(hosts, options);
}
/**
* Construct a connection URL using nodejs's whatwg URL similar to how connection_string.ts
* works
*
* @param options - overrides and settings for URI generation
*/
url(options?: UrlOptions) {
options = {
db: this.options.db,
replicaSet: this.options.replicaSet,
proxyURIParams: this.options.proxyURIParams,
...options
};
const FILLER_HOST = 'fillerHost';
const protocol = this.isServerless ? 'mongodb+srv' : 'mongodb';
const url = new URL(`${protocol}://${FILLER_HOST}`);
if (options.replicaSet) {
url.searchParams.append('replicaSet', options.replicaSet);
}
if (options.proxyURIParams) {
for (const [name, value] of Object.entries(options.proxyURIParams)) {
if (value) {
url.searchParams.append(name, value);
}
}
}
url.pathname = `/${options.db}`;
const username = this.options.username || (this.options.auth && this.options.auth.username);
const password = this.options.password || (this.options.auth && this.options.auth.password);
if (username) {
url.username = username;
}
if (password) {
url.password = password;
}
if (this.isLoadBalanced && !this.isServerless) {
url.searchParams.append('loadBalanced', 'true');
}
if (username || password) {
if (options.authMechanism) {
url.searchParams.append('authMechanism', options.authMechanism);
}
if (options.authMechanismProperties) {
url.searchParams.append(
'authMechanismProperties',
convertToConnStringMap(options.authMechanismProperties)
);
}
if (options.authSource) {
url.searchParams.append('authSource', options.authSource);
}
} else if (this.isServerless) {
url.searchParams.append('ssl', 'true');
url.searchParams.append('authSource', 'admin');
}
let actualHostsString;
// Ignore multi mongos options in serverless testing.
if (options.useMultipleMongoses && !this.isServerless) {
if (this.isLoadBalanced) {
const multiUri = new ConnectionString(this.multiMongosLoadBalancerUri);
if (multiUri.isSRV) {
throw new Error('You cannot pass an SRV connection string to multiMongosLoadBalancerUri');
}
actualHostsString = multiUri.hosts[0].toString();
} else {
expect(this.options.hostAddresses).to.have.length.greaterThan(1);
actualHostsString = this.options.hostAddresses.map(ha => ha.toString()).join(',');
}
} else {
if (this.isLoadBalanced || this.isServerless) {
const singleUri = new ConnectionString(this.singleMongosLoadBalancerUri);
actualHostsString = singleUri.hosts[0].toString();
} else {
actualHostsString = this.options.hostAddresses[0].toString();
}
}
if (!options.authSource) {
url.searchParams.append('authSource', 'admin');
}
const connectionString = url.toString().replace(FILLER_HOST, actualHostsString);
return connectionString;
}
writeConcernMax(): { writeConcern: WriteConcernSettings } {
if (this.topologyType !== TopologyType.Single) {
return { writeConcern: { w: 'majority', wtimeoutMS: 30000 } };
}
return { writeConcern: { w: 1 } };
}
// Accessors and methods Client-Side Encryption
get mongodbClientEncryption() {
return this.clientSideEncryption && this.clientSideEncryption.mongodbClientEncryption;
}
kmsProviders(localKey): Record<string, any> {
return { local: { key: localKey } };
}
}