@@ -437,6 +437,56 @@ def test_cert_ssl_validation_hostname_matching(self):
437437 ssl_match_hostname = False ,
438438 serverSelectionTimeoutMS = 100 ))
439439
440+ def test_validation_with_system_ca_certs (self ):
441+ # Expects the server to be running with the server.pem, ca.pem
442+ # and crl.pem provided in mongodb and the server tests eg:
443+ #
444+ # --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
445+ # --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
446+ # --sslCRLFile=/path/to/pymongo/test/certificates/crl.pem
447+ # --sslWeakCertificateValidation
448+ #
449+ # Also requires an /etc/hosts entry where "server" is resolvable
450+ if not CERT_SSL :
451+ raise SkipTest ("No mongod available over SSL with certs" )
452+
453+ if not SERVER_IS_RESOLVABLE :
454+ raise SkipTest ("No hosts entry for 'server'. Cannot validate "
455+ "hostname in the certificate" )
456+
457+ if sys .version_info < (2 , 7 , 9 ):
458+ raise SkipTest ("SSLContext not available." )
459+
460+ if (sys .platform == "win32"
461+ and sys .version_info [0 ] == 3 and sys .version_info < (3 , 4 )):
462+ raise SkipTest (
463+ "Python 3 can't load Windows system certs before 3.4" )
464+
465+ os .environ ['SSL_CERT_FILE' ] = CA_PEM
466+ try :
467+ with self .assertRaises (ConnectionFailure ):
468+ # Server cert is verified but hostname matching fails
469+ connected (MongoClient (pair ,
470+ ssl = True ,
471+ serverSelectionTimeoutMS = 100 ))
472+
473+ # Server cert is verified. Disable hostname matching.
474+ connected (MongoClient (pair ,
475+ ssl = True ,
476+ ssl_match_hostname = False ,
477+ serverSelectionTimeoutMS = 100 ))
478+
479+ # Server cert and hostname are verified.
480+ connected (MongoClient ('server' ,
481+ ssl = True ,
482+ serverSelectionTimeoutMS = 100 ))
483+
484+ # Server cert and hostname are verified.
485+ connected (
486+ MongoClient (
487+ 'mongodb://server/?ssl=true&serverSelectionTimeoutMS=100' ))
488+ finally :
489+ os .environ .pop ('SSL_CERT_FILE' )
440490
441491 def test_mongodb_x509_auth (self ):
442492 # Expects the server to be running with the server.pem, ca.pem
0 commit comments