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

Commit

Permalink
Merge pull request #14 from theduke/master
Browse files Browse the repository at this point in the history
mongodb:// uris and authentication
  • Loading branch information
kchodorow committed May 10, 2011
2 parents 4d84092 + 07c465b commit 0ff292d
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions handlers.py
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.


from pymongo import Connection, ASCENDING, DESCENDING from pymongo import connection, Connection, ASCENDING, DESCENDING
from pymongo.son import SON from pymongo.son import SON
from pymongo.errors import ConnectionFailure, OperationFailure, AutoReconnect from pymongo.errors import ConnectionFailure, ConfigurationError, OperationFailure, AutoReconnect
from bson import json_util from bson import json_util


import re import re
Expand Down Expand Up @@ -43,25 +43,21 @@ def __init__(self, mongos):


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


def _get_connection(self, name = None, host = None, port = None): def _get_connection(self, name = None, uri='mongodb://localhost:27017'):
if name == None: if name == None:
name = "default" name = "default"


if name in self.connections: if name in self.connections:
return self.connections[name] return self.connections[name]


if port == None:
port = 27107

try: try:
connection = Connection(host = host, port = port, network_timeout = 2) connection = Connection(uri, network_timeout = 2)
except ConnectionFailure: except (ConnectionFailure, ConfigurationError):
return None return None


self.connections[name] = connection self.connections[name] = connection
return connection return connection



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


out(json.dumps(result)) out(json.dumps(result))

def _connect(self, args, out, name = None, db = None, collection = None): def _connect(self, args, out, name = None, db = None, collection = None):
""" """
connect to a mongod connect to a mongod
Expand All @@ -158,21 +154,56 @@ def _connect(self, args, out, name = None, db = None, collection = None):
out('{"ok" : 0, "errmsg" : "_connect must be a POST request"}') out('{"ok" : 0, "errmsg" : "_connect must be a POST request"}')
return return


host = "localhost"
port = 27017
if "server" in args: if "server" in args:
(host, port) = self._get_host_and_port(args.getvalue('server')) try:
uri = args.getvalue('server')
info = connection._parse_uri(uri)
except Exception, e:
print uri
print e
out('{"ok" : 0, "errmsg" : "invalid server uri given", "server" : "%s"}' % uri)
return
else:
uri = 'mongodb://localhost:27017'


if name == None: if name == None:
name = "default" name = "default"


conn = self._get_connection(name, host, port) conn = self._get_connection(name, uri)
if conn != None: if conn != None:
out('{"ok" : 1, "host" : "%s", "port" : %d, "name" : "%s"}' % (host, port, name)) out('{"ok" : 1, "server" : "%s", "name" : "%s"}' % (uri, name))
else: else:
out('{"ok" : 0, "errmsg" : "could not connect", "host" : "%s", "port" : %d, "name" : "%s"}' % (host, port, name)) out('{"ok" : 0, "errmsg" : "could not connect", "server" : "%s", "name" : "%s"}' % (uri, name))

def _authenticate(self, args, out, name = None, db = None, collection = None):
"""
authenticate to the database.
"""

if type(args).__name__ == 'dict':
out('{"ok" : 0, "errmsg" : "_find must be a POST request"}')
return


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


if db == None:
out('{"ok" : 0, "errmsg" : "db must be defined"}')
return

if not 'username' in args:
out('{"ok" : 0, "errmsg" : "username must be defined"}')

if not 'password' in args:
out('{"ok" : 0, "errmsg" : "password must be defined"}')

if not conn[db].authenticate(args.getvalue('username'), args.getvalue('password')):
out('{"ok" : 0, "errmsg" : "authentication failed"}')
else:
out('{"ok" : 1}')

def _find(self, args, out, name = None, db = None, collection = None): def _find(self, args, out, name = None, db = None, collection = None):
""" """
query the database. query the database.
Expand Down

0 comments on commit 0ff292d

Please sign in to comment.