3
votes

I am trying to connect to a MongoDB Database in Rackspace w/ SSL using loopback, but it's not working. It seems to connect fine; if I enter wrong credentials (on purpose) I get an error message saying "Can't connect", but when I use the correct credentials no error shows so I THINK I'm connecting fine. But when I try to query the database it always timesout, any idea whats happening?

My datasources.json looks something like:

"db": {
   "name": "mongodb",
   "url": "mongodb://username:[email protected]:port/dbName?ssl=true",
   "debug": true,
   "connector": "mongodb"
  }

I keep reading things about needing a certificate file, but not sure if that applies in this case.

Any help would be greatly appreciated!

4
Not sure how to fix this, but you can try adding a "secure": true there. You can also see the debug logs by settings the DEBUG env variable and running your app: DEBUG=loopback node server/server.js - Farid Nouri Neshat

4 Answers

1
votes

use datasources.env.js as below

var cfenv = require('cfenv');
var appenv = cfenv.getAppEnv();

// Within the application environment (appenv) there's a services object
var services = appenv.services;

// The services object is a map named by service so we extract the one for MongoDB
var mongodb_services = services["compose-for-mongodb"];

var credentials = mongodb_services[0].credentials;

// Within the credentials, an entry ca_certificate_base64 contains the SSL pinning key
// We convert that from a string into a Buffer entry in an array which we use when
// connecting.
var ca = [new Buffer(credentials.ca_certificate_base64, 'base64')];

var datasource = {
    name: "db",
    connector: "mongodb",
    url:credentials.uri,
    ssl: true,
    sslValidate: false,
    sslCA: ca
};
module.exports = {
    'db': datasource
};

http://madkoding.gitlab.io/2016/08/26/loopback-mongo-ssl/ https://loopback.io/doc/en/lb3/Environment-specific-configuration.html#data-source-configuration

1
votes

Create a Datasource using lb4 datasource command, edit the datasource generated by adding the SSL details to the config object: 'ssl', 'sslvalidated', 'checkserverIdentity, sslCA, sslKey etc.

import fs from 'fs';
import path from 'path';

const ca = fs.readFileSync(
  path.join(__dirname, '../../utils/certs/mongodbca.cert'),
  'utf8',
);

const config = {
  name: 'test_db',
  debug: true,
  connector: 'mongodb',
  url: false,
  host:'hostname',
  port: port,
  user: 'user',
  password: 'password',
  database: 'databasename',
  authSource: 'admin',
  useNewUrlParser: true,
  ssl: true,
  sslValidate: true,
  checkServerIdentity: false,
  sslCA: [ca],
};
0
votes

This worked for me, You can monkey patch the Mongo.connect() function by which you can add the option parameter. Make a boot script file which can use the MongoDB option parameters of SSL certificate to make a secured connection to MongoDB, below code snippet, is written in a boot script js.

//Below code is written in a boot script
var monog_cert_file = fs.readFileSync(path.join(__dirname, '../certificate_dir/mongodb.pem'));
var monog_ca_file = fs.readFileSync(path.join(__dirname, '../certificate_dir/rootCA.pem'));
var monog_key_file = fs.readFileSync(path.join(__dirname, '../certificate_dir/mongodb.pem'));

const mongoOptions = {
            ssl: true,
            sslValidate: false,
            sslCA:monog_ca_file,
            sslKey:monog_key_file,
            sslCert:monog_cert_file,
            authSource:"auth_db_name"
        };

//Patching Mongo connect For option variable
const mongodb = require('mongodb').MongoClient;
const ogConnect = mongodb.connect;
const connectWrapper = function(url,cb) {
      return ogConnect(url, mongoOptions, cb);
 }
mongodb.connect = connectWrapper;
//Patching Mongo connect For option variable
-2
votes

use datasources.json as below

 app_db: {
"host": "127.0.0.1",
"port": 27017,
"database": "test",
"name": "app_db",
"username": "youruser",
"password": "yourpassword",
"connector": "mongodb",
"ssl":true,
"server": {
  "auto_reconnect": true,
  "reconnectTries": 100,
  "reconnectInterval": 1000,
  "sslValidate":false,
  "checkServerIdentity":false,
  "sslKey":fs.readFileSync('path to key'),
  "sslCert":fs.readFileSync('path to certificate'),
  "sslCA":fs.readFileSync('path to CA'),
  "sslPass":"yourpassphrase if any"

} 

username,
password,
auto_reconnect,
tries and interval all are optional.
use below link to get the certificates using OpenSSL
https://docs.mongodb.com/manual/tutorial/configure-ssl/