Skip to content

Commit

Permalink
Fixed auth for MongoClient, getAllRawConnections was wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Dec 11, 2012
1 parent 47e51b7 commit 0435daf
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/mongodb/connection/repl_set.js
Expand Up @@ -1088,9 +1088,8 @@ ReplSet.prototype.allRawConnections = function() {
var allMasterConnections = this._state.master.connectionPool.getAllConnections();
// Add all connections to list
allConnections = allConnections.concat(allMasterConnections);

// If we have read secondary let's add all secondary servers
if(this.readSecondary && Object.keys(this._state.secondaries).length > 0) {
if(Object.keys(this._state.secondaries).length > 0) {
// Get all the keys
var keys = Object.keys(this._state.secondaries);
// For each of the secondaries grab the connections
Expand Down
5 changes: 3 additions & 2 deletions lib/mongodb/db.js
Expand Up @@ -73,7 +73,7 @@ function Db(databaseName, serverConfig, options) {
var overrideUsedFlag = this.options['override_used_flag'] == null ? false : this.options['override_used_flag'];

// Verify that nobody is using this config
if(!overrideUsedFlag && typeof this.serverConfig == 'object' && this.serverConfig._isUsed && this.serverConfig._isUsed()) {
if(!overrideUsedFlag && this.serverConfig != null && typeof this.serverConfig == 'object' && this.serverConfig._isUsed && this.serverConfig._isUsed()) {
throw new Error("A Server or ReplSet instance cannot be shared across multiple Db instances");
} else if(!overrideUsedFlag && typeof this.serverConfig == 'object'){
// Set being used
Expand Down Expand Up @@ -639,7 +639,7 @@ Db.prototype.authenticate = function(username, password, options, callback) {
var numberOfConnections = 0;
var errorObject = null;

if (options['connection' != null]) {
if(options['connection' != null]) {
//if a connection was explicitly passed on options, then we have only one...
numberOfConnections = 1;
} else {
Expand Down Expand Up @@ -1974,6 +1974,7 @@ Db.connect = function(url, options, callback) {

// Parse the string
var object = parse(url);

// Merge in any options for db in options object
if(dbOptions) {
for(var name in dbOptions) object.db_options[name] = dbOptions[name];
Expand Down
6 changes: 4 additions & 2 deletions lib/mongodb/mongo_client.js
Expand Up @@ -102,8 +102,10 @@ MongoClient.connect = function(url, options, callback) {
Db.connect(url, options, function(err, db) {
if(err) return callback(err, null);
// If no write concern is set set the default to w:1
if(db.options !== null && !db.options.safe && !db.options.journal && !db.options.w && !db.options.fsync) {
db.options.w = 1;
if(db.options !== null
&& db.options.safe == null && db.options.journal == null
&& db.options.w == null && db.options.fsync == null && db.options.j == null) {
db.options.w = 1;
}
// Return the db
callback(null, db);
Expand Down
80 changes: 77 additions & 3 deletions test/auxilliary/auth/replicaset_auth_test.js
Expand Up @@ -4,10 +4,12 @@ var testCase = require('nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
nodeunit = require('nodeunit'),
format = require('util').format,
gleak = require('../../../dev/tools/gleak'),
Db = mongodb.Db,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
MongoClient = mongodb.MongoClient,
Server = mongodb.Server,
ReadPreference = mongodb.ReadPreference,
ReplSetServers = mongodb.ReplSetServers,
Expand Down Expand Up @@ -387,9 +389,6 @@ exports.shouldCorrectlyBringReplicasetStepDownPrimaryAndStillReadFromSecondary =
});
}




exports.shouldCorrectlyAuthWithSecondaryAfterKillPrimary = function(test) {
var replSet = new ReplSetServers([
new Server(RS.host, RS.ports[0]), new Server(RS.host, RS.ports[1]), ], {
Expand Down Expand Up @@ -455,7 +454,82 @@ exports.shouldCorrectlyAuthWithSecondaryAfterKillPrimary = function(test) {
});
}

exports.shouldCorrectlyAuthAgainstReplicaSetAdminDbUsingMongoClient = function(test) {
var replSet = new ReplSetServers([
new Server(RS.host, RS.ports[0]), new Server(RS.host, RS.ports[1]), ], {
rs_name: RS.name,
read_secondary: true
});

// var dbName = MONGODB;
var dbName = 'admin';

new Db(dbName, replSet, {w:3}).open(function(err, db_p) {
db_p.admin().addUser("me", "secret", function runWhatever(err, result) {
test.equal(null, err);
test.ok(result != null);
db_p.close();

MongoClient.connect(format("mongodb://me:secret@%s:%s/%s?rs_name=%s&readPreference=secondary&w=3"
, RS.host, RS.ports[0], dbName, RS.name), function(err, db) {
test.equal(null, err);

// Insert document
db.collection('authcollectiontest').insert({a:1}, function(err, result) {
test.equal(null, err);

// Find the document
db.collection('authcollectiontest').find().toArray(function(err, docs) {
test.equal(null, err);
test.equal(1, docs.length);
test.equal(1, docs[0].a);

db.close();
test.done();
});
});
});
});
});
}

exports.shouldCorrectlyAuthAgainstNormalDbUsingMongoClient = function(test) {
var replSet = new ReplSetServers([
new Server(RS.host, RS.ports[0]), new Server(RS.host, RS.ports[1]), ], {
rs_name: RS.name,
read_secondary: true
});

var dbName = MONGODB;

new Db(dbName, replSet, {w:3}).open(function(err, db_p) {
db_p.addUser("me", "secret", function runWhatever(err, result) {
test.equal(null, err);
test.ok(result != null);
db_p.close();

MongoClient.connect(format("mongodb://me:secret@%s:%s/%s?rs_name=%s&readPreference=secondary&w=3"
, RS.host, RS.ports[0], dbName, RS.name), function(err, db) {
test.equal(null, err);

// Insert document
db.collection('authcollectiontest').insert({a:1}, function(err, result) {
test.equal(null, err);

// Find the document
db.collection('authcollectiontest').find().toArray(function(err, docs) {
test.equal(null, err);
test.equal(1, docs.length);
test.equal(1, docs[0].a);

db.close();
test.done();
});
});
});
});
});
}

/**
* Retrieve the server information for the current
Expand Down
18 changes: 18 additions & 0 deletions test/mongo_client_test.js
Expand Up @@ -135,6 +135,24 @@ exports['Should correctly connect using MongoClient to a single server using con
});
}

/**
* @ignore
*/
exports['Should correctly allow for w:0 overriding on the connect url'] = function(test) {
// Connect using the connection string
MongoClient.connect("mongodb://localhost:27017/integration_tests?w=0", function(err, db) {
test.equal(null, err);

db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(null, result);

db.close();
test.done();
});
});
}

/**
* Retrieve the server information for the current
* instance of the db client
Expand Down

0 comments on commit 0435daf

Please sign in to comment.