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
|
/**
* This tests that all the different commands for user manipulation all work properly for all valid
* forms of input.
*/
function runAllUserManagementCommandsTests(conn, writeConcern) {
'use strict';
var hasAuthzError = function(result) {
assert(result instanceof WriteCommandError);
assert.eq(ErrorCodes.Unauthorized, result.code);
};
conn.getDB('admin').createUser({user: 'admin', pwd: 'pwd', roles: ['root']}, writeConcern);
conn.getDB('admin').auth('admin', 'pwd');
conn.getDB('admin').createUser({
user: 'userAdmin',
pwd: 'pwd',
roles: ['userAdminAnyDatabase'],
customData: {userAdmin: true}
},
writeConcern);
conn.getDB('admin').logout();
var userAdminConn = new Mongo(conn.host);
userAdminConn.getDB('admin').auth('userAdmin', 'pwd');
var testUserAdmin = userAdminConn.getDB('test');
testUserAdmin.createRole({
role: 'testRole',
roles: [],
privileges: [{resource: {db: 'test', collection: ''}, actions: ['viewRole']}],
},
writeConcern);
userAdminConn.getDB('admin').createRole({
role: 'adminRole',
roles: [],
privileges: [{resource: {cluster: true}, actions: ['connPoolSync']}]
},
writeConcern);
var db = conn.getDB('test');
// At this point there are 2 handles to the "test" database in use - "testUserAdmin" and "db".
// "testUserAdmin" is on a connection which has been auth'd as a user with the
// 'userAdminAnyDatabase' role. This will be used for manipulating the user defined roles
// used in the test. "db" is a handle to the test database on a connection that has not
// yet been authenticated as anyone. This is the connection that will be used to log in as
// various users and test that their access control is correct.
(function testCreateUser() {
jsTestLog("Testing createUser");
testUserAdmin.createUser({
user: "spencer",
pwd: "pwd",
customData: {zipCode: 10028},
roles: ['readWrite', 'testRole', {role: 'adminRole', db: 'admin'}]
},
writeConcern);
testUserAdmin.createUser({user: "andy", pwd: "pwd", roles: []}, writeConcern);
var user = testUserAdmin.getUser('spencer');
assert.eq(10028, user.customData.zipCode);
assert(db.auth('spencer', 'pwd'));
assert.writeOK(db.foo.insert({a: 1}));
assert.eq(1, db.foo.findOne().a);
assert.doesNotThrow(function() {
db.getRole('testRole');
});
assert.commandWorked(db.adminCommand('connPoolSync'));
db.logout();
assert(db.auth('andy', 'pwd'));
hasAuthzError(db.foo.insert({a: 1}));
assert.throws(function() {
db.foo.findOne();
});
assert.throws(function() {
db.getRole('testRole');
});
})();
(function testUpdateUser() {
jsTestLog("Testing updateUser");
testUserAdmin.updateUser('spencer', {pwd: 'password', customData: {}}, writeConcern);
var user = testUserAdmin.getUser('spencer');
assert.eq(null, user.customData.zipCode);
assert(!db.auth('spencer', 'pwd'));
assert(db.auth('spencer', 'password'));
testUserAdmin.updateUser(
'spencer', {customData: {zipCode: 10036}, roles: ["read", "testRole"]}, writeConcern);
var user = testUserAdmin.getUser('spencer');
assert.eq(10036, user.customData.zipCode);
hasAuthzError(db.foo.insert({a: 1}));
assert.eq(1, db.foo.findOne().a);
assert.eq(1, db.foo.count());
assert.doesNotThrow(function() {
db.getRole('testRole');
});
assert.commandFailedWithCode(db.adminCommand('connPoolSync'), ErrorCodes.Unauthorized);
testUserAdmin.updateUser(
'spencer', {roles: ["readWrite", {role: 'adminRole', db: 'admin'}]}, writeConcern);
assert.writeOK(db.foo.update({}, {$inc: {a: 1}}));
assert.eq(2, db.foo.findOne().a);
assert.eq(1, db.foo.count());
assert.throws(function() {
db.getRole('testRole');
});
assert.commandWorked(db.adminCommand('connPoolSync'));
})();
(function testGrantRolesToUser() {
jsTestLog("Testing grantRolesToUser");
assert.commandFailedWithCode(db.runCommand({collMod: 'foo'}), ErrorCodes.Unauthorized);
testUserAdmin.grantRolesToUser('spencer',
[
'readWrite',
'dbAdmin',
{role: 'readWrite', db: 'test'},
{role: 'testRole', db: 'test'},
'readWrite'
],
writeConcern);
assert.commandWorked(db.runCommand({collMod: 'foo'}));
assert.writeOK(db.foo.update({}, {$inc: {a: 1}}));
assert.eq(3, db.foo.findOne().a);
assert.eq(1, db.foo.count());
assert.doesNotThrow(function() {
db.getRole('testRole');
});
assert.commandWorked(db.adminCommand('connPoolSync'));
})();
(function testRevokeRolesFromUser() {
jsTestLog("Testing revokeRolesFromUser");
testUserAdmin.revokeRolesFromUser(
'spencer',
[
'readWrite',
{role: 'dbAdmin', db: 'test2'}, // role user doesnt have
"testRole"
],
writeConcern);
assert.commandWorked(db.runCommand({collMod: 'foo'}));
hasAuthzError(db.foo.update({}, {$inc: {a: 1}}));
assert.throws(function() {
db.foo.findOne();
});
assert.throws(function() {
db.getRole('testRole');
});
assert.commandWorked(db.adminCommand('connPoolSync'));
testUserAdmin.revokeRolesFromUser(
'spencer', [{role: 'adminRole', db: 'admin'}], writeConcern);
hasAuthzError(db.foo.update({}, {$inc: {a: 1}}));
assert.throws(function() {
db.foo.findOne();
});
assert.throws(function() {
db.getRole('testRole');
});
assert.commandFailedWithCode(db.adminCommand('connPoolSync'), ErrorCodes.Unauthorized);
})();
(function testUsersInfo() {
jsTestLog("Testing usersInfo");
var res = testUserAdmin.runCommand({usersInfo: 'spencer'});
printjson(res);
assert.eq(1, res.users.length);
assert.eq(10036, res.users[0].customData.zipCode);
res = testUserAdmin.runCommand({usersInfo: {user: 'spencer', db: 'test'}});
assert.eq(1, res.users.length);
assert.eq(10036, res.users[0].customData.zipCode);
// UsersInfo results are ordered alphabetically by user field then db field,
// not by user insertion order
res = testUserAdmin.runCommand({usersInfo: ['spencer', {user: 'userAdmin', db: 'admin'}]});
printjson(res);
assert.eq(2, res.users.length);
assert.eq("spencer", res.users[0].user);
assert.eq(10036, res.users[0].customData.zipCode);
assert(res.users[1].customData.userAdmin);
assert.eq("userAdmin", res.users[1].user);
res = testUserAdmin.runCommand({usersInfo: 1});
assert.eq(2, res.users.length);
assert.eq("andy", res.users[0].user);
assert.eq("spencer", res.users[1].user);
assert(!res.users[0].customData);
assert.eq(10036, res.users[1].customData.zipCode);
res = testUserAdmin.runCommand({usersInfo: {forAllDBs: true}});
printjson(res);
assert.eq(4, res.users.length);
assert.eq("admin", res.users[0].user);
assert.eq("andy", res.users[1].user);
assert.eq("spencer", res.users[2].user);
assert.eq("userAdmin", res.users[3].user);
})();
(function testDropUser() {
jsTestLog("Testing dropUser");
assert(db.auth('spencer', 'password'));
assert(db.auth('andy', 'pwd'));
testUserAdmin.dropUser('spencer', writeConcern);
assert(!db.auth('spencer', 'password'));
assert(db.auth('andy', 'pwd'));
assert.eq(1, testUserAdmin.getUsers().length);
})();
(function testDropAllUsersFromDatabase() {
jsTestLog("Testing dropAllUsersFromDatabase");
assert.eq(1, testUserAdmin.getUsers().length);
assert(db.auth('andy', 'pwd'));
testUserAdmin.dropAllUsers(writeConcern);
assert(!db.auth('andy', 'pwd'));
assert.eq(0, testUserAdmin.getUsers().length);
})();
}
|