Skip to content

Commit 37e19cb

Browse files
committed
SERVER-8502 Make sure authMechanism is actually used when running auth passthrough tests
1 parent 3521eaa commit 37e19cb

File tree

5 files changed

+59
-50
lines changed

5 files changed

+59
-50
lines changed

buildscripts/smoke.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ def runTest(test):
464464
'TestData.noJournalPrealloc = ' + ternary( no_preallocj ) + ";" + \
465465
'TestData.auth = ' + ternary( auth ) + ";" + \
466466
'TestData.keyFile = ' + ternary( keyFile , '"' + str(keyFile) + '"' , 'null' ) + ";" + \
467-
'TestData.keyFileData = ' + ternary( keyFile , '"' + str(keyFileData) + '"' , 'null' ) + ";"
467+
'TestData.keyFileData = ' + ternary( keyFile , '"' + str(keyFileData) + '"' , 'null' ) + ";" + \
468+
'TestData.authMechanism = ' + ternary( authMechanism, '"' + str(authMechanism) + '"', 'null') + ";"
468469
if os.sys.platform == "win32":
469470
# double quotes in the evalString on windows; this
470471
# prevents the backslashes from being removed when

src/mongo/shell/dbshell.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -855,42 +855,42 @@ int _main( int argc, char* argv[], char **envp ) {
855855

856856
if ( params.count( "password" ) && password.empty() )
857857
password = mongo::askPassword();
858+
}
858859

859-
// Construct the authentication-related code to execute on shell startup.
860-
//
861-
// This constructs and immediately executes an anonymous function, to avoid
862-
// the shell's default behavior of printing statement results to the console.
863-
//
864-
// It constructs a statement of the following form:
865-
//
866-
// (function() {
867-
// // Set default authentication mechanism and, maybe, authenticate.
868-
// }())
869-
stringstream authStringStream;
870-
authStringStream << "(function() { " << endl;
871-
if ( !authenticationMechanism.empty() ) {
872-
authStringStream << "DB.prototype._defaultAuthenticationMechanism = \"" <<
873-
authenticationMechanism << "\";" << endl;
874-
}
860+
// Construct the authentication-related code to execute on shell startup.
861+
//
862+
// This constructs and immediately executes an anonymous function, to avoid
863+
// the shell's default behavior of printing statement results to the console.
864+
//
865+
// It constructs a statement of the following form:
866+
//
867+
// (function() {
868+
// // Set default authentication mechanism and, maybe, authenticate.
869+
// }())
870+
stringstream authStringStream;
871+
authStringStream << "(function() { " << endl;
872+
if ( !authenticationMechanism.empty() ) {
873+
authStringStream << "DB.prototype._defaultAuthenticationMechanism = \"" <<
874+
authenticationMechanism << "\";" << endl;
875+
}
875876

876-
if ( username.size() ) {
877-
authStringStream << "var username = \"" << username << "\";" << endl;
878-
authStringStream << "var password = \"" << password << "\";" << endl;
879-
if (authenticationDatabase.empty()) {
880-
authStringStream << "var authDb = db;" << endl;
881-
}
882-
else {
883-
authStringStream << "var authDb = db.getSiblingDB(\"" << authenticationDatabase <<
884-
"\");" << endl;
885-
}
886-
authStringStream << "authDb._authOrThrow({ " <<
887-
saslCommandPrincipalFieldName << ": username, " <<
888-
saslCommandPasswordFieldName << ": password });" << endl;
877+
if (!nodb && username.size()) {
878+
authStringStream << "var username = \"" << username << "\";" << endl;
879+
authStringStream << "var password = \"" << password << "\";" << endl;
880+
if (authenticationDatabase.empty()) {
881+
authStringStream << "var authDb = db;" << endl;
889882
}
890-
authStringStream << "}())";
891-
892-
mongo::shell_utils::_dbAuth = authStringStream.str();
883+
else {
884+
authStringStream << "var authDb = db.getSiblingDB(\"" << authenticationDatabase <<
885+
"\");" << endl;
886+
}
887+
authStringStream << "authDb._authOrThrow({ " <<
888+
saslCommandPrincipalFieldName << ": username, " <<
889+
saslCommandPasswordFieldName << ": password });" << endl;
893890
}
891+
authStringStream << "}())";
892+
mongo::shell_utils::_dbAuth = authStringStream.str();
893+
894894

895895
mongo::ScriptEngine::setConnectCallback( mongo::shell_utils::onConnect );
896896
mongo::ScriptEngine::setup();

src/mongo/shell/servers.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -728,21 +728,33 @@ startMongos = function(args){
728728
return MongoRunner.runMongos(args);
729729
}
730730

731+
/**
732+
* Returns a new argArray with any test-specific arguments added.
733+
*/
734+
getTestArgs = function(argArray) {
735+
var programName = argArray[0];
736+
if (programName.endsWith('mongod') || programName.endsWith('mongos')) {
737+
if (jsTest.options().enableTestCommands) {
738+
argArray.push.apply(argArray, ['--setParameter', "enableTestCommands=1"]);
739+
}
740+
if (jsTest.options().authMechanism) {
741+
argArray.push.apply(argArray,
742+
['--setParameter',
743+
"authenticationMechanisms=" + jsTest.options().authMechanism]);
744+
}
745+
}
746+
return argArray;
747+
};
748+
731749
/**
732750
* Start a mongo process with a particular argument array. If we aren't waiting for connect,
733751
* return null.
734752
*/
735753
MongoRunner.startWithArgs = function(argArray, waitForConnect) {
754+
// TODO: Make there only be one codepath for starting mongo processes
736755

756+
argArray = getTestArgs(argArray);
737757
var port = _parsePort.apply(null, argArray);
738-
739-
// Enable test commands.
740-
// TODO: Make there only be one codepath for starting mongo processes
741-
var programName = argArray[0];
742-
if (jsTest.options().enableTestCommands && (programName.endsWith('mongod') || programName.endsWith('mongos'))) {
743-
argArray.push.apply(argArray, ['--setParameter', 'enableTestCommands=1']);
744-
}
745-
746758
var pid = _startMongoProgram.apply(null, argArray);
747759

748760
var conn = null;
@@ -782,11 +794,7 @@ startMongoProgram = function(){
782794
// TODO: Make this work better with multi-version testing so that we can support
783795
// enabling this on 2.4 when testing 2.6
784796
var args = argumentsToArray( arguments );
785-
var programName = args[0];
786-
if (jsTest.options().enableTestCommands && (programName.endsWith('mongod') || programName.endsWith('mongos'))) {
787-
args.push.apply(args, ['--setParameter', 'enableTestCommands=1']);
788-
}
789-
797+
args = getTestArgs(args);
790798
var pid = _startMongoProgram.apply( null, args );
791799

792800
var m;

src/mongo/shell/shell_utils.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,9 @@ namespace mongo {
156156

157157
if ( !_dbConnect.empty() ) {
158158
uassert( 12513, "connect failed", scope.exec( _dbConnect , "(connect)" , false , true , false ) );
159-
if ( !_dbAuth.empty() ) {
160-
installGlobalUtils( scope );
161-
uassert( 12514, "login failed", scope.exec( _dbAuth , "(auth)" , true , true , false ) );
162-
}
159+
}
160+
if ( !_dbAuth.empty() ) {
161+
uassert( 12514, "login failed", scope.exec( _dbAuth , "(auth)" , true , true , false ) );
163162
}
164163
}
165164

src/mongo/shell/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ jsTestOptions = function(){
371371
keyFile : TestData.keyFile,
372372
authUser : "__system",
373373
authPassword : TestData.keyFileData,
374+
authMechanism : TestData.authMechanism,
374375
adminUser : TestData.adminUser || "admin",
375376
adminPassword : TestData.adminPassword || "password" });
376377
}

0 commit comments

Comments
 (0)