Bruno Ambrozio
Bruno Ambrozio

Reputation: 461

How to add x.509 certificate subject as a user to Mongodb 3.4 by Node.js MongoDB Driver API?

I need to add users to my MongoDB 3.4 Replica Set using an Node.js application that already has the Node.js MongoDB Driver API package.

The problem is: The API documentation doesn't cover how to add x.509 Certificate subject as a User.

Does anyone know how to do that? In other words, I need a Node.js mechanism/API which I can use to perform the mongodb command below:

mongo --host mongo-node-0
use admin
db.getSiblingDB("$external").runCommand(
{createUser: "[email protected],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" }
]})

Upvotes: 3

Views: 685

Answers (1)

Lucas Oliveira
Lucas Oliveira

Reputation: 677

Following the Mongo documentation, on Node, execute a command hash against MongoDB. This lets you access any commands not available through the API on the server.

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

So, you can try it:

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

   db.command({
     createUser: "[email protected],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();
   });
 });

Upvotes: 1

Related Questions