Permalink
Browse files

Added kerberos test runner, url parser support and tests

  • Loading branch information...
1 parent 37d4b73 commit ad73c9337df3f4a43fca3143943bbe31f43ea229 @christkv christkv committed Mar 21, 2013
View
@@ -48,9 +48,9 @@
# built documents.
#
# The short X.Y version.
-version = '1.2'
+version = '1.3'
# The full version, including alpha/beta/rc tags.
-release = '1.2.14'
+release = '1.3.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
@@ -174,6 +174,21 @@ exports.parse = function(url, options) {
break;
case 'authSource':
dbOptions.authSource = value;
+ break;
+ case 'authMechanism':
+ if(value == 'GSSAPI') {
+ // If no password provided decode only the principal
+ if(object.auth == null) {
+ var urlDecodeAuthPart = decodeURIComponent(authPart);
+ if(urlDecodeAuthPart.indexOf("@") == -1) throw new Error("GSSAPI requires a provided principal");
+ object.auth = {user: urlDecodeAuthPart, password: null};
+ } else {
+ object.auth.user = decodeURIComponent(object.auth.user);
+ }
+ }
+
+ dbOptions.authMechanism = value;
+ break;
case 'wtimeoutMS':
dbOptions.wtimeoutMS = parseInt(value, 10);
break;
View
@@ -1858,205 +1858,22 @@ Db.DEFAULT_URL = 'mongodb://localhost:27017/default';
* @api public
*/
Db.connect = function(url, options, callback) {
- var args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] == 'function' ? args.pop() : null;
- options = args.length ? args.shift() : null;
- options = options || {};
- var serverOptions = options.server || {};
- var mongosOptions = options.mongos || {};
- var replSetServersOptions = options.replSet || options.replSetServers || {};
- var dbOptions = options.db || {};
-
- // If callback is null throw an exception
- if(callback == null) throw new Error("no callback function provided");
-
- // 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];
- }
-
- // Merge in any options for server in options object
- if(serverOptions) {
- for(var name in serverOptions) object.server_options[name] = serverOptions[name];
- }
-
- // Merge in any replicaset server options
- if(replSetServersOptions) {
- for(var name in replSetServersOptions) object.rs_options[name] = replSetServersOptions[name];
- }
-
- // Merge in any replicaset server options
- if(mongosOptions) {
- for(var name in mongosOptions) object.mongos_options[name] = mongosOptions[name];
- }
-
- // We need to ensure that the list of servers are only either direct members or mongos
- // they cannot be a mix of monogs and mongod's
- var totalNumberOfServers = object.servers.length;
- var totalNumberOfMongosServers = 0;
- var totalNumberOfMongodServers = 0;
- var serverConfig = null;
- var errorServers = {};
-
- // Failure modes
- if(object.servers.length == 0) throw new Error("connection string must contain at least one seed host");
-
- // If we have no db setting for the native parser try to set the c++ one first
- object.db_options.native_parser = _setNativeParser(object.db_options);
- // If no auto_reconnect is set, set it to true as default for single servers
- if(typeof object.server_options.auto_reconnect != 'boolean') {
- object.server_options.auto_reconnect = true;
- }
-
- // If we have more than a server, it could be replicaset or mongos list
- // need to verify that it's one or the other and fail if it's a mix
- // Connect to all servers and run ismaster
- for(var i = 0; i < object.servers.length; i++) {
- // Set up socket options
- var _server_options = {poolSize:1, socketOptions:{connectTimeoutMS:1000}, auto_reconnect:false};
-
- // Ensure we have ssl setup for the servers
- if(object.rs_options.ssl) {
- _server_options.ssl = object.rs_options.ssl;
- _server_options.sslValidate = object.rs_options.sslValidate;
- _server_options.sslCA = object.rs_options.sslCA;
- _server_options.sslCert = object.rs_options.sslCert;
- _server_options.sslKey = object.rs_options.sslKey;
- _server_options.sslPass = object.rs_options.sslPass;
- } else if(object.server_options.ssl) {
- _server_options.ssl = object.server_options.ssl;
- _server_options.sslValidate = object.server_options.sslValidate;
- _server_options.sslCA = object.server_options.sslCA;
- _server_options.sslCert = object.server_options.sslCert;
- _server_options.sslKey = object.server_options.sslKey;
- _server_options.sslPass = object.server_options.sslPass;
- }
-
- // Set up the Server object
- var _server = object.servers[i].domain_socket
- ? new Server(object.servers[i].domain_socket, _server_options)
- : new Server(object.servers[i].host, object.servers[i].port, _server_options);
-
- var connectFunction = function(__server) {
- // Attempt connect
- new Db(object.dbName, __server, {safe:false, native_parser:false}).open(function(err, db) {
- // Update number of servers
- totalNumberOfServers = totalNumberOfServers - 1;
- // If no error do the correct checks
- if(!err) {
- // Close the connection
- db.close(true);
- var isMasterDoc = db.serverConfig.isMasterDoc;
- // Check what type of server we have
- if(isMasterDoc.setName) totalNumberOfMongodServers++;
- if(isMasterDoc.msg && isMasterDoc.msg == "isdbgrid") totalNumberOfMongosServers++;
- } else {
- errorServers[__server.host + ":" + __server.port] = __server;
- }
-
- if(totalNumberOfServers == 0) {
- // If we have a mix of mongod and mongos, throw an error
- if(totalNumberOfMongosServers > 0 && totalNumberOfMongodServers > 0)
- return callback(new Error("cannot combine a list of replicaset seeds and mongos seeds"));
-
- if(totalNumberOfMongodServers == 0 && object.servers.length == 1) {
- var obj = object.servers[0];
- serverConfig = obj.domain_socket ?
- new Server(obj.domain_socket, object.server_options)
- : new Server(obj.host, obj.port, object.server_options);
- } else if(totalNumberOfMongodServers > 0 || totalNumberOfMongosServers > 0) {
- var finalServers = object.servers
- .filter(function(serverObj) {
- return errorServers[serverObj.host + ":" + serverObj.port] == null;
- })
- .map(function(serverObj) {
- return new Server(serverObj.host, serverObj.port, object.server_options);
- });
- // Clean out any error servers
- errorServers = {};
- // Set up the final configuration
- if(totalNumberOfMongodServers > 0) {
- serverConfig = new ReplSet(finalServers, object.rs_options);
- } else {
- serverConfig = new Mongos(finalServers, object.mongos_options);
- }
- }
-
- if(serverConfig == null) return callback(new Error("Could not locate any valid servers in initial seed list"));
- // Set up all options etc and connect to the database
- _finishConnecting(serverConfig, object, options, callback)
- }
- });
- }
-
- // Wrap the context of the call
- connectFunction(_server);
- }
-}
-
-var _setNativeParser = function(db_options) {
- if(typeof db_options.native_parser == 'boolean') return db_options.native_parser;
-
- try {
- require('bson').BSONNative.BSON;
- return true;
- } catch(err) {
- return false;
- }
-}
-
-var _finishConnecting = function(serverConfig, object, options, callback) {
- // Safe settings
- var safe = {};
- // Build the safe parameter if needed
- if(object.db_options.journal) safe.j = object.db_options.journal;
- if(object.db_options.w) safe.w = object.db_options.w;
- if(object.db_options.fsync) safe.fsync = object.db_options.fsync;
- if(object.db_options.wtimeoutMS) safe.wtimeout = object.db_options.wtimeoutMS;
-
- // If we have a read Preference set
- if(object.db_options.read_preference) {
- var readPreference = new ReadPreference(object.db_options.read_preference);
- // If we have the tags set up
- if(object.db_options.read_preference_tags)
- readPreference = new ReadPreference(object.db_options.read_preference, object.db_options.read_preference_tags);
- // Add the read preference
- object.db_options.readPreference = readPreference;
+ // Ensure correct mapping of the callback
+ if(typeof options == 'function') {
+ callback = options;
+ options = {};
}
- // No safe mode if no keys
- if(Object.keys(safe).length == 0) safe = false;
-
- // Add the safe object
- object.db_options.safe = safe;
-
- // Set up the db options
- var db = new Db(object.dbName, serverConfig, object.db_options);
+ // Ensure same behavior as previous version w:0
+ if(url.indexOf("safe") == -1
+ && url.indexOf("w") == -1
+ && url.indexOf("journal") == -1 && url.indexOf("j") == -1
+ && url.indexOf("fsync") == -1) options.w = 0;
- // Open the db
- db.open(function(err, db){
- if(err == null && object.auth){
- // What db to authenticate against
- var authentication_db = db;
- if(object.db_options && object.db_options.authSource) {
- authentication_db = db.db(object.db_options.authSource);
- }
-
- // Authenticate
- authentication_db.authenticate(object.auth.user, object.auth.password, function(err, success){
- if(success){
- callback(null, db);
- } else {
- if(db) db.close();
- callback(err ? err : new Error('Could not authenticate user ' + auth[0]), null);
- }
- });
- } else {
- callback(err, db);
- }
- });
+ // Avoid circular require problem
+ var MongoClient = require('./mongo_client.js').MongoClient;
+ // Attempt to connect
+ MongoClient.connect.call(MongoClient, url, options, callback);
}
/**
Oops, something went wrong.

0 comments on commit ad73c93

Please sign in to comment.