Files
mongo/jstests/replsets/auth_no_pri.js
Vishnu K a800fe4073 SERVER-101495 set priority:0 on nodes that shouldn't become primary. (#32895)
GitOrigin-RevId: a5ac7e9c1a8304528d37dffd6a938e2ebdd04a19
2025-04-09 22:40:32 +00:00

35 lines
1.2 KiB
JavaScript

// Test that you can still authenticate a replset connection to a RS with no primary (SERVER-6665).
import {ReplSetTest} from "jstests/libs/replsettest.js";
const NODE_COUNT = 2;
const rs = new ReplSetTest({
"nodes": [
{},
// Set priority: 0 to ensure that this node can't become the primary.
{rsConfig: {priority: 0}},
],
keyFile: "jstests/libs/key1"
});
const nodes = rs.startSet();
rs.initiate();
// Add user
const primary = rs.getPrimary();
primary.getDB("admin").createUser({user: "admin", pwd: "pwd", roles: ["root"]}, {w: NODE_COUNT});
// Can authenticate replset connection when whole set is up.
const conn = new Mongo(rs.getURL());
assert(conn.getDB('admin').auth('admin', 'pwd'));
assert.commandWorked(conn.getDB('admin').foo.insert({a: 1}, {writeConcern: {w: NODE_COUNT}}));
// Make sure there is no primary
rs.stop(0);
rs.awaitSecondaryNodes(null, [nodes[1]]);
// Make sure you can still authenticate a replset connection with no primary
const conn2 = new Mongo(rs.getURL());
conn2.setSecondaryOk();
assert(conn2.getDB('admin').auth({user: 'admin', pwd: 'pwd', mechanism: "SCRAM-SHA-1"}));
assert.eq(1, conn2.getDB('admin').foo.findOne().a);
rs.stopSet();