fastify / fastify-mongodb

Fastify MongoDB connection plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Connection failes using Scalegrid.io

johntom opened this issue · comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.27.3

Plugin version

4.1.1

Node.js version

14.18.3

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

I'm trybg to update a repo that connects to a number to mongodb databases using sca;egrid.io.
My app that uses the following version works fine

WORKS
  fastify": "^2.6.0",
 "fastify-autoload": "^1.0.0",
 "fastify-cli": "^1.1.0",
 "fastify-mongodb": "^1.0.1",
 "fastify-plugin": "^1.6.0",
 "fastify-session": "^3.3.0",
 "mongodb": "^4.4.1"

const MONGODB_URL_local_todo='mongodb://127.0.0.1:27017/todo'
const MONGODB_URLtodo='mongodb://admin:<Password>@SG-test-fastify-50496.servers.mongodirector.com:27017/todo?ssl=true&connectTimeoutMS=10000&authSource=admin&authSource=admin' // &authMechanism=SCRAM-SHA-1'

async function dbConnector (fastify, options) {
fastify.register(fastifyMongo, {
 // url:MONGODB_URL_local_todo
 url:MONGODB_URLtodo
})
}
the following works with connection string to scalegrid
async function dbConnector (fastify, options) {
  fastify.register(fastifyMongo, {
    url:MONGODB_URLtodo
  })
}

When I update libraries to the following connection fails to scalegrid but works fine to local connection

    "fastify": "^3.27.3",
    "fastify-mongodb": "^4.1.1",
    "fastify-plugin": "^3.0.1",
    "mongodb": "^4.4.1"
    "fastify-autoload": "^3.7.1",
    "fastify-cli": "^2.13.0",
    "fastify-mongodb": "^4.1.1",
    "fastify-plugin": "^3.0.0",

    PS D:\fastify-gp\test-fastify_mongodbA> node app
{"level":50,"time":1648939646981,"pid":17512,"hostname":"DESKTOP-TF4IHHA","reason":{"type":"Unknown","servers":{},"stale":false,"compatible":true,"heartbeatFrequencyMS":10000,"localThresholdMS":15},"stack":"MongoServerSelectionError: self signed 
certificate\n    at Timeout._onTimeout (D:\\fastify-gp\\test-fastify_mongodbA\\node_modules\\mongodb\\lib\\sdam\\topology.js:312:38)\n    at listOnTimeout (internal/timers.js:557:17)\n    at processTimers (internal/timers.js:500:7)","type":"Error","msg":"self signed certificate"}

### Steps to Reproduce

see repo https://github.com/johntom/test-fastify_mongodb

 ////////// populate todo collection in todo database in scalegid.io
     { 
     "_id" : ObjectId("5c1f1314e60a5b0bd0be8066"), 
     "id" : 1.0, 
     "title" : "milk", 
     "desc" : "lower case collection"
     }
   { 
     "_id" : ObjectId("5c204576e60a5b25184c282e"), 
     "id" : 5.0, 
     "title" : "buy milk", 
     "desc" : "need for cereal"
 }
    

### Expected Behavior

I like to see all my connection strings work with scalegrid.io

Easy to start a 30 day free trial at scalegrid

commented

The error comes from mongodb, and the reason already stated.
MongoServerSelectionError: self signed certificate.

Please check how to use scalegrid with mongodb.
https://scalegrid.io/blog/mongodb-ssl-with-self-signed-certificates-in-node-js/

Thanks for prompt reply

  1. Curious as to why it worked with fastify-mondodb 1.0.1 and not 4.1.1
  2. when I apply the following in my main repo with the cert downloaded from scalegrid i still get error
    '''
    const certFileBuf = fs.readFileSync(__dirname + "../../ssl/ca.crt");
    var options = {
    server: { sslCA: certFileBuf}
    };
    fastify .register(require('fastify-mongodb'), { useNewUrlParser: true,url: process.env.MONGODB_URLtodo, name: 'todo' },options)
    I know I must be doing something wrong and I've tried many setting even before original post and did read the docs but clearly missing something.
    TIA
    J
commented

fastify-mongodb@1.0.1 use mongodb@3.1.8 and fastify-mongodb@4.1.1 use mongodb@4.0.1.
The new mongodb version is a complete rewrite version and I do not know what they have been changed internally.

You should probably check the latest document of mongodb.
https://mongodb.github.io/node-mongodb-native/4.4/interfaces/MongoClientOptions.html#sslCA
It seems like the option moved to top-level.

Thanks, that document helped me figure it out.
both connections in the following works

const certFileBuf = fs.readFileSync(__dirname + "../../ssl/ca.crt");
fastify.register(require('fastify-mongodb'), { ssl: true, ca: certFileBuf, useUnifiedTopology: true, useNewUrlParser: true, url: process.env.MONGODB_URLtodo, name: 'todo' })
or
fastify.register(require('fastify-mongodb'),{ tlsAllowInvalidCertificates:true,useUnifiedTopology: true,useNewUrlParser: true, url: process.env.MONGODB_URLtodo, name: 'todo' })