diff --git a/celery/backends/mongodb.py b/celery/backends/mongodb.py index dd698007241..57ed19571eb 100644 --- a/celery/backends/mongodb.py +++ b/celery/backends/mongodb.py @@ -157,6 +157,10 @@ def _get_connection(self): # don't change self.options conf = dict(self.options) conf['host'] = host + if self.user: + conf['username'] = self.user + if self.password: + conf['password'] = self.password self._connection = MongoClient(**conf) @@ -268,27 +272,13 @@ def __reduce__(self, args=(), kwargs=None): return super(MongoBackend, self).__reduce__( args, dict(kwargs, expires=self.expires, url=self.url)) - def _get_database(self): + @cached_property + def database(self): + """Get database from MongoDB connection.""" conn = self._get_connection() db = conn[self.database_name] - if self.user and self.password: - source = self.options.get( - 'authsource', - self.database_name or 'admin' - ) - if not db.authenticate(self.user, self.password, source=source): - raise ImproperlyConfigured( - 'Invalid MongoDB username or password.') return db - @cached_property - def database(self): - """Get database from MongoDB connection. - - performs authentication if necessary. - """ - return self._get_database() - @cached_property def collection(self): """Get the meta-data task collection."""