Skip to content

Commit

Permalink
Merge pull request #7 from paulnues/master
Browse files Browse the repository at this point in the history
Adding SSL config option to Mongo connection.
  • Loading branch information
shakefu committed Oct 29, 2014
2 parents 3ab26b8 + defbcc8 commit be64642
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ however you cannot nest a connection within itself (this will raise a
* **config_host** (``str``) - Hostname to connect to.
* **config_port** (``int``) - Port to connect to.
* **config_replica** (``str``, optional) - Name of the replica set.
* **config_ssl** (``bool``, optional) - If True, use SSL for this connection.

If ``config_replica`` is present on the class, then HumbleDB will automatically
use a :class:`~pymongo.connection.ReplicaSetConnection` for you. (Requires
Expand All @@ -895,6 +896,9 @@ either :func:`Pyconfig.set` (i.e. ``pyconfig.set('humbledb.connection_pool',
``use_greenlets`` with the :class:`~pymongo.connection.Connection`
instance. (This is only needed if you intend on using threading and greenlets
at the same time.)
* **humbledb.ssl** (``bool``, default: ``False``) - Whether to use
``ssl`` with the :class:`~pymongo.connection.Connection`
instance. The mongod or mongos you are connecting to must have SSL enabled.

More configuration settings are going to be added in the near future, so you
can customize your :class:`~pymongo.connection.Connection` to completely suit
Expand Down
2 changes: 1 addition & 1 deletion humbledb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
limitations under the License.
"""
__version__ = '5.4.1'
__version__ = '5.5'


# We only want to allow * imports for the most common classes. If you want
Expand Down
13 changes: 13 additions & 0 deletions humbledb/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def __new__(mcs, name, bases, cls_dict):
if cls_dict.get('config_port', UNSET) is UNSET:
raise TypeError("missing required 'config_port'")

# Validate if pymongo version supports SSL.
if (cls_dict.get('config_ssl', False) is True and
_version._lt('2.1')):
raise TypeError("Need pymongo.version >= 2.1 to use "
"SSL.")

# Create new class
cls = type.__new__(mcs, name, bases, cls_dict)

Expand Down Expand Up @@ -197,6 +203,11 @@ class MyConnection(Mongo):
"""

config_ssl = pyconfig.setting('humbledb.ssl', False)
""" Specifies whether or not to use SSL for a connection.
.. versionadded: 5.5
"""

def __new__(cls):
""" This class cannot be instantiated. """
return cls
Expand All @@ -205,12 +216,14 @@ def __new__(cls):
def _new_connection(cls):
""" Return a new connection to this class' database. """
kwargs = cls._connection_info()

kwargs.update({
'max_pool_size': cls.config_max_pool_size,
'auto_start_request': cls.config_auto_start_request,
'use_greenlets': cls.config_use_greenlets,
'tz_aware': cls.config_tz_aware,
'w': cls.config_write_concern,
'ssl': cls.config_ssl,
})

if _version._gte('2.1.0') and _version._lt('2.2.0'):
Expand Down
38 changes: 37 additions & 1 deletion test/test_humbledb/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from humbledb import Mongo
from humbledb import _version
from humbledb.errors import DatabaseMismatch
from ..util import database_name, eq_, DBTest, raises
from ..util import database_name, ok_, eq_, DBTest, raises


def teardown():
Expand Down Expand Up @@ -141,3 +141,39 @@ class TestDoc(Document):
TestDoc.find()


@raises(TypeError)
def test_mongo_client_with_ssl_before_2_1():
if _version._gte('2.1'):
raise SkipTest("Only test this with version 2.1 or earlier.")

class SSLMongo(Mongo):
config_host = 'localhost'
config_port = 27017
config_ssl = True


def test_mongo_client_with_ssl_after_2_1():
if _version._lt('2.1'):
raise SkipTest("This test requires version 2.1 or later.")

class SSLMongo(Mongo):
config_host = 'localhost'
config_port = 27017
config_ssl = True

from humbledb import Document
from humbledb.errors import ConnectionFailure
class SomeDoc(Document):
config_database = database_name()
config_collection = 'ssl_collection'

name = 'n'

try:
with SSLMongo:
SomeDoc.insert({SomeDoc.name:'foobar'})
ok_(SomeDoc.find({SomeDoc.name:'foobar'}))
except ConnectionFailure:
raise SkipTest("SSL may not be enabled on mongodb server.")


0 comments on commit be64642

Please sign in to comment.