diff options
3 files changed, 107 insertions, 1 deletions
diff --git a/src/mongo/db/s/SConscript b/src/mongo/db/s/SConscript index 241c5fcc69d..32e5028fe3a 100644 --- a/src/mongo/db/s/SConscript +++ b/src/mongo/db/s/SConscript @@ -577,6 +577,7 @@ env.CppUnitTest( 'config/sharding_catalog_manager_clear_jumbo_flag_test.cpp', 'config/sharding_catalog_manager_commit_chunk_migration_test.cpp', 'config/sharding_catalog_manager_config_initialization_test.cpp', + 'config/sharding_catalog_manager_database_operations_test.cpp', 'config/sharding_catalog_manager_drop_coll_test.cpp', 'config/sharding_catalog_manager_ensure_chunk_version_is_greater_than_test.cpp', 'config/sharding_catalog_manager_merge_chunks_test.cpp', diff --git a/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp b/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp index c6077113893..d9cabe9e068 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp @@ -94,9 +94,14 @@ DatabaseType ShardingCatalogManager::createDatabase(OperationContext* opCtx, dbName.toString(), ShardId::kConfigServerId, true, DatabaseVersion::makeFixed()); } + // It is not allowed to create the 'admin' or 'local' databases, including any alternative + // casing. It is allowed to create the 'config' database (handled by the early return above), + // but only with that exact casing. uassert(ErrorCodes::InvalidOptions, str::stream() << "Cannot manually create or shard database '" << dbName << "'", - dbName != NamespaceString::kAdminDb && dbName != NamespaceString::kLocalDb); + !dbName.equalCaseInsensitive(NamespaceString::kAdminDb) && + !dbName.equalCaseInsensitive(NamespaceString::kLocalDb) && + !dbName.equalCaseInsensitive(NamespaceString::kConfigDb)); uassert(ErrorCodes::InvalidNamespace, str::stream() << "Invalid db name specified: " << dbName, diff --git a/src/mongo/db/s/config/sharding_catalog_manager_database_operations_test.cpp b/src/mongo/db/s/config/sharding_catalog_manager_database_operations_test.cpp new file mode 100644 index 00000000000..e1044f53e85 --- /dev/null +++ b/src/mongo/db/s/config/sharding_catalog_manager_database_operations_test.cpp @@ -0,0 +1,100 @@ +/** + * Copyright (C) 2023-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/db/s/config/config_server_test_fixture.h" +#include "mongo/db/s/config/sharding_catalog_manager.h" + +namespace mongo { +namespace { + +class ShardingCatalogManagerDatabaseOperationsTest : public ConfigServerTestFixture { +public: + void setUp() override { + ConfigServerTestFixture::setUp(); + _opCtx = operationContext(); + } + +protected: + OperationContext* _opCtx; +}; + +TEST_F(ShardingCatalogManagerDatabaseOperationsTest, CreateDatabaseAdminFails) { + ASSERT_THROWS_CODE( + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "admin"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); + + // Alternative capitalizations are also invalid + ASSERT_THROWS_CODE( + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "Admin"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); + + ASSERT_THROWS_CODE( + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "aDmIn"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); +} + +TEST_F(ShardingCatalogManagerDatabaseOperationsTest, CreateDatabaseLocalFails) { + ASSERT_THROWS_CODE( + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "local"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); + + // Alternative capitalizations are also invalid + ASSERT_THROWS_CODE( + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "Local"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); + + ASSERT_THROWS_CODE( + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "lOcAl"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); +} + +TEST_F(ShardingCatalogManagerDatabaseOperationsTest, CreateDatabaseConfig) { + // It is allowed to create the "config" database. + ShardingCatalogManager::get(_opCtx)->createDatabase(_opCtx, "config"_sd, boost::none, false); + + // But alternative capitalizations are invalid. + ASSERT_THROWS_CODE(ShardingCatalogManager::get(_opCtx)->createDatabase( + _opCtx, "Config"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); + + ASSERT_THROWS_CODE(ShardingCatalogManager::get(_opCtx)->createDatabase( + _opCtx, "cOnFiG"_sd, boost::none, false), + DBException, + ErrorCodes::InvalidOptions); +} + +} // namespace +} // namespace mongo |