Skip to content

Commit 694d367

Browse files
committed
Merge pull request pandas-dev#34 from manahl/bugfix/MDP-341-authorization-error-seen-in-prod-2
MDP-341 The connectionInfo document format changed in later mongods
2 parents 72421be + c59adc1 commit 694d367

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

CHANGES.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
## Changelog
33

4+
### 1.8 (2015-09-29)
5+
6+
* Bugfix: compatibility with both 3.0 and pre-3.0 MongoDB for
7+
querying current authentications
8+
49
### 1.7 (2015-09-18)
510

611
* Feature: Add support for reading a subset of a pandas DataFrame

arctic/arctic.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ def _parse_db_lib(clz, library):
326326
database_name = clz.DB_PREFIX
327327
return database_name, library
328328

329+
def _is_authenticated_to(self, db_name, auth_users_doc):
330+
# First try userSource to find the database (mongo 2.4), then db (later mongos)
331+
return any(r.get('userSource', r.get('db')) == db_name for r in auth_users_doc)
332+
329333
def __init__(self, arctic, library):
330334
self.arctic = arctic
331335
database_name, library = self._parse_db_lib(library)
@@ -334,11 +338,11 @@ def __init__(self, arctic, library):
334338
self._db = self.arctic._conn[database_name]
335339
self._auth(self._db)
336340

337-
auth_users = self._db.command({'connectionStatus': 1})['authInfo']['authenticatedUsers']
338-
if all((r['userSource'] != self._db.name for r in auth_users)):
341+
auth_users_doc = self._db.command({'connectionStatus': 1})['authInfo']['authenticatedUsers']
342+
if not self._is_authenticated_to(self._db.name, auth_users_doc):
339343
# We're not authenticated to the database,
340344
# ensure we are authed to admin so that we can at least read from it
341-
if all((r['userSource'] != 'admin' for r in auth_users)):
345+
if not self._is_authenticated_to('admin', auth_users_doc):
342346
self._auth(self.arctic._adminDB)
343347

344348
self._library_coll = self._db[library]

tests/unit/test_arctic.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ def test_arctic_lazy_init():
2121
store.list_libraries()
2222
assert mc.called
2323

24-
mock_conn_info = {u'authInfo':
24+
mock_conn_info_mongo2 = {u'authInfo':
2525
{u'authenticatedUsers': [{u'user': u'research', u'userSource': u'admin'}]},
2626
u'ok': 1.0}
2727

28+
mock_conn_info_mongo3 = {u'authInfo':
29+
{u'authenticatedUsers': [{u'user': u'research', u'db': u'admin'}]},
30+
u'ok': 1.0}
31+
2832
mock_conn_info_empty = {u'authInfo':
2933
{u'authenticatedUsers': []},
3034
u'ok': 1.0}
@@ -46,7 +50,7 @@ def test_arctic_auth():
4650
with patch('arctic.arctic.ArcticLibraryBinding.get_library_type', return_value=None, autospec=True):
4751
ga.return_value = Credential('db', 'user', 'pass')
4852
store._conn['arctic_jblackburn'].name = 'arctic_jblackburn'
49-
store._conn['arctic_jblackburn'].command.return_value = mock_conn_info
53+
store._conn['arctic_jblackburn'].command.return_value = mock_conn_info_mongo2
5054
store['jblackburn.library']
5155

5256
# Creating the library will have attempted to auth against it
@@ -70,7 +74,7 @@ def test_arctic_auth_custom_app_name():
7074
with patch('arctic.arctic.ArcticLibraryBinding.get_library_type', return_value=None, autospec=True):
7175
ga.return_value = Credential('db', 'user', 'pass')
7276
store._conn['arctic_jblackburn'].name = 'arctic_jblackburn'
73-
store._conn['arctic_jblackburn'].command.return_value = mock_conn_info
77+
store._conn['arctic_jblackburn'].command.return_value = mock_conn_info_mongo2
7478
store['jblackburn.library']
7579

7680
# Creating the library will have attempted to auth against it
@@ -102,6 +106,13 @@ def test_arctic_auth_admin_reauth():
102106
call('cluster', 'arctic', store._adminDB.name)]
103107

104108

109+
def test_arctic_is_authenticated_to():
110+
self = create_autospec(ArcticLibraryBinding)
111+
assert ArcticLibraryBinding._is_authenticated_to(self, 'admin', mock_conn_info_mongo2['authInfo']['authenticatedUsers'])
112+
assert ArcticLibraryBinding._is_authenticated_to(self, 'admin', mock_conn_info_mongo3['authInfo']['authenticatedUsers'])
113+
assert not ArcticLibraryBinding._is_authenticated_to(self, 'admin', mock_conn_info_empty['authInfo']['authenticatedUsers'])
114+
115+
105116
def test_arctic_connect_hostname():
106117
with patch('pymongo.MongoClient', return_value=MagicMock(), autospec=True) as mc, \
107118
patch('arctic.arctic.mongo_retry', autospec=True) as ar, \

0 commit comments

Comments
 (0)