From 2756fa678945f890c73318efa72d3793c6e918d8 Mon Sep 17 00:00:00 2001 From: Pavi Vetriselvan Date: Mon, 31 Aug 2020 08:53:04 -0400 Subject: SERVER-50640 listCommands should list isMaster in addition to hello --- src/mongo/db/commands/generic.cpp | 45 ++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'src/mongo') diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp index 4374cd38762..a27b1aa133d 100644 --- a/src/mongo/db/commands/generic.cpp +++ b/src/mongo/db/commands/generic.cpp @@ -31,6 +31,7 @@ #include "mongo/platform/basic.h" +#include "mongo/base/string_data.h" #include "mongo/bson/util/bson_extract.h" #include "mongo/bson/util/builder.h" #include "mongo/db/auth/authorization_session.h" @@ -52,6 +53,8 @@ using std::string; using std::stringstream; using std::vector; +constexpr auto kIsMasterString = "isMaster"_sd; + class PingCommand : public BasicCommand { public: PingCommand() : BasicCommand("ping") {} @@ -164,34 +167,38 @@ public: const string& ns, const BSONObj& cmdObj, BSONObjBuilder& result) { - // sort the commands before building the result BSON - std::vector commands; - for (const auto command : globalCommandRegistry()->allCommands()) { - // don't show oldnames - if (command.first == command.second->getName()) - commands.push_back(command.second); + // Sort the command names before building the result BSON. + std::vector commandNames; + const auto commandRegistry = globalCommandRegistry(); + for (const auto command : commandRegistry->allCommands()) { + // Don't show oldnames unless it's "isMaster". The output of the listCommands command + // must include "isMaster," even though it's an alias for the "hello" command, in order + // to preserve backwards compatibility with Ops Manager 4.4. + if (command.first == command.second->getName() || command.first == kIsMasterString) + commandNames.push_back(command.first); } - std::sort(commands.begin(), commands.end(), [](Command* lhs, Command* rhs) { - return (lhs->getName()) < (rhs->getName()); - }); + std::sort(commandNames.begin(), commandNames.end()); BSONObjBuilder b(result.subobjStart("commands")); - for (const auto& c : commands) { - BSONObjBuilder temp(b.subobjStart(c->getName())); - temp.append("help", c->help()); - temp.append("requiresAuth", c->requiresAuth()); + for (const auto& c : commandNames) { + const auto command = commandRegistry->findCommand(c); + auto name = (c == kIsMasterString) ? kIsMasterString : command->getName(); + BSONObjBuilder temp(b.subobjStart(name)); + temp.append("help", command->help()); + temp.append("requiresAuth", command->requiresAuth()); temp.append("slaveOk", - c->secondaryAllowed(opCtx->getServiceContext()) == + command->secondaryAllowed(opCtx->getServiceContext()) == Command::AllowedOnSecondary::kAlways); - temp.append("adminOnly", c->adminOnly()); - // optionally indicates that the command can be forced to run on a slave/secondary - if (c->secondaryAllowed(opCtx->getServiceContext()) == + temp.append("adminOnly", command->adminOnly()); + // Optionally indicates that the command can be forced to run on a secondary. + if (command->secondaryAllowed(opCtx->getServiceContext()) == Command::AllowedOnSecondary::kOptIn) temp.append("slaveOverrideOk", true); - temp.append("apiVersions", c->apiVersions()); - temp.append("deprecatedApiVersions", c->deprecatedApiVersions()); + temp.append("apiVersions", command->apiVersions()); + temp.append("deprecatedApiVersions", command->deprecatedApiVersions()); temp.done(); } + b.done(); return 1; -- cgit v1.2.1