summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/aggregation_currentop.js
blob: 2cb167f092b037c8f0b80027d677ec2ab344fbf9 (plain)
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
/**
 * Tests that the $currentOp aggregation stage behaves as expected. Specifically:
 * - It must be the fist stage in the pipeline.
 * - It can only be run on admin, and the "aggregate" field must be 1.
 * - Only active connections are shown unless {idleConnections: true} is specified.
 * - A user without the inprog privilege can see their own ops, but no-one else's.
 * - A user with the inprog privilege can see all ops.
 * - Non-local readConcerns are rejected.
 * - Collation rules are respected.
 */
(function() {
    "use strict";

    const key = "jstests/libs/key1";

    // Create a new replica set for testing. We set the internalQueryExecYieldIterations parameter
    // so that plan execution yields on every iteration. For some tests, we will temporarily set
    // yields to hang the mongod so we can capture particular operations in the currentOp output.
    const rst = new ReplSetTest({
        name: jsTestName(),
        nodes: 3,
        keyFile: key,
        nodeOptions: {setParameter: {internalQueryExecYieldIterations: 1}}
    });

    const nodes = rst.nodeList();

    rst.startSet();
    rst.initiate({
        _id: jsTestName(),
        members: [
            {_id: 0, host: nodes[0], priority: 1},
            {_id: 1, host: nodes[1], priority: 0},
            {_id: 2, host: nodes[2], arbiterOnly: true}
        ],
    });

    let primary = rst.getPrimary();

    let testDB = primary.getDB(jsTestName());
    let adminDB = primary.getDB("admin");

    // Create an admin user, one user with the inprog privilege, and one without.
    assert.commandWorked(adminDB.runCommand({createUser: "admin", pwd: "pwd", roles: ["root"]}));
    assert(adminDB.auth("admin", "pwd"));

    assert.commandWorked(adminDB.runCommand({
        createRole: "role_inprog",
        roles: [],
        privileges: [{resource: {cluster: true}, actions: ["inprog"]}]
    }));

    assert.commandWorked(adminDB.runCommand(
        {createUser: "user_inprog", pwd: "pwd", roles: ["role_inprog", "readAnyDatabase"]}));

    assert.commandWorked(
        adminDB.runCommand({createUser: "user_no_inprog", pwd: "pwd", roles: ["readAnyDatabase"]}));

    // Create some dummy test data.
    testDB.test.drop();

    for (let i = 0; i < 5; i++) {
        assert.writeOK(testDB.test.insert({_id: i, a: i}));
    }

    // Functions to support running an operation in a parallel shell for testing allUsers behaviour.
    function runInParallelShell({testfunc, username, password}) {
        TestData.aggCurOpTest = testfunc;
        TestData.aggCurOpUser = username;
        TestData.aggCurOpPwd = password;

        assert.commandWorked(
            adminDB.runCommand({configureFailPoint: "setYieldAllLocksHang", mode: "alwaysOn"}));

        testfunc = function() {
            db.getSiblingDB("admin").auth(TestData.aggCurOpUser, TestData.aggCurOpPwd);
            TestData.aggCurOpTest();
            db.getSiblingDB("admin").logout();
        };

        return startParallelShell(testfunc, primary.port);
    }

    function assertCurrentOpHasSingleMatchingEntry({currentOpAggFilter, curOpOpts}) {
        curOpOpts = (curOpOpts || {allUsers: true});

        let result = null;

        assert.soon(
            function() {
                result = adminDB.runCommand({
                    aggregate: 1,
                    pipeline: [{$currentOp: curOpOpts}, {$match: currentOpAggFilter}],
                    cursor: {}
                });

                assert.commandWorked(result);

                return (result.cursor.firstBatch.length === 1);
            },
            function() {
                return "Failed to find operation in $currentOp output: " + tojson(result);
            });
    }

    function waitForParallelShell(awaitShell) {
        assert.commandWorked(
            adminDB.runCommand({configureFailPoint: "setYieldAllLocksHang", mode: "off"}));

        awaitShell();
    }

    /**
     * Restarts a replica set with additional parameters, and optionally re-authenticates.
     */
    function restartReplSet(replSet, newOpts, user, pwd) {
        const numNodes = replSet.nodeList().length;

        for (let n = 0; n < numNodes; n++) {
            replSet.restart(n, newOpts);
        }

        primary = replSet.getPrimary();
        replSet.awaitSecondaryNodes();

        testDB = primary.getDB(jsTestName());
        adminDB = primary.getDB("admin");

        if (user && pwd) {
            adminDB.auth(user, pwd);
        }
    }

    //
    // Authenticate as user_no_inprog.
    //
    assert(adminDB.logout());
    assert(adminDB.auth("user_no_inprog", "pwd"));

    // Test that $currentOp fails with {allUsers: true} for a user without the "inprog" privilege.
    assert.commandFailedWithCode(
        adminDB.runCommand({aggregate: 1, pipeline: [{$currentOp: {allUsers: true}}], cursor: {}}),
        ErrorCodes.Unauthorized);

    // Test that $currentOp succeeds with {allUsers: false} for a user without the "inprog"
    // privilege.
    assert.commandWorked(adminDB.runCommand(
        {aggregate: 1, pipeline: [{$currentOp: {allUsers: false}}], cursor: {}}));

    // Test that $currentOp fails when run as {aggregate: 1} on a database other than admin.
    assert.commandFailedWithCode(
        testDB.runCommand({aggregate: 1, pipeline: [{$currentOp: {}}], cursor: {}}),
        ErrorCodes.InvalidNamespace);

    // Test that $currentOp fails when run on admin without {aggregate: 1}.
    assert.commandFailedWithCode(
        adminDB.runCommand({aggregate: "collname", pipeline: [{$currentOp: {}}], cursor: {}}),
        ErrorCodes.InvalidNamespace);

    // Test that $currentOp accepts all numeric types.
    const ones = [1, 1.0, NumberInt(1), NumberLong(1), NumberDecimal(1)];

    for (let one of ones) {
        assert.commandWorked(
            adminDB.runCommand({aggregate: one, pipeline: [{$currentOp: {}}], cursor: {}}));
    }

    // Test that {aggregate: 1} fails when the first stage in the pipeline is not $currentOp.
    assert.commandFailedWithCode(
        adminDB.runCommand({aggregate: 1, pipeline: [{$match: {}}], cursor: {}}),
        ErrorCodes.InvalidNamespace);

    // Test that $currentOp fails when it is not the first stage in the pipeline. We use two
    // $currentOp stages since any other stage in the initial position will trip the {aggregate: 1}
    // namespace check.
    assert.commandFailedWithCode(
        adminDB.runCommand(
            {aggregate: 1, pipeline: [{$currentOp: {}}, {$currentOp: {}}], cursor: {}}),
        ErrorCodes.BadValue);

    // Test that $currentOp succeeds if local readConcern is specified.
    assert.commandWorked(adminDB.runCommand(
        {aggregate: 1, pipeline: [{$currentOp: {}}], readConcern: {level: "local"}, cursor: {}}));

    // Test that $currentOp fails if a non-local readConcern is specified.
    assert.commandFailedWithCode(adminDB.runCommand({
        aggregate: 1,
        pipeline: [{$currentOp: {}}],
        readConcern: {level: "linearizable"},
        cursor: {}
    }),
                                 ErrorCodes.InvalidOptions);

    // Test that a user without the inprog privilege cannot see another user's operations.
    // Temporarily log in as 'user_inprog' to validate that the op is present in $currentOp output.
    assert(adminDB.logout());
    assert(adminDB.auth("user_inprog", "pwd"));

    let awaitShell = runInParallelShell({
        testfunc: function() {
            assert.eq(db.getSiblingDB(jsTestName())
                          .test.find({})
                          .comment("agg_current_op_allusers_test")
                          .itcount(),
                      5);
        },
        username: "user_inprog",
        password: "pwd"
    });

    assertCurrentOpHasSingleMatchingEntry({
        currentOpAggFilter: {"command.comment": "agg_current_op_allusers_test"},
        curOpOpts: {allUsers: true}
    });

    // Log back in as 'user_no_inprog' and validate that the user cannot see the op.
    assert(adminDB.logout());
    assert(adminDB.auth("user_no_inprog", "pwd"));

    assert.eq(adminDB
                  .runCommand({
                      aggregate: 1,
                      pipeline: [
                          {$currentOp: {allUsers: false}},
                          {$match: {"command.comment": "agg_current_op_allusers_test"}}
                      ],
                      cursor: {}
                  })
                  .cursor.firstBatch.length,
              0);

    waitForParallelShell(awaitShell);

    //
    // Authenticate as user_inprog.
    //
    assert(adminDB.logout());
    assert(adminDB.auth("user_inprog", "pwd"));

    // Test that $currentOp with {allUsers: true} succeeds for a user with the "inprog"
    // privilege.
    assert.commandWorked(
        adminDB.runCommand({aggregate: 1, pipeline: [{$currentOp: {allUsers: true}}], cursor: {}}));

    // Test that {idleConnections: false} returns only active connections.
    assert.eq(adminDB
                  .runCommand({
                      aggregate: 1,
                      pipeline: [
                          {$currentOp: {allUsers: true, idleConnections: false}},
                          {$match: {"active": false}}
                      ],
                      cursor: {}
                  })
                  .cursor.firstBatch.length,
              0);

    // Test that {idleConnections: true} returns inactive connections.
    const idleConn = new Mongo(primary.host);

    assert.gte(adminDB
                   .runCommand({
                       aggregate: 1,
                       pipeline: [
                           {$currentOp: {allUsers: true, idleConnections: true}},
                           {$match: {active: false}}
                       ],
                       cursor: {}
                   })
                   .cursor.firstBatch.length,
               1);

    // Test that a user with the inprog privilege can see another user's operations with
    // {allUsers: true}
    awaitShell = runInParallelShell({
        testfunc: function() {
            assert.eq(db.getSiblingDB(jsTestName())
                          .test.find({})
                          .comment("agg_current_op_allusers_test")
                          .itcount(),
                      5);
        },
        username: "user_no_inprog",
        password: "pwd"
    });

    assertCurrentOpHasSingleMatchingEntry(
        {currentOpAggFilter: {"command.comment": "agg_current_op_allusers_test"}});

    waitForParallelShell(awaitShell);

    // Test that collation rules apply to matches on $currentOp output.
    assert.eq(
        adminDB
            .runCommand({
                aggregate: 1,
                pipeline:
                    [{$currentOp: {}}, {$match: {"command.comment": "AGG_currént_op_COLLATION"}}],
                collation: {locale: "en_US", strength: 1},  // Case and diacritic insensitive.
                comment: "agg_current_op_collation",
                cursor: {}
            })
            .cursor.firstBatch.length,
        1);

    // Test that $currentOp is explainable.
    const explainPlan = assert.commandWorked(adminDB.runCommand({
        aggregate: 1,
        pipeline:
            [{$currentOp: {idleConnections: true, allUsers: false}}, {$match: {desc: "test"}}],
        explain: true
    }));

    const expectedStages =
        [{$currentOp: {idleConnections: true, allUsers: false}}, {$match: {desc: "test"}}];

    assert.eq(explainPlan.stages, expectedStages);

    // Test that the allUsers parameter is ignored when authentication is disabled.
    restartReplSet(rst, {keyFile: null});

    // Ensure that there is at least one other connection present.
    const otherConn = new Mongo(primary.host);

    // Verify that $currentOp displays all operations when auth is disabled regardless of the
    // allUsers parameter, by checking that the output is the same in both cases. We project static
    // fields from each operation so that a thread which becomes active between the two aggregations
    // is still comparable across the output of both.
    let aggCmd = {
        aggregate: 1,
        pipeline: [
            {$currentOp: {allUsers: true, idleConnections: true}},
            {$project: {desc: 1, threadId: 1, connectionId: 1, appName: 1}},
            {$sort: {threadId: 1}}
        ],
        cursor: {}
    };

    const aggAllUsersTrue = assert.commandWorked(adminDB.runCommand(aggCmd));
    aggCmd.pipeline[0].$currentOp.allUsers = false;
    const aggAllUsersFalse = assert.commandWorked(adminDB.runCommand(aggCmd));

    assert.eq(aggAllUsersFalse.cursor.firstBatch, aggAllUsersTrue.cursor.firstBatch);
})();