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

Commit 72bb011

Browse files
daprahamianmbroadst
authored andcommitted
fix: do not attempt to auth against an arbiter
Fixes NODE-1909
1 parent 3c72479 commit 72bb011

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

Diff for: lib/connection/connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function performInitialHandshake(conn, options, callback) {
155155
conn.lastIsMasterMS = new Date().getTime() - start;
156156

157157
const credentials = options.credentials;
158-
if (credentials) {
158+
if (!ismaster.arbiterOnly && credentials) {
159159
credentials.resolveAuthMechanism(ismaster);
160160
authenticate(conn, credentials, callback);
161161
return;

Diff for: test/tests/unit/connect_tests.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
const BSON = require('bson');
4+
const mock = require('mongodb-mock-server');
5+
const expect = require('chai').expect;
6+
7+
const connect = require('../../../lib/connection/connect');
8+
const MongoCredentials = require('../../../lib/auth/mongo_credentials').MongoCredentials;
9+
const genClusterTime = require('./common').genClusterTime;
10+
11+
describe('Connect Tests', function() {
12+
const test = {};
13+
beforeEach(() => {
14+
return mock.createServer().then(mockServer => {
15+
test.server = mockServer;
16+
test.connectOptions = {
17+
host: test.server.host,
18+
port: test.server.port,
19+
bson: new BSON(),
20+
credentials: new MongoCredentials({
21+
username: 'testUser',
22+
password: 'pencil',
23+
source: 'admin',
24+
mechanism: 'plain'
25+
})
26+
};
27+
});
28+
});
29+
30+
afterEach(() => mock.cleanup());
31+
it('should auth against a non-arbiter', function(done) {
32+
const whatHappened = {};
33+
34+
test.server.setMessageHandler(request => {
35+
const doc = request.document;
36+
const $clusterTime = genClusterTime(Date.now());
37+
38+
if (doc.ismaster) {
39+
whatHappened.ismaster = true;
40+
request.reply(
41+
Object.assign({}, mock.DEFAULT_ISMASTER, {
42+
$clusterTime
43+
})
44+
);
45+
} else if (doc.saslStart) {
46+
whatHappened.saslStart = true;
47+
request.reply({ ok: 1 });
48+
}
49+
});
50+
51+
connect(test.connectOptions, err => {
52+
try {
53+
expect(whatHappened).to.have.property('ismaster', true);
54+
expect(whatHappened).to.have.property('saslStart', true);
55+
} catch (_err) {
56+
err = _err;
57+
}
58+
59+
done(err);
60+
});
61+
});
62+
63+
it('should not auth against an arbiter', function(done) {
64+
const whatHappened = {};
65+
test.server.setMessageHandler(request => {
66+
const doc = request.document;
67+
const $clusterTime = genClusterTime(Date.now());
68+
if (doc.ismaster) {
69+
whatHappened.ismaster = true;
70+
request.reply(
71+
Object.assign({}, mock.DEFAULT_ISMASTER, {
72+
$clusterTime,
73+
arbiterOnly: true
74+
})
75+
);
76+
} else if (doc.saslStart) {
77+
whatHappened.saslStart = true;
78+
request.reply({ ok: 0 });
79+
}
80+
});
81+
82+
connect(test.connectOptions, err => {
83+
try {
84+
expect(whatHappened).to.have.property('ismaster', true);
85+
expect(whatHappened).to.not.have.property('saslStart');
86+
} catch (_err) {
87+
err = _err;
88+
}
89+
90+
done(err);
91+
});
92+
});
93+
});

0 commit comments

Comments
 (0)