Skip to content

Commit

Permalink
Merge pull request #4530 from telefonicaid/hardening/4517-onlynames-i…
Browse files Browse the repository at this point in the history
…n-listdatabase-command

ADD nameOnly to listDatabase command on MongoDB
  • Loading branch information
mapedraza authored Mar 7, 2024
2 parents d7793bc + 5555108 commit 0f77457
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix: lighter operation to get databases list from MongoDB (#4517)
1 change: 1 addition & 0 deletions src/lib/mongoBackend/MongoGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ bool getOrionDatabases(std::vector<std::string>* dbsP)

orion::BSONObjBuilder bob;
bob.append("listDatabases", 1);
bob.append("nameOnly", true);

if (!orion::runDatabaseCommand("admin", bob.obj(), &result, &err))
{
Expand Down
15 changes: 8 additions & 7 deletions src/lib/mongoDriver/mongoConnectionPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,30 @@ static int pingConnection(mongoc_client_t* conn, const char* db, bool mtenat)
// ping will be listDatabases in the admin DB. But if we run in not mtenant
// mode listCollections in the default database will suffice

std::string cmd;
std::string cmdString;
std::string effectiveDb;

bson_t* ping = bson_new();
if (mtenat)
{
cmd = "listDatabases";
cmdString = "listDatabases";
BSON_APPEND_INT32(ping, "listDatabases", 1);
BSON_APPEND_BOOL(ping, "nameOnly", true);
effectiveDb = "admin";
}
else
{
cmd = "listCollections";
cmdString = "listCollections";
BSON_APPEND_INT32(ping, "listCollections", 1);
effectiveDb = db;
}

bson_t* ping = bson_new();
BSON_APPEND_INT32(ping, cmd.c_str(), 1);
mongoc_database_t* database = mongoc_client_get_database(conn, effectiveDb.c_str());

bson_error_t error;
int r = 0;
if (!mongoc_database_command_with_opts(database, ping, NULL, NULL, NULL, &error))
{
LM_T(LmtMongo, ("ping command (%s at db %s) falied: %s", cmd.c_str(), effectiveDb.c_str(), error.message));
LM_T(LmtMongo, ("ping command (%s at db %s) falied: %s", cmdString.c_str(), effectiveDb.c_str(), error.message));
// Reference error message: Authentication failed
// Reference error message: command listCollections requires authentication
if ((strstr(error.message, "Authentication failed") != NULL) || (strstr(error.message, "requires authentication") != NULL)) {
Expand Down

0 comments on commit 0f77457

Please sign in to comment.