Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoengine using Authentication Database

I am not sure how to connect to a mongodb database that uses an authentication database with mongoengine.

On the command prompt I need to do mongo hostname:27017/myApp -u "test" -p "test" --authenticationDatabase admin, but I don't see where I'd pass this as an argument to mongoengine so I use the admin database for auth but connect to the myApp database for my models?

I believe this is where it's explained in the PyMongo guide:

https://api.mongodb.com/python/current/examples/authentication.html

>>> from pymongo import MongoClient
>>> client = MongoClient('example.com')
>>> db = client.the_database
>>> db.authenticate('user', 'password', source='source_database')

and I found the pull request that added this to mongoengine:

https://github.com/MongoEngine/mongoengine/pull/590/files

It looks like you just add authentication_source as an argument to connect like connect(authentication_source='admin'). It'd be nice if it was better documented.

http://docs.mongoengine.org/apireference.html?highlight=authentication_source

like image 847
Rob Avatar asked Nov 06 '16 01:11

Rob


People also ask

What is an authentication database?

What Does Database Authentication Mean? Database authentication is the process or act of confirming that a user who is attempting to log in to a database is authorized to do so, and is only accorded the rights to perform activities that he or she has been authorized to do.

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

Does MongoDB have authentication?

MongoDB supports x. 509 certificate authentication for client authentication and internal authentication of the members of replica sets and sharded clusters.


2 Answers

According to the mongoengine connecting guide, the connect() method support URI style connections. i.e.

connect(
   'project1'
   host='mongodb://username:password@host1:port1/databaseName'
)

In that sense, you can also specify the authentication source database as below:

"mongodb://username:password@host1:port1/database?authSource=source_database"

See also MongoDB connection string URI for more MongoDB URI examples. Also Authentication options through connection string

like image 113
Wan Bachtiar Avatar answered Sep 20 '22 03:09

Wan Bachtiar


The API has been updated, so this is the right way to do it now:

connect('mydb',
        host="localhost",
        username="admin",
        password="secret",
        authentication_source='your_auth_db')
like image 25
E.Bloch Avatar answered Sep 20 '22 03:09

E.Bloch