-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtls_support.test.ts
More file actions
295 lines (247 loc) · 8.9 KB
/
tls_support.test.ts
File metadata and controls
295 lines (247 loc) · 8.9 KB
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
import * as process from 'node:process';
import * as tls from 'node:tls';
import { expect } from 'chai';
import { promises as fs } from 'fs';
import * as sinon from 'sinon';
import {
LEGACY_HELLO_COMMAND,
MongoClient,
type MongoClientOptions,
MongoServerSelectionError
} from '../mongodb';
const REQUIRED_ENV = ['MONGODB_URI', 'TLS_KEY_FILE', 'TLS_CA_FILE', 'TLS_CRL_FILE'];
describe('TLS Support', function () {
for (const key of REQUIRED_ENV) {
if (process.env[key] == null) {
throw new Error(`skipping TLS tests, ${key} environment variable is not defined`);
}
}
const CONNECTION_STRING = process.env.MONGODB_URI as string;
const TLS_CERT_KEY_FILE = process.env.TLS_KEY_FILE as string;
const TLS_CA_FILE = process.env.TLS_CA_FILE as string;
const TLS_CRL_FILE = process.env.TLS_CRL_FILE as string;
const tlsSettings = {
tls: true,
tlsCertificateKeyFile: TLS_CERT_KEY_FILE,
tlsCAFile: TLS_CA_FILE
};
it(
'should connect with tls via client options',
makeConnectionTest(CONNECTION_STRING, tlsSettings)
);
beforeEach(function () {
if (
this.currentTest?.title === 'should connect with tls via url options' &&
process.platform === 'win32'
) {
this.currentTest.skipReason = 'TODO(NODE-5803): Un-skip Windows TLS tests via URL';
return this.skip();
}
});
it(
'should connect with tls via url options',
makeConnectionTest(
`${CONNECTION_STRING}?${Object.keys(tlsSettings)
.map(key => `${key}=${tlsSettings[key]}`)
.join('&')}`
)
);
context('when tls filepaths are provided', () => {
let client: MongoClient;
afterEach(async () => {
await client?.close();
});
context('when tls filepaths have length > 0', () => {
context('when auto family options are not set', function () {
let tlsSpy;
afterEach(function () {
sinon.restore();
});
beforeEach(function () {
client = new MongoClient(CONNECTION_STRING, tlsSettings);
tlsSpy = sinon.spy(tls, 'connect');
});
it('sets the default options', async function () {
await client.connect();
expect(tlsSpy).to.have.been.calledWith(
sinon.match({
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
})
);
});
});
context('when auto select family options are set', function () {
let tlsSpy;
afterEach(function () {
sinon.restore();
});
beforeEach(function () {
client = new MongoClient(CONNECTION_STRING, {
...tlsSettings,
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100
});
tlsSpy = sinon.spy(tls, 'connect');
});
it('sets the provided options', async function () {
await client.connect();
expect(tlsSpy).to.have.been.calledWith(
sinon.match({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100,
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
})
);
});
});
context('when connection will succeed', () => {
beforeEach(async () => {
client = new MongoClient(CONNECTION_STRING, tlsSettings);
});
it('should read in files async at connect time', async () => {
expect(client.options).property('tlsCAFile', TLS_CA_FILE);
expect(client.options).property('tlsCertificateKeyFile', TLS_CERT_KEY_FILE);
expect(client.options).not.have.property('ca');
expect(client.options).not.have.property('key');
expect(client.options).not.have.property('cert');
await client.connect();
expect(client.options).property('ca').to.exist;
expect(client.options).property('key').to.exist;
expect(client.options).property('cert').to.exist;
});
context('when client has been opened and closed more than once', function () {
it('should only read files once', async () => {
await client.connect();
await client.close();
const caFileAccessTime = (await fs.stat(TLS_CA_FILE)).atime;
const certKeyFileAccessTime = (await fs.stat(TLS_CERT_KEY_FILE)).atime;
await client.connect();
expect((await fs.stat(TLS_CA_FILE)).atime).to.deep.equal(caFileAccessTime);
expect((await fs.stat(TLS_CERT_KEY_FILE)).atime).to.deep.equal(certKeyFileAccessTime);
});
});
});
context('when the connection will fail', () => {
beforeEach(async () => {
client = new MongoClient(CONNECTION_STRING, {
tls: true,
tlsCRLFile: TLS_CRL_FILE,
serverSelectionTimeoutMS: 2000,
connectTimeoutMS: 2000
});
});
it('should read in files async at connect time', async () => {
expect(client.options).property('tlsCRLFile', TLS_CRL_FILE);
expect(client.options).not.have.property('crl');
const err = await client.connect().catch(e => e);
expect(err).to.be.instanceof(Error);
expect(client.options).property('crl').to.exist;
});
context('when client has been opened and closed more than once', function () {
it('should only read files once', async () => {
await client.connect().catch(e => e);
await client.close();
const crlFileAccessTime = (await fs.stat(TLS_CRL_FILE)).atime;
const err = await client.connect().catch(e => e);
expect(err).to.be.instanceof(Error);
expect((await fs.stat(TLS_CRL_FILE)).atime).to.deep.equal(crlFileAccessTime);
});
});
});
});
context('when tlsCAFile has length === 0', () => {
beforeEach(() => {
client = new MongoClient(CONNECTION_STRING, {
tls: true,
tlsCAFile: '',
tlsCertificateKeyFile: TLS_CERT_KEY_FILE
});
});
it('should throw an error at connect time', async () => {
const err = await client.connect().catch(e => e);
expect(err).to.be.instanceof(Error);
});
});
context('when tlsCertificateKeyFile has length === 0', () => {
beforeEach(() => {
client = new MongoClient(CONNECTION_STRING, {
tls: true,
tlsCAFile: TLS_CA_FILE,
tlsCertificateKeyFile: ''
});
});
it('should throw an error at connect time', async () => {
const err = await client.connect().catch(e => e);
expect(err).to.be.instanceof(Error);
});
});
});
context('when providing tlsCRLFile', () => {
context('when the file will revoke the certificate', () => {
let client: MongoClient;
beforeEach(() => {
client = new MongoClient(CONNECTION_STRING, {
tls: true,
tlsCAFile: TLS_CA_FILE,
tlsCRLFile: TLS_CRL_FILE,
serverSelectionTimeoutMS: 5000,
connectTimeoutMS: 5000
});
});
afterEach(async () => {
await client?.close();
});
it('throws a MongoServerSelectionError', async () => {
const err = await client.connect().catch(e => e);
expect(err).to.be.instanceOf(MongoServerSelectionError);
});
});
});
context('when tlsCertificateKeyFile is provided, but tlsCAFile is missing', () => {
let client: MongoClient;
beforeEach(() => {
client = new MongoClient(CONNECTION_STRING, {
tls: true,
tlsCertificateKeyFile: TLS_CERT_KEY_FILE,
serverSelectionTimeoutMS: 5000,
connectTimeoutMS: 5000
});
});
afterEach(async () => {
if (client) await client.close();
});
it('throws a MongoServerSelectionError', async () => {
const err = await client.connect().catch(e => e);
expect(err).to.be.instanceOf(MongoServerSelectionError);
});
});
context('when tlsCAFile is provided, but tlsCertificateKeyFile is missing', () => {
let client: MongoClient;
beforeEach(() => {
client = new MongoClient(CONNECTION_STRING, {
tls: true,
tlsCAFile: TLS_CA_FILE
});
});
afterEach(async () => {
if (client) await client.close();
});
it('connects without error', async () => {
const clientOrError = await client.connect().catch(e => e);
expect(clientOrError).to.be.instanceOf(MongoClient);
});
});
});
function makeConnectionTest(connectionString: string, clientOptions?: MongoClientOptions) {
return async function () {
const client = new MongoClient(connectionString, clientOptions);
await client.connect();
await client.db('admin').command({ [LEGACY_HELLO_COMMAND]: 1 });
await client.db('test').collection('test').findOne({});
return await client.close();
};
}