'Not able to connect mongodb with ssl and ca certificate in nest js

Hi am attempting to connect mongoose with ssl and ca certificate for this i am trying to with below code :

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import * as fs  from 'fs'

const {
  MONGO_DBNAME: DBNAME,
  MONGO_PRIMARY_HOST: PRIMARY_HOST,
  MONGO_SECONDARY_HOST: SECONDARY_HOST,
  MONGO_PORT: PORT,
  MONGO_USERNAME: USERNAME,
  MONGO_PASSWORD: PASSWORD,
  MONGO_REPLICASET: REPLICASET,
  MONGO_AUTH_SOURCE: AUTH_SOURCE,
  MONGO_SSL_CA_PATH,
  MONGO_SSL_CERT_PATH,
  MONGO_SSL_CERT_KEY
} = process.env

const MONGO_CONFIG = {
  DBNAME,
  PRIMARY_HOST,
  SECONDARY_HOST,
  PORT,
  USERNAME,
  PASSWORD,
  REPLICASET,
  AUTH_SOURCE,
  OPTIONS: {
    db: {
      native_parser: true
    },
    server: {
      poolSize: 5
    }
  }
}

const connectionuri =
  'mongodb://' + encodeURIComponent(MONGO_CONFIG.USERNAME) +
  ':' + encodeURIComponent(MONGO_CONFIG.PASSWORD) +
  '@' + MONGO_CONFIG.PRIMARY_HOST +
  ':' + MONGO_CONFIG.PORT + ',' +  MONGO_CONFIG.SECONDARY_HOST +
  ':' +  MONGO_CONFIG.PORT + '/' +  MONGO_CONFIG.DBNAME +
  '?' + 'replicaSet=' + MONGO_CONFIG.REPLICASET + '&' +
  'ssl=true&authSource=' + MONGO_CONFIG.AUTH_SOURCE

@Module({
  imports: [AuthModule, UserModule, BookmarkModule, MongooseModule.forRoot(connectionuri, {
    ssl: true,
    sslValidate: true,
    sslCA: String([fs.readFileSync(MONGO_SSL_CA_PATH)]),
    sslKey: String(fs.readFileSync(MONGO_SSL_CERT_PATH)),
    sslCert: String(fs.readFileSync(MONGO_SSL_CERT_KEY)),
    connectTimeoutMS: 5000,
    useNewUrlParser: true,
    useFindAndModify: false,
    useCreateIndex: true,
    useUnifiedTopology: true
  }), CustomerModule, ConfigModule],
  controllers: [],
  providers: [],
})
export class AppModule {}

But connection was not getting success getting error as like below:

[Nest] 11898  - 01/04/2022, 16:57:36   ERROR [ExceptionHandler] ENAMETOOLONG: name too long, open '-----BEGIN CERTIFICATE-----

Above the details what i am trying but not getting connection if anyone knows what i am doing wrong please correct me.



Solution 1:[1]

if you remove useCreateIndex, useFindAndModify it will solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charfeddine Mohamed Ali