Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Secure Server Setup with Mongoose

The setup i am trying to success is to have a node process which create databases, and other servers access those databases with a secure way.
So my idea was to create the database from node with a user and pass. Then open the server mongodb port to open access and lock the mongo admin user. If that theory is good:

  1. How to make user with mongoose so that database will be accessible only with that user?
  2. On the /etc/mongodb.conf should i only add bind_ip = 0.0.0.0 and that's all?

PS: i am using Ubuntu 16:04 and latest Mongodb.

Edit: 13/08/17
What i have success until now is to addUser = db.createUser({user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]}); for admin database, connect with it while database is under --auth and trying to create other database via that connection, like below.

var adminConnection = mongoose.createConnection('mongodb://admin:admin@localhost:27017/admin', {
    useMongoClient: true
});
console.log(typeof adminConnection.db.executeDbAdminCommand);//function
like image 931
Honchar Denys Avatar asked Jul 12 '17 10:07

Honchar Denys


2 Answers

Your  /etc/mongod.conf  YAML file will be look like this

storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log


# network interfaces  put your ip in bindIp in form of Array like below
net:
  port: 27017
  bindIp: [127.0.0.1,84.20.57.18]

#before enabling security authorization you must add mongodb database user
security:
  authorization: "enabled"

#Replication oplogsize mb set based on Main Memory of your Ubuntu Server (It will be good to set 1024 for speed of database Operation). In replSetName give your Replica set name or your Project Name Ex: smartcity
replication:
  oplogSizeMB: 1024
  replSetName: "smartcity"

In node js how to use mongoose and connecting to your mongodb database as follows

var mongoose = require('mongoose');
var options = {
    useMongoClient:true
};
var dbUrl = 'mongodb://<dbusername>:<dbpassword>@<db-ipaddress>:27017/<dbname>?replicaSet=<replicasetname>';//Ex:"mongodb://smartcityUser:[email protected]:27017/smartcity?replicaSet=smartcity"

mongoose.connect(dbUrl,options);
mongoose.Promise = global.Promise;

May my work solve your issue and all best

like image 78
Ratan Uday Kumar Avatar answered Oct 12 '22 12:10

Ratan Uday Kumar


Generally i almost did what i wanted. Here is the solution.

var a_conn = mongoose.createConnection('mongodb://admin:admin@localhost:27017/admin', {
    useMongoClient: true
});
a_conn.once('open', function() {
    a_conn.useDb('w_one');
    a_conn.otherDbs[0].db.addUser('user', 'pass', {
        db: 'w_one',
        roles: ["readWrite"]
    });
    var Schema = mongoose.Schema({});
    var Collection = a_conn.otherDbs[0].model('cool', Schema, 'cool');
    var doc = new Collection({});
    doc.save(function() {
        doc.remove(function() {
            var testConn = mongoose.createConnection('mongodb://user:pass@localhost:27017/w_one', {
                useMongoClient: true
            });
            testConn.once('open', function() {
                //Collection.collection.drop('cool');
                console.log('Database is ready.');
            });
        });
    });
});

Generally i am creating Collection with document to create database, and when i am removing that Collection, database automatically get deleted, if there will be option to not delete it, that will be good improvement for the solution.

like image 28
Honchar Denys Avatar answered Oct 12 '22 13:10

Honchar Denys