Commit ad331238 by Carlos Andrés Rocha

Cleanup mongodb authentication

Authenticate to the database before getting a reference to the
collection.
parent b30d46f8
...@@ -62,10 +62,12 @@ class MongoBackend(object): ...@@ -62,10 +62,12 @@ class MongoBackend(object):
**extra **extra
) )
self.collection = self.connection[db_name][collection_name] self.database = self.connection[db_name]
if user or password: if user or password:
self.collection.database.authenticate(user, password) self.database.authenticate(user, password)
self.collection = self.database[collection_name]
self._create_indexes() self._create_indexes()
...@@ -86,5 +88,8 @@ class MongoBackend(object): ...@@ -86,5 +88,8 @@ class MongoBackend(object):
try: try:
self.collection.insert(event, manipulate=False) self.collection.insert(event, manipulate=False)
except PyMongoError: except PyMongoError:
# The event will be lost in case of a connection error.
# pymongo will re-connect/re-authenticate automatically
# during the next event.
msg = 'Error inserting to MongoDB event tracker backend' msg = 'Error inserting to MongoDB event tracker backend'
log.exception(msg) log.exception(msg)
...@@ -45,7 +45,7 @@ class TestMongoBackend(TestCase): ...@@ -45,7 +45,7 @@ class TestMongoBackend(TestCase):
def test_authentication_settings(self): def test_authentication_settings(self):
backend = MongoBackend(user=sentinel.user, password=sentinel.password) backend = MongoBackend(user=sentinel.user, password=sentinel.password)
backend.collection.database.authenticate.assert_called_once_with(sentinel.user, sentinel.password) backend.database.authenticate.assert_called_once_with(sentinel.user, sentinel.password)
def test_mongo_insertion_error(self): def test_mongo_insertion_error(self):
self.backend.collection.insert.side_effect = PyMongoError self.backend.collection.insert.side_effect = PyMongoError
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment