Skip to content

Commit

Permalink
Merge pull request #207 from harlov/AuthSource_ignored_on_connect
Browse files Browse the repository at this point in the history
Auth using authsource db
  • Loading branch information
IlyaSkriblovsky committed Apr 26, 2017
2 parents 80c65aa + 2ff44f0 commit 1c9e701
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bugfixes
^^^^^^^^

- Memory leak fixed in `Collection.bulk_write()`
- Use authSource as auth database if specify in connect uri

Release 16.3.0 (2016-11-25)
---------------------------
Expand Down
13 changes: 13 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

from __future__ import absolute_import, division
from mock import patch
from pymongo.errors import OperationFailure
import os
import shutil
Expand Down Expand Up @@ -163,6 +164,18 @@ def test_AuthConnectionPoolUri(self):
self.assertEqual(cnt, n)
finally:
yield conn.disconnect()

@defer.inlineCallbacks
def test_AuthConnectionPoolUriAuthSource(self):
def authenticate(database, username, password, mechanism):
self.assertEqual(database, self.db2)

with patch('txmongo.connection.ConnectionPool.authenticate', side_effect=authenticate):
conn = connection.ConnectionPool(
"mongodb://{0}:{1}@{2}:{3}/{4}?authSource={5}".format(self.login1, self.password1, mongo_host,
mongo_port, self.db1, self.db2)
)
yield conn.disconnect()

@defer.inlineCallbacks
def test_AuthFailAtStartup(self):
Expand Down
3 changes: 2 additions & 1 deletion txmongo/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ def __init__(self, uri="mongodb://127.0.0.1:27017", pool_size=1, ssl_context_fac
]

if self.__uri['database'] and self.__uri['username'] and self.__uri['password']:
self.authenticate(self.__uri['database'], self.__uri['username'],
auth_db = self.__uri['options'].get('authsource') or self.__uri['database']
self.authenticate(auth_db, self.__uri['username'],
self.__uri['password'],
self.__uri['options'].get('authmechanism', 'DEFAULT'))

Expand Down

0 comments on commit 1c9e701

Please sign in to comment.