Skip to content

Commit 5b4326e

Browse files
josephdprinceEvergreen Agent
authored andcommitted
SERVER-82543 Finish removing dollar tenant in js serverless tests
1 parent 753c16d commit 5b4326e

10 files changed

+164
-123
lines changed

jstests/serverless/initial_sync_change_collection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ const endOplogTimestamp = oplogDocs.at(-1).ts;
9797
// oplog only after the data cloning is done. And so, the change collection already exists in place
9898
// to capture oplog entries. As such, the change collection entries and the oplog entries for
9999
// timestamp range ('startOplogTimestamp', 'endOplogTimestamp'] must be the same.
100-
verifyChangeCollectionEntries(secondary, startOplogTimestamp, endOplogTimestamp, userInfo.tenantId);
100+
verifyChangeCollectionEntries(secondary,
101+
startOplogTimestamp,
102+
endOplogTimestamp,
103+
userInfo.tenantId,
104+
_createTenantToken({tenant: userInfo.tenantId}));
101105

102106
// The state of the change collection after the initial sync is not consistent with the primary.
103107
// This is because the change collection's data is never cloned to the secondary, only it's creation

jstests/serverless/libs/change_collection_util.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
// Verifies that the oplog and change collection entries are the same for the provided tenant
44
// 'tenantId' for the specified timestamp window:- (startOplogTimestamp, endOplogTimestamp].
55
export function verifyChangeCollectionEntries(
6-
connection, startOplogTimestamp, endOplogTimestamp, tenantId) {
6+
connection, startOplogTimestamp, endOplogTimestamp, tenantId, token) {
77
// Fetch the oplog documents for the provided tenant for the specified timestamp window. Note
88
// that the startOplogTimestamp is expected to be just before the first write, while the
99
// endOplogTimestamp is expected to be the timestamp of the final write in the test.
10+
connection._setSecurityToken(undefined);
1011
const oplogColl = connection.getDB("local").oplog.rs;
1112
const oplogEntries = oplogColl
1213
.find({
@@ -18,15 +19,17 @@ export function verifyChangeCollectionEntries(
1819
})
1920
.toArray();
2021

22+
// Set token for the following command
23+
connection._setSecurityToken(token);
24+
2125
// Fetch all documents from the tenant's change collection for the specified timestamp window.
2226
const changeCollectionEntries =
2327
assert
2428
.commandWorked(connection.getDB("config").runCommand({
2529
find: "system.change_collection",
2630
filter:
2731
{$and: [{_id: {$gt: startOplogTimestamp}}, {_id: {$lte: endOplogTimestamp}}]},
28-
batchSize: 1000000,
29-
$tenant: tenantId
32+
batchSize: 1000000
3033
}))
3134
.cursor.firstBatch;
3235

@@ -84,8 +87,7 @@ export class ChangeStreamMultitenantReplicaSetTest extends ReplSetTest {
8487
this.startSet({setParameter});
8588
this.initiate();
8689

87-
// Create a root user within the multitenant environment to enable passing '$tenant' to
88-
// commands.
90+
// Create a root user within the multitenant environment
8991
assert.commandWorked(this.getPrimary().getDB("admin").runCommand(
9092
{createUser: "root", pwd: "pwd", roles: ["root"]}));
9193

@@ -137,20 +139,16 @@ export class ChangeStreamMultitenantReplicaSetTest extends ReplSetTest {
137139

138140
const adminDb = tokenConn.getDB("admin");
139141

140-
// Login to the root user with 'ActionType::useTenant' such that the '$tenant' can be
141-
// used.
142142
assert(adminDb.auth("root", "pwd"));
143143

144144
// Create the user with the provided roles if it does not exist.
145+
tokenConn._setSecurityToken(_createTenantToken({tenant: tenantId}));
145146
const existingUser =
146-
assert
147-
.commandWorked(adminDb.runCommand(
148-
{find: "system.users", filter: {user: user}, $tenant: tenantId}))
147+
assert.commandWorked(adminDb.runCommand({find: "system.users", filter: {user: user}}))
149148
.cursor.firstBatch;
150149
if (existingUser.length === 0) {
151150
assert.commandWorked(
152-
tokenConn.getDB("$external")
153-
.runCommand({createUser: user, '$tenant': tenantId, roles: userRoles}));
151+
tokenConn.getDB("$external").runCommand({createUser: user, roles: userRoles}));
154152
}
155153

156154
// Set the provided tenant id into the security token for the user.

jstests/serverless/list_databases_for_all_tenants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function createMultitenantDatabases(conn, tokenConn, num) {
6767
{"name": 'auto_gen_db_' + i.toString(), "tenantId": kTenant, "empty": false});
6868
}
6969
// Reset token
70-
conn._setSecurityToken("");
70+
conn._setSecurityToken(undefined);
7171
return [tenantIds, tokens, expectedDatabases];
7272
}
7373

@@ -223,7 +223,7 @@ function runTestInvalidCommands(primary) {
223223
createUser: "unauthorizedUsr",
224224
roles: [{role: 'readWriteAnyDatabase', db: 'admin'}]
225225
}));
226-
primary._setSecurityToken("");
226+
primary._setSecurityToken(undefined);
227227
tokenConn._setSecurityToken(
228228
_createSecurityToken({user: "unauthorizedUsr", db: '$external', tenant: kTenant}, kVTSKey));
229229
const tokenAdminDB = tokenConn.getDB("admin");

jstests/serverless/multitenancy_initial_sync_fails_no_auth_schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ assert.commandWorked(primary.getDB('$external').runCommand({
4141
let res = assert.commandWorked(adminDb.runCommand({find: "system.users", filter: {}}));
4242
assert.eq(1, res.cursor.firstBatch.length);
4343

44-
primary._setSecurityToken("");
44+
primary._setSecurityToken(undefined);
4545

4646
// Delete the auth schema doc. This should cause initial sync to fail, because a user exists
4747
// without an auth schema doc.

jstests/serverless/multitenancy_with_mongoq_basic_commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,6 @@ const tokenDB = tokenConn.getDB(kDbName);
698698
// This should fail since dbCheck is not supporting using a security token.
699699
{ assert.commandFailedWithCode(tokenDB.runCommand({dbCheck: kCollName}), ErrorCodes.Unauthorized); }
700700

701-
primary._setSecurityToken("");
701+
primary._setSecurityToken(undefined);
702702

703703
rst.stopSet();

jstests/serverless/serverless_slow_log_tenantid.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rst.initiate();
1919
const kTenant = ObjectId();
2020
const adminDb = rst.getPrimary().getDB('admin');
2121

22-
// Must be authenticated as a user with ActionType::useTenant in order to use $tenant.
22+
// Create a user for testing
2323
assert.commandWorked(adminDb.runCommand({createUser: 'admin', pwd: 'pwd', roles: ['root']}));
2424
assert(adminDb.auth('admin', 'pwd'));
2525

@@ -31,8 +31,9 @@ assert.commandWorked(adminDb.setProfilingLevel(2, {slowms: -1}));
3131

3232
const primary = rst.getPrimary();
3333

34-
assert.commandWorked(primary.getDB("test").runCommand(
35-
{insert: "foo", documents: [{_id: 0, a: 1, b: 1}], '$tenant': kTenant}));
34+
primary._setSecurityToken(_createTenantToken({tenant: kTenant}));
35+
assert.commandWorked(
36+
primary.getDB("test").runCommand({insert: "foo", documents: [{_id: 0, a: 1, b: 1}]}));
3637

3738
print(`Checking ${primary.fullOptions.logFile} for client metadata message`);
3839
const log = cat(primary.fullOptions.logFile);
@@ -49,4 +50,6 @@ for (var a of log.split("\n")) {
4950
assert(predicate.test(log),
5051
"'Slow query' log line missing in mongod log file!\n" +
5152
"Log file contents: " + rst.getPrimary().fullOptions.logFile);
53+
54+
primary._setSecurityToken(undefined);
5255
rst.stopSet();

jstests/serverless/tenant_migration_shard_merge_abort_garbage_collection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {extractUUIDFromObject} from "jstests/libs/uuid_util.js";
1717
import {TenantMigrationTest} from "jstests/replsets/libs/tenant_migration_test.js";
1818
import {makeTenantDB} from "jstests/replsets/libs/tenant_migration_util.js";
1919

20-
// Disabling featureFlagRequireTenantID to allow using a tenantId prefix (instead of $tenant) and
20+
// Disabling featureFlagRequireTenantID to allow using a tenantId prefix and
2121
// reusing the same code to test garbage collection with and without multitenancy support.
2222
function runTest({multitenancySupport}) {
2323
const setParameter = {

jstests/serverless/upgrade_to_use_multitenancy_support.js

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if (featureFlagRequireTenantId) {
2020
* tenantId.
2121
*/
2222
function runFindOnPrefixedDb(conn, prefixedDb, collName, expectedDocsReturned) {
23+
conn._setSecurityToken(undefined);
2324
const res =
2425
assert.commandWorked(conn.getDB(prefixedDb).runCommand({find: collName, filter: {}}));
2526
assert(arrayEq(expectedDocsReturned, res.cursor.firstBatch), tojson(res));
@@ -31,34 +32,35 @@ function runFindOnPrefixedDb(conn, prefixedDb, collName, expectedDocsReturned) {
3132
* Runs a findAndModify using a prefixed db.
3233
*/
3334
function runFindAndModOnPrefixedDb(conn, prefixedDb, collName, query, update, expectedDocReturned) {
35+
conn._setSecurityToken(undefined);
3436
const res = assert.commandWorked(
3537
conn.getDB(prefixedDb).runCommand({findAndModify: collName, query: query, update: update}));
3638
assert.eq(res.value, expectedDocReturned);
3739
}
3840

3941
/*
40-
* Runs a find using $tenant, and asserts the find returns 'expectedDocsReturned'. Also
41-
* checks that the "ns" returned in the cursor result is serialized as expected, without the
42+
* Runs a find using unsigned security token, and asserts the find returns 'expectedDocsReturned'.
43+
* Also checks that the "ns" returned in the cursor result is serialized as expected, without the
4244
* tenantId.
4345
*/
44-
function runFindUsingDollarTenant(conn, db, collName, tenantId, expectedDocsReturned) {
45-
const res = assert.commandWorked(
46-
conn.getDB(db).runCommand({find: collName, filter: {}, $tenant: tenantId}));
46+
function runFindUsingSecurityToken(conn, db, collName, token, expectedDocsReturned) {
47+
conn._setSecurityToken(token);
48+
const res = assert.commandWorked(conn.getDB(db).runCommand({find: collName, filter: {}}));
4749
assert(arrayEq(expectedDocsReturned, res.cursor.firstBatch), tojson(res));
4850
const namespace = db + "." + collName;
4951
assert.eq(res.cursor.ns, namespace);
5052
}
5153

5254
/*
53-
* Runs a find using $tenant and prefixed db, and asserts the find returns
55+
* Runs a find using unsigned security token and prefixed db, and asserts the find returns
5456
* 'expectedDocsReturned'. Also checks that the "ns" returned in the cursor result is serialized
5557
* as expected, including the tenantId.
5658
*/
57-
function runFindUsingDollarTenantAndPrefix(
58-
conn, prefixedDb, collName, tenantId, expectedDocsReturned) {
59+
function runFindUsingSecurityTokenAndPrefix(
60+
conn, prefixedDb, collName, token, expectedDocsReturned) {
61+
conn._setSecurityToken(token);
5962
const res = assert.commandWorked(
60-
conn.getDB(prefixedDb)
61-
.runCommand({find: collName, filter: {}, $tenant: tenantId, expectPrefix: true}));
63+
conn.getDB(prefixedDb).runCommand({find: collName, filter: {}, expectPrefix: true}));
6264
assert(arrayEq(expectedDocsReturned, res.cursor.firstBatch), tojson(res));
6365
const prefixedNamespace = prefixedDb + "." + collName;
6466
assert.eq(res.cursor.ns, prefixedNamespace);
@@ -78,15 +80,10 @@ function assertFindBothTenantsPrefixedDb(
7880
* Runs a find for both tenants using a prefixed db, and asserts the find returns
7981
* 'expectedDocsReturned'.
8082
*/
81-
function assertFindBothTenantsUsingDollarTenant(conn,
82-
db,
83-
collName,
84-
tenantId1,
85-
tenantId2,
86-
expectedDocsReturnedTenant1,
87-
expectedDocsReturnedTenant2) {
88-
runFindUsingDollarTenant(conn, db, collName, tenantId1, expectedDocsReturnedTenant1);
89-
runFindUsingDollarTenant(conn, db, collName, tenantId2, expectedDocsReturnedTenant2);
83+
function assertFindBothTenantsUsingSecurityToken(
84+
conn, db, collName, token1, token2, expectedDocsReturnedTenant1, expectedDocsReturnedTenant2) {
85+
runFindUsingSecurityToken(conn, db, collName, token1, expectedDocsReturnedTenant1);
86+
runFindUsingSecurityToken(conn, db, collName, token2, expectedDocsReturnedTenant2);
9087
}
9188

9289
const rst = new ReplSetTest({
@@ -106,6 +103,9 @@ const kTenant2 = ObjectId();
106103
const kDbName = "test";
107104
const kCollName = "foo";
108105

106+
const kToken1 = _createTenantToken({tenant: kTenant1});
107+
const kToken2 = _createTenantToken({tenant: kTenant2});
108+
109109
// Create a root user and login on both the primary and secondary.
110110
const primaryAdminDb = originalPrimary.getDB('admin');
111111
let secondaryAdminDb = originalSecondary.getDB('admin');
@@ -147,16 +147,16 @@ assertFindBothTenantsPrefixedDb(
147147
originalSecondary, tenant1DbPrefixed, tenant2DbPrefixed, kCollName, tenant1Docs, tenant2Docs);
148148

149149
// Now check that we find the docs for both tenants when reading from the secondary using
150-
// $tenant and a security token. The primary does not yet support $tenant or a security token
150+
// a security token. The primary does not yet support a security token
151151
// since it does not have multitenancySupport enabled.
152-
assertFindBothTenantsUsingDollarTenant(
153-
originalSecondary, kDbName, kCollName, kTenant1, kTenant2, tenant1Docs, tenant2Docs);
152+
assertFindBothTenantsUsingSecurityToken(
153+
originalSecondary, kDbName, kCollName, kToken1, kToken2, tenant1Docs, tenant2Docs);
154154

155-
// Also assert both tenants find the new doc on the secondary using $tenant and a prefixed db.
156-
runFindUsingDollarTenantAndPrefix(
157-
originalSecondary, tenant1DbPrefixed, kCollName, kTenant1, tenant1Docs);
158-
runFindUsingDollarTenantAndPrefix(
159-
originalSecondary, tenant2DbPrefixed, kCollName, kTenant2, tenant2Docs);
155+
// Also assert both tenants find the new doc on the secondary using token and a prefixed db.
156+
runFindUsingSecurityTokenAndPrefix(
157+
originalSecondary, tenant1DbPrefixed, kCollName, kToken1, tenant1Docs);
158+
runFindUsingSecurityTokenAndPrefix(
159+
originalSecondary, tenant2DbPrefixed, kCollName, kToken2, tenant2Docs);
160160

161161
// Now insert a new doc for both tenants using the prefixed db, and assert that we can find it
162162
// on both the primary and secondary.
@@ -185,18 +185,18 @@ assertFindBothTenantsPrefixedDb(originalSecondary,
185185
allTenant1Docs,
186186
allTenant2Docs);
187187

188-
// Assert both tenants find the new doc on the secondary using $tenant.
189-
assertFindBothTenantsUsingDollarTenant(
190-
originalSecondary, kDbName, kCollName, kTenant1, kTenant2, allTenant1Docs, allTenant2Docs);
188+
// Assert both tenants find the new doc on the secondary using token.
189+
assertFindBothTenantsUsingSecurityToken(
190+
originalSecondary, kDbName, kCollName, kToken1, kToken2, allTenant1Docs, allTenant2Docs);
191191

192-
// Assert both tenants find the new doc on the secondary using $tenant and a prefixed db.
193-
runFindUsingDollarTenantAndPrefix(
194-
originalSecondary, tenant1DbPrefixed, kCollName, kTenant1, allTenant1Docs);
195-
runFindUsingDollarTenantAndPrefix(
196-
originalSecondary, tenant2DbPrefixed, kCollName, kTenant2, allTenant2Docs);
192+
// Assert both tenants find the new doc on the secondary using token and a prefixed db.
193+
runFindUsingSecurityTokenAndPrefix(
194+
originalSecondary, tenant1DbPrefixed, kCollName, kToken1, allTenant1Docs);
195+
runFindUsingSecurityTokenAndPrefix(
196+
originalSecondary, tenant2DbPrefixed, kCollName, kToken2, allTenant2Docs);
197197

198198
// Now run findAndModify on one doc using a prefixed db and check that we can read from the
199-
// secondary using just $tenant and $tenant and a prefix.
199+
// secondary using just token and a prefix.
200200
runFindAndModOnPrefixedDb(originalPrimary,
201201
tenant1DbPrefixed,
202202
kCollName,
@@ -212,18 +212,18 @@ runFindAndModOnPrefixedDb(originalPrimary,
212212

213213
const modifiedTenant1Docs = tenant1Docs.concat([{_id: 2, x: 4}]);
214214
const modifiedTenant2Docs = tenant2Docs.concat([{_id: 12, a: 40}]);
215-
assertFindBothTenantsUsingDollarTenant(originalSecondary,
216-
kDbName,
217-
kCollName,
218-
kTenant1,
219-
kTenant2,
220-
modifiedTenant1Docs,
221-
modifiedTenant2Docs);
222-
223-
runFindUsingDollarTenantAndPrefix(
224-
originalSecondary, tenant1DbPrefixed, kCollName, kTenant1, modifiedTenant1Docs);
225-
runFindUsingDollarTenantAndPrefix(
226-
originalSecondary, tenant2DbPrefixed, kCollName, kTenant2, modifiedTenant2Docs);
215+
assertFindBothTenantsUsingSecurityToken(originalSecondary,
216+
kDbName,
217+
kCollName,
218+
kToken1,
219+
kToken2,
220+
modifiedTenant1Docs,
221+
modifiedTenant2Docs);
222+
223+
runFindUsingSecurityTokenAndPrefix(
224+
originalSecondary, tenant1DbPrefixed, kCollName, kToken1, modifiedTenant1Docs);
225+
runFindUsingSecurityTokenAndPrefix(
226+
originalSecondary, tenant2DbPrefixed, kCollName, kToken2, modifiedTenant2Docs);
227227

228228
// Now, restart the primary and enable multitenancySupport. The secondary will step up to
229229
// become primary.
@@ -250,31 +250,33 @@ assertFindBothTenantsPrefixedDb(originalSecondary,
250250
modifiedTenant2Docs);
251251

252252
// Now check that we find the docs for both tenants when reading from both the primary and
253-
// secondary using $tenant.
254-
assertFindBothTenantsUsingDollarTenant(originalPrimary,
255-
kDbName,
256-
kCollName,
257-
kTenant1,
258-
kTenant2,
259-
modifiedTenant1Docs,
260-
modifiedTenant2Docs);
261-
assertFindBothTenantsUsingDollarTenant(originalSecondary,
262-
kDbName,
263-
kCollName,
264-
kTenant1,
265-
kTenant2,
266-
modifiedTenant1Docs,
267-
modifiedTenant2Docs);
268-
269-
// Also check that both tenants find the new doc on the primary and secondary using $tenant and
270-
// a prefixed db.
271-
runFindUsingDollarTenantAndPrefix(
272-
originalPrimary, tenant1DbPrefixed, kCollName, kTenant1, modifiedTenant1Docs);
273-
runFindUsingDollarTenantAndPrefix(
274-
originalSecondary, tenant2DbPrefixed, kCollName, kTenant2, modifiedTenant2Docs);
275-
runFindUsingDollarTenantAndPrefix(
276-
originalPrimary, tenant1DbPrefixed, kCollName, kTenant1, modifiedTenant1Docs);
277-
runFindUsingDollarTenantAndPrefix(
278-
originalSecondary, tenant2DbPrefixed, kCollName, kTenant2, modifiedTenant2Docs);
279-
253+
// secondary using token.
254+
assertFindBothTenantsUsingSecurityToken(originalPrimary,
255+
kDbName,
256+
kCollName,
257+
kToken1,
258+
kToken2,
259+
modifiedTenant1Docs,
260+
modifiedTenant2Docs);
261+
assertFindBothTenantsUsingSecurityToken(originalSecondary,
262+
kDbName,
263+
kCollName,
264+
kToken1,
265+
kToken2,
266+
modifiedTenant1Docs,
267+
modifiedTenant2Docs);
268+
269+
// Also check that both tenants find the new doc on the primary and secondary using token and a
270+
// prefixed db.
271+
runFindUsingSecurityTokenAndPrefix(
272+
originalPrimary, tenant1DbPrefixed, kCollName, kToken1, modifiedTenant1Docs);
273+
runFindUsingSecurityTokenAndPrefix(
274+
originalSecondary, tenant2DbPrefixed, kCollName, kToken2, modifiedTenant2Docs);
275+
runFindUsingSecurityTokenAndPrefix(
276+
originalPrimary, tenant1DbPrefixed, kCollName, kToken1, modifiedTenant1Docs);
277+
runFindUsingSecurityTokenAndPrefix(
278+
originalSecondary, tenant2DbPrefixed, kCollName, kToken2, modifiedTenant2Docs);
279+
280+
originalPrimary._setSecurityToken(undefined);
281+
originalSecondary._setSecurityToken(undefined);
280282
rst.stopSet();

0 commit comments

Comments
 (0)