Skip to content

Commit

Permalink
Fix celery#5734 Celery does not consider authMechanism on mongodb bac…
Browse files Browse the repository at this point in the history
…kend URLs
  • Loading branch information
spengjie committed Oct 24, 2019
1 parent 1076d83 commit 8ad942b
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions celery/backends/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 8ad942b

Please sign in to comment.