Skip to content

Commit cc943f1

Browse files
Len Buckensbehackett
authored andcommitted
allow ssl_cert_reqs options to be passed as string
1 parent 2598869 commit cc943f1

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ The following is a list of people who have contributed to
7575
- Can Zhang (cannium)
7676
- Sergey Azovskov (last-g)
7777
- Heewa Barfchin (heewa)
78+
- Len Buckens (buckensl)

pymongo/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def validate_cert_reqs(option, value):
126126
if HAS_SSL:
127127
if value in (ssl.CERT_NONE, ssl.CERT_OPTIONAL, ssl.CERT_REQUIRED):
128128
return value
129+
elif isinstance(value, basestring) and hasattr(ssl, value) and \
130+
getattr(ssl, value) in (ssl.CERT_NONE, ssl.CERT_OPTIONAL, ssl.CERT_REQUIRED):
131+
return getattr(ssl, value)
129132
raise ConfigurationError("The value of %s must be one of: "
130133
"`ssl.CERT_NONE`, `ssl.CERT_OPTIONAL` or "
131134
"`ssl.CERT_REQUIRED" % (option,))

test/test_common.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
from bson.objectid import ObjectId
2828
from bson.son import SON
2929
from pymongo.connection import Connection
30+
from pymongo import common
3031
from pymongo.mongo_client import MongoClient
3132
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
3233
from pymongo.errors import ConfigurationError, OperationFailure
3334
from test import host, port, pair, version, skip_restricted_localhost
3435
from test.utils import catch_warnings, drop_collections
36+
from ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
3537

3638
have_uuid = True
3739
try:
@@ -520,6 +522,20 @@ def test_mongo_replica_set_client(self):
520522
finally:
521523
ctx.exit()
522524

525+
def test_validate_cert_reqs(self):
526+
self.assertRaises(ConfigurationError, common.validate_cert_reqs, 'ssl_cert_reqs', 3)
527+
self.assertRaises(ConfigurationError, common.validate_cert_reqs, 'ssl_cert_reqs', -1)
528+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', None), None)
529+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', CERT_NONE), CERT_NONE)
530+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', CERT_OPTIONAL), CERT_OPTIONAL)
531+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', CERT_REQUIRED), CERT_REQUIRED)
532+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', 0), CERT_NONE)
533+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', 1), CERT_OPTIONAL)
534+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', 2), CERT_REQUIRED)
535+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', 'CERT_NONE'), CERT_NONE)
536+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', 'CERT_OPTIONAL'), CERT_OPTIONAL)
537+
self.assertEqual(common.validate_cert_reqs('ssl_cert_reqs', 'CERT_REQUIRED'), CERT_REQUIRED)
538+
523539

524540
if __name__ == "__main__":
525541
unittest.main()

0 commit comments

Comments
 (0)