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
|
// test that rotating logs creates a new log file for the old logs
// then redo that test with auth confirming that the needed roles are as expected
var logDir = "/tmp";
var logPath = logDir + "/testlog";
// start up a mongod with a log path
var mongodInstance = MongoRunner.runMongod({logpath: logPath});
var newDB = mongodInstance.getDB("log_rotate");
newDB.dropDatabase();
var i;
for (i = 0; i < 100; i++) {
newDB.logtest.insert({thing: i});
}
// rotate logs and see that file count in the log directory increases by one
var pre_count = listFiles(logDir).length;
assert.commandWorked(newDB.adminCommand({logRotate: 1}));
var post_count = listFiles(logDir).length;
assert.eq(pre_count + 1, post_count);
// shut down mongod
MongoRunner.stopMongod(mongodInstance.port);
// now again but with auth to confirm necessary roles are as expected
var authzErrorCode = 13;
var dbAdminUsername = 'admin';
var dbAdminPassword = 'admin';
mongodInstance = MongoRunner.runMongod({logpath: logPath, auth: ""});
// create a user to create logs
newDB = mongodInstance.getDB('admin');
newDB.dropDatabase();
newDB.createUser({'user':dbAdminUsername, 'pwd':dbAdminPassword, 'roles':['userAdminAnyDatabase']});
newDB.auth(dbAdminUsername, dbAdminPassword);
pre_count = listFiles(logDir).length;
// create user without the clusterAdmin role and make sure that logRotate fails
newDB.createUser({'user':'nonClusterAdmin', 'pwd':'nonClusterAdmin', 'roles':['dbAdmin','read','readWrite','dbAdmin','userAdmin']});
newDB.auth('nonClusterAdmin', 'nonClusterAdmin');
assert.commandFailedWithCode(newDB.runCommand('logRotate'), authzErrorCode);
// remove the above user that was created
newDB.auth(dbAdminUsername, dbAdminPassword);
newDB.dropUser('nonClusterAdmin');
// create user with the clusterAdmin role and ensure that logRotate passes
newDB.createUser({'user':'clusterAdmin', 'pwd':'clusterAdmin', 'roles':['clusterAdmin']});
newDB.auth('clusterAdmin', 'clusterAdmin');
assert.commandWorked(newDB.runCommand('logRotate'));
post_count = listFiles(logDir).length;
assert.eq(pre_count + 1, post_count);
// shut down mongod
MongoRunner.stopMongod(mongodInstance.port);
|