How to Resolve Authentication Issues in MongoDB Atlas?

I’m facing same issue unable to authorise please let me. Know how can I resolve

Which lab specifically? A link would be very helpful.


Video: Unit Testing and Analyze JUnit Reports
even though running superpassword as password and superuser as username through environments variables still i’m facing this issue

But for what course, please? A link to the lab would tell me that.

Course - Jenkins Pipeline:Unit Testing

Which lab from that course? If I recall correctly, he initially has you run just the nodejs application locally, w/o Jenkins. Is this what you’re doing here, or what else? If it’s in Jenkins, you’ll need to somehow pass the mongo parameters via Jenkins’ what are you doing in that case?

Course - Jenkins Pipeline:Unit Testing
https://learn.kodekloud.com/user/courses/jenkins-pipelines
Video: Unit Testing and Analyze JUnit Reports
from moment 3:50 onwards

I have explicitly indicated in the steps as you can see in the image:

OK, I have looked over the course, and have a working Jenkins file for it. So it’s possible:

pipeline {
    agent any
    tools { nodejs 'NodeJS 16.20.2' }
    environment {

       MONGO_URI = "mongodb+srv://supercluster.d83jj.mongodb.net/superData"
    }
    stages {
        stage('Install Dependencies') {
            steps {

                sh 'npm install --no-audit'

            }
        }
        stage('Run Unit Tests') {
                    // some block

            steps {
                withCredentials([usernamePassword(credentialsId: 'mongo-db-credentials', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
                    sh 'npm test'
               }
            }
       }
    }
}

Sorry it took a while. Part of the problem here is that you are not supplying the info needed to speed your support:

  • You should include your work, such as whatever you’re using for your jenkins file, NOT AS AN IMAGE, but as a code block, as I’ve done here above.
  • You should detail the steps you did. This is particularly true when you’re doing a follow along, because I cannot tell what you did, what you listened to, or what failed.

If you’ll do that, you’ll get MUCH faster service from us. Please do that!

I used exactly your Jenkinsfile and the error remains the same. Authentication error.

pipeline {

agent any

tools { nodejs 'NodeJS 16.20.2' }

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

stages {

    stage('Install Dependencies') {
        steps {
            sh 'npm install --no-audit'
        }
    }

    stage('Run Unit Tests') {
        steps {

            withCredentials([usernamePassword(credentialsId: 'mongo-db-credentials', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
                sh 'npm test'
           }

        }
    }

}

}

Output:

You need to define the credential mongo-db-credentials correctly under Manage Jenkins / Credentials. My Jenkins looks like this:

and for the resource form:

In my Jenkins, I initially had the config slightly off, and at run time the pipeline did not find the credential. I deleted it and recreated it, and it worked. You might want to try this as well.

Mr. Rob, I did what you mentioned above and it still gives me “bad auth: authentication failed”. I deleted the credentials and created them again.
I tested via CLI by exporting the variables outside the Jenkins environment, i.e. on my local machine, and I got the same error. For me, either the password does not match or there is some origin locking on the server/database.

By the way, I created a mongo database on my Atlas to test the communication from here to my environment and it works perfectly.

username and password can be found in the document:

Just to make sure it’s really your access to the Atlas instance we use for this course, please try the following modified pipeline I just tried locally:

pipeline {
    agent any
    tools { nodejs 'NodeJS 16.20.2' }
    environment {

       MONGO_URI = "mongodb+srv://supercluster.d83jj.mongodb.net/superData"
    }
    stages {
        stage('Install Dependencies') {
            steps {

                sh 'npm install --no-audit'

            }
        }
        stage('Run Unit Tests') {
		    // some block

            steps {
		withCredentials([usernamePassword(credentialsId: 'mongo-db-credentials', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
                    // sh 'npm test'
		    sh 'echo $MONGO_PASSWORD'
                    sh 'echo $MONGO_USERNAME'
               }
            }
       }
    }
}

This worked for me as well, with this from the output:

[Pipeline] { (Run Unit Tests)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $MONGO_PASSWORD
[Pipeline] {
[Pipeline] sh
+ echo ****
****
[Pipeline] sh
+ echo superuser
superuser

OK, that looks OK. Assuming the password is OK, you do appear to have a valid credential.

I installed mongosh on my virtual. This works:

ubuntu@jenkins:~$ mongosh "mongodb+srv://supercluster.d83jj.mongodb.net/superData" -u superuser
Enter password: *************
Current Mongosh Log ID:	67eeeb7c77dba0315d300588
Connecting to:		mongodb+srv://<credentials>@supercluster.d83jj.mongodb.net/superData?appName=mongosh+2.4.2
Using MongoDB:		8.0.6
Using Mongosh:		2.4.2

For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/

Atlas atlas-11b0vt-shard-0 [primary] superData> db.planets.find()
[
  {
    _id: ObjectId('64de122465ba6ca132e2d047'),
    id: 4,
    name: 'Mars',
    image: 'https://gitlab.com/sidd-harth/solar-system/-/raw/main/images/mars.png',
    velocity: '24',
    distance: '227',
    description: 'Mars is the fourth planet from the Sun and is the second smallest planet in the solar system. Named after the Roman god of war, Mars is also often described as the “Red Planet” due to its reddish appearance. Mars is a terrestrial planet with a thin atmosphere composed primarily of carbon dioxide. Mars has two known moons, Phobos and Deimos (fear and dread, after attendants of Ares, the Greek god of war, equivalent to the Roman Mars). Searches for more satellites have been unsuccessful, putting the maximum radius of any other satellites at 90 m (100 yd).'
  },
...

See if this fails for you, in which case for some reason, you can’t use the DB. Otherwise, it’s some other config issue we haven’t covered yet.

Password “SuperPassword”? Your output is similar to mine if I use the wrong password.

Wow, the password has capital letters “SuperPassword”, the “S” and the “P”. In the lab it is completely lowercase “superpassword”.

Figured it had to be something like that :slight_smile: OK, you should be good to go now.

Thank you very much for all the comments.