Permalink
Browse files

SERVER-7124 connectionStatus command

Currently only returns list of users logged in on current connection
  • Loading branch information...
1 parent 925f701 commit a7d5f6fa9d14ed29d42f531074e5014fc8dd78a0 @RedBeard0531 RedBeard0531 committed Dec 21, 2012
Showing with 87 additions and 0 deletions.
  1. +26 −0 jstests/connection_status.js
  2. +1 −0 src/mongo/SConscript
  3. +60 −0 src/mongo/db/commands/connection_status.cpp
@@ -0,0 +1,26 @@
+// Tests the connectionStatus command
+
+var dbName = 'connection_status';
+var myDB = db.getSiblingDB(dbName);
+
+function test(userName) {
+ myDB.addUser(userName, "weak password");
+ myDB.auth(userName, "weak password");
+
+ var output = myDB.runCommand("connectionStatus");
+ assert.commandWorked(output);
+ var users = output.authInfo.authenticatedUsers;
+
+ var matches = 0;
+ for (var i=0; i < users.length; i++) {
+ if (users[i].userSource != dbName)
+ continue;
+
+ assert.eq(users[i].user, userName);
+ matches++;
+ }
+ assert.eq(matches, 1);
+}
+
+test("someone");
+test("someone else"); // replaces someone
@@ -215,6 +215,7 @@ env.StaticLibrary("coredb", [
"client/authentication_table_server.cpp",
"client/parallel.cpp",
"db/commands.cpp",
+ "db/commands/connection_status.cpp",
"db/commands/fail_point_cmd.cpp",
"db/commands/find_and_modify_common.cpp",
"db/commands/hashcmd.cpp",
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2010 10gen Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <mongo/pch.h>
+
+#include "mongo/db/auth/authorization_manager.h"
+#include "mongo/db/commands.h"
+
+namespace mongo {
+ class CmdConnectionStatus : public Command {
+ public:
+ CmdConnectionStatus() : Command("connectionStatus") {}
+ virtual bool requiresAuth() { return false; }
+ virtual bool logTheOp() { return false; }
+ virtual bool slaveOk() const { return true; }
+ virtual LockType locktype() const { return NONE; }
+ virtual void addRequiredPrivileges(const std::string& dbname,
+ const BSONObj& cmdObj,
+ std::vector<Privilege>* out) {} // No auth required
+
+ void help(stringstream& h) const {
+ h << "Returns connection-specific information such as logged-in users";
+ }
+
+ bool run(const string&, BSONObj& cmdObj, int, string& errmsg,
+ BSONObjBuilder& result, bool fromRepl) {
+ AuthorizationManager* authMgr = ClientBasic::getCurrent()->getAuthorizationManager();
+
+ BSONObjBuilder authInfo(result.subobjStart("authInfo"));
+ {
+ BSONArrayBuilder authenticatedUsers(authInfo.subarrayStart("authenticatedUsers"));
+
+ PrincipalSet::NameIterator nameIter = authMgr->getAuthenticatedPrincipalNames();
+ for ( ; nameIter.more(); nameIter.next()) {
+ BSONObjBuilder principal(authenticatedUsers.subobjStart());
+ principal.append("user", nameIter->getUser());
+ principal.append("userSource", nameIter->getDB());
+ principal.doneFast();
+ }
+ authenticatedUsers.doneFast();
+ }
+ authInfo.doneFast();
+
+ return true;
+ }
+ } cmdConnectionStatus;
+}

0 comments on commit a7d5f6f

Please sign in to comment.