2015-11-19 14:01:59 -05:00
|
|
|
// Tests authentication with replica sets using key files.
|
|
|
|
|
//
|
|
|
|
|
// This test requires users to persist across a restart.
|
|
|
|
|
// @tags: [requires_persistence]
|
2011-10-05 16:55:55 -04:00
|
|
|
|
2014-10-14 09:08:22 -04:00
|
|
|
var testInvalidAuthStates = function() {
|
2011-10-05 16:55:55 -04:00
|
|
|
print("check that 0 is in recovering");
|
2015-12-10 10:21:51 -05:00
|
|
|
rs.waitForState(rs.nodes[0], ReplSetTest.State.RECOVERING);
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
print("shut down 1, 0 still in recovering.");
|
|
|
|
|
rs.stop(1);
|
|
|
|
|
sleep(5);
|
|
|
|
|
|
2015-12-10 10:21:51 -05:00
|
|
|
rs.waitForState(rs.nodes[0], ReplSetTest.State.RECOVERING);
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
print("shut down 2, 0 becomes a secondary.");
|
|
|
|
|
rs.stop(2);
|
|
|
|
|
|
2015-12-10 10:21:51 -05:00
|
|
|
rs.waitForState(rs.nodes[0], ReplSetTest.State.SECONDARY);
|
2011-10-05 16:55:55 -04:00
|
|
|
|
2015-11-19 11:06:58 -05:00
|
|
|
rs.restart(1, {"keyFile" : key1});
|
|
|
|
|
rs.restart(2, {"keyFile" : key1});
|
2011-10-05 16:55:55 -04:00
|
|
|
};
|
|
|
|
|
|
2014-11-24 17:12:40 -05:00
|
|
|
var name = "rs_auth2";
|
|
|
|
|
var path = "jstests/libs/";
|
|
|
|
|
|
2015-11-19 11:06:58 -05:00
|
|
|
// These keyFiles have their permissions set to 600 later in the test.
|
|
|
|
|
var key1 = path+"key1";
|
|
|
|
|
var key2 = path+"key2";
|
2014-11-24 17:12:40 -05:00
|
|
|
|
|
|
|
|
var rs = new ReplSetTest({name: name, nodes: 3});
|
|
|
|
|
var nodes = rs.startSet();
|
|
|
|
|
var hostnames = rs.nodeList();
|
|
|
|
|
rs.initiate({ "_id" : name,
|
|
|
|
|
"members" : [
|
|
|
|
|
{"_id" : 0, "host" : hostnames[0], "priority" : 2},
|
2014-12-02 15:43:13 -05:00
|
|
|
{"_id" : 1, "host" : hostnames[1], priority: 0},
|
|
|
|
|
{"_id" : 2, "host" : hostnames[2], priority: 0}
|
2014-11-24 17:12:40 -05:00
|
|
|
]});
|
|
|
|
|
|
2015-11-25 11:20:43 -05:00
|
|
|
var master = rs.getPrimary();
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
print("add an admin user");
|
2013-11-06 14:49:35 -05:00
|
|
|
master.getDB("admin").createUser({user: "foo", pwd: "bar", roles: jsTest.adminUserRoles},
|
|
|
|
|
{w: 3, wtimeout: 30000});
|
2014-10-14 09:08:22 -04:00
|
|
|
var m = rs.nodes[0];
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
print("starting 1 and 2 with key file");
|
|
|
|
|
rs.stop(1);
|
2015-11-19 11:06:58 -05:00
|
|
|
rs.restart(1, {"keyFile" : key1});
|
2011-10-05 16:55:55 -04:00
|
|
|
rs.stop(2);
|
2015-11-19 11:06:58 -05:00
|
|
|
rs.restart(2, {"keyFile" : key1});
|
2011-10-05 16:55:55 -04:00
|
|
|
|
2014-10-14 09:08:22 -04:00
|
|
|
// auth to all nodes with auth
|
|
|
|
|
rs.nodes[1].getDB("admin").auth("foo", "bar");
|
|
|
|
|
rs.nodes[2].getDB("admin").auth("foo", "bar");
|
|
|
|
|
testInvalidAuthStates();
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
print("restart mongod with bad keyFile");
|
|
|
|
|
|
|
|
|
|
rs.stop(0);
|
2015-11-19 11:06:58 -05:00
|
|
|
m = rs.restart(0, {"keyFile" : key2});
|
2011-10-05 16:55:55 -04:00
|
|
|
|
2014-10-14 09:08:22 -04:00
|
|
|
//auth to all nodes
|
|
|
|
|
rs.nodes[0].getDB("admin").auth("foo", "bar");
|
|
|
|
|
rs.nodes[1].getDB("admin").auth("foo", "bar");
|
|
|
|
|
rs.nodes[2].getDB("admin").auth("foo", "bar");
|
|
|
|
|
testInvalidAuthStates();
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
rs.stop(0);
|
2015-11-19 11:06:58 -05:00
|
|
|
m = rs.restart(0, {"keyFile" : key1});
|
2011-10-05 16:55:55 -04:00
|
|
|
|
|
|
|
|
print("0 becomes a secondary");
|