diff options
author | Jordi Serra Torrens <jordi.serra-torrens@mongodb.com> | 2023-01-20 12:31:34 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-01-20 17:19:01 +0000 |
commit | 63fa196dd22e3e075ebfc250acf2a0c62b73997a (patch) | |
tree | 2deaa3b1304ac1c10cda273e5bda9bed8dc516fa | |
parent | b5a637d8376faec06007b5748aed3f7dd5c55eae (diff) | |
download | mongo-63fa196dd22e3e075ebfc250acf2a0c62b73997a.tar.gz |
SERVER-72535 Disallow creating the 'admin', 'local', and 'config' databases with alternative casings on sharded clusters
(cherry picked from commit ab4d1bf2ff349a82c67a09aba9452ed54d673069)
-rw-r--r-- | src/mongo/db/s/config/sharding_catalog_manager_create_database_test.cpp | 54 | ||||
-rw-r--r-- | src/mongo/db/s/config/sharding_catalog_manager_database_operations.cpp | 14 |
2 files changed, 62 insertions, 6 deletions
diff --git a/src/mongo/db/s/config/sharding_catalog_manager_create_database_test.cpp b/src/mongo/db/s/config/sharding_catalog_manager_create_database_test.cpp index ff81beb3f14..e66f1314795 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_create_database_test.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_create_database_test.cpp @@ -245,5 +245,59 @@ TEST_F(CreateDatabaseTest, createDatabaseNoShards) { ErrorCodes::ShardNotFound); } +TEST_F(CreateDatabaseTest, CreateDatabaseAdminFails) { + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "admin"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); + + // Alternative capitalizations are also invalid + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "Admin"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); + + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "aDmIn"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); +} + +TEST_F(CreateDatabaseTest, CreateDatabaseLocalFails) { + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "local"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); + + // Alternative capitalizations are also invalid + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "Local"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); + + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "lOcAl"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); +} + +TEST_F(CreateDatabaseTest, CreateDatabaseConfigFails) { + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "config"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); + + // Alternative capitalizations are also invalid + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "Config"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); + + ASSERT_THROWS_CODE(ShardingCatalogManager::get(operationContext()) + ->createDatabase(operationContext(), "cOnFiG"_sd, ShardId()), + DBException, + ErrorCodes::InvalidOptions); +} + } // namespace } // namespace mongo 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 ce328247bd9..026d47cf45e 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 @@ -63,12 +63,14 @@ DatabaseType ShardingCatalogManager::createDatabase(OperationContext* opCtx, ShardId primaryShard) { invariant(nsIsDbOnly(dbName)); - // The admin and config databases should never be explicitly created. They "just exist", - // i.e. getDatabase will always return an entry for them. - if (dbName == "admin" || dbName == "config") { - uasserted(ErrorCodes::InvalidOptions, - str::stream() << "cannot manually create database '" << dbName << "'"); - } + // The admin, local and config databases should never be explicitly created, in any casing. They + // "just exist", i.e. getDatabase will always return an entry for them. + uassert(ErrorCodes::InvalidOptions, + str::stream() << "cannot manually create database '" << dbName << "'", + !dbName.equalCaseInsensitive(NamespaceString::kAdminDb) && + !dbName.equalCaseInsensitive(NamespaceString::kLocalDb) && + !dbName.equalCaseInsensitive(NamespaceString::kConfigDb)); + const auto shardPtr = !primaryShard.isValid() ? nullptr : uassertStatusOK(Grid::get(opCtx)->shardRegistry()->getShard(opCtx, primaryShard)); |