Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix: do not attempt to auth against an arbiter
Browse files Browse the repository at this point in the history
Fixes NODE-1909
  • Loading branch information
daprahamian authored and mbroadst committed Mar 22, 2019
1 parent 3c72479 commit 72bb011
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/connection/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function performInitialHandshake(conn, options, callback) {
conn.lastIsMasterMS = new Date().getTime() - start;

const credentials = options.credentials;
if (credentials) {
if (!ismaster.arbiterOnly && credentials) {
credentials.resolveAuthMechanism(ismaster);
authenticate(conn, credentials, callback);
return;
Expand Down
93 changes: 93 additions & 0 deletions test/tests/unit/connect_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';

const BSON = require('bson');
const mock = require('mongodb-mock-server');
const expect = require('chai').expect;

const connect = require('../../../lib/connection/connect');
const MongoCredentials = require('../../../lib/auth/mongo_credentials').MongoCredentials;
const genClusterTime = require('./common').genClusterTime;

describe('Connect Tests', function() {
const test = {};
beforeEach(() => {
return mock.createServer().then(mockServer => {
test.server = mockServer;
test.connectOptions = {
host: test.server.host,
port: test.server.port,
bson: new BSON(),
credentials: new MongoCredentials({
username: 'testUser',
password: 'pencil',
source: 'admin',
mechanism: 'plain'
})
};
});
});

afterEach(() => mock.cleanup());
it('should auth against a non-arbiter', function(done) {
const whatHappened = {};

test.server.setMessageHandler(request => {
const doc = request.document;
const $clusterTime = genClusterTime(Date.now());

if (doc.ismaster) {
whatHappened.ismaster = true;
request.reply(
Object.assign({}, mock.DEFAULT_ISMASTER, {
$clusterTime
})
);
} else if (doc.saslStart) {
whatHappened.saslStart = true;
request.reply({ ok: 1 });
}
});

connect(test.connectOptions, err => {
try {
expect(whatHappened).to.have.property('ismaster', true);
expect(whatHappened).to.have.property('saslStart', true);
} catch (_err) {
err = _err;
}

done(err);
});
});

it('should not auth against an arbiter', function(done) {
const whatHappened = {};
test.server.setMessageHandler(request => {
const doc = request.document;
const $clusterTime = genClusterTime(Date.now());
if (doc.ismaster) {
whatHappened.ismaster = true;
request.reply(
Object.assign({}, mock.DEFAULT_ISMASTER, {
$clusterTime,
arbiterOnly: true
})
);
} else if (doc.saslStart) {
whatHappened.saslStart = true;
request.reply({ ok: 0 });
}
});

connect(test.connectOptions, err => {
try {
expect(whatHappened).to.have.property('ismaster', true);
expect(whatHappened).to.not.have.property('saslStart');
} catch (_err) {
err = _err;
}

done(err);
});
});
});

0 comments on commit 72bb011

Please sign in to comment.