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
|
// Tests that users can only use transactions that they created.
// @tags: [uses_transactions]
(function() {
"use strict";
const rst = new ReplSetTest({nodes: 1, keyFile: "jstests/libs/key1"});
rst.startSet();
rst.initiate();
const adminDB = rst.getPrimary().getDB("admin");
// Create the admin user.
assert.commandWorked(adminDB.runCommand({createUser: "admin", pwd: "admin", roles: ["root"]}));
assert.eq(1, adminDB.auth("admin", "admin"));
// Set up the test database.
const dbName = "test";
const collName = "transactions";
const testDB = adminDB.getSiblingDB(dbName);
testDB.dropDatabase();
assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}}));
// Create two users, "Alice" and "Mallory".
assert.commandWorked(
testDB.runCommand({createUser: "Alice", pwd: "pwd", roles: ["readWrite"]}));
assert.commandWorked(
testDB.runCommand({createUser: "Mallory", pwd: "pwd", roles: ["readWrite"]}));
adminDB.logout();
// Alice starts a transaction.
assert.eq(1, testDB.auth("Alice", "pwd"));
const lsid = assert.commandWorked(testDB.runCommand({startSession: 1})).id;
assert.commandWorked(testDB.runCommand({
insert: collName,
documents: [{_id: "alice-1"}],
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(0),
startTransaction: true,
autocommit: false
}));
testDB.logout();
// Mallory cannot continue the transaction. Using the same lsid for two different users creates
// two distinct sessions on the server. Mallory's session does not have an open transaction.
assert.eq(1, testDB.auth("Mallory", "pwd"));
assert.commandFailedWithCode(testDB.runCommand({
insert: collName,
documents: [{_id: "mallory"}],
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false
}),
ErrorCodes.NoSuchTransaction);
// Mallory cannot commit the transaction.
assert.commandFailedWithCode(adminDB.runCommand({
commitTransaction: 1,
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false,
writeConcern: {w: "majority"}
}),
ErrorCodes.NoSuchTransaction);
// Mallory cannot abort the transaction.
assert.commandFailedWithCode(adminDB.runCommand({
abortTransaction: 1,
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false,
writeConcern: {w: "majority"}
}),
ErrorCodes.NoSuchTransaction);
testDB.logout();
// An unauthenticated user cannot continue the transaction.
assert.commandFailedWithCode(testDB.runCommand({
insert: collName,
documents: [{_id: "unauthenticated"}],
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false
}),
ErrorCodes.Unauthorized);
// An unauthenticated user cannot commit the transaction.
assert.commandFailedWithCode(adminDB.runCommand({
commitTransaction: 1,
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false,
writeConcern: {w: "majority"}
}),
ErrorCodes.Unauthorized);
// An unauthenticated user cannot abort the transaction.
assert.commandFailedWithCode(adminDB.runCommand({
abortTransaction: 1,
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false,
writeConcern: {w: "majority"}
}),
ErrorCodes.Unauthorized);
// Alice can continue the transaction.
assert.eq(1, testDB.auth("Alice", "pwd"));
assert.commandWorked(testDB.runCommand({
insert: collName,
documents: [{_id: "alice-2"}],
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(1),
autocommit: false
}));
// Alice can commit the transaction.
assert.commandWorked(adminDB.runCommand({
commitTransaction: 1,
lsid: lsid,
txnNumber: NumberLong(0),
stmtId: NumberInt(2),
autocommit: false,
writeConcern: {w: "majority"}
}));
// We do not see the writes from Mallory or the unauthenticated user.
assert.eq(1, testDB[collName].find({_id: "alice-1"}).itcount());
assert.eq(1, testDB[collName].find({_id: "alice-2"}).itcount());
assert.eq(0, testDB[collName].find({_id: "mallory"}).itcount());
assert.eq(0, testDB[collName].find({_id: "unauthenticated"}).itcount());
assert.commandWorked(testDB.runCommand({endSessions: [lsid]}));
testDB.logout();
rst.stopSet();
}());
|