summaryrefslogtreecommitdiff
path: root/jstests/auth/role_management_commands_edge_cases.js
blob: 8adb757169251489c6e59bbeee5435b2c87f3900 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
 * This tests that all the different commands for role manipulation all properly handle invalid
 * and atypical inputs.
 */

function runTest(conn) {
    var db = conn.getDB('test');
    var admin = conn.getDB('admin');
    admin.createUser({user: 'userAdmin', pwd: 'pwd', roles: ['userAdminAnyDatabase']});
    admin.auth('userAdmin', 'pwd');

    (function testCreateRole() {
        jsTestLog("Testing createRole");

        // Role with no privs
        db.createRole({role: "role1", roles: [], privileges: []});

        // Role with duplicate other roles
        db.createRole({role: "role2", roles: ['read', 'read', 'role1', 'role1'], privileges: []});
        assert.eq(2, db.getRole("role2").roles.length);

        // Role with duplicate privileges
        db.createRole({
            role: "role3",
            roles: ['role2'],
            privileges: [
                {resource: {db: 'test', collection: ''}, actions: ['find']},
                {resource: {db: 'test', collection: ''}, actions: ['find']}
            ]
        });
        assert.eq(1, db.getRole("role3", {showPrivileges: true}).privileges.length);

        // Try to create role that already exists.
        assert.throws(function() {
            db.createRole({role: 'role2', roles: [], privileges: []});
        });

        // Try to create role with no name
        assert.throws(function() {
            db.createRole({role: '', roles: [], privileges: []});
        });

        // Try to create a role the wrong types for the arguments
        assert.throws(function() {
            db.createRole({role: 1, roles: [], privileges: []});
        });
        assert.throws(function() {
            db.createRole({role: ["role4", "role5"], roles: [], privileges: []});
        });
        assert.throws(function() {
            db.createRole({role: 'role6', roles: 1, privileges: []});
        });
        assert.throws(function() {
            db.createRole({role: 'role7', roles: [], privileges: 1});
        });

        // Try to create a role with an invalid privilege
        assert.throws(function() {
            db.createRole(
                {role: 'role8', roles: [], privileges: [{resource: {}, actions: ['find']}]});
        });
        assert.throws(function() {
            db.createRole({
                role: 'role9',
                roles: [],
                privileges: [{resource: {db: "test", collection: "foo"}, actions: []}]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role10',
                roles: [],
                privileges: [{resource: {db: "test"}, actions: ['find']}]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role11',
                roles: [],
                privileges: [{resource: {collection: "foo"}, actions: ['find']}]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role12',
                roles: [],
                privileges: [{resource: {anyResource: false}, actions: ['find']}]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role13',
                roles: [],
                privileges: [{
                    resource: {db: "test", collection: "foo", cluster: true},
                    actions: ['find']
                }]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role14',
                roles: [],
                privileges: [{resource: {cluster: false}, actions: ['find']}]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role15',
                roles: [],
                privileges: [{resource: {db: "test", collection: "$cmd"}, actions: ['find']}]
            });
        });
        assert.throws(function() {
            db.createRole({
                role: 'role16',
                roles: [],
                privileges:
                    [{resource: {db: "test", collection: "foo"}, actions: ['fakeAction']}]
            });
        });

        // Try to create role containing itself in its roles array
        assert.throws(function() {
            db.createRole({role: 'role17', roles: ['role10'], privileges: []});
        });

        assert.eq(3, db.getRoles().length);
    })();

    (function testUpdateRole() {
        jsTestLog("Testing updateRole");

        // Try to update role that doesn't exist
        assert.throws(function() {
            db.updateRole("fakeRole", {roles: []});
        });

        // Try to update role to have a role that doesn't exist
        assert.throws(function() {
            db.updateRole("role1", {roles: ['fakeRole']});
        });

        // Try to update a built-in role
        assert.throws(function() {
            db.updateRole("read", {roles: ['readWrite']});
        });

        // Try to create a cycle in the role graph
        assert.throws(function() {
            db.updateRole("role1", {roles: ['role1']});
        });
        assert.eq(0, db.getRole('role1').roles.length);

        assert.throws(function() {
            db.updateRole("role1", {roles: ['role2']});
        });
        assert.eq(0, db.getRole('role1').roles.length);

        assert.throws(function() {
            db.updateRole("role1", {roles: ['role3']});
        });
        assert.eq(0, db.getRole('role1').roles.length);
    })();

    (function testGrantRolesToRole() {
        jsTestLog("Testing grantRolesToRole");

        // Grant role1 to role2 even though role2 already has role1
        db.grantRolesToRole("role2", ['role1']);
        assert.eq(2, db.getRole('role2').roles.length);

        // Try to grant a role that doesn't exist
        assert.throws(function() {
            db.grantRolesToRole("role1", ['fakeRole']);
        });

        // Try to grant *to* a role that doesn't exist
        assert.throws(function() {
            db.grantRolesToRole("fakeRole", ['role1']);
        });

        // Must specify at least 1 role
        assert.throws(function() {
            db.grantRolesToRole("role1", []);
        });

        // Try to grant to a built-in role
        assert.throws(function() {
            db.grantRolesToRole("read", ['role1']);
        });

        // Try to create a cycle in the role graph
        assert.throws(function() {
            db.grantRolesToRole("role1", ['role1']);
        });
        assert.eq(0, db.getRole('role1').roles.length);

        assert.throws(function() {
            db.grantRolesToRole("role1", ['role2']);
        });
        assert.eq(0, db.getRole('role1').roles.length);

        assert.throws(function() {
            db.grantRolesToRole("role1", ['role3']);
        });
        assert.eq(0, db.getRole('role1').roles.length);
    })();

    (function testRevokeRolesFromRole() {
        jsTestLog("Testing revokeRolesFromRole");

        // Try to revoke a role that doesn't exist
        // Should not error but should do nothing.
        assert.doesNotThrow(function() {
            db.revokeRolesFromRole("role2", ['fakeRole']);
        });

        // Try to revoke role3 from role2 even though role2 does not contain role3.
        // Should not error but should do nothing.
        assert.doesNotThrow(function() {
            db.revokeRolesFromRole("role2", ['role3']);
        });
        assert.eq(2, db.getRole("role2").roles.length);

        // Must revoke something
        assert.throws(function() {
            db.revokeRolesFromRole("role2", []);
        });

        // Try to remove from built-in role
        assert.throws(function() {
            db.revokeRolesFromRole("readWrite", ['read']);
        });

    })();

    (function testGrantPrivilegesToRole() {
        jsTestLog("Testing grantPrivilegesToRole");

        // Must grant something
        assert.throws(function() {
            db.grantPrivilegesToRole("role1", []);
        });

        var basicPriv = {resource: {db: 'test', collection: ""}, actions: ['find']};

        // Invalid first argument
        assert.throws(function() {
            db.grantPrivilegesToRole(["role1", "role2"], [basicPriv]);
        });

        // Try to grant to role that doesn't exist
        assert.throws(function() {
            db.grantPrivilegesToRole("fakeRole", [basicPriv]);
        });

        // Test with invalid privileges
        var badPrivs = [];
        badPrivs.push("find");
        badPrivs.push({resource: {db: 'test', collection: ""}, actions: ['fakeAction']});
        badPrivs.push({resource: {db: ['test'], collection: ""}, actions: ['find']});
        badPrivs.push({resource: {db: 'test', collection: ""}});
        badPrivs.push({actions: ['find']});
        badPrivs.push({resource: {db: 'test', collection: ""}, actions: []});
        badPrivs.push({resource: {db: 'test'}, actions: ['find']});
        badPrivs.push({resource: {collection: 'test'}, actions: ['find']});
        badPrivs.push({resource: {}, actions: ['find']});
        badPrivs.push({resource: {db: 'test', collection: "", cluster: true}, actions: ['find']});

        for (var i = 0; i < badPrivs.length; i++) {
            assert.throws(function() {
                db.grantPrivilegesToRole("role1", [badPrivs[i]]);
            });
        }

        assert.eq(0, db.getRole('role1', {showPrivileges: true}).privileges.length);
    })();

    (function testRevokePrivilegesFromRole() {
        jsTestLog("Testing revokePrivilegesFromRole");

        // Try to revoke a privilege the role doesn't have
        // Should not error but should do nothing.
        assert.doesNotThrow(function() {
            db.revokePrivilegesFromRole(
                "role3", [{resource: {db: "test", collection: "foobar"}, actions: ["insert"]}]);
        });
        assert.eq(0, db.getRole("role2", {showPrivileges: true}).privileges.length);

        // Must revoke something
        assert.throws(function() {
            db.revokePrivilegesFromRole("role3", []);
        });

        // Try to remove from built-in role
        assert.throws(function() {
            db.revokePrivilegesFromRole(
                "readWrite", [{resource: {db: 'test', collection: ''}, actions: ['find']}]);
        });

        var basicPriv = {resource: {db: 'test', collection: ""}, actions: ['find']};

        // Invalid first argument
        assert.throws(function() {
            db.revokePrivilegesFromRole(["role3", "role2"], [basicPriv]);
        });

        // Try to revoke from role that doesn't exist
        assert.throws(function() {
            db.revokePrivilegesToRole("fakeRole", [basicPriv]);
        });

        // Test with invalid privileges
        var badPrivs = [];
        badPrivs.push("find");
        badPrivs.push({resource: {db: 'test', collection: ""}, actions: ['fakeAction']});
        badPrivs.push({resource: {db: ['test'], collection: ""}, actions: ['find']});
        badPrivs.push({resource: {db: 'test', collection: ""}});
        badPrivs.push({actions: ['find']});
        badPrivs.push({resource: {db: 'test', collection: ""}, actions: []});
        badPrivs.push({resource: {db: 'test'}, actions: ['find']});
        badPrivs.push({resource: {collection: 'test'}, actions: ['find']});
        badPrivs.push({resource: {}, actions: ['find']});
        badPrivs.push({resource: {db: 'test', collection: "", cluster: true}, actions: ['find']});

        for (var i = 0; i < badPrivs.length; i++) {
            assert.throws(function() {
                db.revokePrivilegesFromRole("role3", [badPrivs[i]]);
            });
        }

        assert.eq(1, db.getRole('role3', {showPrivileges: true}).privileges.length);
    })();

    (function testRolesInfo() {
        jsTestLog("Testing rolesInfo");

        // Try to get role that does not exist
        assert.eq(null, db.getRole('fakeRole'));

        // Pass wrong type for role name
        assert.throws(function() {
            db.getRole(5);
        });

        assert.throws(function() {
            db.getRole([]);
        });

        assert.throws(function() {
            db.getRole(['role1', 'role2']);
        });
    })();

    (function testDropRole() {
        jsTestLog("Testing dropRole");

        // Try to drop a role that doesn't exist
        // Should not error but should do nothing
        assert.doesNotThrow(function() {
            db.dropRole('fakeRole');
        });

        // Try to drop a built-in role
        assert.throws(function() {
            db.dropRole('read');
        });

        assert.eq(3, db.getRoles().length);
    })();

    // dropAllRolesFromDatabase ignores its arguments, so there's nothing to test for it.
}

jsTest.log('Test standalone');
var conn = MongoRunner.runMongod({auth: ''});
runTest(conn);
MongoRunner.stopMongod(conn);

jsTest.log('Test sharding');
var st = new ShardingTest({shards: 2, config: 3, keyFile: 'jstests/libs/key1'});
runTest(st.s);
st.stop();