Skip to content

Commit 97382cb

Browse files
committed
Fixes propert detection of SSL based replicaset using MongoClient.connect and Db.connect #849
1 parent 97abcc5 commit 97382cb

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

lib/mongodb/db.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2018,10 +2018,16 @@ Db.connect = function(url, options, callback) {
20182018
// need to verify that it's one or the other and fail if it's a mix
20192019
// Connect to all servers and run ismaster
20202020
for(var i = 0; i < object.servers.length; i++) {
2021+
// Set up socket options
2022+
var _server_options = {socketOptions:{connectTimeoutMS:1000}, auto_reconnect:false};
2023+
// Add ssl if missing
2024+
if(object.server_options.socketOptions &&
2025+
object.server_options.socketOptions.ssl) _server_options.ssl = object.server_options.socketOptions.ssl;
2026+
20212027
// Set up the Server object
20222028
var _server = object.servers[i].domain_socket
2023-
? new Server(object.servers[i].domain_socket, {socketOptions:{connectTimeoutMS:1000}, auto_reconnect:false})
2024-
: new Server(object.servers[i].host, object.servers[i].port, {socketOptions:{connectTimeoutMS:1000}, auto_reconnect:false});
2029+
? new Server(object.servers[i].domain_socket, _server_options)
2030+
: new Server(object.servers[i].host, object.servers[i].port, _server_options);
20252031

20262032
// Attempt connect
20272033
new Db(object.dbName, _server, {safe:false, native_parser:false}).open(function(err, db) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var mongodb = process.env['TEST_NATIVE'] != null ? require('../../../lib/mongodb').native() : require('../../../lib/mongodb').pure();
2+
3+
var testCase = require('nodeunit').testCase,
4+
debug = require('util').debug,
5+
inspect = require('util').inspect,
6+
nodeunit = require('nodeunit'),
7+
gleak = require('../../../dev/tools/gleak'),
8+
Db = mongodb.Db,
9+
Cursor = mongodb.Cursor,
10+
Collection = mongodb.Collection,
11+
Server = mongodb.Server,
12+
MongoClient = mongodb.MongoClient,
13+
ServerManager = require('../../../test/tools/server_manager').ServerManager,
14+
Step = require("step");
15+
16+
var MONGODB = 'integration_tests';
17+
var serverManager = null;
18+
var ssl = true;
19+
20+
/**
21+
* Retrieve the server information for the current
22+
* instance of the db client
23+
*
24+
* @ignore
25+
*/
26+
exports.setUp = function(callback) {
27+
callback();
28+
}
29+
30+
/**
31+
* Retrieve the server information for the current
32+
* instance of the db client
33+
*
34+
* @ignore
35+
*/
36+
exports.tearDown = function(callback) {
37+
callback();
38+
}
39+
40+
exports.shouldCorrectlyCommunicateUsingSSLSocket = function(test) {
41+
if(process.env['JENKINS']) return test.done();
42+
var db1 = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize:4, ssl:ssl}), {w:0, native_parser: (process.env['TEST_NATIVE'] != null)});
43+
// All inserted docs
44+
var docs = [];
45+
var errs = [];
46+
var insertDocs = [];
47+
48+
// Start server
49+
serverManager = new ServerManager({auth:false, purgedirectories:true, journal:true, ssl:ssl})
50+
serverManager.start(true, function() {
51+
MongoClient.connect("mongodb://localhost:27017/test?ssl=true", function(err, db) {
52+
test.equal(null, err);
53+
test.ok(db != null);
54+
55+
db.close();
56+
serverManager.killAll();
57+
test.done();
58+
});
59+
});
60+
}
61+
62+
/**
63+
* Retrieve the server information for the current
64+
* instance of the db client
65+
*
66+
* @ignore
67+
*/
68+
exports.noGlobalsLeaked = function(test) {
69+
var leaks = gleak.detectNew();
70+
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
71+
test.done();
72+
}

test/auxilliary/ssl/repl_set_ssl_test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var testCase = require('nodeunit').testCase,
99
Cursor = mongodb.Cursor,
1010
Collection = mongodb.Collection,
1111
Server = mongodb.Server,
12+
MongoClient = mongodb.MongoClient,
1213
ReplSetServers = mongodb.ReplSetServers,
1314
ReplicaSetManager = require('../../../test/tools/replica_set_manager').ReplicaSetManager,
1415
Step = require("step");
@@ -28,7 +29,7 @@ exports.setUp = function(callback) {
2829
RS = new ReplicaSetManager({retries:120,
2930
ssl:ssl,
3031
arbiter_count:1,
31-
secondary_count:1,
32+
secondary_count:2,
3233
passive_count:1});
3334
RS.startSet(true, function(err, result) {
3435
if(err != null) throw err;
@@ -66,6 +67,16 @@ exports.shouldCorrectlyConncetToSSLBasedReplicaset = function(test) {
6667
p_db.close();
6768
});
6869
}
70+
71+
exports.shouldCorrectlyConncetMongoClient = function(test) {
72+
// Connect to the replicaset
73+
MongoClient.connect("mongodb://localhost:" + RS.ports[1] + ",localhost:" + RS.ports[0] + "/foo?ssl=true", function(err, db) {
74+
test.equal(null, err);
75+
test.ok(db != null);
76+
db.close();
77+
test.done();
78+
});
79+
}
6980

7081
/**
7182
* Retrieve the server information for the current

test/tools/server_manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ServerManager.prototype.stop = function(signal, callback) {
114114

115115
ServerManager.prototype.killAll = function(callback) {
116116
exec('killall -9 mongod', function(err, stdout, stderr) {
117-
callback(null, null);
117+
if(typeof callback == 'function') callback(null, null);
118118
});
119119
}
120120

0 commit comments

Comments
 (0)