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

Commit 0ff292d

Browse files
committed
Merge pull request #14 from theduke/master
mongodb:// uris and authentication
2 parents 4d84092 + 07c465b commit 0ff292d

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
@@ -43,25 +43,21 @@ def __init__(self, mongos):
4343

4444
self._connect(args, out.ostream, name = name)
4545

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

5050
if name in self.connections:
5151
return self.connections[name]
52-
53-
if port == None:
54-
port = 27107
55-
52+
5653
try:
57-
connection = Connection(host = host, port = port, network_timeout = 2)
58-
except ConnectionFailure:
54+
connection = Connection(uri, network_timeout = 2)
55+
except (ConnectionFailure, ConfigurationError):
5956
return None
6057

6158
self.connections[name] = connection
6259
return connection
6360

64-
6561
def _get_host_and_port(self, server):
6662
host = "localhost"
6763
port = 27017
@@ -148,7 +144,7 @@ def _status(self, args, out, name = None, db = None, collection = None):
148144
result['connections'][name] = "%s:%d" % (conn.host, conn.port)
149145

150146
out(json.dumps(result))
151-
147+
152148
def _connect(self, args, out, name = None, db = None, collection = None):
153149
"""
154150
connect to a mongod
@@ -158,21 +154,56 @@ def _connect(self, args, out, name = None, db = None, collection = None):
158154
out('{"ok" : 0, "errmsg" : "_connect must be a POST request"}')
159155
return
160156

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

166169
if name == None:
167170
name = "default"
168171

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

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

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

0 commit comments

Comments
 (0)