Rails Mongoid fails to authenticate - failed with error 13: "not authorized for query on my_db.my_collection"
Asked Answered
C

1

13

This issue was said to be resolved the latest version of Moped but still happens to me. I have a rails 4.2 app with Mongoid, created a user for a MongoDB DB with readWrite and dbOwner roles, and set auth=true in the mong.conf file.
I can perform any actions on the DB with that user credentials using the Mongo shell or a simple Java application using their Mongo driver.
However, when trying to authenticate with Mongoid I always get this error:

failed with error 13: "not authorized for query on my_db.my_collection"

Here is the relevant portion of my mongoid.yml file:

production:
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: my_db
      hosts:
              - localhost:27017
      username: my_username
      password: my_password

I've also tried replacing the host with remote address of the server and access it remotely ( which works with the authentication option disabled ) without success. For what's it worth, I can see that the credentials by debugging the mpped/node.rb file, in the ensure_connected method I see that @credentials variable contains my username and password What am I missing here? Thanks!

Cogan answered 26/4, 2015 at 9:22 Comment(8)
#17376750Odelle
Thanks but as I said I also used the readWrite role and it works from other clients. That's not the right answerCogan
If you escalate the user to type dbOwner does the error still occur?Jenness
@Cogan : Did you find a solution to your problem?Acroter
@Jenness actually the user already had dbOwner and readWrite permissions and not dbAdmin and readWrite like I first said, so that didn't solve it...Cogan
@Elyasin : No, I'm still stuck with it...Cogan
According to your question this happens to you on your PROD environment. Can you confirm ? How is the DEV environment ?Acroter
it doesn't work on both environmentsCogan
B
12

Ran into this problem also. I would be willing to guess you are running MongoDB 3+?

The bad news: Moped 2 (which used by mongoid today) does not support authentication for MongoDB 3+.

Moped also falls short on a lot of essential functionality for supporting server version 3.0. For example, it simply doesn’t support authentication with MongoDB 3.0 or recent versions of the wire protocol (which includes commands such as listCollections, listIndexes, and the ability to do bulk writes). Integrating Mongoid with Ruby driver 2.0 will bring a lot of this functionality and forward-compatibility to the ODM. Source

The good news: A major rewrite is underway right now (Mongoid 5), which will be supported by the MongoDB team. You can pull from master today and test it out...but I would not recommend for production yet as it is highly unstable.

Solution options

  1. Remove user/pass auth from MongoDB and any public DNS to the database. Use SSH with a private key and connect to the private address.
  2. Downgrade to MongoDB 2.x
  3. Use Mongoid 5
Breen answered 16/6, 2015 at 18:33 Comment(2)
Thanks for this, I upgraded to mongoid 5, since now it is available, still same error with mongo, instead of moped!Dilly
mongoid 5.0 now supports authentication : docs.mongodb.org/ecosystem/tutorial/ruby-mongoid-tutorialCogan

© 2022 - 2024 — McMap. All rights reserved.