0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

don't allow shards on localhost unless allowLocal : true SHARDING-47

This commit is contained in:
Eliot Horowitz 2009-10-12 13:27:04 -04:00
parent 25ab0cc339
commit c628c7b93f
3 changed files with 22 additions and 5 deletions

View File

@ -14,6 +14,7 @@ assert( 3 , test1.count() );
assert( ! s.admin.runCommand( { addshard: "sdd$%" } ).ok , "bad hostname" );
assert( ! s.admin.runCommand( { addshard: "127.0.0.1:43415" } ).ok , "host not up" );
assert( ! s.admin.runCommand( { addshard: "127.0.0.1:43415" , allowLocal : true } ).ok , "host not up" );
s.stop();

View File

@ -471,11 +471,25 @@ namespace mongo {
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
ScopedDbConnection conn( configServer.getPrimary() );
string host = cmdObj["addshard"].valuestrsafe();
if ( host == "localhost" || host.find( "localhost:" ) == 0 ||
host == "127.0.0.1" || host.find( "127.0.0.1:" ) == 0 ){
if ( cmdObj["allowLocal"].type() != Bool ||
! cmdObj["allowLocal"].boolean() ){
errmsg =
"can't use localhost as a shard since all shards need to communicate. "
"allowLocal to override for testing";
return false;
}
}
BSONObj shard;
{
BSONObjBuilder b;
b.append( "host" , cmdObj["addshard"].valuestrsafe() );
b.append( "host" , host );
if ( cmdObj["maxSize"].isNumber() )
b.append( cmdObj["maxSize"] );
shard = b.obj();
@ -490,18 +504,20 @@ namespace mongo {
}
try {
ScopedDbConnection newShardConn( shard["host"].valuestrsafe() );
ScopedDbConnection newShardConn( host );
newShardConn->getLastError();
newShardConn.done();
}
catch ( DBException& e ){
errmsg = "couldn't connect to new shard";
result.append( "host" , shard["host"].valuestrsafe() );
result.append( "host" , host );
result.append( "exception" , e.what() );
result.append( "ok" , 0 );
conn.done();
return false;
}
conn->insert( "config.shards" , shard );
result.append( "ok", 1 );

View File

@ -138,7 +138,7 @@ ShardingTest = function( testName , numServers , verboseLevel , numMongos ){
this._serverNames.forEach(
function(z){
admin.runCommand( { addshard : z } );
admin.runCommand( { addshard : z , allowLocal : true } );
}
);
}