1
- .. _kotlin-sync-enable- tls:
1
+ .. _kotlin-sync-tls:
2
2
3
- ==============================
4
- Enable TLS/SSL on a Connection
5
- ==============================
3
+ ==========================
4
+ Enable TLS on a Connection
5
+ ==========================
6
6
7
7
.. facet::
8
8
:name: genre
@@ -20,52 +20,48 @@ Enable TLS/SSL on a Connection
20
20
Overview
21
21
--------
22
22
23
- In this guide, you can learn how to connect to MongoDB instances with the
24
- `TLS/SSL <https://en.wikipedia.org/wiki/Transport_Layer_Security>`__
25
- security protocol using the underlying TLS/SSL support in the JDK. To
26
- configure your connection to use TLS/SSL, enable the TLS/SSL settings in
27
- either the `ConnectionString <{+core-api+}/com/mongodb/ConnectionString.html>`__
28
- or `MongoClientSettings <{+core-api+}/com/mongodb/MongoClientSettings.html>`__.
23
+ In this guide, you can learn how to use the
24
+ :wikipedia:`TLS <w/index.php?title=Transport_Layer_Security&oldid=1239598620>`
25
+ security protocol when connecting to MongoDB by using the {+driver-short+}.
29
26
30
- .. note:: Debugging TLS/SSL
27
+ .. note:: Debugging TLS
31
28
32
- If you experience trouble setting up your TLS/SSL connection, you can
33
- use the ``-Djavax.net.debug=all`` system property to view more
34
- log statements. See `the Oracle guide to debugging TLS/ SSL connections
29
+ If you experience trouble setting up your TLS connection, you can
30
+ use the ``-Djavax.net.debug=all`` system property to view helpful
31
+ log statements. See `Debugging SSL/TLS connections
35
32
<https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ReadDebug.html>`__
36
- for more information.
33
+ in the Java language documentation for more information.
37
34
38
- .. _tls -enable:
35
+ .. _kotlin-sync-tls -enable:
39
36
40
- Enable TLS/SSL
41
- --------------
37
+ Enable TLS
38
+ ----------
42
39
43
- You can enable TLS/SSL for the connection to your MongoDB instance
44
- in two different ways: through a parameter in your connection string, or
45
- using a method in the ``MongoClientSettings.Builder`` class.
40
+ You can enable TLS on a connection to your MongoDB instance
41
+ in the following ways:
42
+
43
+ - Setting the ``tls`` parameter in your connection string
44
+ - Using the ``enabled()`` method from the ``SslSettings.Builder`` class when creating a
45
+ ``MongoClientSettings`` instance
46
46
47
47
.. note:: DNS Seedlist Protocol Enables TLS
48
48
49
49
If you connect by using the DNS seedlist protocol, indicated by the
50
50
``mongodb+srv`` prefix in your connection string, the driver
51
- automatically enables TLS/SSL. To disable it, set the ``tls``
52
- parameter value to ``false`` in your connection string, or set the
53
- ``enabled`` property to ``false`` in the ``SslSettings.Builder``
54
- block when creating a ``MongoClientSettings`` instance.
51
+ automatically enables TLS.
55
52
56
53
To learn more about connection behavior when you use a DNS seedlist,
57
- see the :manual:`SRV Connection Format </reference/connection-string/#std-label-connections-dns-seedlist >`
58
- section in the Server manual.
54
+ see the :manual:`SRV Connection Format </reference/connection-string/#srv-connection-format >`
55
+ section of the Connection Strings guide in the Server manual.
59
56
60
57
.. tabs::
61
58
62
- .. tab:: ConnectionString
59
+ .. tab:: Connection String
63
60
:tabid: connectionstring
64
61
65
- To enable TLS/SSL on a connection with a `ConnectionString
66
- <{+api+}/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`__, assign the connection string
67
- parameter ``tls`` a value of ``true`` in the connection string passed to
68
- ``MongoClient.create()``:
62
+ To enable TLS on a connection by using a connection string, set the connection string
63
+ parameter ``tls`` to ``true`` in the connection string passed to
64
+ ``MongoClient.create()``, as shown in the following code:
69
65
70
66
.. literalinclude:: /includes/connect/tls.kt
71
67
:start-after: start-tls-connection-string
@@ -77,11 +73,9 @@ using a method in the ``MongoClientSettings.Builder`` class.
77
73
.. tab:: MongoClientSettings
78
74
:tabid: mongoclientsettings
79
75
80
- To configure your ``MongoClient``'s TLS/SSL connection options using the
81
- ``MongoClientSettings.Builder`` class, call the
82
- `applyToSslSettings() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyToSslSettings(com.mongodb.Block)>`__
83
- method. Set the ``enabled`` property to ``true`` in the ``SslSettings.Builder``
84
- block to enable TLS/SSL:
76
+ To enable TLS within a ``MongoClientSettings`` instance, use the
77
+ ``applyToSslSettings()`` builder method. Set the ``enabled`` property to ``true``
78
+ in the ``SslSettings.Builder`` block, as shown in the following code:
85
79
86
80
.. literalinclude:: /includes/connect/tls.kt
87
81
:start-after: start-tls-mongo-client-settings
@@ -95,55 +89,49 @@ using a method in the ``MongoClientSettings.Builder`` class.
95
89
Configure Certificates
96
90
----------------------
97
91
98
- {+language+} applications that initiate TLS/SSL requests require access to
99
- cryptographic certificates that prove identity for the application
100
- itself and other applications with which the application
101
- interacts. You can configure access to these certificates in your application with
102
- the following mechanisms:
103
-
104
- - The JVM trust store and JVM key store
105
- - A client-specific trust store and key store
106
-
107
- .. note::
92
+ {+language+} applications that initiate TLS requests require access to
93
+ cryptographic certificates that prove the application's identity and verify
94
+ other applications with which the {+language+} application interacts. You can configure
95
+ access to these certificates in your application in the following ways:
108
96
109
- The following sections are based on the documentation for Oracle JDK,
110
- so some parts may be inapplicable to your JDK or to the custom TLS/SSL
111
- implementation you use.
97
+ - Using a JVM trust store and JVM key store
98
+ - Using a client-specific trust store and key store
112
99
113
- .. _tls -configure-jvm-truststore:
100
+ .. _kotlin-sync-tls -configure-jvm-truststore:
114
101
115
102
Configure the JVM Trust Store
116
103
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117
104
118
105
.. note::
119
106
120
107
By default, the JRE includes many commonly used public certificates
121
- from signing authorities like `Let's Encrypt
122
- <https://letsencrypt.org/>`__. As a result, you can connect to
123
- instances of :atlas:`MongoDB Atlas </?jmp=docs_driver_kotlin >` ( or any other
108
+ from signing authorities such as `Let's Encrypt
109
+ <https://letsencrypt.org/>`__. As a result, you can connect to a
110
+ :atlas:`MongoDB Atlas </>` instance, or any other
124
111
server whose certificate is signed by an authority in the JRE's default
125
- certificate store) with TLS/SSL without configuring the trust store.
112
+ certificate store, with TLS enabled without configuring the trust store.
126
113
127
114
The JVM trust store saves certificates that securely identify other
128
- applications with which your {+language+} application interacts. Using these
115
+ applications with which your {+language+} application interacts. By using these
129
116
certificates, your application can prove that the connection to another
130
117
application is genuine and secure from tampering by third parties.
131
118
132
119
If your MongoDB instance uses a certificate that is signed by an
133
120
authority that is not present in the JRE's default certificate store,
134
- your application must configure two system properties to initiate
135
- SSL/TLS requests. These properties ensure that your application can
136
- validate the TLS/SSL certificate presented by a connected MongoDB instance.
121
+ your application must configure the following system properties to initiate
122
+ TLS requests.
137
123
138
- - ``javax.net.ssl.trustStore``: the path to a trust store containing the
139
- certificate of the signing authority
124
+ - ``javax.net.ssl.trustStore``: Path to a trust store containing the client's TLS
125
+ certificates
140
126
141
- - ``javax.net.ssl.trustStorePassword``: the password to access the trust
127
+ - ``javax.net.ssl.trustStorePassword``: Password to access the trust
142
128
store defined in ``javax.net.ssl.trustStore``
143
129
144
- You can create a trust store with the `keytool
145
- <https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html>`__
146
- command line tool provided as part of the JDK:
130
+ These properties ensure that your application can
131
+ validate the TLS certificate presented by a connected MongoDB instance.
132
+
133
+ You can create a trust store by using the `keytool <https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html>`__
134
+ command line tool from the JDK as shown in the following terminal command:
147
135
148
136
.. code-block:: console
149
137
@@ -160,53 +148,48 @@ Configure the JVM Key Store
160
148
instance to validate client certificates.
161
149
162
150
The JVM key store saves certificates that securely identify your {+language+}
163
- application to other applications. Using these certificates, other
151
+ application to other applications. By using these certificates, other
164
152
applications can prove that the connection to your application is
165
153
genuine and secure from tampering by third parties.
166
154
167
- An application that initiates TLS/SSL requests needs to set two JVM system
168
- properties to ensure that the client presents a TLS/SSL certificate to
155
+ An application that initiates TLS requests must set the following JVM system
156
+ properties to ensure that the client presents a TLS certificate to
169
157
the MongoDB server:
170
158
171
- - ``javax.net.ssl.keyStore``: the path to a key store containing the client's
159
+ - ``javax.net.ssl.keyStore``: Path to a key store containing the client's
172
160
TLS/SSL certificates
173
161
174
- - ``javax.net.ssl.keyStorePassword``: the password to access the key store
162
+ - ``javax.net.ssl.keyStorePassword``: Password to access the key store
175
163
defined in ``javax.net.ssl.keyStore``
176
164
177
- You can create a key store with the `keytool
165
+ You can create a key store by using the `keytool
178
166
<https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html>`__
179
167
or `openssl <https://www.openssl.org/docs/manmaster/man1/openssl.html>`__
180
168
command line tool.
181
169
182
- For more information on configuring a {+language+} application to use TLS/SSL ,
183
- please see the `JSSE Reference Guide
184
- <https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html>`__ .
170
+ To learn more about configuring a {+language+} application to use TLS,
171
+ see the `JSSE Reference Guide <https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html>`__
172
+ in the Java language documentation .
185
173
186
174
.. _tls-disable-hostname-verification:
187
175
188
176
Configure a Client-Specific Trust Store and Key Store
189
177
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190
178
191
- You can configure a client-specific trust store and key store using the
179
+ You can configure a client-specific trust store and key store by using the
192
180
``init()`` method of the ``SSLContext`` class.
193
181
194
- You can find an example showing how to configure a client with an ``SSLContext``
195
- instance in the
196
- :ref:`Customize TLS/SSL Configuration with an SSLContext section of this guide <tls-custom-sslContext>`.
197
-
198
- For more information on the ``SSLContext`` class, see the API
199
- documentation for `SSL Context <https://docs.oracle.com/en/java/javase/16/docs/api/java.base/javax/net/ssl/SSLContext.html>`__.
182
+ Find an example showing how to configure a client to use an ``SSLContext``
183
+ instance in the :ref:`Customize TLS Configuration with an SSLContext section of this guide <kotlin-sync-tls-custom-sslContext>`.
200
184
201
185
Disable Hostname Verification
202
186
-----------------------------
203
187
204
188
By default, the driver ensures that the hostname included in the server's
205
- TLS/SSL certificates matches the hostnames provided when constructing
189
+ TLS certificates matches the hostnames provided when constructing
206
190
a ``MongoClient``. To disable hostname verification for your
207
- application, you can explicitly disable this by setting the
208
- ``invalidHostNameAllowed`` property of the builder to ``true`` in the
209
- ``applytoSslSettings()`` builder lambda:
191
+ application, set the ``invalidHostNameAllowed`` property of the builder to ``true`` in the
192
+ ``applytoSslSettings()`` builder block:
210
193
211
194
.. literalinclude:: /includes/connect/tls.kt
212
195
:start-after: start-disable-hostname-verification
@@ -217,12 +200,11 @@ application, you can explicitly disable this by setting the
217
200
218
201
.. warning::
219
202
220
- Disabling hostname verification can make your configuration
221
- `insecure <https://tlseminar.github.io/docs/mostdangerous.pdf>`__.
222
- Disable hostname verification only for testing purposes or
223
- when there is no other alternative.
203
+ Disabling hostname verification makes your application insecure and potentially
204
+ vulnerable to expired certificates and foreign processes posing as valid client
205
+ instances.
224
206
225
- .. _tls -restrict-tls-1.2:
207
+ .. _kotlin-sync-tls -restrict-tls-1.2:
226
208
227
209
Restrict Connections to TLS 1.2 Only
228
210
------------------------------------
@@ -237,16 +219,16 @@ To restrict your application to use only the TLS 1.2 protocol, set the
237
219
the TLS 1.2 protocol, upgrade to a later release to connect by using
238
220
TLS 1.2.
239
221
240
- .. _tls -custom-sslContext:
222
+ .. _kotlin-sync-tls -custom-sslContext:
241
223
242
- Customize TLS/SSL Configuration through the Java SE SSLContext
243
- --------------------------------------------------------------
224
+ Customize TLS Configuration through the Java SE SSLContext
225
+ ----------------------------------------------------------
244
226
245
- If your TLS/SSL configuration requires customization, you can
227
+ If your TLS configuration requires customization, you can
246
228
set the ``sslContext`` property of your ``MongoClient`` by
247
229
passing an `SSLContext
248
230
<https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLContext.html>`__
249
- object to the builder in the ``applyToSslSettings()`` lambda :
231
+ object to the ``context()`` method builder in the ``applyToSslSettings()`` block :
250
232
251
233
.. literalinclude:: /includes/connect/tls.kt
252
234
:start-after: start-ssl-context
@@ -255,6 +237,9 @@ object to the builder in the ``applyToSslSettings()`` lambda:
255
237
:copyable:
256
238
:dedent:
257
239
240
+ For more information on the ``SSLContext`` class, see the API
241
+ documentation for `SSL Context <https://docs.oracle.com/en/java/javase/16/docs/api/java.base/javax/net/ssl/SSLContext.html>`__.
242
+
258
243
Online Certificate Status Protocol (OCSP)
259
244
-----------------------------------------
260
245
@@ -263,12 +248,12 @@ revoked. A certificate authority can add an X.509 certificate to the
263
248
Certificate Revocation List (CRL) before the expiry time to invalidate
264
249
the certificate. When a client sends an X.509 certificate during the TLS
265
250
handshake, the CA's revocation server checks the CRL and returns a status
266
- of " good", " revoked" , or " unknown" .
251
+ of `` good``, `` revoked`` , or `` unknown`` .
267
252
268
253
The driver supports the following variations of OCSP:
269
254
270
- - ** Client-Driven OCSP**
271
- - ** OCSP Stapling**
255
+ - Client-Driven OCSP
256
+ - OCSP Stapling
272
257
273
258
The following sections describe the differences between them and how to enable
274
259
them for your application.
@@ -338,3 +323,12 @@ For more information about OCSP, check out the following resources:
338
323
339
324
- Oracle JDK 8 Documentation on `how to enable OCSP for an application <https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ocsp.html>`__
340
325
- :rfc:`Official IETF specification for OCSP (RFC 6960) <6960>`
326
+
327
+ API Documentation
328
+ -----------------
329
+
330
+ For more information about any of the methods or types discussed in this guide,
331
+ see the following API documentation:
332
+
333
+ - `ConnectionString <{+core-api+}/com/mongodb/ConnectionString.html>`__
334
+ - `MongoClientSettings <{+core-api+}/com/mongodb/MongoClientSettings.html>`__
0 commit comments