-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathqueryable_encryption_change_stream.js
393 lines (353 loc) · 14.6 KB
/
queryable_encryption_change_stream.js
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
//
// Basic $changeStream tests for operations that perform queryable encryption.
//
// @tags: [
// change_stream_does_not_expect_txns,
// assumes_unsharded_collection,
// requires_fcv_71
// ]
//
import {EncryptedClient, isEnterpriseShell} from "jstests/fle2/libs/encrypted_client_util.js";
import {
canonicalizeEventForTesting,
ChangeStreamTest
} from "jstests/libs/query/change_stream_util.js";
if (!isEnterpriseShell()) {
jsTestLog("Skipping test as it requires the enterprise module");
quit();
}
const dbName = "qetestdb";
const collName = "qetestcoll";
const initialConn = db.getMongo();
const testDb = db.getSiblingDB(dbName);
const placeholderBinData0 = BinData(0, "WMdGo/tcDkE4UL6bgGYTN6oKFitgLXvhyhB9sbKxprk=");
const placeholderBinData6 = BinData(6, "WMdGo/tcDkE4UL6bgGYTN6oKFitgLXvhyhB9sbKxprk=");
const placeholderOID = ObjectId();
function replaceRandomDataWithPlaceholders(event) {
for (let field in event) {
if (!Object.prototype.hasOwnProperty.call(event, field)) {
continue;
}
if (event[field] instanceof BinData) {
if (event[field].subtype() === 6) {
event[field] = placeholderBinData6;
} else if (event[field].subtype() === 0) {
event[field] = placeholderBinData0;
}
} else if (event[field] instanceof ObjectId) {
event[field] = placeholderOID;
} else if (typeof event[field] === "object") {
replaceRandomDataWithPlaceholders(event[field]);
}
}
}
const eventModifier = function(event, expected) {
if (event.hasOwnProperty("fullDocument") || event.hasOwnProperty("documentKey")) {
replaceRandomDataWithPlaceholders(event);
}
return canonicalizeEventForTesting(event, expected);
};
testDb.dropDatabase();
let encryptedClient = new EncryptedClient(initialConn, dbName);
assert.commandWorked(encryptedClient.createEncryptionCollection(collName, {
encryptedFields: {
"fields": [
{
"path": "first",
"bsonType": "string",
/* contention: 0 is required for the cleanup tests to work */
"queries": {"queryType": "equality", "contention": 0}
},
]
}
}));
const cst = new ChangeStreamTest(testDb, {eventModifier});
const ecoll = encryptedClient.getDB()[collName];
const [escName, ecocName] = (() => {
let names = encryptedClient.getStateCollectionNamespaces(collName);
return [names.esc, names.ecoc];
})();
const escInsertChange = {
documentKey: {_id: placeholderBinData0},
fullDocument: {_id: placeholderBinData0},
ns: {db: dbName, coll: escName},
operationType: "insert",
};
const ecocInsertChange = {
documentKey: {_id: placeholderOID},
fullDocument: {_id: placeholderOID, fieldName: "first", value: placeholderBinData0},
ns: {db: dbName, coll: ecocName},
operationType: "insert",
};
function expectedEDCInsertChange(id, last, implicitShardKey = undefined) {
let expected = {
documentKey: {_id: id},
fullDocument: {
_id: id,
first: placeholderBinData6,
last: last,
"__safeContent__": [placeholderBinData0]
},
ns: {db: dbName, coll: collName},
operationType: "insert",
};
if (encryptedClient.useImplicitSharding && implicitShardKey) {
expected.documentKey = Object.assign(expected.documentKey, implicitShardKey);
}
return expected;
}
let expectedChange = undefined;
const testValues = [
["frodo", "baggins"],
["merry", "brandybuck"],
["pippin", "took"],
["sam", "gamgee"],
["rosie", "gamgee"],
["paladin", "took"],
];
let cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
let cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
// Test that if there are no changes, we return an empty batch.
assert.eq(0, cursor.firstBatch.length, "Cursor had changes: " + tojson(cursor));
assert.eq(0, cursordb.firstBatch.length, "Cursor had changes: " + tojson(cursordb));
jsTestLog("Testing single insert");
{
assert.commandWorked(ecoll.einsert({_id: 0, first: "frodo", last: "baggins"}));
expectedChange = expectedEDCInsertChange(0, "baggins", {last: "baggins"});
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expectedChange]});
cst.assertNextChangesEqualUnordered(
{cursor: cursordb, expectedChanges: [expectedChange, escInsertChange, ecocInsertChange]});
cst.assertNoChange(cursor);
cst.assertNoChange(cursordb);
}
jsTestLog("Testing second insert");
{
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
assert.commandWorked(ecoll.einsert({_id: 1, first: "merry", last: "brandybuck"}));
expectedChange = expectedEDCInsertChange(1, "brandybuck", {last: "brandybuck"});
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expectedChange]});
cst.assertNextChangesEqualUnordered(
{cursor: cursordb, expectedChanges: [expectedChange, escInsertChange, ecocInsertChange]});
cst.assertNoChange(cursor);
cst.assertNoChange(cursordb);
}
jsTestLog("Testing replacement update");
{
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
assert.commandWorked(
ecoll.ereplaceOne({last: "baggins"}, {first: "pippin", last: "took", location: "shire"}));
expectedChange = expectedEDCInsertChange(0, "took", {last: "baggins"});
expectedChange.operationType = "replace";
expectedChange.fullDocument.location = "shire";
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expectedChange]});
cst.assertNextChangesEqualUnordered(
{cursor: cursordb, expectedChanges: [expectedChange, escInsertChange, ecocInsertChange]});
}
jsTestLog("Testing upsert");
{
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
assert.commandWorked(ecoll.ereplaceOne(
{last: "gamgee"}, {_id: 2, first: "sam", last: "gamgee"}, {upsert: true}));
expectedChange = expectedEDCInsertChange(2, "gamgee", {last: "gamgee"});
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expectedChange]});
cst.assertNextChangesEqualUnordered(
{cursor: cursordb, expectedChanges: [expectedChange, escInsertChange, ecocInsertChange]});
cst.assertNoChange(cursor);
cst.assertNoChange(cursordb);
}
jsTestLog("Testing modification update");
{
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
assert.commandWorked(ecoll.eupdateOne({last: "gamgee"}, {$set: {first: "rosie"}}));
expectedChange = {
documentKey: {_id: 2},
ns: {db: dbName, coll: collName},
operationType: "update",
updateDescription: {
removedFields: [],
updatedFields: {first: placeholderBinData6, "__safeContent__.1": placeholderBinData0},
truncatedArrays: []
},
};
let safeContentPullChange = {
documentKey: {_id: 2},
ns: {db: dbName, coll: collName},
operationType: "update",
updateDescription: {
removedFields: [],
updatedFields: {"__safeContent__": [placeholderBinData0]},
truncatedArrays: []
},
};
if (encryptedClient.useImplicitSharding) {
expectedChange.documentKey.last = "gamgee";
safeContentPullChange.documentKey.last = "gamgee";
}
cst.assertNextChangesEqual(
{cursor: cursor, expectedChanges: [expectedChange, safeContentPullChange]});
cst.assertNextChangesEqualUnordered({
cursor: cursordb,
expectedChanges: [expectedChange, escInsertChange, ecocInsertChange, safeContentPullChange]
});
cst.assertNoChange(cursor);
cst.assertNoChange(cursordb);
}
jsTestLog("Testing findAndModify");
{
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
assert.commandWorked(ecoll.erunCommand({
findAndModify: ecoll.getName(),
query: {last: "took"},
update: {$set: {first: "paladin"}, $unset: {location: ""}},
}));
expectedChange = {
documentKey: {_id: 0},
ns: {db: dbName, coll: collName},
operationType: "update",
updateDescription: {
removedFields: ["location"],
updatedFields: {first: placeholderBinData6, "__safeContent__.1": placeholderBinData0},
truncatedArrays: []
},
};
let safeContentPullChange = {
documentKey: {_id: 0},
ns: {db: dbName, coll: collName},
operationType: "update",
updateDescription: {
removedFields: [],
updatedFields: {"__safeContent__": [placeholderBinData0]},
truncatedArrays: []
},
};
if (encryptedClient.useImplicitSharding) {
expectedChange.documentKey.last = "took";
safeContentPullChange.documentKey.last = "took";
}
cst.assertNextChangesEqual(
{cursor: cursor, expectedChanges: [expectedChange, safeContentPullChange]});
cst.assertNextChangesEqualUnordered({
cursor: cursordb,
expectedChanges: [expectedChange, escInsertChange, ecocInsertChange, safeContentPullChange]
});
cst.assertNoChange(cursor);
cst.assertNoChange(cursordb);
}
jsTestLog("Testing delete");
{
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
assert.commandWorked(ecoll.edeleteOne({last: "gamgee"}));
expectedChange = {
documentKey: {_id: 2},
ns: {db: dbName, coll: collName},
operationType: "delete",
};
if (encryptedClient.useImplicitSharding) {
expectedChange.documentKey.last = "gamgee";
}
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expectedChange]});
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [expectedChange]});
cst.assertNoChange(cursor);
cst.assertNoChange(cursordb);
}
const ecocRenameChange = {
operationType: "rename",
ns: {db: dbName, coll: ecocName},
to: {db: dbName, coll: ecocName + ".compact"},
};
const escDeleteChange = {
operationType: "delete",
ns: {db: dbName, coll: escName},
documentKey: {_id: placeholderBinData0},
};
const ecocCompactDropChange = {
operationType: "drop",
ns: {db: dbName, coll: ecocName + ".compact"},
};
jsTestLog("Testing compact");
{
// all non-anchors will be deleted by compact
const deleteCount = testDb[escName].countDocuments({value: {$exists: false}});
const numUniqueValues = testValues.length;
encryptedClient.assertEncryptedCollectionCounts(collName, 2, numUniqueValues, numUniqueValues);
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
encryptedClient.runEncryptionOperation(() => {
assert.commandWorked(ecoll.compact());
});
encryptedClient.assertEncryptedCollectionCounts(collName, 2, numUniqueValues, 0);
const anchorCount = testDb[escName].countDocuments({value: {$exists: true}});
const nonAnchorCount = testDb[escName].countDocuments({value: {$exists: false}});
assert.eq(anchorCount, numUniqueValues);
assert.eq(nonAnchorCount, 0);
cst.assertNoChange(cursor);
escInsertChange.fullDocument.value = placeholderBinData0;
// temp ecoc rename
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [ecocRenameChange]});
// normal anchor inserts
for (let i = 0; i < numUniqueValues; i++) {
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [escInsertChange]});
}
// non-anchor deletes
for (let i = 0; i < deleteCount; i++) {
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [escDeleteChange]});
}
// temp ecoc drop
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [ecocCompactDropChange]});
cst.assertNoChange(cursordb);
}
jsTestLog("Testing cleanup");
{
// insert new documents for each test value, so the ECOC & ESC have documents to clean up
for (let val of testValues) {
assert.commandWorked(ecoll.einsert({first: val[0], last: val[1]}));
}
// ESC doesn't have null anchors yet, so the total delete count == ESC count before cleanup
const deleteCount = testDb[escName].countDocuments({});
const nonAnchorCount = testDb[escName].countDocuments({value: {$exists: false}});
const anchorCount = deleteCount - nonAnchorCount;
const numUniqueValues = testValues.length;
encryptedClient.assertEncryptedCollectionCounts(
collName, 2 + numUniqueValues, numUniqueValues * 2, numUniqueValues);
assert.eq(anchorCount, numUniqueValues);
assert.eq(nonAnchorCount, numUniqueValues);
cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: testDb[collName]});
cursordb = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
encryptedClient.runEncryptionOperation(() => {
assert.commandWorked(ecoll.cleanup());
});
encryptedClient.assertEncryptedCollectionCounts(
collName, 2 + numUniqueValues, numUniqueValues, 0);
encryptedClient.assertESCNonAnchorCount(collName, 0);
cst.assertNoChange(cursor);
// temp ecoc rename
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [ecocRenameChange]});
// null anchor inserts
escInsertChange.fullDocument.value = placeholderBinData0;
for (let i = 0; i < anchorCount; i++) {
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [escInsertChange]});
}
// non-anchors and regular anchors are deleted from ESC
for (let i = 0; i < deleteCount; i++) {
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [escDeleteChange]});
}
// temp ecoc.compact drop
cst.assertNextChangesEqual({cursor: cursordb, expectedChanges: [ecocCompactDropChange]});
cst.assertNoChange(cursordb);
}
cst.cleanUp();