summaryrefslogtreecommitdiff
path: root/jstests/auth/user_management_commands_edge_cases.js
blob: b8f11505c58fe3d9112e232a4d8d7e98ef5ee7bc (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
/**
 * This tests that all the different commands for user 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 testCreateUser() {
         jsTestLog("Testing createUser");

         db.createUser({user: 'user1', pwd: 'pwd', roles: []});

         // Try to create duplicate user
         assert.throws(function() {
                           db.createUser({user: 'user1', pwd: 'pwd', roles: ['read']});
                       });
         assert.eq(0, db.getUser('user1').roles.length);

         // Try to create user with role that doesn't exist
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: ['fakeRole']});
                       });

         // Try to create user with invalid arguments
         assert.throws(function() {
                           db.createUser({user: '', pwd: 'pwd', roles: ['read']});
                       });
         assert.throws(function() {
                           db.createUser({user: ['user2'], pwd: 'pwd', roles: ['read']});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: '', roles: ['read']});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: ['pwd'], roles: ['read']});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: ['']});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: [{}]});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: [1]});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: [{role: 'read'}]});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: [{db: 'test'}]});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: [{role: 'read',
                                                                              db:''}]});
                       });
         assert.throws(function() {
                           db.createUser({user: 'user2', pwd: 'pwd', roles: [{role: '',
                                                                              db: 'test'}]});
                       });
         assert.throws(function() {
                           db.createUser({user: 'null\u0000char', pwd: 'pwd', roles: []});
                       });
        assert.throws(function() {
                           db.createUser({user: 'null\0char', pwd: 'pwd', roles: []});
                       });
         // Regression test for SERVER-17125
         assert.throws(function() {
                           db.getSiblingDB('$external').createUser({user: '', roles: []});
                       });

         assert.eq(1, db.getUsers().length);
     })();

    (function testUpdateUser() {
         jsTestLog("Testing updateUser");

         // Must update something
         assert.throws(function() {
                           db.updateUser('user1', {});
                       });

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

         // Try to update user that doesn't exist
         assert.throws(function() {
                           db.updateUser('fakeUser', {roles: ['read']});
                       });

         // Try to update user with invalid password
         assert.throws(function() {
                           db.updateUser('user1', {pwd: ''});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {pwd: 5});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {pwd: ['a']});
                       });


         // Try to update user with invalid customData
         assert.throws(function() {
                           db.updateUser('user1', {customData: 1});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {customData: ""});
                       });


         // Try to update with invalid "roles" argument
         assert.throws(function() {
                           db.updateUser('user1', {roles: 'read'});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: ['']});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: [{}]});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: [1]});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: [{role: 'read'}]});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: [{db: 'test'}]});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: [{role: '', db:'test'}]});
                       });
         assert.throws(function() {
                           db.updateUser('user1', {roles: [{role: 'read', db: ''}]});
                       });

         assert.eq(0, db.getUser('user1').roles.length);
     })();

    (function testGrantRolesToUser() {
         jsTestLog("Testing grantRolesToUser");

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

         // Try to grant to user that doesn't exist
         assert.throws(function() {
                           db.grantRolesToUser('fakeUser', {roles: ['read']});
                       });

         // Must grant something
         assert.throws(function() {
                           db.grantRolesToUser('user1', []);
                       });

         // Try to grant with invalid arguments
         assert.throws(function() {
                           db.grantRolesToUser('user1', 1);
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', [{}]);
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', [1]);
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', 'read');
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', [{role: 'read'}]);
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', [{db: 'test'}]);
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', [{role: 'read', db: ''}]);
                       });
         assert.throws(function() {
                           db.grantRolesToUser('user1', [{role: '', db: 'test'}]);
                       });

         assert.eq(0, db.getUser('user1').roles.length);
         assert.eq(null, db.getUser('user1').customData);
         // Make sure password didn't change
         assert(new Mongo(db.getMongo().host).getDB(db.getName()).auth('user1', 'pwd'));
     })();

    (function testRevokeRolesFromUser() {
         jsTestLog("Testing revokeRolesFromUser");

         // Revoking a role the user doesn't have should succeed but do nothing
         db.revokeRolesFromUser('user1', ['read']);

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

         // Try to revoke from user that doesn't exist
         assert.throws(function() {
                           db.revokeRolesFromUser('fakeUser', {roles: ['read']});
                       });

         // Must revoke something
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', []);
                       });

         // Try to revoke with invalid arguments
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', 1);
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', [{}]);
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', [1]);
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', 'read');
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', [{role: 'read'}]);
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', [{db: 'test'}]);
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', [{role: 'read', db: ''}]);
                       });
         assert.throws(function() {
                           db.revokeRolesFromUser('user1', [{role: '', db: 'test'}]);
                       });

         assert.eq(0, db.getUser('user1').roles.length);
     })();

    (function testUsersInfo() {
         jsTestLog("Testing usersInfo");

         // Try to get user that does not exist
         assert.eq(null, db.getUser('fakeUser'));

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

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

         assert.throws(function() {
                           db.getUser(['user1']);
                       });

     })();

    (function testDropUser() {
         jsTestLog("Testing dropUser");

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

         assert.eq(1, db.getUsers().length);
     })();

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

jsTest.log('Test standalone');
var conn = MongoRunner.runMongod({ auth: '' });
conn.getDB('admin').runCommand({setParameter:1, newCollectionsUsePowerOf2Sizes: false});
runTest(conn);
MongoRunner.stopMongod(conn.port);

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