如何通过Node.js MongoDB驱动程序API将x.509证书主题作为用户添加到Mongodb 3.4中?

时间:2017-03-22 10:19:22

标签: node.js mongodb x509

我需要使用已经拥有Node.js MongoDB驱动程序API包的Node.js应用程序将用户添加到我的MongoDB 3.4副本集。

问题是:The API documentation并未涵盖add x.509 Certificate subject as a User的方法。

有谁知道怎么做?换句话说,我需要一个Node.js机制/ API,我可以使用它来执行下面的mongodb命令:

mongo --host mongo-node-0
use admin
db.getSiblingDB("$external").runCommand(
{createUser: "emailAddress=foo@bar.com,CN=admin,OU=Clients,O=FOO,L=Dublin,ST=Ireland,C=IE",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db:"admin" },
{ role: "clusterAdmin",  db: "admin" }
]})

1 个答案:

答案 0 :(得分:1)

Mongo文档之后,在Node上,对MongoDB执行命令哈希。这使您可以访问服务器上通过API不可用的任何命令。

command(selector[, options], callback)
    Arguments:  

        selector (object) – the command hash to send to the server, ex: {ping:1}.
        [options] (object) – additional options for the command.
        callback (function) – this will be called after executing this method. The command always return the whole result of the command as the second parameter.

    Returns:    

    null

所以,你可以尝试一下:

 var db = new Db('$external', new MongoServer('localhost', 27017));
 db.open(function(err, db) {
   if (err) {
     console.log(err);
   }

   db.command({
     createUser: "emailAddress=foo@bar.com,CN=admin,OU=Clients,O=FOO,L=Dublin,ST=Ireland,C=IE",
     roles: [
       { role: "userAdminAnyDatabase", db: "admin" },
       { role: "dbAdminAnyDatabase", db: "admin" },
       { role: "readWriteAnyDatabase", db:"admin" },
       { role: "clusterAdmin",  db: "admin" }
     ]}, function(err, result){
       if (err) {
         console.log(err);
       }
       console.log(result)

       db.close();
   });
 });