Skip to content

Conversation

SavingFrame
Copy link
Contributor

This PR updates the MongoDB connection logic in the Django MongoDB integration to be compatible with PyMongo 4.x. The old method of authenticating db via self.database.authenticate(user, password) has been removed, since it was deprecated and fully removed in PyMongo 4.x.

Changes Made:

  • Refactored the _connect() method to utilize the predefined options to handle user credentials.
  • Removed the deprecated authenticate(user, password) method.
  • Added checks to prevent conflicting credentials between USER/PASSWORD (from settings) and OPTIONS['username'] / OPTIONS['password'].

Copy link
Collaborator

@timgraham timgraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this patch, Ivan!


def _connect(self):
settings_dict = self.settings_dict
settings_dict = deepcopy(self.settings_dict)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, there's no need for a copy.

user = settings_dict.get("USER")
password = settings_dict.get("PASSWORD")
options = settings_dict.get("OPTIONS", {})
options_user = options.pop("username", None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should ignore the possibility the developer put username and password in OPTIONS as that's just creating extra complexity, and it breaks the assumption in other places that the USER/PASSWORD will be set (e.g. https://github.com/mongodb-labs/django-mongodb/blob/main/django_mongodb/client.py).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, fine!
I reworked it and made it as simple as possible

password = settings_dict["PASSWORD"]
if user and password and not self.database.authenticate(user, password):
raise ImproperlyConfigured("Invalid username or password.")
# We can check if the credentials are correct by calling server_info().
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, but I think we can omit the comment since this is no different from the behavior of other backends.

database.authenticate() was removed in PyMongo 4.
@timgraham timgraham changed the title Fix connecting to the db on using default "USER" and "PASSWORD" fix username/password authentication Oct 15, 2024
@timgraham timgraham merged commit 64890ad into mongodb:main Oct 15, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants