Skip to content

Commit 4b7cc70

Browse files
committed
Don't mask invalid hostname certificate errors. PYTHON-478
1 parent 9dc5e7a commit 4b7cc70

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

pymongo/pool.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,8 @@ def connect(self, pair):
250250
ca_certs=self.ssl_ca_certs,
251251
cert_reqs=self.ssl_cert_reqs)
252252
if self.ssl_cert_reqs:
253-
try:
254-
match_hostname(sock.getpeercert(), hostname)
255-
except CertificateError, e:
256-
raise ConnectionFailure("SSL certificate validation "
257-
"failed: %s" % e)
253+
match_hostname(sock.getpeercert(), hostname)
254+
258255
except ssl.SSLError:
259256
sock.close()
260257
raise ConnectionFailure("SSL handshake failed. MongoDB may "

test/test_ssl.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def is_server_resolvable():
8787
SERVER_IS_RESOLVABLE = is_server_resolvable()
8888

8989

90-
class TestNoSSLModule(unittest.TestCase):
90+
class TestClientSSL(unittest.TestCase):
9191

9292
def test_no_ssl_module(self):
9393
# Test that ConfigurationError is raised if the ssl
@@ -109,20 +109,6 @@ def test_no_ssl_module(self):
109109
self.assertRaises(ConfigurationError,
110110
MongoReplicaSetClient, ssl_certfile=CLIENT_PEM)
111111

112-
113-
class TestSSL(unittest.TestCase):
114-
115-
def setUp(self):
116-
if not HAS_SSL:
117-
raise SkipTest("The ssl module is not available.")
118-
119-
if sys.version.startswith('3.0'):
120-
raise SkipTest("Python 3.0.x has problems "
121-
"with SSL and socket timeouts.")
122-
123-
if not SIMPLE_SSL:
124-
raise SkipTest("No simple mongod available over SSL")
125-
126112
def test_config_ssl(self):
127113
"""Tests various ssl configurations"""
128114
self.assertRaises(ConfigurationError, MongoClient, ssl='foo')
@@ -187,6 +173,20 @@ def test_config_ssl(self):
187173
ssl_keyfile=CLIENT_PEM,
188174
ssl_certfile=CLIENT_PEM)
189175

176+
177+
class TestSSL(unittest.TestCase):
178+
179+
def setUp(self):
180+
if not HAS_SSL:
181+
raise SkipTest("The ssl module is not available.")
182+
183+
if sys.version.startswith('3.0'):
184+
raise SkipTest("Python 3.0.x has problems "
185+
"with SSL and socket timeouts.")
186+
187+
if not SIMPLE_SSL:
188+
raise SkipTest("No simple mongod available over SSL")
189+
190190
def test_simple_ssl(self):
191191
# Expects the server to be running with ssl and with
192192
# no --sslPEMKeyFile or with --sslWeakCertificateValidation
@@ -279,6 +279,10 @@ def test_cert_ssl_validation(self):
279279
ssl_ca_certs=CA_PEM)
280280
response = client.admin.command('ismaster')
281281
if 'setName' in response:
282+
if response['primary'].split(":")[0] != 'server':
283+
raise SkipTest("No hosts in the replicaset for 'server'. "
284+
"Cannot validate hostname in the certificate")
285+
282286
client = MongoReplicaSetClient('server',
283287
replicaSet=response['setName'],
284288
w=len(response['hosts']),
@@ -314,8 +318,13 @@ def test_cert_ssl_validation_optional(self):
314318
ssl_certfile=CLIENT_PEM,
315319
ssl_cert_reqs=ssl.CERT_OPTIONAL,
316320
ssl_ca_certs=CA_PEM)
321+
317322
response = client.admin.command('ismaster')
318323
if 'setName' in response:
324+
if response['primary'].split(":")[0] != 'server':
325+
raise SkipTest("No hosts in the replicaset for 'server'. "
326+
"Cannot validate hostname in the certificate")
327+
319328
client = MongoReplicaSetClient('server',
320329
replicaSet=response['setName'],
321330
w=len(response['hosts']),

0 commit comments

Comments
 (0)