summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-09-10 10:36:11 -0400
committerEliot Horowitz <eliot@10gen.com>2009-09-10 10:36:11 -0400
commitbbc07f85eca951f216ffba82cd15fa08f1d9e6e4 (patch)
tree7ae13ad3f31a3d975318a25523154d2fd74a3c2d
parentb9d61d202c7acebac58ce55c1da89f8e67dd29a7 (diff)
downloadmongo-bbc07f85eca951f216ffba82cd15fa08f1d9e6e4.tar.gz
don't allow db.eval on sharded collections SHARDING-27
-rw-r--r--client/clientOnly.cpp4
-rw-r--r--jstests/sharding/features1.js14
-rw-r--r--s/d_logic.cpp16
-rw-r--r--s/d_logic.h5
-rw-r--r--s/server.cpp4
-rw-r--r--scripting/sm_db.cpp18
6 files changed, 61 insertions, 0 deletions
diff --git a/client/clientOnly.cpp b/client/clientOnly.cpp
index db833e48eda..fbdad64143f 100644
--- a/client/clientOnly.cpp
+++ b/client/clientOnly.cpp
@@ -26,4 +26,8 @@ namespace mongo {
string getDbContext() {
return "in client only mode";
}
+
+ bool haveLocalShardingInfo( const string& ns ){
+ return false;
+ }
}
diff --git a/jstests/sharding/features1.js b/jstests/sharding/features1.js
index ee09106fec1..d2f692a7ddc 100644
--- a/jstests/sharding/features1.js
+++ b/jstests/sharding/features1.js
@@ -61,6 +61,20 @@ s.sync();
printjson( db.system.indexes.find( { ns : "test.foo3" } ).toArray() );
assert( ! s.admin.runCommand( { shardcollection : "test.foo3" , key : { num : 1 } } ).ok , "shard with unique index" );
+// ----- eval -----
+
+db.foo2.save( { num : 5 , a : 7 } );
+db.foo3.save( { num : 5 , a : 8 } );
+
+assert.eq( 1 , db.foo3.count() , "eval pre1" );
+assert.eq( 1 , db.foo2.count() , "eval pre2" );
+
+assert.eq( 8 , db.eval( function(){ return db.foo3.findOne().a; } ), "eval 1 " );
+assert.throws( function(){ db.eval( function(){ return db.foo2.findOne().a; } ) } , "eval 2" )
+
+assert.eq( 1 , db.eval( function(){ return db.foo3.count(); } ), "eval 3 " );
+assert.throws( function(){ db.eval( function(){ return db.foo2.count(); } ) } , "eval 4" )
+
// ---- unique shard key ----
diff --git a/s/d_logic.cpp b/s/d_logic.cpp
index 15c65ad154b..902df4f479c 100644
--- a/s/d_logic.cpp
+++ b/s/d_logic.cpp
@@ -390,6 +390,22 @@ namespace mongo {
} moveShardFinishCmd;
+ bool haveLocalShardingInfo( const string& ns ){
+ if ( shardConfigServer.empty() )
+ return false;
+
+
+ unsigned long long version = myVersions[ns];
+ if ( version == 0 )
+ return false;
+
+ NSVersions * versions = clientShardVersions.get();
+ if ( ! versions )
+ return false;
+
+ return true;
+ }
+
/**
* @ return true if not in sharded mode
or if version for this client is ok
diff --git a/s/d_logic.h b/s/d_logic.h
index f3460f3f6ce..3e483c45396 100644
--- a/s/d_logic.h
+++ b/s/d_logic.h
@@ -7,6 +7,11 @@
namespace mongo {
/**
+ * @return true if we have any shard info for the ns
+ */
+ bool haveLocalShardingInfo( const string& ns );
+
+ /**
* @return true if the current threads shard version is ok, or not in sharded version
*/
bool shardVersionOk( const string& ns , string& errmsg );
diff --git a/s/server.cpp b/s/server.cpp
index f878c1372c8..d74e18aaa5e 100644
--- a/s/server.cpp
+++ b/s/server.cpp
@@ -44,6 +44,10 @@ namespace mongo {
return "?";
}
+ bool haveLocalShardingInfo( const string& ns ){
+ assert( 0 );
+ }
+
void usage( char * argv[] ){
out() << argv[0] << " usage:\n\n";
out() << " -v+ verbose\n";
diff --git a/scripting/sm_db.cpp b/scripting/sm_db.cpp
index 1b1c8bcf0b5..a9b3dc89862 100644
--- a/scripting/sm_db.cpp
+++ b/scripting/sm_db.cpp
@@ -4,6 +4,8 @@
namespace mongo {
+ bool haveLocalShardingInfo( const string& ns );
+
// ------------ some defs needed ---------------
JSObject * doCreateCollection( JSContext * cx , JSObject * db , const string& shortName );
@@ -298,6 +300,13 @@ namespace mongo {
assert( JS_SetProperty( cx , obj , "_db" , &(argv[1]) ) );
assert( JS_SetProperty( cx , obj , "_shortName" , &(argv[2]) ) );
assert( JS_SetProperty( cx , obj , "_fullName" , &(argv[3]) ) );
+
+ Convertor c(cx);
+ if ( haveLocalShardingInfo( c.toString( argv[3] ) ) ){
+ JS_ReportError( cx , "can't use sharded collection from db.eval" );
+ return JS_FALSE;
+ }
+
return JS_TRUE;
}
@@ -327,6 +336,8 @@ namespace mongo {
return JS_TRUE;
JSObject * coll = doCreateCollection( cx , JSVAL_TO_OBJECT( db ) , name );
+ if ( ! coll )
+ return JS_FALSE;
c.setProperty( obj , collname.c_str() , OBJECT_TO_JSVAL( coll ) );
*objp = obj;
return JS_TRUE;
@@ -355,6 +366,11 @@ namespace mongo {
name += "." + shortName;
c.setProperty( coll , "_fullName" , c.toval( name.c_str() ) );
+ if ( haveLocalShardingInfo( name ) ){
+ JS_ReportError( cx , "can't use sharded collection from db.eval" );
+ return 0;
+ }
+
return coll;
}
@@ -388,6 +404,8 @@ namespace mongo {
return JS_TRUE;
JSObject * coll = doCreateCollection( cx , obj , collname );
+ if ( ! coll )
+ return JS_FALSE;
c.setProperty( obj , collname.c_str() , OBJECT_TO_JSVAL( coll ) );
*objp = obj;