-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclient_side_encryption.prose.25.lookup.test.ts
More file actions
363 lines (330 loc) · 10.3 KB
/
client_side_encryption.prose.25.lookup.test.ts
File metadata and controls
363 lines (330 loc) · 10.3 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
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { expect } from 'chai';
import { BSON, type Document, type MongoClient } from '../../../src';
import { getCSFLEKMSProviders } from '../../csfle-kms-providers';
import { type TestConfiguration } from '../../tools/runner/config';
import { getEncryptExtraOptions } from '../../tools/utils';
const defaultMetadata: MongoDBMetadataUI = {
requires: {
topology: '!single',
clientSideEncryption: '>=6.3.0',
mongodb: '>=7.0.0'
}
};
const readFixture = async (name: string) =>
BSON.EJSON.parse(
await fs.readFile(
path.resolve(__dirname, `../../spec/client-side-encryption/etc/data/lookup/${name}`),
'utf8'
)
);
const newEncryptedClient = ({ configuration }: { configuration: TestConfiguration }) =>
configuration.newClient(
{},
{
writeConcern: { w: 'majority' },
autoEncryption: {
keyVaultNamespace: 'db.keyvault',
kmsProviders: { local: getCSFLEKMSProviders().local },
extraOptions: getEncryptExtraOptions()
}
}
);
describe('$lookup support', defaultMetadata, function () {
before(async function () {
const mochaTest = { metadata: defaultMetadata };
if (!this.configuration.filters.MongoDBVersionFilter.filter(mochaTest)) {
return;
}
if (!this.configuration.filters.MongoDBTopologyFilter.filter(mochaTest)) {
return;
}
if (!this.configuration.filters.ClientSideEncryptionFilter.filter(mochaTest)) {
return;
}
let unencryptedClient: MongoClient, encryptedClient: MongoClient;
try {
/**
* Create an encrypted MongoClient configured with:
*
* ```txt
* AutoEncryptionOpts(
* keyVaultNamespace="db.keyvault",
* kmsProviders={"local": { "key": "<base64 decoding of LOCAL_MASTERKEY>" }}
* )
* ```
*/
encryptedClient = newEncryptedClient(this);
/** Drop database db. */
await encryptedClient.db('db').dropDatabase();
/** Insert `key-doc.json` into db.keyvault. */
const keyDoc = await readFixture('key-doc.json');
await encryptedClient.db('db').collection('keyvault').insertOne(keyDoc);
/**
* Create the following collections:
* ```
* db.csfle with options: { "validator": { "$jsonSchema": "<schema-csfle.json>"}}.
* db.csfle2 with options: { "validator": { "$jsonSchema": "<schema-csfle2.json>"}}.
* db.qe with options: { "encryptedFields": "<schema-qe.json>"}.
* db.qe2 with options: { "encryptedFields": "<schema-qe2.json>"}.
* db.no_schema with no options.
* db.no_schema2 with no options.
* ```
*/
const collections = [
{
name: 'csfle',
options: { validator: { $jsonSchema: await readFixture('schema-csfle.json') } },
document: { csfle: 'csfle' }
},
{
name: 'csfle2',
options: { validator: { $jsonSchema: await readFixture('schema-csfle2.json') } },
document: { csfle2: 'csfle2' }
},
{
name: 'qe',
options: { encryptedFields: await readFixture('schema-qe.json') },
document: { qe: 'qe' }
},
{
name: 'qe2',
options: { encryptedFields: await readFixture('schema-qe2.json') },
document: { qe2: 'qe2' }
},
{
name: 'no_schema',
options: {},
document: { no_schema: 'no_schema' }
},
{
name: 'no_schema2',
options: {},
document: { no_schema2: 'no_schema2' }
}
];
for (const { name, options } of collections) {
await encryptedClient.db('db').createCollection(name, options);
}
/** Create an unencrypted MongoClient. */
unencryptedClient = this.configuration.newClient({}, { writeConcern: { w: 'majority' } });
/**
* ```
* {"csfle": "csfle"} into db.csfle
* Use the unencrypted client to retrieve it. Assert the csfle field is BSON binary.
* {"csfle2": "csfle2"} into db.csfle2
* Use the unencrypted client to retrieve it. Assert the csfle2 field is BSON binary.
* {"qe": "qe"} into db.qe
* Use the unencrypted client to retrieve it. Assert the qe field is BSON binary.
* {"qe2": "qe2"} into db.qe2
* Use the unencrypted client to retrieve it. Assert the qe2 field is BSON binary.
* {"no_schema": "no_schema"} into db.no_schema
* {"no_schema2": "no_schema2"} into db.no_schema2
* ```
*/
for (const { name, document } of collections) {
const { insertedId } = await encryptedClient.db('db').collection(name).insertOne(document);
if (name.startsWith('no_')) continue;
expect(await unencryptedClient.db('db').collection(name).findOne(insertedId))
.to.have.property(Object.keys(document)[0])
.that.has.property('_bsontype', 'Binary');
}
} finally {
await unencryptedClient?.close();
await encryptedClient?.close();
}
});
const test = function (
title: string,
collName: string,
pipeline: Document[],
expected: Document | RegExp,
metadata?: MongoDBMetadataUI
) {
describe(title.slice(0, title.indexOf(':')), function () {
let client: MongoClient;
beforeEach(async function () {
client = newEncryptedClient(this);
});
afterEach(async function () {
await client.close();
});
it(title.slice(title.indexOf(':') + 1).trim(), metadata ?? defaultMetadata, async () => {
const collection = client.db('db').collection(collName);
const actual = await collection
.aggregate(pipeline)
.toArray()
.catch(error => error);
const expectedError = expected instanceof RegExp;
if (expectedError) {
expect(actual).to.be.instanceOf(Error);
if (!expected.test(actual.message)) {
throw actual;
}
} else if (actual instanceof Error) {
throw actual;
} else {
expect(actual).to.have.lengthOf(1);
expect(actual[0]).to.deep.equal(expected);
}
});
});
};
test(
'Case 1: db.csfle joins db.no_schema',
'csfle',
[
{ $match: { csfle: 'csfle' } },
{
$lookup: {
from: 'no_schema',
as: 'matched',
pipeline: [{ $match: { no_schema: 'no_schema' } }, { $project: { _id: 0 } }]
}
},
{ $project: { _id: 0 } }
],
{ csfle: 'csfle', matched: [{ no_schema: 'no_schema' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 2: db.qe joins db.no_schema',
'qe',
[
{ $match: { qe: 'qe' } },
{
$lookup: {
from: 'no_schema',
as: 'matched',
pipeline: [
{ $match: { no_schema: 'no_schema' } },
{ $project: { _id: 0, __safeContent__: 0 } }
]
}
},
{ $project: { _id: 0, __safeContent__: 0 } }
],
{ qe: 'qe', matched: [{ no_schema: 'no_schema' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 3: db.no_schema joins db.csfle',
'no_schema',
[
{ $match: { no_schema: 'no_schema' } },
{
$lookup: {
from: 'csfle',
as: 'matched',
pipeline: [{ $match: { csfle: 'csfle' } }, { $project: { _id: 0 } }]
}
},
{ $project: { _id: 0 } }
],
{ no_schema: 'no_schema', matched: [{ csfle: 'csfle' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 4: db.no_schema joins db.qe',
'no_schema',
[
{ $match: { no_schema: 'no_schema' } },
{
$lookup: {
from: 'qe',
as: 'matched',
pipeline: [{ $match: { qe: 'qe' } }, { $project: { _id: 0, __safeContent__: 0 } }]
}
},
{ $project: { _id: 0 } }
],
{ no_schema: 'no_schema', matched: [{ qe: 'qe' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 5: db.csfle joins db.csfle2',
'csfle',
[
{ $match: { csfle: 'csfle' } },
{
$lookup: {
from: 'csfle2',
as: 'matched',
pipeline: [{ $match: { csfle2: 'csfle2' } }, { $project: { _id: 0 } }]
}
},
{ $project: { _id: 0 } }
],
{ csfle: 'csfle', matched: [{ csfle2: 'csfle2' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 6: db.qe joins db.qe2',
'qe',
[
{ $match: { qe: 'qe' } },
{
$lookup: {
from: 'qe2',
as: 'matched',
pipeline: [{ $match: { qe2: 'qe2' } }, { $project: { _id: 0, __safeContent__: 0 } }]
}
},
{ $project: { _id: 0, __safeContent__: 0 } }
],
{ qe: 'qe', matched: [{ qe2: 'qe2' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 7: db.no_schema joins db.no_schema2',
'no_schema',
[
{ $match: { no_schema: 'no_schema' } },
{
$lookup: {
from: 'no_schema2',
as: 'matched',
pipeline: [{ $match: { no_schema2: 'no_schema2' } }, { $project: { _id: 0 } }]
}
},
{ $project: { _id: 0 } }
],
{ no_schema: 'no_schema', matched: [{ no_schema2: 'no_schema2' }] },
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 8: db.csfle joins db.qe',
'csfle',
[
{ $match: { csfle: 'qe' } },
{
$lookup: {
from: 'qe',
as: 'matched',
pipeline: [{ $match: { qe: 'qe' } }, { $project: { _id: 0 } }]
}
},
{ $project: { _id: 0 } }
],
/not supported/i,
{ requires: { ...defaultMetadata.requires, mongodb: '>=8.1.0' } }
);
test(
'Case 9: test error with <8.1',
'csfle',
[
{ $match: { csfle: 'csfle' } },
{
$lookup: {
from: 'no_schema',
as: 'matched',
pipeline: [{ $match: { no_schema: 'no_schema' } }, { $project: { _id: 0 } }]
}
},
{ $project: { _id: 0 } }
],
/Upgrade/i,
{ requires: { ...defaultMetadata.requires, mongodb: '>=7.0.0 <8.1.0' } }
);
});