16

I can't seem to find any documentation for SEQUELIZE.JS on how to use a CA.crt in order to enable connection to my database sitting on a remote server.

I figure its something in the options but I can't seem to figure it out

I have tried

{
 'ssl': true
 'dialectOptions':{
   ssl: {
     ca: 'path/to/ca'
   }
 }     
}

and a few other things but nothing seem to work for me.

Can anybody help me?

Edit:

Here is an error i get when using the ca thing

error connecting to db { Error: unable to verify the first certificate
at TLSSocket.<anonymous>
Travis Delly
  • 1,194
  • 2
  • 11
  • 20

2 Answers2

18

As you don't mention the backend DB of choice, I'll give a mysql sample and how I'd suggest you go about it.

First, confirm the connection using the dialect directly, so for mysql2 supplying variables as necessary:

const connection = mysql.createConnection({
  host: dbVars.host,
  user: dbVars.user,
  database: dbVars.database,
  password: dbVars.password,
  ssl: {
    key: cKey,
    cert: cCert,
    ca: cCA
  }
});

Once that connection is confirmed, move it to Sequelize as:

const sequelize = new Sequelize(dbVars.database, dbVars.user, dbVars.password, {
  host: dbVars.host,
  dialect: 'mysql',
  dialectOptions: {
    ssl: {
      key: cKey,
      cert: cCert,
      ca: cCA
    }
  }
});

Note: loading the certs properly was a learning curve and required a direct import using a raw-loader. Example:

import cKey from 'raw-loader!../certs/client-key.pem'; 
Mark
  • 610
  • 9
  • 22
0

Thanks to Mark's answer above, I was able to connect to a Postgres RDS instance from a Node.js Lambda function as follows:

        const sequelize = new Sequelize(POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD, {
            host: POSTGRES_HOST,
            port: POSTGRES_PORT,
            dialect: 'postgres',
            dialectOptions: {
              ssl: {
                // CAUTION: there are better ways to load the certificate, see comments below
                ca: fs.readFileSync(join(__dirname, 'rds-combined-ca-bundle.pem')).toString()
              }
            }
          });

(Obviously this required the PEM file to be available, see Using SSL/TLS to encrypt a connection to a DB instance)

therightstuff
  • 833
  • 1
  • 16
  • 21