-
Notifications
You must be signed in to change notification settings - Fork 1.2k
connect() called with username and password arguments returns an unauthenticated pymongo.connection.Connection object #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I just got hit by this one. This happens in my tests setup. Before creating the Flask app (using class ApiTestCase(unittest.TestCase):
"""Parent class of all API test cases"""
def setUp(self):
self.connection = me.connect(db='test',
host='localhost',
port=27017,
username='user',
password='pwd')
# Workaround for
# https://github.com/MongoEngine/mongoengine/issues/851
me.connection.get_db()
self.connection.drop_database('test')
app = create_app('testing') Adding a call to Maybe I'm doing it wrong in the first place... Anyway, like @bmbouter, I think the way the docs are formulated lets us expect an authenticated connection object. My colleague and I spent a few hours trying to figure this out. Should we not consider this a bug? If not, we may want to at least add a note in the docs about this behavior, but what workaround should we suggest? |
Also experienced this problem. Found another solution by using mongo connection URI mongoengine.connect(username='user', password='pwd', host='localhost', port=27017, db='test') # Does not work
mongoengine.connect(host='mongodb://user:pwd@localhost:27017/test') # Works I am also using My work around is actually to patch
|
@bmbouter Issue is quite old but did you had a chance to test this? |
@bagerard I did not. |
I've just hit this one too. It looks like the problem is in mongoengine/mongoengine/connection.py Line 283 in f0fad6d
we don't care about them at this point ". When I change this to leave in the username/password (commenting lines 269-270) then I can run connection.drop_database(TEST_DB_NAME) without this authentication error.
Is there a good reason for dropping the authentication details at this point? The workaround by @allanlei of connecting with a full db uri with user/pass dets in works well though - thanks! |
@aparrish Could you provide a snippet showing how you were authenticating before switching to using the URI? |
Just going to jump in with mine...
which becomes:
|
Whoops, I think you meant to tag someone else! |
I digged a bit into this and although When using "kwargs" authentication with
So a workaround for your use case would be to call URI style connection ( The fact that there is a different behavior is confusing, I agree but somehow this is consistent with pymongo's interface... |
Thanks for digging into this. Calling |
The mongoengine documentation indicates that the
mongoengine.connect()
method takes keyword parametersusername
andpassword
and returns apymongo.connection.Connection
object. While not explicitly stated I have an expectation that the username and password passed into connect() are authenticated immediately and that thepymongo.connection.Connection
object returned is authenticated. The unexpected behavior is that it is not authentication! If authentication is required and you attempt to list the collections you'll receive the following exception:pymongo.errors.OperationFailure: database error: not authorized for query on example_db.system.namespaces
Authentication doesn't occur until
mongoengine.connection.get_db()
is actually called although themongoengine.connect()
method has everything it needs to attempt authentication. Another downside of this is that the authentication error isn't explicit with the call to connect(). It will fail some time later whenget_db()
is called. Here is a reproducer snippet:You'll notice that this doesn't occur for normal mongoengine use, but this is still a problem. This problem affects users who are transitioning from using PyMongo directly to mongoengine. The first step in that process is to convert the connection so that it is managed using mongoengine instead of PyMongo. As soon as this happens username and password authentication will stop working for any existing code that uses the
pymongo.connection.Connection
returned from mongoengine.connect().This problem affects 0.7.10+ all the way up to the latest release and master.
The text was updated successfully, but these errors were encountered: