MongoEngine / eve-mongoengine

An Eve extension for MongoEngine ODM support
Other
39 stars 28 forks source link

No authorization support for MongoDB #4

Closed Amorano closed 10 years ago

Amorano commented 10 years ago

I could not get my connections to work and then noticed that there is no authenticate operation even if you supply a proper MONGO_USERNAME and MONGO_PASSWORD variable.

I traced the connection to the class MongoengineDataLayer and re-wrote the start of the init to the following:

def init(self, ext): """ Constructor.

    :param ext: instance of :class:`EveMongoengine`.
    """

    username = ext.app.config['MONGO_USERNAME']
    password = ext.app.config['MONGO_PASSWORD']
    auth = (username, password)

    if any(auth) and not all(auth):
        raise Exception('Must set both USERNAME and PASSWORD or neither')

    self.conn = connect(ext.app.config['MONGO_DBNAME'],
                        host=ext.app.config['MONGO_HOST'],
                        port=ext.app.config['MONGO_PORT'])

    db = self.conn[ext.app.config['MONGO_DBNAME']]
    if any(auth):
        db.authenticate(username, password)

This now accounts for a passed username and password, and does connect if proper.

I would submit a patch/pull, except I dont work in indents with spacing (SCiTE autoscript saves them as TABS) so on a save it blew the diff out to every line.

HTH

hellerstanislav commented 10 years ago

Hello, the master branch contains now your (slightly modified) patch. Please, test this configuration for me, I do not have any mongo with auth setup running (an Im wee bit lazy to do so...).

Edit: btw. Thank you very much for this effort!

Amorano commented 10 years ago

Working great here. Thanks!