Skip to content

Mongoose 5.0.1: Authentication failed with authMechanism=SCRAM-SHA-1 in MongoDB docker container #6052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BorntraegerMarc opened this issue Jan 26, 2018 · 11 comments

Comments

@BorntraegerMarc
Copy link
Contributor

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
We have set up a MongoDB docker container and want to connect to it with a custom mongo user. We have tried many connection attempts. Like: await Mongoose.connect('mongodb://db-user:4567@server-db:27017/admin?authSource=admin');

or: await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authMechanism=SCRAM-SHA-1');

or: await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db');

Then I see in the console:

[conn1] SCRAM-SHA-1 authentication failed for db-user on server-db from client 172.22.0.3:56428 ; UserNotFound: Could not find user db-user@server-db
2018-01-26T14:58:50.918+0000 I NETWORK  [conn1] end connection 172.22.0.3:56428 (0 connections now open)
(node:45) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Authentication failed.

So according to the logs I'm 100% sure that my backend server and my mongoDB instance have connection to each other. The failure is really at authenticating.

Connecting to the mongodb without username + password like this actually works fine: await Mongoose.connect('mongodb://server-db:27017/server-db'); -> However of course an exception is thrown as soon as I try to create new collections, etc.

If the current behavior is a bug, please provide the steps to reproduce.

My MongoDB file looks like this:

FROM mongo:3.6.2
COPY ./mongo.config.js /docker-entrypoint-initdb.d/
EXPOSE 27017
CMD ["mongod", "--auth"]

Inside mongo.config.js:

let res = [
  "use admin",
  db.createUser({
    user: 'admin',
    pwd: '1234',
    roles: [{
      role: "root", db: "admin"
    }]
  }),
  "use server-db",
  db.createUser({
    user: 'db-user',
    pwd: '4567',
    roles: [{
      role: "readWrite", db: "server-db"
    }]
  })
]
printjson(res)

And actually I see the successful creation of the user in the console:

Successfully added user: {
  "user" : "admin",
  "roles" : [
    {
      "role" : "root",
      "db" : "admin"
    }
  ]
}
Successfully added user: {
  "user" : "db-user",
  "roles" : [
     {
     "role" : "readWrite",
     "db" : "server-db"
     }
  ]
}

And logging on a terminal also works:

root@5a4292713f21:/# mongo -u "db-user" -p "4567"
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
> use server-db
switched to db server-db
> db.createCollection("test")
{ "ok" : 1 }
> show collections
test

What is the expected behavior?
Mongoose should connect successfully with the existing username + password.

I have searched through all possible issues in this repository and also on stackoverflow. I have a feeling that this might be a bug when connecting to a docker MongoDB instance.

Please mention your node.js, mongoose and MongoDB version.
Node: v8.9.1
MongoDB: 3.6.2
Mongoose: 5.0.1

@BorntraegerMarc BorntraegerMarc changed the title Authentification failed with authMechanism=SCRAM-SHA-1 in MongoDB docker container Mongoose 5.0.1: Authentication failed with authMechanism=SCRAM-SHA-1 in MongoDB docker container Jan 26, 2018
@vkarpov15
Copy link
Collaborator

Try this:

await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authSource=admin')

If I'm reading this correctly, you're connecting to 'server-db' and trying to authenticate against the 'admin' user that's defined on the 'admin' db. You need to tell MongoDB what database your user is defined on regardless of whether you're using docker or not.

@dlipovac012
Copy link

I've tried this but without any luck.. When i change the name of the database from server-db to test everything works without any issue, but i dont really know how to specify the name of the database. Any suggestions?

@BorntraegerMarc
Copy link
Contributor Author

@vkarpov15 me and @dlipovac012 are working on the same project.

@vkarpov15
Copy link
Collaborator

@dlipovac012 try await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authSource=test')

@dlipovac012
Copy link

dlipovac012 commented Feb 19, 2018

@vkarpov15 Nope.. Didn't work.. I got the same issue as before, saying: MongoError: there are no users authenticated... When I try to login to DB through terminal, it shows me the users inside admin collection, but when I try to connect to that database through Robo3T or through the app, it always says that there are no users inside admin and that they are stored inside some test db which I have no idea how it gets generated.

@BorntraegerMarc
Copy link
Contributor Author

@vkarpov15 Do you have more suggestions or is this a bug?

@vkarpov15
Copy link
Collaborator

@BorntraegerMarc @dlipovac012 nope I'm pretty baffled. You're certain that:

await Mongoose.connect('mongodb://db-user:4567@server-db:27017/server-db?authSource=test')

Does not work but the below does?

await Mongoose.connect('mongodb://db-user:4567@server-db:27017/test')

@dlipovac012
Copy link

dlipovac012 commented Mar 22, 2018

@vkarpov15 Actually, no. When we create admin user, it gets created in test database, which we dont want to happen. So basically, both codes are working, but we would want to store that user not in this test db, but rather in admin, and we cant find the way for some reason.

Here's the link to this dummy application that mimics our real application: https://github.com/dlipovac012/nestmongoauth

If it helps somehow, here's the screenshot of the terminal with error:
screen shot 2018-03-22 at 3 17 58 pm

@BorntraegerMarc
Copy link
Contributor Author

Turns out there was a misconfiguration in the mongo docker container. We didn't use the env variables specified here: docker-library/mongo#189

Now it works! Thanks for your support!

@BlackHole1
Copy link

@vkarpov15 Brother, this is awesome! I have wasted 4 hours on this issue. Thank you very much!

@landaisgu
Copy link

Try this:

await Mongoose.connect('mongodb://admin:1234@server-db:27017/server-db?authSource=admin')

If I'm reading this correctly, you're connecting to 'server-db' and trying to authenticate against the 'admin' user that's defined on the 'admin' db. You need to tell MongoDB what database your user is defined on regardless of whether you're using docker or not.

Thank you for your help, I had a headache with this issue for my project using .Net Core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants