Play-ReactiveMongo Authentication Failed with MongoDB 3 - mongodb

I have problem using play-reactivemongo to connect to MongoDB 3 and encountered the "'not authorized for query on XXX' (code = 13)" problem. The program ran perfectly ok when MongoDB was started without --auth.
The database version should be 3.0.4, as shown in the log.
2015-10-24T15:58:09.868+0800 I CONTROL [initandlisten] db version v3.0.4
I have used version 0.11.7 play2-reactivemongo plugin.
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24",
I have specified the authMode in the mongodb.uri like this.
mongodb.uri = "mongodb://postsUser:12345678#localhost:27017/posts?authMode=?authMode=scram-sha1&authSource=posts&rm.tcpNoDelay=true"
I found the following error in mongodb.log.
2015-10-24T16:41:24.977+0800 I ACCESS [conn105] Failed to authenticate postsUser#posts with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document
Why MONGODB-CR is used instead of SCRAM-SHA-1? Suppose SCRAM-SHA-1 should be default, right?

Appending following to the connection string works for me
authMode=scram-sha1
so the final connection string is
mongodb://user:pass#ipaddress:27017/litmus?3treadPreference=primary&authMode=scram-sha1
I also have nodejs script connecting to same db, interestingly it connects perfectly without appending authMode=scram-sha1 flag.
Seems to be unhandled case in reactive mongo

Sorry, I made a typo in the mongodb.uri
authMode=?authMode=scram-sha1
and causing the DB to authenticate with mongodb-cr. However, I still got the "not authorized to query" error on first access to DB and subsequent db access were fine. In mongodb.log, I could see the following.
2015-10-25T01:28:45.242+0800 I QUERY [conn33] assertion 13 not authorized for query on posts.posts ns:posts.posts query:{}
2015-10-25T01:28:45.247+0800 I ACCESS [conn33] Successfully authenticated as principal postsUser on posts
I better start another question tomorrow.

Related

Error connecting to mongodb container version 6.0.2 | amd64/mongo

Iam able to connect mongo container versions 4.X and 5.X with the below code using amd64/mongo
auth := options.Credential{
AuthSource: admin,
Username: userName,
Password: pass,
}
opts := options.Client().ApplyURI(URI).SetAuth(auth).SetTLSConfig(&config)
client, err := mongo.Connect(ctx, opts)
But when i try to upgrade the container to version 6.0.2 from amd64/mongo
It fails with the below error
:Unable to connect to thedatabase :connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
I believe by default it tries to pick SCRAM-SHA-1
do I need to set a mongoDB server param in my mongo run script file like below ?
--authenticationMechanisms=SCRAM-SHA-1
All i'm trying to do is connect to db and change the admin and db password using below code , not sure even if this is depreciated now in the mongo version 6.0.2
res := struct{ Ok int }{}
opts := options.RunCmd().SetReadPreference(readpref.Primary())
command := bson.D{{"updateUser", usrName}, {"pwd", pass}}
err = client.Database(db).RunCommand(context.TODO(), command, opts).Decode(&res)
not sure where am i making mistake , the error message is not straight forward . Can anyone help me here ?
So was able to resolve this , The problem was my scripts were using mongo but that is removed in the latest mongo version 6.0 , So i used mongosh, when I try to init the mongo container, that worked.
https://www.mongodb.com/docs/mongodb-shell/

Using MongoDB with Chicago Boss

I'm a newbie to Erlang and I'm using Chicago Boss for a small web project. I have had no issues working with the in-memory db but when I configured it to use mongodb it doesn't go quite as expected. Below is my boss.config database section.
{db_host, "127.0.0.1"},
{db_port, 27017 },
{db_adapter, mongodb},
% {db_username, "boss"},
% {db_password, "boss"},
{db_database, "asm_dashboard"},
I have made sure mongodb is running. I have tried installing mongodb-erlang(https://github.com/comtihon/mongodb-erlang) both manually and using rebar but I still end up with the same exception:
[error] gen_server <0.182.0> terminated with reason: {'module could not be loaded',[{mongo,connect,[{"127.0.0.1",27017}],[]}
Please assist.
First, can you post your whole boss.config file ?
Try this template of mysql DB, I think it is the same, but it should be declared at etc/web.conf
%% Mysql DB Config of ChicagoBoss
{db_schema, "your_db_name"}.
{db_username, "boss"}.
{db_password, "boss"}.
{dp_ip, "127.0.0.1"}.
{db_port, 27017 }.
{pool_size, 10}.

What version of Salat/Casbah should I use to connect to Mongo 3.x server?

I am using https://github.com/salat/salat. But it seems that salat is using Casbah 2.7. Our mongo server is 3.0.6. But we are not able to connect and always got this error.
Configuration error: Configuration error[Access denied to MongoDB database: [mg_prod] with user: [mydbuser]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94)
at play.api.Configuration.reportError(Configuration.scala:743)
at se.radley.plugin.salat.SalatPlugin$MongoSource.connection(SalatPlugin.scala:36)
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:136)
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:131)
Can you please give me some idea how should I fix this issue?
As a workaround you can add
"org.mongodb" %% "casbah" % "2.8.2"
to your build.sbt file and the error should be gone.

MongoLab MongoDB login failed error

I'm getting this error when trying to login to MongoLab database via command line in Ubuntu
> 2015-07-02T13:44:05.682-0400 I NETWORK DBClientCursor::init call() failed
>2015-07-02T13:44:05.694-0400 E QUERY Error: error doing query: failed
at DBQuery._exec (src/mongo/shell/query.js:83:36)
at DBQuery.hasNext (src/mongo/shell/query.js:240:10)
at DBCollection.findOne (src/mongo/shell/collection.js:187:19)
at DB.runCommand (src/mongo/shell/db.js:58:41)
at DB.isMaster (src/mongo/shell/db.js:680:51)
at DB._getDefaultAuthenticationMechanism (src/mongo/shell/db.js:1227:27)
at DB._authOrThrow (src/mongo/shell/db.js:1252:33)
at (auth):6:8
at (auth):7:2 at src/mongo/shell/query.js:83
exception: login failed
Double-check that your client supports the version of MongoDB your server is running. If you're using one of our experimental databases, they were recently upgraded to MongoDB version 3.0 an its new SCRAM-SHA-1 authentication mechanism. Connecting to these databases now requires the 3.0 Mongo shell or a driver that supports SCRAM-SHA-1.
http://docs.mongodb.org/manual/release-notes/3.0-scram/#upgrade-drivers
If you have any questions, feel free to contact us at support#mongolab.com and we'd be happy to help.
Regards,
Jared

Can't Connect to MongoDB from play app with salat: command failed [listDatabases]

I am trying to get started with the salat plugin in playframework. I have configured the database in application.conf and added all the dependencies to Build.scala and added salat to the play.plugins file. I haven't actually added any code to the project yet, I just followed the instructions on the github page, and then tried to run the project. I am getting the following error message
(Server started, use Ctrl+D to stop and go back to the console...)
[info] play - mongodb [default] connected at heroku_app4620908#ds031907.mongolab.com:31907/heroku_app4620908
[error] application -
! #6bchnaacn - Internal server error, for request [GET /] ->
play.api.Configuration$$anon$1: Configuration error [couldn't connect to [ds031907.mongolab.com/107.21.153.211:31907]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:71) ~[play_2.9.1-2.0.3.jar:2.0.3]
at play.api.Configuration.reportError(Configuration.scala:258) ~[play_2.9.1-2.0.3.jar:2.0.3]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:105) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:98) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
Caused by: com.mongodb.CommandResult$CommandFailure: command failed [listDatabases]: { "serverUsed" : "db-uri" , "errmsg" : "need to login" , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:88) ~[mongo-java-driver-2.8.0.jar:na
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134) ~[mongo-java-driver-2.8.0.jar:na]
at com.mongodb.Mongo.getDatabaseNames(Mongo.java:356) ~[mongo-java-driver-2.8.0.jar:na]
at com.mongodb.casbah.MongoConnection.getDatabaseNames(MongoConnection.scala:190) ~[casbah-core_2.9.1-2.4.1.jar:2.4.1]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:103) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:98) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
I am stumped because I added my password and everything to the conf file. From the log it looks as though whatever is trying to connect to the database for me, is not logging in first, using the info I provided.
As you say, it looks like the root issue is that MongoDB is rejecting the command "listDatabases". This command requires administrator access to the MongoDB process since it reveals information about the other databases hosted there.
Unfortunately, the message it returns, "need to login", is a little misleading. You have logged in! You just don't have permission to list the databases.
Here's a simple experiment you can try yourself with the MongoDB shell. See that "show dbs" fails with the same error message you got in your app, but "show collections", which doesn't require access to any other databases outside your own, succeeds?
% mongo ds031907.mongolab.com:31907/heroku_app4620908 -u heroku_app4620908 -p your_password
MongoDB shell version: 2.0.7
connecting to: ds031907.mongolab.com:31907/heroku_app4620908
> show dbs
Fri Aug 17 13:12:10 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
> show collections
system.indexes
system.users
(Note: I did this with my own MongoLab account and modified the text when copying and pasting here so you could just copy it into your terminal.)
Is there a way to avoid making the listDatabases call? I'm not familiar with the framework you're using.
I've the problem as well...
it should come with the SalatPlugin's onStart method that is requesting all database names: source._2.connection.getDatabaseNames().
This code is just testing the aliveness of the server...
I'm gonna check with leon how we could this differently! Sadly, you won't be able to connect until this will be fixed!
Stay tuned on this issue https://github.com/leon/play-salat/issues/23
This is fixed in the latest version of play-salat, it now uses getCollectionNames instead
use admin
db.addUser('userName', 'userPassword')
db.auth('userName', 'userPassword')
show dbs
java:
DB db = mongo.getDB("YouDBName") ;
db.authenticate("userName", "userPassword".toCharArray()) ;
System.out.println(mongo.getDatabaseNames()) ;