Unable to resolve the authentication issue related to MongoDB

export MONGO_URI=“mongodb+srv://$MONGO_USERNAME:[email protected]/superData” npm test
— Shell Script
41s

  • export MONGO_URI=mongodb+srv://superuser:****@supercluster.d83jj.mongodb.net/superData

  • npm test

[email protected] test

mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit

(node:39777) [MONGOOSE] DeprecationWarning: Mongoose: the strictQuery option will be switched back to false by default in Mongoose 7. Use mongoose.set('strictQuery', false); if you want to prepare for this change. Or use mongoose.set('strictQuery', true); to suppress this warning.

(Use node --trace-deprecation ... to show where the warning was created)

Server successfully running on port - 4000

error!! MongoServerError: bad auth : authentication failed

Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
Ooops, We only have 9 planets and a sun. Select a number from 0 - 9
script returned exit code 8

“I am using correct password and username for the mongoDB, still I am getting errors when I setup my pipeline. Kindly help.”

Below is the Jenkins code:
pipeline {
agent any
tools {
nodejs ‘Nodejs-22.16.0’
}

/environment {
MONGO_URI = “mongodb+srv://supercluster.d83jj.mongodb.net/superData”
}
/

stages {
stage(‘Install Dependencies’) {
steps {
sh ‘npm install --no-audit’
}
}
stage(‘Dependency Scanning’) {
parallel {
stage(‘NPM Dependency Audit’) {
steps {
sh ‘’’
npm audit --audit-level=critical
echo $?
‘’’
}
}
stage(‘OWASP Dependency Check’) {
steps {
dependencyCheck additionalArguments: ‘’’
–scan './'
–out './'
–format 'ALL'
–prettyPrint’‘’, odcInstallation: ‘OWASP-DepCheck-12.10’

        dependencyCheckPublisher failedTotalCritical: 2, failedTotalHigh: 2, pattern: 'dependency-check-report.xml', skipNoReportFiles: true, stopBuild: true, unstableTotalCritical: 1, unstableTotalHigh: 3

        junit allowEmptyResults: true, keepProperties: true, testResults: 'dependency-check-junit.xml'

        publishHTML([allowMissing: true, alwaysLinkToLastBuild: true, icon: '', keepAll: true, reportDir: './', 
        reportFiles: 'dependency-check-jenkins.html', reportName: 'Dependency-Check HTML Report', reportTitles: '', useWrapperFileDirectly: true])
      }
    }
  }
}
stage('Unit Testing') {
  steps {
    withCredentials([usernamePassword(credentialsId: 'mongo-db-creds', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
      sh'''
      export MONGO_URI="mongodb+srv://$MONGO_USERNAME:[email protected]/superData"
      npm test
      '''
    }
    junit allowEmptyResults: true, keepProperties: true, testResults: 'test-results.xml'
  }
}

}
}

A couple things to note:

  1. For MongoDB credentials, things like the password and the user name are CASE DEPENDENT. SuperPassword != superpassword.
  2. This is a good topic to search the forum for, since it gets asked a lot for this course. Here’s an example.

what is the correct password and username? I am very confused right now. Can you please provide me with that.

export MONGO_USERNAME=“superuser” export MONGO_PASSWORD=“SuperPassword” export MONGO_URI=“mongodb+srv://$MONGO_USERNAME:[email protected]/superData” echo “MongoDB URI: $MONGO_URI” npm test
— Shell Script
9s

  • export MONGO_USERNAME=superuser

  • export MONGO_PASSWORD=SuperPassword

  • export MONGO_URI=mongodb+srv://superuser:[email protected]/superData

  • echo MongoDB URI: mongodb+srv://superuser:[email protected]/superData

MongoDB URI: mongodb+srv://superuser:[email protected]/superData

  • npm test

[email protected] test

mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit

superuser SuperPassword

MongoDB URI: mongodb+srv://superuser:[email protected]/superData

Server successfully running on port - 4000

MongoDB Connection Successful

I figured out the solution. Currently I have hard-coded the values to debug and it worked for me.

My Observation: Might be the bug/issue with Jenkins as it was not picking up the MongoDB credentials from the Jenkins Credentials. Also, in the course and KodeKloud notes, password mentioned is incorrect and needs to be updated as “SuperPassword”.