jstests/auth/currentop_cursors_auth.js
// Run the various auth tests on the given shard or mongoS connection.
Run the various auth tests on the given shard or mongoS connection.
Run the various auth tests on the given shard or mongoS connection .
function runCursorTests(conn) {
    const db = conn.getDB("test");
    const adminDB = db.getSiblingDB("admin");
    assert.commandWorked(adminDB.logout());
    assert(adminDB.auth("ted", pass), "Authentication 2 Failed");
    const coll = db.jstests_currentop_cursors_auth;
    coll.drop();
    for (let i = 0; i < 5; ++i) {
        assert.commandWorked(coll.insert({val: i}));
    }
    const cursorId =
        assert.commandWorked(db.runCommand({find: "jstests_currentop_cursors_auth", batchSize: 2}))
            .cursor.id;
    let result = adminDB
                     .aggregate([
                         {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                         {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
                     ])
                     .toArray();
    assert.eq(result.length, 1, result);
    assert.commandWorked(adminDB.logout());
    assert(adminDB.auth("yuta", pass), "Authentication 3 Failed");
    result = adminDB
                 .aggregate([
                     {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                     {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
                 ])
                 .toArray();
    assert.eq(result.length, 0, result);
    result = adminDB
                 .aggregate([
                     {$currentOp: {localOps: true, idleCursors: true}},
                     {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
                 ])
                 .toArray();
    assert.eq(result.length, 0, result);
    if (FixtureHelpers.isMongos(db)) {
        assert.commandFailedWithCode(adminDB.runCommand({
            aggregate: 1,
            pipeline: [{$currentOp: {localOps: false, idleCursors: true}}],
            cursor: {}
        }),
                                     ErrorCodes.Unauthorized);
    }
    const secondCursorId =
        assert.commandWorked(db.runCommand({find: "jstests_currentop_cursors_auth", batchSize: 2}))
            .cursor.id;
    result = adminDB
                 .aggregate([
                     {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                     {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": secondCursorId}]}}
                 ])
                 .toArray();
    assert.eq(result.length, 1, result);
    assert.commandWorked(adminDB.logout());
    assert(adminDB.auth("ted", pass), "Authentication 4 Failed");
    result = adminDB
                 .aggregate([
                     {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
                     {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
                 ])
                 .toArray();
    assert.eq(result.length, 1, result);
    result =
        adminDB
            .aggregate([
                {$currentOp: {localOps: true, allUsers: true, idleCursors: true}},
                {$match: {type: "idleCursor", "cursor.cursorId": {$in: [cursorId, secondCursorId]}}}
            ])
            .toArray();
    assert.eq(result.length, 2, result);
    if (FixtureHelpers.isMongos(db)) {
        result = adminDB
                     .aggregate([
                         {$currentOp: {localOps: false, allUsers: true, idleCursors: true}},
                         {$match: {type: "idleCursor", shard: st.rs0.name}}
                     ])
                     .toArray();
        assert.eq(result.length, 2, result);
    }
    assert.commandWorked(
        db.runCommand({killCursors: coll.getName(), cursors: [cursorId, secondCursorId]}));
    assert.commandWorked(adminDB.logout());
}
function runCursorTests ( conn ) { const db = conn . getDB ( "test" ) ; const adminDB = db . getSiblingDB ( "admin" ) ; assert . commandWorked ( adminDB . logout ( ) ) ; assert ( adminDB . auth ( "ted" , pass ) , "Authentication 2 Failed" ) ; const coll = db . jstests_currentop_cursors_auth ; coll . drop ( ) ; for ( let i = 0 ; i < 5 ; ++ i ) { assert . commandWorked ( coll . insert ( { val : i } ) ) ; } const cursorId = assert . commandWorked ( db . runCommand ( { find : "jstests_currentop_cursors_auth" , batchSize : 2 } ) ) . cursor . id ; let result = adminDB . aggregate ( [ { $currentOp : { localOps : true , allUsers : false , idleCursors : true } } , { $match : { $and : [ { type : "idleCursor" } , { "cursor.cursorId" : cursorId } ] } } ] ) . toArray ( ) ; assert . eq ( result . length , 1 , result ) ; assert . commandWorked ( adminDB . logout ( ) ) ; assert ( adminDB . auth ( "yuta" , pass ) , "Authentication 3 Failed" ) ; result = adminDB . aggregate ( [ { $currentOp : { localOps : true , allUsers : false , idleCursors : true } } , { $match : { $and : [ { type : "idleCursor" } , { "cursor.cursorId" : cursorId } ] } } ] ) . toArray ( ) ; assert . eq ( result . length , 0 , result ) ; result = adminDB . aggregate ( [ { $currentOp : { localOps : true , idleCursors : true } } , { $match : { $and : [ { type : "idleCursor" } , { "cursor.cursorId" : cursorId } ] } } ] ) . toArray ( ) ; assert . eq ( result . length , 0 , result ) ; if ( FixtureHelpers . isMongos ( db ) ) { assert . commandFailedWithCode ( adminDB . runCommand ( { aggregate : 1 , pipeline : [ { $currentOp : { localOps : false , idleCursors : true } } ] , cursor : { } } ) , ErrorCodes . Unauthorized ) ; } const secondCursorId = assert . commandWorked ( db . runCommand ( { find : "jstests_currentop_cursors_auth" , batchSize : 2 } ) ) . cursor . id ; result = adminDB . aggregate ( [ { $currentOp : { localOps : true , allUsers : false , idleCursors : true } } , { $match : { $and : [ { type : "idleCursor" } , { "cursor.cursorId" : secondCursorId } ] } } ] ) . toArray ( ) ; assert . eq ( result . length , 1 , result ) ; assert . commandWorked ( adminDB . logout ( ) ) ; assert ( adminDB . auth ( "ted" , pass ) , "Authentication 4 Failed" ) ; result = adminDB . aggregate ( [ { $currentOp : { localOps : true , allUsers : false , idleCursors : true } } , { $match : { $and : [ { type : "idleCursor" } , { "cursor.cursorId" : cursorId } ] } } ] ) . toArray ( ) ; assert . eq ( result . length , 1 , result ) ; result = adminDB . aggregate ( [ { $currentOp : { localOps : true , allUsers : true , idleCursors : true } } , { $match : { type : "idleCursor" , "cursor.cursorId" : { $in : [ cursorId , secondCursorId ] } } } ] ) . toArray ( ) ; assert . eq ( result . length , 2 , result ) ; if ( FixtureHelpers . isMongos ( db ) ) { result = adminDB . aggregate ( [ { $currentOp : { localOps : false , allUsers : true , idleCursors : true } } , { $match : { type : "idleCursor" , shard : st . rs0 . name } } ] ) . toArray ( ) ; assert . eq ( result . length , 2 , result ) ; } assert . commandWorked ( db . runCommand ( { killCursors : coll . getName ( ) , cursors : [ cursorId , secondCursorId ] } ) ) ; assert . commandWorked ( adminDB . logout ( ) ) ; }
Run the various auth tests on the given shard or mongoS connection.
Run the various auth tests on the given shard or mongoS connection .
// Log in as the root user. // Verify that we can see our own cursor with {allUsers: false}. // Log in as the non-root user. // Verify that we cannot see the root user's cursor. // Make sure that the behavior is the same when 'allUsers' is not explicitly specified. // Verify that the user without the 'inprog' privilege cannot view shard cursors via mongoS. // Create a cursor with the second (non-root) user and confirm that we can see it. // Log back in with the root user and confirm that the first cursor is still present. // Confirm that the root user can see both users' cursors with {allUsers: true}. // The root user can also see both cursors on the shard via mongoS with {localOps: false}. // Clean up the cursors so that they don't affect subsequent tests. // Make sure to logout to allow __system user to use the implicit session.