How to add x.509 certificate subject as user in Mongodb 3.4 via MongoDB's Node.js API?
I need to add users to my MongoDB 3.4 Replica Set using a Node.js app that already has a MongoDB Node.js API package.
The problem is this: The API documentation does not describe how to add the x.509 certificate subject as a user .
Does anyone know how to do this? In other words, I need a Node.js / API engine that I can use to execute the mongodb command below:
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" }
]})
+3
source to share
1 answer
Following Mongo documentation , on Node perform a command hash against MongoDB. This allows you to 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:
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();
});
});
+1
source to share