summaryrefslogtreecommitdiff
path: root/jstests/auth/list_collections_own_collections.js
blob: 82d17afc787ac61bdb89f70222ceca839782d309 (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
// Test nameOnly option of listCollections
(function() {
    "use strict";

    const dbName = "list_collections_own_collections";

    const nameSort = (a, b) => a.name > b.name;
    const resFoo = {"name": "foo", "type": "collection"};
    const resBar = {"name": "bar", "type": "view"};
    const resOther =
        [{"name": "otherCollection", "type": "collection"}, {"name": "otherView", "type": "view"}];
    const resSystemViews = {"name": "system.views", "type": "collection"};

    function runTestOnConnection(conn) {
        const admin = conn.getDB("admin");
        const db = conn.getDB(dbName);

        assert.commandWorked(admin.runCommand({createUser: "root", pwd: "root", roles: ["root"]}));
        assert(admin.auth("root", "root"));

        function createTestRoleAndUser(roleName, privs) {
            assert.commandWorked(
                admin.runCommand({createRole: roleName, roles: [], privileges: privs}));

            const userName = "user|" + roleName;
            assert.commandWorked(db.runCommand(
                {createUser: userName, pwd: "pwd", roles: [{role: roleName, db: "admin"}]}));
        }

        createTestRoleAndUser("roleWithExactNamespacePrivileges", [
            {resource: {db: dbName, collection: "foo"}, actions: ["find"]},
            {resource: {db: dbName, collection: "bar"}, actions: ["find"]}
        ]);

        createTestRoleAndUser("roleWithExactNamespaceAndSystemPrivileges", [
            {resource: {db: dbName, collection: "foo"}, actions: ["find"]},
            {resource: {db: dbName, collection: "bar"}, actions: ["find"]},
            {resource: {db: dbName, collection: "system.views"}, actions: ["find"]}
        ]);

        createTestRoleAndUser("roleWithCollectionPrivileges", [
            {resource: {db: "", collection: "foo"}, actions: ["find"]},
            {resource: {db: "", collection: "bar"}, actions: ["find"]}
        ]);

        createTestRoleAndUser("roleWithCollectionAndSystemPrivileges", [
            {resource: {db: "", collection: "foo"}, actions: ["find"]},
            {resource: {db: "", collection: "bar"}, actions: ["find"]},
            {resource: {db: "", collection: "system.views"}, actions: ["find"]}
        ]);

        createTestRoleAndUser("roleWithDatabasePrivileges", [
            {resource: {db: dbName, collection: ""}, actions: ["find"]},
        ]);

        createTestRoleAndUser("roleWithDatabaseAndSystemPrivileges", [
            {resource: {db: dbName, collection: ""}, actions: ["find"]},
            {resource: {db: dbName, collection: "system.views"}, actions: ["find"]}
        ]);

        createTestRoleAndUser("roleWithAnyNormalResourcePrivileges", [
            {resource: {db: "", collection: ""}, actions: ["find"]},
        ]);

        createTestRoleAndUser("roleWithAnyNormalResourceAndSystemPrivileges", [
            {resource: {db: "", collection: ""}, actions: ["find"]},
            {resource: {db: "", collection: "system.views"}, actions: ["find"]}
        ]);

        // Create the collection and view used by the tests.
        assert.commandWorked(db.dropDatabase());
        assert.commandWorked(db.createCollection("foo"));
        assert.commandWorked(db.createView("bar", "foo", []));

        // Create a collection and view that are never granted specific permissions, to ensure
        // they're only returned by listCollections when the role has access to the whole db/server.
        assert.commandWorked(db.createCollection("otherCollection"));
        assert.commandWorked(db.createView("otherView", "otherCollection", []));

        admin.logout();

        function runTestOnRole(roleName, expectedColls) {
            jsTestLog(roleName);
            const userName = "user|" + roleName;
            assert(db.auth(userName, "pwd"));

            let res;

            res = db.runCommand({listCollections: 1});
            assert.commandFailed(res);
            res = db.runCommand({listCollections: 1, nameOnly: true});
            assert.commandFailed(res);
            res = db.runCommand({listCollections: 1, authorizedCollections: true});
            assert.commandFailed(res);

            res = db.runCommand({listCollections: 1, nameOnly: true, authorizedCollections: true});
            assert.commandWorked(res);
            assert.eq(expectedColls.sort(nameSort), res.cursor.firstBatch.sort(nameSort));

            res = db.runCommand({
                listCollections: 1,
                nameOnly: true,
                authorizedCollections: true,
                filter: {"name": "foo"}
            });
            assert.commandWorked(res);
            assert.eq([resFoo], res.cursor.firstBatch);

            db.logout();
        }

        runTestOnRole("roleWithExactNamespacePrivileges", [resFoo, resBar]);
        runTestOnRole("roleWithExactNamespaceAndSystemPrivileges",
                      [resFoo, resBar, resSystemViews]);

        runTestOnRole("roleWithCollectionPrivileges", [resFoo, resBar]);
        runTestOnRole("roleWithCollectionAndSystemPrivileges", [resFoo, resBar, resSystemViews]);

        runTestOnRole("roleWithDatabasePrivileges", [resFoo, resBar, ...resOther]);
        runTestOnRole("roleWithDatabaseAndSystemPrivileges",
                      [resFoo, resBar, ...resOther, resSystemViews]);

        runTestOnRole("roleWithAnyNormalResourcePrivileges", [resFoo, resBar, ...resOther]);
        runTestOnRole("roleWithAnyNormalResourceAndSystemPrivileges",
                      [resFoo, resBar, ...resOther, resSystemViews]);
    }

    function runNoAuthTestOnConnection(conn) {
        const admin = conn.getDB("admin");
        const db = conn.getDB(dbName);

        assert.commandWorked(db.dropDatabase());
        assert.commandWorked(db.createCollection("foo"));
        assert.commandWorked(db.createView("bar", "foo", []));

        var resFull = db.runCommand({listCollections: 1});
        assert.commandWorked(resFull);
        var resAuthColls = db.runCommand({listCollections: 1, authorizedCollections: true});
        assert.commandWorked(resAuthColls);
        assert.eq(resFull.cursor.firstBatch.sort(nameSort),
                  resAuthColls.cursor.firstBatch.sort(nameSort));

        var resNameOnly = db.runCommand({listCollections: 1, nameOnly: true});
        assert.commandWorked(resNameOnly);
        var resNameOnlyAuthColls =
            db.runCommand({listCollections: 1, nameOnly: true, authorizedCollections: true});
        assert.commandWorked(resNameOnlyAuthColls);
        assert.eq(resNameOnly.cursor.firstBatch.sort(nameSort),
                  resNameOnlyAuthColls.cursor.firstBatch.sort(nameSort));

        var resWithFilter = db.runCommand({
            listCollections: 1,
            nameOnly: true,
            authorizedCollections: true,
            filter: {"name": "foo"}
        });
        assert.commandWorked(resWithFilter);
        assert.eq([{"name": "foo", "type": "collection"}], resWithFilter.cursor.firstBatch);
    }

    const mongod = MongoRunner.runMongod({auth: ''});
    runTestOnConnection(mongod);
    MongoRunner.stopMongod(mongod);

    const st = new ShardingTest({
        shards: 1,
        mongos: 1,
        config: 1,
        other: {keyFile: 'jstests/libs/key1', shardAsReplicaSet: false}
    });
    runTestOnConnection(st.s0);
    st.stop();

    const mongodNoAuth = MongoRunner.runMongod();
    runNoAuthTestOnConnection(mongodNoAuth);
    MongoRunner.stopMongod(mongodNoAuth);

    const stNoAuth =
        new ShardingTest({shards: 1, mongos: 1, config: 1, other: {shardAsReplicaSet: false}});
    runNoAuthTestOnConnection(stNoAuth.s0);
    stNoAuth.stop();

}());