comtihon / mongodb-erlang

MongoDB driver for Erlang
Apache License 2.0
342 stars 268 forks source link

Can't connect to MongoDB with user name and password #159

Closed ivan-perl closed 6 years ago

ivan-perl commented 7 years ago

I have MongoDB server installed locally on standard port. I need to be able to connect to it with login and password. Here is what I'm trying to do:

1> application:ensure_all_started (mongodb), 1> Database = <<"database_name">>, 1> Login = <<"user_login">>, 1> Password = <<"user_password">>, 1> {ok, Connection} = mc_worker_api:connect ([{database, Database}, {login, Login},{password, Password}]).

As a result I got following: {ok,<0.68.0>} ** exception error: <<"Can't pass authentification">>

In server logs I see only this: 2017-03-02T21:17:15.969+0000 I NETWORK [thread1] connection accepted from 127.0.0.1:57109 #101005 (4 connections now open) 2017-03-02T21:17:15.980+0000 I - [conn101005] end connection 127.0.0.1:57109 (4 connections now open)

MongoDB server version: 3.4.0

With the same login and password I can connect both from native mongo cli client and form Java.

comtihon commented 7 years ago

Sounds like a bug. I'll check ability to do auth with 3.4+

InoMurko commented 7 years ago

I have tried this on a single node with 3.4.1 and it works.

`mkdir -p ../data/ mkdir -p ../data/mongosvr/rs-0

mongod --httpinterface --shardsvr --dbpath ../data/mongosvr/rs-0 --rest --port 27091 --pidfilepath ../data/mongosvr/rs-0.pid 2>&1 | sed "s/.*/$red&$default/"

mongo localhost:27091 < admin.js ` admin.js is

use admin var adminUser = { "user": "admin", "pwd": "admin_pass", "roles": [ { "role": "userAdminAnyDatabase", "db": "admin" } ] }; db.createUser(adminUser);

Restart mongo.

mongod --config mongod.conf --httpinterface --shardsvr --dbpath ../data/mongosvr/rs-0 --rest --port 27091 --pidfilepath ../data/mongosvr/rs-0.pid 2>&1 | sed "s/.*/$red&$default/"

mongo -u admin -p admin_pass --authenticationDatabase admin --host localhost --port 27091 dev.js

where dev.js is

use database_name db.createUser( { user: "dev", pwd: "dev_pass", roles: [{ role: "readWrite", db: "database_name" }]});

Eshell V8.2 (abort with ^G) 1> application:ensure_all_started (mongodb). {ok,[bson,crypto,poolboy,mongodb]} 2> mc_worker_api:connect ([{host, "localhost"},{port, 27091},{database, <<"database_name">>}, {login, <<"dev">>},{password, <<"dev_pass">>}]). {ok,<0.68.0>}

2017-03-03T17:51:42.717+0100 I NETWORK  [thread1] connection accepted from 127.0.0.1:50175 #14 (1 connection now open)
2017-03-03T17:51:42.785+0100 I ACCESS   [conn14] Successfully authenticated as principal dev on rezolve_core_dev
comtihon commented 7 years ago

@ivan-perl Are you still having problems with login?

fabianTMC commented 7 years ago

I am having the same issue on 3.4.2. I have found that it occurs when the authenticationDatabase is not admin but the database that is specified to be connected to, in @ivan-perl's case, database_name

ivan-perl commented 7 years ago

No, I still have the same issue with passing credentials. When I setting noauth = true everything working like a charm, but auth still not working. I even reproduced your steps with your exact json for user creation and still got following:

3> application:ensure_all_started (mongodb), 3> mc_worker_api:connect ([{host, "localhost"},{port, 27017},{database, <<"bitnami_df">>}, {login, <<"dev">>},{password, <<"dev_pass">>}]). {ok,<0.76.0>} ** exception error: <<"Can't pass authentification">>

To give you more context, I'm using MongoDB deployment which is delivered within Bitnami stack with DreamFactory platform. At this moment my deployment is dreamfactory-2.4.1-2

immnas commented 7 years ago

Hi, Have you find any solution for this issue? I have same issue with mongodb 3.4.4.

comtihon commented 7 years ago

Hi, no I haven't A little bit busy now. Will try to search later, but will be thankful for pr with fix.

waqadeer commented 6 years ago

Is it fixed? Related issue https://github.com/comtihon/mongodb-erlang/issues/180

comtihon commented 6 years ago

I don't think so. Will try to reproduce it tomorrow.

comtihon commented 6 years ago

I still can't reproduce this bug. What I did:
Start mongo as in tutorial:

sudo docker run --name some-mongo -v /home/tihon/db:/data/db  -d -p 27017:27017 mongo --auth
sudo docker exec -it some-mongo mongo admin
connecting to: admin
> db.createUser({ user: 'jsmith', pwd: 'some-initial-password', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: {
    "user" : "jsmith",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

Now connect from mongodb shell:

erl -pa ebin -pa deps/*/ebin
1> application:ensure_all_started(mongodb).
{ok,[bson,crypto,poolboy,pbkdf2,mongodb]}
2> mc_worker_api:connect([{database, <<"admin">>}, {login, <<"jsmith">>}, {password, <<"some-initial-password">>}]).
{ok,<0.72.0>}
3>

So, everything is working.

Anyway, I beleive that thare is some error, as this topic is quite long. I've merged liuhaifeng's PR https://github.com/comtihon/mongodb-erlang/pull/181 Please, check if it helps you.