-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathmultitenancy_with_mongoq_basic_commands.js
706 lines (597 loc) · 30.7 KB
/
multitenancy_with_mongoq_basic_commands.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Test basic db operations in multitenancy using a securityToken.
import {arrayEq} from "jstests/aggregation/extras/utils.js";
import {FeatureFlagUtil} from "jstests/libs/feature_flag_util.js";
import {ReplSetTest} from "jstests/libs/replsettest.js";
function checkNsSerializedCorrectly(kDbName, kCollectionName, nsField) {
const nss = kDbName + (kCollectionName == "" ? "" : "." + kCollectionName);
assert.eq(nsField, nss);
}
const kVTSKey = 'secret';
const rst = new ReplSetTest({
nodes: 2,
nodeOptions: {
auth: '',
setParameter: {
multitenancySupport: true,
featureFlagSecurityToken: true,
testOnlyValidatedTenancyScopeKey: kVTSKey,
}
}
});
rst.startSet({keyFile: 'jstests/libs/key1'});
rst.initiate();
const primary = rst.getPrimary();
const adminDb = primary.getDB('admin');
assert.commandWorked(adminDb.runCommand({createUser: 'admin', pwd: 'pwd', roles: ['root']}));
assert(adminDb.auth('admin', 'pwd'));
const featureFlagRequireTenantId = FeatureFlagUtil.isEnabled(
adminDb,
"RequireTenantID",
);
const kTenant = ObjectId();
const kOtherTenant = ObjectId();
const kDbName = 'myDb';
const kCollName = 'myColl';
const kViewName = "view1";
const tokenConn = new Mongo(primary.host);
const securityToken =
_createSecurityToken({user: "userTenant1", db: '$external', tenant: kTenant}, kVTSKey);
const tokenDB = tokenConn.getDB(kDbName);
// In this jstest, the collection (defined by kCollName) and the document "{_id: 0, a: 1, b: 1}"
// for the tenant (defined by kTenant) will be reused by all command tests. So, any test which
// changes the collection name or document should reset it.
// Test commands using a security token for one tenant.
{
// Create a user for kTenant and then set the security token on the connection.
primary._setSecurityToken(_createTenantToken({tenant: kTenant}));
assert.commandWorked(primary.getDB('$external').runCommand({
createUser: "userTenant1",
roles:
[{role: 'dbAdminAnyDatabase', db: 'admin'}, {role: 'readWriteAnyDatabase', db: 'admin'}]
}));
tokenConn._setSecurityToken(securityToken);
// Logout the root user to avoid multiple authentication.
tokenConn.getDB("admin").logout();
// Create a collection for the tenant kTenant and then insert into it.
assert.commandWorked(tokenDB.createCollection(kCollName));
assert.commandWorked(
tokenDB.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
// Find the document and call getMore on the cursor
{
// Add additional document in order to have two batches for getMore
assert.commandWorked(
tokenDB.runCommand({insert: kCollName, documents: [{_id: 100, x: 1}]}));
const findRes = assert.commandWorked(
tokenDB.runCommand({find: kCollName, filter: {a: 1}, batchSize: 1}));
assert(arrayEq([{_id: 0, a: 1, b: 1}], findRes.cursor.firstBatch), tojson(findRes));
checkNsSerializedCorrectly(kDbName, kCollName, findRes.cursor.ns);
const getMoreRes = assert.commandWorked(
tokenDB.runCommand({getMore: findRes.cursor.id, collection: kCollName}));
checkNsSerializedCorrectly(kDbName, kCollName, getMoreRes.cursor.ns);
}
// Test the aggregate command.
{
const aggRes = assert.commandWorked(
tokenDB.runCommand({aggregate: kCollName, pipeline: [{$match: {a: 1}}], cursor: {}}));
assert(arrayEq([{_id: 0, a: 1, b: 1}], aggRes.cursor.firstBatch), tojson(aggRes));
checkNsSerializedCorrectly(kDbName, kCollName, aggRes.cursor.ns);
}
// Find and modify the document.
{
const fad1 = assert.commandWorked(
tokenDB.runCommand({findAndModify: kCollName, query: {a: 1}, update: {$inc: {a: 10}}}));
assert.eq({_id: 0, a: 1, b: 1}, fad1.value, tojson(fad1));
const fad2 = assert.commandWorked(tokenDB.runCommand(
{findAndModify: kCollName, query: {a: 11}, update: {$set: {a: 1, b: 1}}}));
assert.eq({_id: 0, a: 11, b: 1}, fad2.value, tojson(fad2));
}
// Create a view on the collection, and check that listCollections sees the original
// collection, the view, and the system.views collection. Then, call $listCatalog on
// the collection and views and validate the results.
{
const targetViews = 'system.views';
assert.commandWorked(
tokenDB.runCommand({"create": kViewName, "viewOn": kCollName, pipeline: []}));
const colls =
assert.commandWorked(tokenDB.runCommand({listCollections: 1, nameOnly: true}));
assert.eq(3, colls.cursor.firstBatch.length, tojson(colls.cursor.firstBatch));
const expectedColls = [
{"name": kCollName, "type": "collection"},
{"name": targetViews, "type": "collection"},
{"name": kViewName, "type": "view"}
];
assert(arrayEq(expectedColls, colls.cursor.firstBatch), tojson(colls.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, "$cmd.listCollections", colls.cursor.ns);
const prefixedDbName = kTenant + '_' + tokenDB.getName();
const targetDb = featureFlagRequireTenantId ? tokenDB.getName() : prefixedDbName;
const tokenAdminDB = tokenConn.getDB('admin');
// Get catalog without specifying target collection (collectionless).
let result =
tokenAdminDB.runCommand({aggregate: 1, pipeline: [{$listCatalog: {}}], cursor: {}});
let resultArray = result.cursor.firstBatch;
// Check that the resulting array of catalog entries contains our target databases and
// namespaces.
assert(resultArray.some((entry) => (entry.db === targetDb) && (entry.name === kCollName)),
tojson(resultArray));
// Also check that the resulting array contains views specific to our target database.
assert(resultArray.some((entry) => (entry.db === targetDb) && (entry.name === targetViews)),
tojson(resultArray));
assert(resultArray.some((entry) => (entry.db === targetDb) && (entry.name === kViewName)),
tojson(resultArray));
// Get catalog when specifying our target collection, which should only return one
// result.
result =
tokenDB.runCommand({aggregate: kCollName, pipeline: [{$listCatalog: {}}], cursor: {}});
resultArray = result.cursor.firstBatch;
// Check that the resulting array of catalog entries contains our target databases and
// namespaces.
assert.eq(resultArray.length, 1, tojson(resultArray));
assert(resultArray.some((entry) => (entry.db === targetDb) && (entry.name === kCollName)),
tojson(resultArray));
}
// Test explain command with find
{
const cmdRes = tokenDB.runCommand({explain: {find: kCollName, filter: {a: 1}}});
assert.eq(1, cmdRes.executionStats.nReturned, tojson(cmdRes));
checkNsSerializedCorrectly(kDbName, kCollName, cmdRes.queryPlanner.namespace);
}
// Test count and distinct command.
{
assert.commandWorked(tokenDB.runCommand(
{insert: kCollName, documents: [{_id: 1, c: 1, d: 1}, {_id: 2, c: 1, d: 2}]}));
const resCount =
assert.commandWorked(tokenDB.runCommand({count: kCollName, query: {c: 1}}));
assert.eq(2, resCount.n, tojson(resCount));
const resDistinct =
assert.commandWorked(tokenDB.runCommand({distinct: kCollName, key: 'd', query: {}}));
assert.eq([1, 2], resDistinct.values.sort(), tojson(resDistinct));
}
// Test count on view collection.
{
const resCount =
assert.commandWorked(tokenDB.runCommand({count: kViewName, query: {c: 1}}));
assert.eq(2, resCount.n, tojson(resCount));
}
// Rename the collection.
{
const fromName = kDbName + '.' + kCollName;
const toName = fromName + "_renamed";
const tokenAdminDB = tokenConn.getDB('admin');
assert.commandWorked(
tokenAdminDB.runCommand({renameCollection: fromName, to: toName, dropTarget: true}));
// Verify the the renamed collection by findAndModify existing documents.
const fad1 = assert.commandWorked(tokenDB.runCommand(
{findAndModify: kCollName + "_renamed", query: {a: 1}, update: {$set: {a: 11, b: 1}}}));
assert.eq({_id: 0, a: 1, b: 1}, fad1.value, tojson(fad1));
// Reset the collection name and document data.
assert.commandWorked(
tokenAdminDB.runCommand({renameCollection: toName, to: fromName, dropTarget: true}));
assert.commandWorked(tokenDB.runCommand(
{findAndModify: kCollName, query: {a: 11}, update: {$set: {a: 1, b: 1}}}));
}
// ListDatabases only returns databases associated with kTenant.
{
// Create databases for kTenant. A new database is implicitly created when a collection
// is created.
const kOtherDbName = 'otherDb';
assert.commandWorked(tokenConn.getDB(kOtherDbName).createCollection("collName"));
const tokenAdminDB = tokenConn.getDB('admin');
const dbs =
assert.commandWorked(tokenAdminDB.runCommand({listDatabases: 1, nameOnly: true}));
assert.eq(3, dbs.databases.length, tojson(dbs));
const expectedDbs = ["admin", kDbName, kOtherDbName];
assert(arrayEq(expectedDbs, dbs.databases.map(db => db.name)), tojson(dbs));
}
{
// Test the collStats command.
let res = assert.commandWorked(tokenDB.runCommand({collStats: kCollName}));
checkNsSerializedCorrectly(kDbName, kCollName, res.ns);
// perform the same test on a timeseries collection
const timeFieldName = "time";
const tsColl = "timeseriesColl";
assert.commandWorked(
tokenDB.createCollection(tsColl, {timeseries: {timeField: timeFieldName}}));
res = assert.commandWorked(tokenDB.runCommand({collStats: tsColl}));
checkNsSerializedCorrectly(kDbName, tsColl, res.ns);
checkNsSerializedCorrectly(kDbName, 'system.buckets.' + tsColl, res.timeseries.bucketsNs);
}
// Drop the collection, and then the database. Check that listCollections no longer returns
// the 3 collections.
{
// Drop the collection, and check that the "ns" returned is serialized correctly.
const dropRes = assert.commandWorked(tokenDB.runCommand({drop: kCollName}));
checkNsSerializedCorrectly(kDbName, kCollName, dropRes.ns);
const collsAfterDropColl = assert.commandWorked(
tokenDB.runCommand({listCollections: 1, nameOnly: true, filter: {name: kCollName}}));
assert.eq(0,
collsAfterDropColl.cursor.firstBatch.length,
tojson(collsAfterDropColl.cursor.firstBatch));
assert.commandWorked(tokenDB.runCommand({dropDatabase: 1}));
const collsAfterDropDb =
assert.commandWorked(tokenDB.runCommand({listCollections: 1, nameOnly: true}));
assert.eq(0,
collsAfterDropDb.cursor.firstBatch.length,
tojson(collsAfterDropDb.cursor.firstBatch));
// Reset the collection and document.
assert.commandWorked(
tokenDB.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
}
// Test that transactions can be run successfully.
{
const session = tokenDB.getMongo().startSession();
const sessionDb = session.getDatabase(kDbName);
session.startTransaction();
assert.commandWorked(sessionDb.runCommand(
{delete: kCollName, deletes: [{q: {_id: 0, a: 1, b: 1}, limit: 1}]}));
session.commitTransaction_forTesting();
const findRes = assert.commandWorked(tokenDB.runCommand({find: kCollName}));
assert.eq(0, findRes.cursor.firstBatch.length, tojson(findRes.cursor.firstBatch));
// Reset the collection and document.
assert.commandWorked(
tokenDB.runCommand({insert: kCollName, documents: [{_id: 0, a: 1, b: 1}]}));
}
// Test createIndexes, listIndexes and dropIndexes command.
{
var sortIndexesByName = function(indexes) {
return indexes.sort(function(a, b) {
return a.name > b.name;
});
};
var getIndexesKeyAndName = function(indexes) {
return sortIndexesByName(indexes).map(function(index) {
return {key: index.key, name: index.name};
});
};
let res = assert.commandWorked(tokenDB.runCommand({
createIndexes: kCollName,
indexes: [{key: {a: 1}, name: "indexA"}, {key: {b: 1}, name: "indexB"}]
}));
assert.eq(3, res.numIndexesAfter, tojson(res));
res = assert.commandWorked(tokenDB.runCommand({listIndexes: kCollName}));
assert.eq(3, res.cursor.firstBatch.length, tojson(res.cursor.firstBatch));
assert(arrayEq(
[
{key: {"_id": 1}, name: "_id_"},
{key: {a: 1}, name: "indexA"},
{key: {b: 1}, name: "indexB"}
],
getIndexesKeyAndName(res.cursor.firstBatch)),
tojson(res.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollName, res.cursor.ns);
// Drop those new created indexes.
res = assert.commandWorked(
tokenDB.runCommand({dropIndexes: kCollName, index: ["indexA", "indexB"]}));
res = assert.commandWorked(tokenDB.runCommand({listIndexes: kCollName}));
assert.eq(1, res.cursor.firstBatch.length, tojson(res.cursor.firstBatch));
assert(
arrayEq([{key: {"_id": 1}, name: "_id_"}], getIndexesKeyAndName(res.cursor.firstBatch)),
tojson(res.cursor.firstBatch));
}
// Test aggregation stage commands
{
// Create a number of collections and entries used to test agg stages
const kCollA = "collA";
const kCollB = "collB";
const kCollC = "collC";
const kCollD = "collD";
assert.commandWorked(tokenDB.createCollection(kCollA));
assert.commandWorked(tokenDB.createCollection(kCollB));
const collADocs = [
{_id: 0, start: "a", end: "b"},
{_id: 1, start: "b", end: "c"},
{_id: 2, start: "c", end: "d"}
];
const collBDocs = [
{_id: 0, ref: "a", payload: "1"},
{_id: 1, ref: "c", payload: "2"},
{_id: 2, ref: "e", payload: "3"}
];
assert.commandWorked(tokenDB.runCommand({insert: kCollA, documents: collADocs}));
assert.commandWorked(tokenDB.runCommand({insert: kCollB, documents: collBDocs}));
// Set up assertion targets
const graphLookupTarget = [
{_id: 0, connections: []},
{_id: 1, connections: [{_id: 0, start: "a", end: "b"}]},
{_id: 2, connections: [{_id: 0, start: "a", end: "b"}, {_id: 1, start: "b", end: "c"}]}
];
const lookupTarget = [
{_id: 0, refs: [{_id: 0, ref: "a", payload: "1"}]},
{_id: 1, refs: []},
{_id: 2, refs: [{_id: 1, ref: "c", payload: "2"}]}
];
// $graphLookup agg stage
const graphLookupStage = {
$graphLookup: {
from: kCollA,
startWith: "$start",
connectFromField: "start",
connectToField: "end",
as: "connections"
}
};
{
const graphLookupRes = assert.commandWorked(tokenDB.runCommand({
aggregate: kCollA,
pipeline: [graphLookupStage, {$project: {_id: 1, connections: 1}}],
cursor: {}
}));
assert(arrayEq(graphLookupTarget, graphLookupRes.cursor.firstBatch),
tojson(graphLookupRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollA, graphLookupRes.cursor.ns);
}
// $out agg stage using string input for collection name
const outStageStr = {$out: kCollC};
{
const outStrRes = assert.commandWorked(tokenDB.runCommand(
{aggregate: kCollA, pipeline: [graphLookupStage, outStageStr], cursor: {}}));
checkNsSerializedCorrectly(kDbName, kCollA, outStrRes.cursor.ns);
// Because we're using the same graphLookup stage from the first test, we should see the
// exact same results but stored in kCollC
let projectRes = assert.commandWorked(tokenDB.runCommand(
{aggregate: kCollC, pipeline: [{$project: {_id: 1, connections: 1}}], cursor: {}}));
assert(arrayEq(graphLookupTarget, projectRes.cursor.firstBatch),
tojson(projectRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollC, projectRes.cursor.ns);
assert.commandWorked(tokenDB.runCommand({drop: kCollC}));
}
// $out agg stage using object input for collection name
const outStageObj = {$out: {db: kDbName, coll: kCollD}};
{
const outObjRes = assert.commandWorked(tokenDB.runCommand(
{aggregate: kCollA, pipeline: [graphLookupStage, outStageObj], cursor: {}}));
checkNsSerializedCorrectly(kDbName, kCollA, outObjRes.cursor.ns);
// Because we're using the same graphLookup stage from the first test, we should see the
// exact same results but stored in kCollD
let projectRes = assert.commandWorked(tokenDB.runCommand(
{aggregate: kCollD, pipeline: [{$project: {_id: 1, connections: 1}}], cursor: {}}));
assert(arrayEq(graphLookupTarget, projectRes.cursor.firstBatch),
tojson(projectRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollD, projectRes.cursor.ns);
assert.commandWorked(tokenDB.runCommand({drop: kCollD}));
}
// $lookup agg stage using nested pipeline input
const lookupPipelineStage = {
$lookup: {from: kCollB, let: {kCollA_start: "$start"},
pipeline: [{$match: {$expr: {$eq: ["$$kCollA_start","$ref"]}}}], as: "refs"}
};
{
const lookupPipelineRes = assert.commandWorked(tokenDB.runCommand({
aggregate: kCollA,
pipeline: [lookupPipelineStage, {$project: {_id: 1, refs: 1}}],
cursor: {}
}));
assert(arrayEq(lookupTarget, lookupPipelineRes.cursor.firstBatch),
tojson(lookupPipelineRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollA, lookupPipelineRes.cursor.ns);
}
// $merge agg stage
const mergeStage = {$merge: {into: kCollD}};
{
const mergeRes = assert.commandWorked(
tokenDB.runCommand({aggregate: kCollA, pipeline: [mergeStage], cursor: {}}));
checkNsSerializedCorrectly(kDbName, kCollA, mergeRes.cursor.ns);
// Merging kCollA into a new collection kCollD should give us matching contents
let findRes = assert.commandWorked(tokenDB.runCommand({find: kCollD}));
assert(arrayEq(collADocs, findRes.cursor.firstBatch),
tojson(findRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollD, findRes.cursor.ns);
assert.commandWorked(tokenDB.runCommand({drop: kCollD}));
}
// $unionWith agg stage
const unionWithStage = {$unionWith: kCollB};
{
const unionWithRes = assert.commandWorked(
tokenDB.runCommand({aggregate: kCollA, pipeline: [unionWithStage], cursor: {}}));
assert(arrayEq(collADocs.concat(collBDocs), unionWithRes.cursor.firstBatch),
tojson(unionWithRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollA, unionWithRes.cursor.ns);
}
// $collStats agg stage
const collStatsStage = {$collStats: {latencyStats: {}}};
{
// create a new collection to isolate stats results from other tests
assert.commandWorked(tokenDB.runCommand({insert: kCollD, documents: collADocs}));
const collStatsRes = assert.commandWorked(
tokenDB.runCommand({aggregate: kCollD, pipeline: [collStatsStage], cursor: {}}));
assert.eq(
1, collStatsRes.cursor.firstBatch.length, tojson(collStatsRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollD, collStatsRes.cursor.firstBatch[0].ns);
checkNsSerializedCorrectly(kDbName, kCollD, collStatsRes.cursor.ns);
let stats = collStatsRes.cursor.firstBatch[0];
assert('latencyStats' in stats, tojson(stats));
assert.eq(stats.latencyStats.writes.ops, 1, tojson(stats));
assert.eq(stats.latencyStats.reads.ops, 1, tojson(stats));
assert.eq(stats.latencyStats.commands.ops, 0, tojson(stats));
assert.eq(stats.latencyStats.transactions.ops, 0, tojson(stats));
// Also check the next() cursor results.
const collStatsResNext = tokenDB.collD.aggregate(collStatsStage).next();
checkNsSerializedCorrectly(kDbName, kCollD, collStatsResNext.ns);
assert('latencyStats' in collStatsResNext, tojson(collStatsResNext));
assert.eq(collStatsResNext.latencyStats.writes.ops, 1, tojson(collStatsResNext));
assert.eq(collStatsResNext.latencyStats.reads.ops, 2, tojson(collStatsResNext));
assert.eq(collStatsResNext.latencyStats.commands.ops, 0, tojson(collStatsResNext));
assert.eq(collStatsResNext.latencyStats.transactions.ops, 0, tojson(collStatsResNext));
assert.commandWorked(tokenDB.runCommand({drop: kCollD}));
}
// Clean up remaining test collections.
assert.commandWorked(tokenDB.runCommand({drop: kCollA}));
assert.commandWorked(tokenDB.runCommand({drop: kCollB}));
}
// Test the validate command.
{
const validateRes = assert.commandWorked(tokenDB.runCommand({validate: kCollName}));
assert(validateRes.valid, tojson(validateRes));
checkNsSerializedCorrectly(kDbName, kCollName, validateRes.ns);
}
}
// Test commands using a security token for a different tenant and check that this tenant cannot
// access the other tenant's collection.
{
// Create a user for a different tenant, and set the security token on the connection.
// We reuse the same connection, but swap the token out.
primary._setSecurityToken(_createTenantToken({tenant: kOtherTenant}));
assert.commandWorked(primary.getDB('$external').runCommand({
createUser: "userTenant2",
roles:
[{role: 'dbAdminAnyDatabase', db: 'admin'}, {role: 'readWriteAnyDatabase', db: 'admin'}]
}));
const securityTokenOtherTenant =
_createSecurityToken({user: "userTenant2", db: '$external', tenant: kOtherTenant}, kVTSKey);
tokenConn._setSecurityToken(securityTokenOtherTenant);
const tokenDB2 = tokenConn.getDB(kDbName);
const findOtherUser =
assert.commandWorked(tokenDB2.runCommand({find: kCollName, filter: {a: 1}}));
assert.eq(findOtherUser.cursor.firstBatch.length, 0, tojson(findOtherUser));
const explainOtherUser =
assert.commandWorked(tokenDB2.runCommand({explain: {find: kCollName, filter: {a: 1}}}));
assert.eq(explainOtherUser.executionStats.nReturned, 0, tojson(explainOtherUser));
const fadOtherUser = assert.commandWorked(
tokenDB2.runCommand({findAndModify: kCollName, query: {b: 1}, update: {$inc: {b: 10}}}));
assert.eq(null, fadOtherUser.value, tojson(fadOtherUser));
const countOtherUser =
assert.commandWorked(tokenDB2.runCommand({count: kCollName, query: {c: 1}}));
assert.eq(0, countOtherUser.n, tojson(countOtherUser));
const distinctOtherUer =
assert.commandWorked(tokenDB2.runCommand({distinct: kCollName, key: 'd', query: {}}));
assert.eq([], distinctOtherUer.values, tojson(distinctOtherUer));
const fromName = kDbName + '.' + kCollName;
const toName = fromName + "_renamed";
assert.commandFailedWithCode(tokenConn.getDB("admin").runCommand(
{renameCollection: fromName, to: toName, dropTarget: true}),
ErrorCodes.NamespaceNotFound);
assert.commandFailedWithCode(tokenDB2.runCommand({listIndexes: kCollName}),
ErrorCodes.NamespaceNotFound);
assert.commandFailedWithCode(
tokenDB2.runCommand({dropIndexes: kCollName, index: ["indexA", "indexB"]}),
ErrorCodes.NamespaceNotFound);
assert.commandFailedWithCode(tokenDB2.runCommand({validate: kCollName}),
ErrorCodes.NamespaceNotFound);
// ListDatabases with securityToken of kOtherTenant cannot access databases created by
// kTenant.
const dbsWithDiffToken = assert.commandWorked(
tokenConn.getDB('admin').runCommand({listDatabases: 1, nameOnly: true}));
// Only the 'admin' db exists
assert.eq(1, dbsWithDiffToken.databases.length, tojson(dbsWithDiffToken));
const expectedAdminDb = "admin";
assert(arrayEq([expectedAdminDb], dbsWithDiffToken.databases.map(db => db.name)),
tojson(dbsWithDiffToken));
// Attempt to drop the database, then check it was not dropped.
assert.commandWorked(tokenDB2.runCommand({dropDatabase: 1}));
// Run listCollections using the original user's security token to see the collection
// exists.
tokenConn._setSecurityToken(securityToken);
const collsAfterDropOtherTenant =
assert.commandWorked(tokenDB.runCommand({listCollections: 1, nameOnly: true}));
assert.eq(1,
collsAfterDropOtherTenant.cursor.firstBatch.length,
tojson(collsAfterDropOtherTenant.cursor.firstBatch));
}
// Test aggregation stage commands
{
// Create a number of collections and entries used to test agg stages
const kCollA = "collA";
const kCollB = "collB";
const kCollC = "collC";
const kCollD = "collD";
assert.commandWorked(tokenDB.createCollection(kCollA));
assert.commandWorked(tokenDB.createCollection(kCollB));
const collADocs = [
{_id: 0, start: "a", end: "b"},
{_id: 1, start: "b", end: "c"},
{_id: 2, start: "c", end: "d"}
];
const collBDocs = [
{_id: 0, ref: "a", payload: "1"},
{_id: 1, ref: "c", payload: "2"},
{_id: 2, ref: "e", payload: "3"}
];
assert.commandWorked(tokenDB.runCommand({insert: kCollA, documents: collADocs}));
assert.commandWorked(tokenDB.runCommand({insert: kCollB, documents: collBDocs}));
// Set up agg stage input documents
const lookupPlannerStage = {
$lookup: {from: kCollB, localField: "start", foreignField: "ref", as: "refs"}
};
// Set up assertion targets
const lookupTarget = [
{_id: 0, refs: [{_id: 0, ref: "a", payload: "1"}]},
{_id: 1, refs: []},
{_id: 2, refs: [{_id: 1, ref: "c", payload: "2"}]}
];
// $lookup agg stage that exercises query planner.
{
const lookupPlannerRes = assert.commandWorked(tokenDB.runCommand({
aggregate: kCollA,
pipeline: [lookupPlannerStage, {$project: {_id: 1, refs: 1}}],
cursor: {}
}));
assert(arrayEq(lookupTarget, lookupPlannerRes.cursor.firstBatch),
tojson(lookupPlannerRes.cursor.firstBatch));
checkNsSerializedCorrectly(kDbName, kCollA, lookupPlannerRes.cursor.ns);
}
}
// Test commands using a privleged user with an unsigned security token and check that the user can
// still run commands on the doc when on the correct connection, but not when on a different
// connection.
{
const privelegedConn = new Mongo(primary.host);
assert(privelegedConn.getDB('admin').auth('admin', 'pwd'));
privelegedConn._setSecurityToken(_createTenantToken({tenant: kTenant}));
const privelegedDB = privelegedConn.getDB(kDbName);
const otherPrivelegedConn = new Mongo(primary.host);
assert(otherPrivelegedConn.getDB('admin').auth('admin', 'pwd'));
otherPrivelegedConn._setSecurityToken(_createTenantToken({tenant: kOtherTenant}));
const otherPrivelegedDB = otherPrivelegedConn.getDB(kDbName);
// Find and modify the document
{
const fadCorrectConnection = assert.commandWorked(privelegedDB.runCommand(
{findAndModify: kCollName, query: {b: 1}, update: {$inc: {b: 10}}}));
assert.eq({_id: 0, a: 1, b: 1}, fadCorrectConnection.value, tojson(fadCorrectConnection));
const fadOtherConnection = assert.commandWorked(otherPrivelegedDB.runCommand(
{findAndModify: kCollName, query: {b: 11}, update: {$inc: {b: 10}}}));
assert.eq(null, fadOtherConnection.value, tojson(fadOtherConnection));
// Reset document data.
assert.commandWorked(privelegedDB.runCommand(
{findAndModify: kCollName, query: {b: 11}, update: {$set: {a: 1, b: 1}}}));
}
// Rename the collection
{
const fromName = kDbName + '.' + kCollName;
const toName = fromName + "_renamed";
const privelegedAdminDB = privelegedConn.getDB('admin');
assert.commandWorked(privelegedAdminDB.runCommand(
{renameCollection: fromName, to: toName, dropTarget: true}));
// Verify the the renamed collection by findAndModify existing documents.
const fad1 = assert.commandWorked(privelegedDB.runCommand(
{findAndModify: kCollName + "_renamed", query: {a: 1}, update: {$set: {a: 11, b: 1}}}));
assert.eq({_id: 0, a: 1, b: 1}, fad1.value, tojson(fad1));
// Reset the collection name and document data.
assert.commandWorked(privelegedAdminDB.runCommand(
{renameCollection: toName, to: fromName, dropTarget: true}));
assert.commandWorked(privelegedDB.runCommand(
{findAndModify: kCollName, query: {a: 11}, update: {$set: {a: 1, b: 1}}}));
}
}
// Test collMod
{
// Create the index used for collMod
let res = assert.commandWorked(tokenDB.runCommand({
createIndexes: kCollName,
indexes: [{key: {c: 1}, name: "indexC", expireAfterSeconds: 50}]
}));
assert.eq(2, res.numIndexesAfter, tojson(res));
jsTestLog(`Created index`);
// Modify the index with the tenantId
res = assert.commandWorked(tokenDB.runCommand(
{"collMod": kCollName, "index": {"keyPattern": {c: 1}, expireAfterSeconds: 100}}));
assert.eq(50, res.expireAfterSeconds_old, tojson(res));
assert.eq(100, res.expireAfterSeconds_new, tojson(res));
// Drop the index created
assert.commandWorked(tokenDB.runCommand({dropIndexes: kCollName, index: ["indexC"]}));
}
// Test dbCheck command.
// This should fail since dbCheck is not supporting using a security token.
{
assert.commandFailedWithCode(tokenDB.runCommand({dbCheck: kCollName}), ErrorCodes.Unauthorized);
}
primary._setSecurityToken(undefined);
rst.stopSet();