Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 07c465b

Browse files
author
theduke
committed
Added support for all pymongo supported mongodb:// uri schemes in _connect, mostly to support authentication. Also added new api call _authenticate for dbs. (post data: username, password).
1 parent 6a9fe32 commit 07c465b

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

handlers.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from pymongo import Connection, ASCENDING, DESCENDING
15+
from pymongo import connection, Connection, ASCENDING, DESCENDING
1616
from pymongo.son import SON
17-
from pymongo.errors import ConnectionFailure, OperationFailure, AutoReconnect
17+
from pymongo.errors import ConnectionFailure, ConfigurationError, OperationFailure, AutoReconnect
1818
from bson import json_util
1919

2020
import re
@@ -41,25 +41,21 @@ def __init__(self, mongos):
4141

4242
self._connect(args, out.ostream, name = name)
4343

44-
def _get_connection(self, name = None, host = None, port = None):
44+
def _get_connection(self, name = None, uri='mongodb://localhost:27017'):
4545
if name == None:
4646
name = "default"
4747

4848
if name in self.connections:
4949
return self.connections[name]
50-
51-
if port == None:
52-
port = 27107
53-
50+
5451
try:
55-
connection = Connection(host = host, port = port, network_timeout = 2)
56-
except ConnectionFailure:
52+
connection = Connection(uri, network_timeout = 2)
53+
except (ConnectionFailure, ConfigurationError):
5754
return None
5855

5956
self.connections[name] = connection
6057
return connection
6158

62-
6359
def _get_host_and_port(self, server):
6460
host = "localhost"
6561
port = 27017
@@ -146,7 +142,7 @@ def _status(self, args, out, name = None, db = None, collection = None):
146142
result['connections'][name] = "%s:%d" % (conn.host, conn.port)
147143

148144
out(json.dumps(result))
149-
145+
150146
def _connect(self, args, out, name = None, db = None, collection = None):
151147
"""
152148
connect to a mongod
@@ -156,21 +152,56 @@ def _connect(self, args, out, name = None, db = None, collection = None):
156152
out('{"ok" : 0, "errmsg" : "_connect must be a POST request"}')
157153
return
158154

159-
host = "localhost"
160-
port = 27017
161155
if "server" in args:
162-
(host, port) = self._get_host_and_port(args.getvalue('server'))
156+
try:
157+
uri = args.getvalue('server')
158+
info = connection._parse_uri(uri)
159+
except Exception, e:
160+
print uri
161+
print e
162+
out('{"ok" : 0, "errmsg" : "invalid server uri given", "server" : "%s"}' % uri)
163+
return
164+
else:
165+
uri = 'mongodb://localhost:27017'
163166

164167
if name == None:
165168
name = "default"
166169

167-
conn = self._get_connection(name, host, port)
170+
conn = self._get_connection(name, uri)
168171
if conn != None:
169-
out('{"ok" : 1, "host" : "%s", "port" : %d, "name" : "%s"}' % (host, port, name))
172+
out('{"ok" : 1, "server" : "%s", "name" : "%s"}' % (uri, name))
170173
else:
171-
out('{"ok" : 0, "errmsg" : "could not connect", "host" : "%s", "port" : %d, "name" : "%s"}' % (host, port, name))
174+
out('{"ok" : 0, "errmsg" : "could not connect", "server" : "%s", "name" : "%s"}' % (uri, name))
175+
176+
def _authenticate(self, args, out, name = None, db = None, collection = None):
177+
"""
178+
authenticate to the database.
179+
"""
180+
181+
if type(args).__name__ == 'dict':
182+
out('{"ok" : 0, "errmsg" : "_find must be a POST request"}')
183+
return
172184

185+
conn = self._get_connection(name)
186+
if conn == None:
187+
out('{"ok" : 0, "errmsg" : "couldn\'t get connection to mongo"}')
188+
return
173189

190+
if db == None:
191+
out('{"ok" : 0, "errmsg" : "db must be defined"}')
192+
return
193+
194+
if not 'username' in args:
195+
out('{"ok" : 0, "errmsg" : "username must be defined"}')
196+
197+
if not 'password' in args:
198+
out('{"ok" : 0, "errmsg" : "password must be defined"}')
199+
200+
if not conn[db].authenticate(args.getvalue('username'), args.getvalue('password')):
201+
out('{"ok" : 0, "errmsg" : "authentication failed"}')
202+
else:
203+
out('{"ok" : 1}')
204+
174205
def _find(self, args, out, name = None, db = None, collection = None):
175206
"""
176207
query the database.

0 commit comments

Comments
 (0)