diff options
author | Randolph Tan <randolph@10gen.com> | 2020-04-02 16:02:52 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-04-14 22:02:54 +0000 |
commit | eb939801412d100b3d0a09ab9dacfc1c64694395 (patch) | |
tree | 788676aa8a2585440b19e49d5c201f9c124c9f61 /jstests/sharding | |
parent | d0b9ca3aa8015954b9d0fa4f2f14c142c32625f5 (diff) | |
download | mongo-eb939801412d100b3d0a09ab9dacfc1c64694395.tar.gz |
SERVER-35804 Disallow dropping config and admin from mongos
Diffstat (limited to 'jstests/sharding')
7 files changed, 27 insertions, 174 deletions
diff --git a/jstests/sharding/basic_drop_coll.js b/jstests/sharding/basic_drop_coll.js index e457dc5c9fd..dd868506a24 100644 --- a/jstests/sharding/basic_drop_coll.js +++ b/jstests/sharding/basic_drop_coll.js @@ -17,6 +17,11 @@ assert.neq(null, testDB.bar.findOne({x: 1})); assert.commandWorked(testDB.runCommand({drop: 'bar'})); assert.eq(null, testDB.bar.findOne({x: 1})); +assert.commandFailedWithCode(st.s.getDB('admin').runCommand({drop: 'secrets'}), + ErrorCodes.IllegalOperation); +assert.commandFailedWithCode(st.s.getDB('config').runCommand({drop: 'settings'}), + ErrorCodes.IllegalOperation); + // Test dropping a sharded collection. assert.commandWorked(st.s.adminCommand({enableSharding: 'test'})); diff --git a/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js b/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js index 8edcab6b8e8..14a866ab7d7 100644 --- a/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js +++ b/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js @@ -169,7 +169,15 @@ checkCommandConfigSvr( {_configsvrRemoveShard: newShardName}, setupFuncs.addShard, cleanupFuncs.noop); // dropCollection -checkCommandMongos({drop: ns}, setupFuncs.createDatabase, cleanupFuncs.dropDatabase); +// We can't use the checkCommandMongos wrapper because it calls adminCommand and dropping admin +// collections are not allowed in mongos. +checkCommand(st.s.getDB(dbName), + {drop: collName}, + unacceptableWCsForMongos, + acceptableWCsForMongos, + false, + setupFuncs.createDatabase, + cleanupFuncs.dropDatabase); checkCommandConfigSvr( {_configsvrDropCollection: ns}, setupFuncs.createDatabase, cleanupFuncs.dropDatabase); diff --git a/jstests/sharding/drop_collection_in_admin_and_config_dbs.js b/jstests/sharding/drop_collection_in_admin_and_config_dbs.js deleted file mode 100644 index 0015204b0c9..00000000000 --- a/jstests/sharding/drop_collection_in_admin_and_config_dbs.js +++ /dev/null @@ -1,137 +0,0 @@ -/** - * The admin and config databases are special in that their primary shard is always the config - * shard. This test verifies that dropping unsharded collections in the admin or config database - * applies the drop on (only) the config shard. - */ - -(function() { -"use strict"; - -load("jstests/sharding/libs/track_unsharded_collections_helpers.js"); - -// Disable the logical session cache refresh on config servers because it auto-recreates the -// config.system.sessions collection if config.system.sessions is dropped, and this test manually -// controls config.system.sessions. -const st = new ShardingTest({ - shards: 1, - other: {configOptions: {setParameter: {"disableLogicalSessionCacheRefresh": true}}} -}); - -(() => { - jsTest.log( - "Dropping a user collection in the admin database only executes on the config shard"); - - const [collName, ns] = getNewNs("admin"); - - // Insert a document through mongos; it should be inserted on the config shard. - assert.commandWorked(st.s.getDB("admin").getCollection(collName).insert( - {_id: 1}, {writeConcern: {w: "majority"}})); - assert.eq({_id: 1}, st.configRS.getPrimary().getDB("admin").getCollection(collName).findOne()); - assert.eq(null, st.rs0.getPrimary().getDB("admin").getCollection(collName).findOne()); - - // Insert a document directly on the shard. - assert.commandWorked(st.shard0.getDB("admin").getCollection(collName).insert( - {_id: 2}, {writeConcern: {w: "majority"}})); - assert.eq({_id: 2}, st.rs0.getPrimary().getDB("admin").getCollection(collName).findOne()); - - // Drop the collection through mongos; it should be executed only on the config shard. - st.s.getDB("admin").getCollection(collName).drop(); - assert.eq(null, st.configRS.getPrimary().getDB("admin").getCollection(collName).findOne()); - assert.eq({_id: 2}, st.rs0.getPrimary().getDB("admin").getCollection(collName).findOne()); -})(); - -(() => { - jsTest.log( - "Dropping a user collection in the config database only executes on the config shard"); - - const [collName, ns] = getNewNs("config"); - - // Insert a document through mongos; it should be inserted on the config shard. - assert.commandWorked(st.s.getDB("config").getCollection(collName).insert( - {_id: 1}, {writeConcern: {w: "majority"}})); - assert.eq({_id: 1}, st.configRS.getPrimary().getDB("config").getCollection(collName).findOne()); - assert.eq(null, st.rs0.getPrimary().getDB("config").getCollection(collName).findOne()); - - // Insert a document directly on the shard. - assert.commandWorked(st.shard0.getDB("config").getCollection(collName).insert( - {_id: 2}, {writeConcern: {w: "majority"}})); - assert.eq({_id: 2}, st.rs0.getPrimary().getDB("config").getCollection(collName).findOne()); - - // Drop the collection through mongos; it should be executed only on the config shard. - st.s.getDB("config").getCollection(collName).drop(); - assert.eq(null, st.configRS.getPrimary().getDB("config").getCollection(collName).findOne()); - assert.eq({_id: 2}, st.rs0.getPrimary().getDB("config").getCollection(collName).findOne()); -})(); - -(() => { - jsTest.log("Dropping config.system.sessions always only executes on non-config shards"); - - // Wait for the config.system.sessions collection to be created and sharded. - assert.soon(function() { - return st.s.getDB("config").collections.findOne({_id: "config.system.sessions"}) != null; - }); - assert.eq(0, - st.configRS.getPrimary() - .getDB("config") - .getCollectionInfos({name: "system.sessions"}) - .length); - assert.eq(1, st.shard0.getDB("config").getCollectionInfos({name: "system.sessions"}).length); - - // Create a separate config.system.sessions directly on the config shard. - assert.commandWorked(st.configRS.getPrimary().getDB("config").system.sessions.insert( - {_id: 1}, {writeConcern: {w: "majority"}})); - assert.eq(1, - st.configRS.getPrimary() - .getDB("config") - .getCollectionInfos({name: "system.sessions"}) - .length); - - // Drop the config.system.sessions collection through mongos; the drop should be executed only - // on the regular shard. - st.s.getDB("config").system.sessions.drop(); - assert.eq(0, st.shard0.getDB("config").getCollectionInfos({name: "system.sessions"}).length); - assert.eq(1, - st.configRS.getPrimary() - .getDB("config") - .getCollectionInfos({name: "system.sessions"}) - .length); - - // Re-create a config.system.sessions collection directly on the regular shard. - assert.commandWorked(st.shard0.getDB("config").system.sessions.insert( - {_id: 2}, {writeConcern: {w: "majority"}})); - assert.eq(1, st.shard0.getDB("config").getCollectionInfos({name: "system.sessions"}).length); - - // Drop the config.system.sessions collection through mongos; the drop should be executed only - // on the regular shard even though config.system.sessions is marked as dropped: true. - assert.eq(true, - st.s.getDB("config").collections.findOne({_id: "config.system.sessions"}).dropped); - st.s.getDB("config").system.sessions.drop(); - assert.eq(0, st.shard0.getDB("config").getCollectionInfos({name: "system.sessions"}).length); - assert.eq(1, - st.configRS.getPrimary() - .getDB("config") - .getCollectionInfos({name: "system.sessions"}) - .length); - - // Re-create a config.system.sessions collection directly on the regular shard. - assert.commandWorked(st.shard0.getDB("config").system.sessions.insert( - {_id: 2}, {writeConcern: {w: "majority"}})); - assert.eq(1, st.shard0.getDB("config").getCollectionInfos({name: "system.sessions"}).length); - - // Remove the entry for config.system.sessions from config.collections. - assert.commandWorked(st.s.getDB("config").collections.remove({_id: "config.system.sessions"}, - {writeConcern: {w: "majority"}})); - - // Drop the config.system.sessions collection through mongos; the drop should be executed only - // on the regular shard even though config.system.sessions is not in config.collections. - st.s.getDB("config").system.sessions.drop(); - assert.eq(0, st.shard0.getDB("config").getCollectionInfos({name: "system.sessions"}).length); - assert.eq(1, - st.configRS.getPrimary() - .getDB("config") - .getCollectionInfos({name: "system.sessions"}) - .length); -})(); - -st.stop(); -})(); diff --git a/jstests/sharding/drop_configdb.js b/jstests/sharding/drop_configdb.js index 180741530b9..5e2b704cf6d 100644 --- a/jstests/sharding/drop_configdb.js +++ b/jstests/sharding/drop_configdb.js @@ -3,17 +3,9 @@ (function() { "use strict"; -var getConfigsvrToWriteTo = function(st) { - if (st.configRS) { - return st.configRS.getPrimary(); - } else { - return st._configServers[0]; - } -}; - var st = new ShardingTest({shards: 2}); var mongos = st.s; -var config = getConfigsvrToWriteTo(st).getDB('config'); +var config = st.configRS.getPrimary().getDB('config'); // Try to drop config db via configsvr @@ -26,10 +18,8 @@ assert.eq("Cannot drop 'config' database if mongod started with --configsvr", var config = mongos.getDB("config"); print("1: Try to drop config database via mongos"); -assert.eq(0, config.dropDatabase().ok); - -// 20 = ErrorCodes::IllegalOperation -assert.eq(20, config.dropDatabase().code); +assert.commandFailedWithCode(config.dropDatabase(), ErrorCodes.IllegalOperation); +assert.commandFailedWithCode(mongos.getDB("admin").dropDatabase(), ErrorCodes.IllegalOperation); st.stop(); -}());
\ No newline at end of file +}()); diff --git a/jstests/sharding/query_config.js b/jstests/sharding/query_config.js index 889999b6173..582d9899940 100644 --- a/jstests/sharding/query_config.js +++ b/jstests/sharding/query_config.js @@ -84,7 +84,9 @@ var testListConfigCollections = function(st) { var cursor; var cursorArray; - dropCollectionIfExists(userAddedColl); + // Dropping collections under config is not allowed via mongos. + let configPrimary = st.configRS.getPrimary(); + dropCollectionIfExists(configPrimary.getDB("config").userAddedColl); configDB.createCollection(userAddedColl.getName()); configCollList.push(userAddedColl.getName()); @@ -103,8 +105,6 @@ var testListConfigCollections = function(st) { assert(cursor.hasNext()); assert.eq(cursor.objsLeftInBatch(), 1); assert(cursorArray.indexOf(cursor.next().name) > -1); - - assert(userAddedColl.drop()); }; /** @@ -256,8 +256,9 @@ var queryUserCreated = function(database) { var cursorArray; var result; - // Setup. - dropCollectionIfExists(userColl); + // Dropping collections under config is not allowed via mongos. + let configPrimary = st.configRS.getPrimary(); + dropCollectionIfExists(configPrimary.getDB(database.getName()).userColl); for (var i = 0; i < userCollData.length; i++) { assert.commandWorked(userColl.insert(userCollData[i])); } @@ -328,8 +329,6 @@ var queryUserCreated = function(database) { assert.eq( sortArrayById(result.results), [{_id: 1, value: {count: 2}}, {_id: 2, value: {count: 3}}, {_id: 3, value: {count: 2}}]); - - assert(userColl.drop()); }; var st = new ShardingTest({shards: 2, mongos: 1}); diff --git a/jstests/sharding/range_deleter_interacts_correctly_with_refine_shard_key.js b/jstests/sharding/range_deleter_interacts_correctly_with_refine_shard_key.js index 74241d79631..bba1f712d2f 100644 --- a/jstests/sharding/range_deleter_interacts_correctly_with_refine_shard_key.js +++ b/jstests/sharding/range_deleter_interacts_correctly_with_refine_shard_key.js @@ -355,10 +355,10 @@ function test(st, description, testBody) { hangDonorAtEndOfMigration.off(); // TODO (SERVER-47003): There will be a left-over entry in config.migrations after the - // previous moveChunk fails with MaxTimeMSExpired, so we drop the collection. Otherwise - // future migrations would receive a DuplicateKeyError when trying to update + // previous moveChunk fails with MaxTimeMSExpired, so we clear the collection. + // Otherwise future migrations would receive a DuplicateKeyError when trying to update // config.migrations. - st.config.getSiblingDB('config').migrations.drop(); + st.config.getSiblingDB('config').migrations.remove({}, false /* justOne */); }); test(st, diff --git a/jstests/sharding/sessions_collection_auto_healing.js b/jstests/sharding/sessions_collection_auto_healing.js index 6efb9cf2274..6e80c992776 100644 --- a/jstests/sharding/sessions_collection_auto_healing.js +++ b/jstests/sharding/sessions_collection_auto_healing.js @@ -146,18 +146,6 @@ var shardConfig = shard.getDB("config"); validateSessionsCollection(shard, true, false); } -// Test that if we drop the collection, it will be recreated only by the config server. -{ - assertDropCollection(mongosConfig, "system.sessions"); - validateSessionsCollection(shard, false, false); - - assert.commandWorked(shardAdmin.runCommand({refreshLogicalSessionCacheNow: 1})); - validateSessionsCollection(shard, false, false); - - assert.commandWorked(configAdmin.runCommand({refreshLogicalSessionCacheNow: 1})); - validateSessionsCollection(shard, true, true); -} - st.stop(); rs.stopSet(); })(); |