Skip to content

Commit 40f241d

Browse files
committed
Fix exception handling when ssl isn't available.
1 parent d2a2866 commit 40f241d

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

pymongo/mongo_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,6 @@ def __init__(self, host=None, port=None, max_pool_size=10,
259259
self.__ssl_cert_reqs = options.get('ssl_cert_reqs', None)
260260
self.__ssl_ca_certs = options.get('ssl_ca_certs', None)
261261

262-
if self.__use_ssl and not HAS_SSL:
263-
raise ConfigurationError("The ssl module is not available. If you "
264-
"are using a python version previous to "
265-
"2.6 you must install the ssl package "
266-
"from PyPI.")
267-
268262
ssl_kwarg_keys = [k for k in kwargs.keys() if k.startswith('ssl_')]
269263
if self.__use_ssl == False and ssl_kwarg_keys:
270264
raise ConfigurationError("ssl has not been enabled but the "
@@ -282,6 +276,12 @@ def __init__(self, host=None, port=None, max_pool_size=10,
282276
# ssl options imply ssl = True
283277
self.__use_ssl = True
284278

279+
if self.__use_ssl and not HAS_SSL:
280+
raise ConfigurationError("The ssl module is not available. If you "
281+
"are using a python version previous to "
282+
"2.6 you must install the ssl package "
283+
"from PyPI.")
284+
285285
self.__use_greenlets = options.get('use_greenlets', False)
286286
self.__pool = pool_class(
287287
None,

pymongo/mongo_replica_set_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,6 @@ def __init__(self, hosts_or_uri=None, max_pool_size=10,
467467
self.__ssl_cert_reqs = self.__opts.get('ssl_cert_reqs', None)
468468
self.__ssl_ca_certs = self.__opts.get('ssl_ca_certs', None)
469469

470-
if self.__use_ssl and not common.HAS_SSL:
471-
raise ConfigurationError("The ssl module is not available. If you "
472-
"are using a python version previous to "
473-
"2.6 you must install the ssl package "
474-
"from PyPI.")
475-
476470
ssl_kwarg_keys = [k for k in kwargs.keys() if k.startswith('ssl_')]
477471
if self.__use_ssl == False and ssl_kwarg_keys:
478472
raise ConfigurationError("ssl has not been enabled but the "
@@ -490,6 +484,12 @@ def __init__(self, hosts_or_uri=None, max_pool_size=10,
490484
# ssl options imply ssl = True
491485
self.__use_ssl = True
492486

487+
if self.__use_ssl and not common.HAS_SSL:
488+
raise ConfigurationError("The ssl module is not available. If you "
489+
"are using a python version previous to "
490+
"2.6 you must install the ssl package "
491+
"from PyPI.")
492+
493493
super(MongoReplicaSetClient, self).__init__(**self.__opts)
494494
if self.slave_okay:
495495
warnings.warn("slave_okay is deprecated. Please "

test/test_ssl.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# mongod --dbpath /path/to/data/directory --sslOnNormalPorts \
4141
# --sslPEMKeyFile /path/to/mongo/jstests/libs/server.pem \
4242
# --sslCAFile /path/to/mongo/jstests/libs/ca.pem \
43+
# --sslCRLFile /path/to/mongo/jstests/libs/crl.pem \
4344
# --sslWeakCertificateValidation
4445
# Also, make sure you have 'server' as an alias for localhost in /etc/hosts
4546
#
@@ -73,7 +74,7 @@ def is_server_resolvable():
7374
pass
7475

7576
if SIMPLE_SSL:
76-
# Is MongoDB configured with server.pem and ca.pem from
77+
# Is MongoDB configured with server.pem, ca.pem, and crl.pem from
7778
# mongodb jstests/lib?
7879
try:
7980
MongoClient(host, port, connectTimeoutMS=100, ssl=True,
@@ -97,10 +98,16 @@ def test_no_ssl_module(self):
9798
"without it."
9899
)
99100

101+
# Explicit
100102
self.assertRaises(ConfigurationError,
101103
MongoClient, ssl=True)
102104
self.assertRaises(ConfigurationError,
103105
MongoReplicaSetClient, ssl=True)
106+
# Implied
107+
self.assertRaises(ConfigurationError,
108+
MongoClient, ssl_certfile=CLIENT_PEM)
109+
self.assertRaises(ConfigurationError,
110+
MongoReplicaSetClient, ssl_certfile=CLIENT_PEM)
104111

105112

106113
class TestSSL(unittest.TestCase):

0 commit comments

Comments
 (0)