forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_encryption.test.ts
More file actions
419 lines (369 loc) · 15.8 KB
/
client_encryption.test.ts
File metadata and controls
419 lines (369 loc) · 15.8 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
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
414
415
416
417
418
419
import { Binary, BSON, deserialize } from 'bson';
import { expect } from 'chai';
import * as fs from 'fs';
import { resolve } from 'path';
import * as sinon from 'sinon';
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
import * as cryptoCallbacks from '../../../src/client-side-encryption/crypto_callbacks';
import {
MongoCryptCreateDataKeyError,
MongoCryptCreateEncryptedCollectionError
} from '../../../src/client-side-encryption/errors';
import { StateMachine } from '../../../src/client-side-encryption/state_machine';
import { MongoClient } from '../../../src/mongo_client';
const { EJSON } = BSON;
class MockClient {
options: any;
s: { options: any };
constructor(options?: any) {
this.options = { options: options || {} };
this.s = { options: this.options };
}
db(dbName) {
return {
async createCollection(name, options) {
return { namespace: `${dbName}.${name}`, options };
}
};
}
}
describe('ClientEncryption', function () {
this.timeout(12000);
context('with stubbed key material and fixed random source', function () {
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
});
beforeEach(() => {
const rndData = Buffer.from(
'\x4d\x06\x95\x64\xf5\xa0\x5e\x9e\x35\x23\xb9\x8f\x57\x5a\xcb\x15',
'latin1'
);
let rndPos = 0;
sandbox.stub(cryptoCallbacks, 'randomHook').callsFake((buffer, count) => {
if (rndPos + count > rndData.length) {
return new Error('Out of fake random data');
}
buffer.set(rndData.subarray(rndPos, rndPos + count));
rndPos += count;
return count;
});
// stubbed out for AWS unit testing below
sandbox.stub(StateMachine.prototype, 'fetchKeys').callsFake((client, ns, filter) => {
filter = deserialize(filter);
const keyIds = filter.$or[0]._id.$in.map(key => key.toString('hex'));
const fileNames = keyIds.map(keyId =>
resolve(`${__dirname}/data/keys/${keyId.toUpperCase()}-local-document.json`)
);
const contents = fileNames.map(filename =>
EJSON.parse(fs.readFileSync(filename, { encoding: 'utf-8' }))
);
return Promise.resolve(contents);
});
});
// This exactly matches _test_encrypt_fle2_explicit from the C tests
it('should explicitly encrypt and decrypt with the "local" KMS provider (FLE2, exact result)', function () {
const encryption = new ClientEncryption(new MockClient(), {
keyVaultNamespace: 'client.encryption',
kmsProviders: { local: { key: Buffer.alloc(96) } }
});
const encryptOptions = {
keyId: new Binary(Buffer.from('ABCDEFAB123498761234123456789012', 'hex'), 4),
algorithm: 'Unindexed'
};
return encryption
.encrypt('value123', encryptOptions)
.then(encrypted => {
expect(encrypted._bsontype).to.equal('Binary');
expect(encrypted.sub_type).to.equal(6);
return encryption.decrypt(encrypted);
})
.then(decrypted => {
expect(decrypted).to.equal('value123');
});
});
});
it('should provide the libmongocrypt version', function () {
expect(ClientEncryption.libmongocryptVersion).to.be.a('string');
});
describe('constructor', () => {
describe('_timeoutMS', () => {
const LOCAL_MASTERKEY = Buffer.from(
'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk',
'base64'
);
context('when timeoutMS is provided in ClientEncryptionOptions and client', function () {
it('sets clientEncryption._timeoutMS to ClientEncryptionOptions.timeoutMS value', function () {
const client = new MongoClient('mongodb://a/', { timeoutMS: 100 });
const clientEncryption = new ClientEncryption(client, {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { local: { key: LOCAL_MASTERKEY } },
timeoutMS: 500
});
expect(clientEncryption._timeoutMS).to.equal(500);
});
});
context('when timeoutMS is only provided in ClientEncryptionOptions', function () {
it('sets clientEncryption._timeoutMS to ClientEncryptionOptions.timeoutMS value', function () {
const client = new MongoClient('mongodb://a/');
const clientEncryption = new ClientEncryption(client, {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { local: { key: LOCAL_MASTERKEY } },
timeoutMS: 500
});
expect(clientEncryption._timeoutMS).to.equal(500);
});
});
context('when timeoutMS is only provided in client', function () {
it('sets clientEncryption._timeoutMS to client.timeoutMS value', function () {
const client = new MongoClient('mongodb://a/', { timeoutMS: 100 });
const clientEncryption = new ClientEncryption(client, {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { local: { key: LOCAL_MASTERKEY } }
});
expect(clientEncryption._timeoutMS).to.equal(100);
});
});
});
});
describe('createEncryptedCollection()', () => {
let clientEncryption;
const client = new MockClient();
let db;
const collectionName = 'secure';
beforeEach(async function () {
clientEncryption = new ClientEncryption(client, {
keyVaultNamespace: 'client.encryption',
kmsProviders: { local: { key: Buffer.alloc(96, 0) } }
});
db = client.db('createEncryptedCollectionDb');
});
afterEach(async () => {
sinon.restore();
});
context('validates input', () => {
it('throws TypeError if options are omitted', async () => {
const error = await clientEncryption
.createEncryptedCollection(db, collectionName)
.catch(error => error);
expect(error)
.to.be.instanceOf(TypeError)
.to.match(/provider/);
});
it('throws TypeError if options.createCollectionOptions are omitted', async () => {
const error = await clientEncryption
.createEncryptedCollection(db, collectionName, {})
.catch(error => error);
expect(error)
.to.be.instanceOf(TypeError)
.to.match(/encryptedFields/);
});
it('throws TypeError if options.createCollectionOptions.encryptedFields are omitted', async () => {
const error = await clientEncryption
.createEncryptedCollection(db, collectionName, { createCollectionOptions: {} })
.catch(error => error);
expect(error)
.to.be.instanceOf(TypeError)
.to.match(/Cannot read properties/);
});
});
context('when options.encryptedFields.fields is not an array', () => {
it('does not generate any encryption keys', async () => {
const createCollectionSpy = sinon.spy(db, 'createCollection');
const createDataKeySpy = sinon.spy(clientEncryption, 'createDataKey');
await clientEncryption.createEncryptedCollection(db, collectionName, {
createCollectionOptions: { encryptedFields: { fields: 'not an array' } }
});
expect(createDataKeySpy.callCount).to.equal(0);
const options = createCollectionSpy.getCall(0).args[1];
expect(options).to.deep.equal({
encryptedFields: { fields: 'not an array' },
timeoutMS: undefined
});
});
});
context('when options.encryptedFields.fields elements are not objects', () => {
it('they are passed along to createCollection', async () => {
const createCollectionSpy = sinon.spy(db, 'createCollection');
const keyId = new Binary(Buffer.alloc(16, 0));
const createDataKeyStub = sinon.stub(clientEncryption, 'createDataKey').resolves(keyId);
await clientEncryption.createEncryptedCollection(db, collectionName, {
createCollectionOptions: {
encryptedFields: { fields: ['not an array', { keyId: null }, { keyId: {} }] }
}
});
expect(createDataKeyStub.callCount).to.equal(1);
const options = createCollectionSpy.getCall(0).args[1];
expect(options).to.deep.equal({
encryptedFields: { fields: ['not an array', { keyId: keyId }, { keyId: {} }] },
timeoutMS: undefined
});
});
});
it('only passes options.masterKey to createDataKey', async () => {
const masterKey = Symbol('key');
const createDataKey = sinon
.stub(clientEncryption, 'createDataKey')
.resolves(new Binary(Buffer.alloc(16, 0)));
const result = await clientEncryption.createEncryptedCollection(db, collectionName, {
provider: 'aws',
createCollectionOptions: { encryptedFields: { fields: [{}] } },
masterKey
});
expect(result).to.have.property('collection');
expect(createDataKey).to.have.been.calledOnceWithExactly('aws', {
masterKey,
timeoutContext: undefined
});
});
context('when createDataKey rejects', () => {
const customErrorEvil = new Error('evil!');
const customErrorGood = new Error('good!');
const keyId = new Binary(Buffer.alloc(16, 0), 4);
const createCollectionOptions = {
encryptedFields: { fields: [{}, {}, { keyId: 'cool id!' }, {}] }
};
const createDataKeyRejection = async () => {
const stub = sinon.stub(clientEncryption, 'createDataKey');
stub.onCall(0).resolves(keyId);
stub.onCall(1).rejects(customErrorEvil);
stub.onCall(2).rejects(customErrorGood);
stub.onCall(4).resolves(keyId);
const error = await clientEncryption
.createEncryptedCollection(db, collectionName, {
provider: 'local',
createCollectionOptions
})
.catch(error => error);
// At least make sure the function did not succeed
expect(error).to.be.instanceOf(Error);
return error;
};
it('throws MongoCryptCreateDataKeyError', async () => {
const error = await createDataKeyRejection();
expect(error).to.be.instanceOf(MongoCryptCreateDataKeyError);
});
it('thrown error has a cause set to the first error that was thrown from createDataKey', async () => {
const error = await createDataKeyRejection();
expect(error.cause).to.equal(customErrorEvil);
expect(error.message).to.include(customErrorEvil.message);
});
it('thrown error contains partially filled encryptedFields.fields', async () => {
const error = await createDataKeyRejection();
expect(error.encryptedFields).property('fields').that.is.an('array');
expect(error.encryptedFields.fields).to.have.lengthOf(
createCollectionOptions.encryptedFields.fields.length
);
expect(error.encryptedFields.fields).to.have.nested.property('[0].keyId', keyId);
expect(error.encryptedFields.fields).to.not.have.nested.property('[1].keyId');
expect(error.encryptedFields.fields).to.have.nested.property('[2].keyId', 'cool id!');
});
});
context('when createCollection rejects', () => {
const customError = new Error('evil!');
const keyId = new Binary(Buffer.alloc(16, 0), 4);
const createCollectionRejection = async () => {
const stubCreateDataKey = sinon.stub(clientEncryption, 'createDataKey');
stubCreateDataKey.onCall(0).resolves(keyId);
stubCreateDataKey.onCall(1).resolves(keyId);
stubCreateDataKey.onCall(2).resolves(keyId);
sinon.stub(db, 'createCollection').rejects(customError);
const createCollectionOptions = {
encryptedFields: { fields: [{}, {}, { keyId: 'cool id!' }] }
};
const error = await clientEncryption
.createEncryptedCollection(db, collectionName, {
provider: 'local',
createCollectionOptions
})
.catch(error => error);
// At least make sure the function did not succeed
expect(error).to.be.instanceOf(Error);
return error;
};
it('throws MongoCryptCreateEncryptedCollectionError', async () => {
const error = await createCollectionRejection();
expect(error).to.be.instanceOf(MongoCryptCreateEncryptedCollectionError);
});
it('thrown error has a cause set to the error that was thrown from createCollection', async () => {
const error = await createCollectionRejection();
expect(error.cause).to.equal(customError);
expect(error.message).to.include(customError.message);
});
it('thrown error contains filled encryptedFields.fields', async () => {
const error = await createCollectionRejection();
expect(error.encryptedFields).property('fields').that.is.an('array');
expect(error.encryptedFields.fields).to.have.nested.property('[0].keyId', keyId);
expect(error.encryptedFields.fields).to.have.nested.property('[1].keyId', keyId);
expect(error.encryptedFields.fields).to.have.nested.property('[2].keyId', 'cool id!');
});
});
context('when there are nullish keyIds in the encryptedFields.fields array', function () {
it('does not mutate the input fields array when generating data keys', async () => {
const encryptedFields = Object.freeze({
escCollection: 'esc',
eccCollection: 'ecc',
ecocCollection: 'ecoc',
fields: Object.freeze([
Object.freeze({ keyId: false }),
Object.freeze({
keyId: null,
path: 'name',
bsonType: 'int',
queries: Object.freeze({ contentionFactor: 0 })
}),
null
])
});
const keyId = new Binary(Buffer.alloc(16, 0), 4);
sinon.stub(clientEncryption, 'createDataKey').resolves(keyId);
const { collection, encryptedFields: resultEncryptedFields } =
await clientEncryption.createEncryptedCollection(db, collectionName, {
provider: 'local',
createCollectionOptions: {
encryptedFields
}
});
expect(collection).to.have.property('namespace', 'createEncryptedCollectionDb.secure');
expect(encryptedFields, 'original encryptedFields should be unmodified').nested.property(
'fields[0].keyId',
false
);
expect(
resultEncryptedFields,
'encryptedFields created by helper should have replaced nullish keyId'
).nested.property('fields[1].keyId', keyId);
expect(encryptedFields, 'original encryptedFields should be unmodified').nested.property(
'fields[2]',
null
);
});
it('generates dataKeys for all null keyIds in the fields array', async () => {
const encryptedFields = Object.freeze({
escCollection: 'esc',
eccCollection: 'ecc',
ecocCollection: 'ecoc',
fields: Object.freeze([
Object.freeze({ keyId: null }),
Object.freeze({ keyId: null }),
Object.freeze({ keyId: null })
])
});
const keyId = new Binary(Buffer.alloc(16, 0), 4);
sinon.stub(clientEncryption, 'createDataKey').resolves(keyId);
const { collection, encryptedFields: resultEncryptedFields } =
await clientEncryption.createEncryptedCollection(db, collectionName, {
provider: 'local',
createCollectionOptions: {
encryptedFields
}
});
expect(collection).to.have.property('namespace', 'createEncryptedCollectionDb.secure');
expect(resultEncryptedFields.fields).to.have.lengthOf(3);
expect(resultEncryptedFields.fields.filter(({ keyId }) => keyId === null)).to.have.lengthOf(
0
);
});
});
});
});