Skip to content

Commit c3c62ad

Browse files
committed
PYTHON-2396 Deprecate ssl_keyfile and ssl_certfile URI options (#616)
(cherry picked from commit 6e1009e)
1 parent fdbe38f commit c3c62ad

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

doc/examples/tls.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,31 @@ PyMongo can be configured to present a client certificate using the
153153
... tls=True,
154154
... tlsCertificateKeyFile='/path/to/client.pem')
155155

156-
If the private key for the client certificate is stored in a separate file use
157-
the ``ssl_keyfile`` option::
156+
If the private key for the client certificate is stored in a separate file,
157+
it should be concatenated with the certificate file. For example, to
158+
concatenate a PEM-formatted certificate file ``cert.pem`` and a PEM-formatted
159+
keyfile ``key.pem`` into a single file ``combined.pem``, on Unix systems,
160+
users can run::
161+
162+
$ cat key.pem cert.pem > combined.pem
163+
164+
PyMongo can be configured with the concatenated certificate keyfile using the
165+
``tlsCertificateKeyFile`` option::
158166

159167
>>> client = pymongo.MongoClient('example.com',
160168
... tls=True,
161-
... tlsCertificateKeyFile='/path/to/client.pem',
162-
... ssl_keyfile='/path/to/key.pem')
169+
... tlsCertificateKeyFile='/path/to/combined.pem')
163170

164-
Python 2.7.9+ (pypy 2.5.1+) and 3.3+ support providing a password or passphrase
165-
to decrypt encrypted private keys. Use the ``tlsCertificateKeyFilePassword``
166-
option::
171+
If the private key contained in the certificate keyfile is encrypted,
172+
Python 2.7.9+ (pypy 2.5.1+) and 3.3+ support providing a password or
173+
passphrase to decrypt the encrypted private key. The password/passphrase
174+
can be specified using the ``tlsCertificateKeyFilePassword`` option::
167175

168176
>>> client = pymongo.MongoClient('example.com',
169177
... tls=True,
170-
... tlsCertificateKeyFile='/path/to/client.pem',
171-
... ssl_keyfile='/path/to/key.pem',
178+
... tlsCertificateKeyFile='/path/to/combined.pem',
172179
... tlsCertificateKeyFilePassword=<passphrase>)
173180

174-
175181
These options can also be passed as part of the MongoDB URI.
176182

177183
.. _OCSP:

pymongo/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,14 @@ def validate_tzinfo(dummy, value):
711711
'ssl_match_hostname': ('renamed', 'tlsAllowInvalidHostnames'),
712712
'ssl_crlfile': ('renamed', 'tlsCRLFile'),
713713
'ssl_ca_certs': ('renamed', 'tlsCAFile'),
714+
'ssl_certfile': ('removed', (
715+
'Instead of using ssl_certfile to specify the certificate file, '
716+
'use tlsCertificateKeyFile to pass a single file containing both '
717+
'the client certificate and the private key')),
718+
'ssl_keyfile': ('removed', (
719+
'Instead of using ssl_keyfile to specify the private keyfile, '
720+
'use tlsCertificateKeyFile to pass a single file containing both '
721+
'the client certificate and the private key')),
714722
'ssl_pem_passphrase': ('renamed', 'tlsCertificateKeyFilePassword'),
715723
'waitqueuemultiple': ('removed', (
716724
'Instead of using waitQueueMultiple to bound queuing, limit the size '

pymongo/mongo_client.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -458,28 +458,18 @@ def __init__(
458458
certificates passed from the other end of the connection.
459459
Implies ``tls=True``. Defaults to ``None``.
460460
- `tlsCertificateKeyFile`: A file containing the client certificate
461-
and private key. If you want to pass the certificate and private
462-
key as separate files, use the ``ssl_certfile`` and ``ssl_keyfile``
463-
options instead. Implies ``tls=True``. Defaults to ``None``.
461+
and private key. Implies ``tls=True``. Defaults to ``None``.
464462
- `tlsCRLFile`: A file containing a PEM or DER formatted
465463
certificate revocation list. Only supported by python 2.7.9+
466464
(pypy 2.5.1+). Implies ``tls=True``. Defaults to ``None``.
467465
- `tlsCertificateKeyFilePassword`: The password or passphrase for
468-
decrypting the private key in ``tlsCertificateKeyFile`` or
469-
``ssl_keyfile``. Only necessary if the private key is encrypted.
470-
Only supported by python 2.7.9+ (pypy 2.5.1+) and 3.3+. Defaults
471-
to ``None``.
466+
decrypting the private key in ``tlsCertificateKeyFile``. Only
467+
necessary if the private key is encrypted. Only supported by
468+
python 2.7.9+ (pypy 2.5.1+) and 3.3+. Defaults to ``None``.
472469
- `tlsDisableOCSPEndpointCheck`: (boolean) If ``True``, disables
473470
certificate revocation status checking via the OCSP responder
474471
specified on the server certificate. Defaults to ``False``.
475472
- `ssl`: (boolean) Alias for ``tls``.
476-
- `ssl_certfile`: The certificate file used to identify the local
477-
connection against mongod. Implies ``tls=True``. Defaults to
478-
``None``.
479-
- `ssl_keyfile`: The private keyfile used to identify the local
480-
connection against mongod. Can be omitted if the keyfile is
481-
included with the ``tlsCertificateKeyFile``. Implies ``tls=True``.
482-
Defaults to ``None``.
483473
484474
| **Read Concern options:**
485475
| (If not set explicitly, this will use the server default)
@@ -520,6 +510,10 @@ def __init__(
520510
521511
.. versionchanged:: 3.12
522512
Added the ``server_api`` keyword argument.
513+
The following keyword arguments were deprecated:
514+
515+
- ``ssl_certfile`` and ``ssl_keyfile`` were deprecated in favor
516+
of ``tlsCertificateKeyFile``.
523517
524518
.. versionchanged:: 3.11
525519
Added the following keyword arguments and URI options:

test/test_ssl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from test.utils import (EventListener,
4343
cat_files,
4444
connected,
45+
ignore_deprecations,
4546
remove_all_users)
4647

4748

@@ -99,6 +100,7 @@ def test_no_ssl_module(self):
99100
MongoClient, ssl_certfile=CLIENT_PEM)
100101

101102
@unittest.skipUnless(HAVE_SSL, "The ssl module is not available.")
103+
@ignore_deprecations
102104
def test_config_ssl(self):
103105
# Tests various ssl configurations
104106
self.assertRaises(ValueError, MongoClient, ssl='foo')
@@ -202,6 +204,7 @@ def test_simple_ssl(self):
202204
self.assertClientWorks(self.client)
203205

204206
@client_context.require_ssl_certfile
207+
@ignore_deprecations
205208
def test_ssl_pem_passphrase(self):
206209
# Expects the server to be running with server.pem and ca.pem
207210
#
@@ -234,6 +237,7 @@ def test_ssl_pem_passphrase(self):
234237

235238
@client_context.require_ssl_certfile
236239
@client_context.require_no_auth
240+
@ignore_deprecations
237241
def test_cert_ssl_implicitly_set(self):
238242
# Expects the server to be running with server.pem and ca.pem
239243
#
@@ -257,6 +261,7 @@ def test_cert_ssl_implicitly_set(self):
257261

258262
@client_context.require_ssl_certfile
259263
@client_context.require_no_auth
264+
@ignore_deprecations
260265
def test_cert_ssl_validation(self):
261266
# Expects the server to be running with server.pem and ca.pem
262267
#
@@ -294,6 +299,7 @@ def test_cert_ssl_validation(self):
294299

295300
@client_context.require_ssl_certfile
296301
@client_context.require_no_auth
302+
@ignore_deprecations
297303
def test_cert_ssl_uri_support(self):
298304
# Expects the server to be running with server.pem and ca.pem
299305
#
@@ -307,6 +313,7 @@ def test_cert_ssl_uri_support(self):
307313

308314
@client_context.require_ssl_certfile
309315
@client_context.require_no_auth
316+
@ignore_deprecations
310317
def test_cert_ssl_validation_optional(self):
311318
# Expects the server to be running with server.pem and ca.pem
312319
#
@@ -337,6 +344,7 @@ def test_cert_ssl_validation_optional(self):
337344

338345
@client_context.require_ssl_certfile
339346
@client_context.require_server_resolvable
347+
@ignore_deprecations
340348
def test_cert_ssl_validation_hostname_matching(self):
341349
# Expects the server to be running with server.pem and ca.pem
342350
#
@@ -404,6 +412,7 @@ def test_cert_ssl_validation_hostname_matching(self):
404412
**self.credentials))
405413

406414
@client_context.require_ssl_certfile
415+
@ignore_deprecations
407416
def test_ssl_crlfile_support(self):
408417
if not hasattr(ssl, 'VERIFY_CRL_CHECK_LEAF') or _ssl.IS_PYOPENSSL:
409418
self.assertRaises(
@@ -442,6 +451,7 @@ def test_ssl_crlfile_support(self):
442451

443452
@client_context.require_ssl_certfile
444453
@client_context.require_server_resolvable
454+
@ignore_deprecations
445455
def test_validation_with_system_ca_certs(self):
446456
# Expects the server to be running with server.pem and ca.pem.
447457
#
@@ -559,6 +569,7 @@ def test_wincertstore(self):
559569

560570
@client_context.require_auth
561571
@client_context.require_ssl_certfile
572+
@ignore_deprecations
562573
def test_mongodb_x509_auth(self):
563574
host, port = client_context.host, client_context.port
564575
ssl_client = MongoClient(
@@ -667,6 +678,7 @@ def test_mongodb_x509_auth(self):
667678
self.fail("Invalid certificate accepted.")
668679

669680
@client_context.require_ssl_certfile
681+
@ignore_deprecations
670682
def test_connect_with_ca_bundle(self):
671683
def remove(path):
672684
try:

0 commit comments

Comments
 (0)