Skip to content

Commit fb8128b

Browse files
committed
DOCSP-48679: strongly recommend Netty
1 parent 2ddcc6d commit fb8128b

File tree

4 files changed

+88
-18
lines changed

4 files changed

+88
-18
lines changed

examples/src/test/kotlin/TlsTest.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
21
import com.mongodb.ConnectionString
32
import com.mongodb.MongoClientSettings
3+
import com.mongodb.connection.SslSettings
4+
import com.mongodb.connection.TransportSettings
45
import com.mongodb.kotlin.client.coroutine.MongoClient
56
import config.getConfig
67
import io.netty.handler.ssl.SslContextBuilder
@@ -12,6 +13,7 @@ import org.junit.jupiter.api.TestInstance
1213
import javax.net.ssl.SSLContext
1314
import kotlin.test.assertEquals
1415

16+
1517
// :replace-start: {
1618
// "terms": {
1719
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string>\""
@@ -67,6 +69,28 @@ internal class TlsTest {
6769
assertEquals(true, settings.sslSettings.isEnabled)
6870
}
6971

72+
@Test
73+
fun nettyTlsConfigurationTest() = runBlocking {
74+
// :snippet-start: netty-tls-configuration
75+
val sslContext = SslContextBuilder.forClient()
76+
.sslProvider(SslProvider.OPENSSL)
77+
.build()
78+
79+
val settings = MongoClientSettings.builder()
80+
.applyToSslSettings { builder: SslSettings.Builder -> builder.enabled(true) }
81+
.transportSettings(
82+
TransportSettings.nettyBuilder()
83+
.sslContext(sslContext)
84+
.build()
85+
)
86+
.build()
87+
88+
val mongoClient = MongoClient.create(settings);
89+
// :snippet-end:
90+
mongoClient.close()
91+
assertEquals(true, settings.sslSettings.isEnabled)
92+
}
93+
7094
@Test
7195
fun customTlsConfigurationTest() = runBlocking {
7296
// :snippet-start: custom-tls-configuration

snooty.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ core-api = "{+api-root+}/mongodb-driver-core/com/mongodb"
3232
driver-api = "{+api-root+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine"
3333
stable-api = "Stable API"
3434
mongocrypt-version = "{+full-version+}"
35-
nettyVersion = "io.netty:netty-all:4.1.79.Final"
3635
snappyVersion = "org.xerial.snappy:snappy-java:1.1.8.4"
3736
zstdVersion = "com.github.luben:zstd-jni:1.5.5-2"
3837
logbackVersion = "1.2.11"
3938
log4j2Version = "2.17.1"
4039
serializationVersion = "1.6.0"
4140
kotlinx-dt-version = "0.6.1"
41+
netty-version = "io.netty:netty-all:4.1.87.Final"
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
val sslContext = SslContextBuilder.forClient()
22
.sslProvider(SslProvider.OPENSSL)
33
.build()
4+
45
val settings = MongoClientSettings.builder()
5-
.applyToSslSettings { builder -> builder.enabled(true) }
6-
.streamFactoryFactory(
7-
NettyStreamFactoryFactory.builder()
6+
.applyToSslSettings { builder: SslSettings.Builder -> builder.enabled(true) }
7+
.transportSettings(
8+
TransportSettings.nettyBuilder()
89
.sslContext(sslContext)
910
.build()
1011
)
1112
.build()
12-
val mongoClient = MongoClient.create(settings)
13+
14+
val mongoClient = MongoClient.create(settings);

source/fundamentals/connection/tls.txt

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ Enable TLS/SSL on a Connection
2020
Overview
2121
--------
2222

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+}/ConnectionString.html>`__
28-
or `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__.
23+
In this guide, you can learn how to use the **TLS protocol** to secure your
24+
connection to a MongoDB deployment. TLS is a cryptographic protocol that
25+
secures communication between your application and MongoDB. To configure
26+
your connection to use TLS, enable the TLS option and provide your
27+
certificates for validation when creating a client.
2928

30-
.. note:: Debugging TLS/SSL
29+
By default, the driver supports TLS/SSL connections to MongoDB
30+
servers using the underlying support for TLS/SSL provided by the JDK.
31+
This can be changed either by using the `Netty API
32+
<https://netty.io/4.1/api/>`__ or the extensibility of the `Java SE
33+
API <https://docs.oracle.com/javase/8/docs/api/>`__.
3134

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
35-
<https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ReadDebug.html>`__
36-
for more information.
35+
.. tip:: Prefer Netty for Asynchronous Apps
36+
37+
We recommend using Netty for asychronous applications because it supports
38+
asynchronous I/O and handles high connection volumes effectively. To
39+
learn about using Netty to configure your TLS settings, see the
40+
:ref:`kotlin-tls-netty-sslContext` section of this guide.
3741

3842
.. _tls-enable:
3943

@@ -82,6 +86,14 @@ using a method in the ``MongoClientSettings.Builder`` class.
8286
.. literalinclude:: /examples/generated/TlsTest.snippet.tls-mongoclient-settings.kt
8387
:language: kotlin
8488

89+
.. note:: Debugging TLS/SSL
90+
91+
If you experience trouble setting up your TLS/SSL connection, you can
92+
use the ``-Djavax.net.debug=all`` system property to view more
93+
log statements. See `the Oracle guide to debugging TLS/SSL connections
94+
<https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ReadDebug.html>`__
95+
for more information.
96+
8597
.. _tls_configure-certificates:
8698

8799
Configure Certificates
@@ -225,6 +237,38 @@ To restrict your application to use only the TLS 1.2 protocol, set the
225237
the TLS 1.2 protocol, upgrade to a later release to connect by using
226238
TLS 1.2.
227239

240+
.. _kotlin-tls-netty-sslContext:
241+
242+
Configure TLS/SSL by Using Netty SslContext
243+
-------------------------------------------
244+
245+
Include the following import statements:
246+
247+
.. code-block:: kotlin
248+
:copyable: true
249+
250+
import com.mongodb.MongoClientSettings
251+
import com.mongodb.connection.SslSettings
252+
import com.mongodb.connection.TransportSettings
253+
import com.mongodb.kotlin.client.coroutine.MongoClient
254+
import io.netty.handler.ssl.SslContextBuilder
255+
import io.netty.handler.ssl.SslProvider
256+
257+
.. note:: Netty Package Version
258+
259+
The driver tests with Netty package version ``{+netty-version+}``
260+
261+
To instruct the driver to use
262+
`io.netty.handler.ssl.SslContext <https://netty.io/4.1/api/io/netty/handler/ssl/SslContext.html>`__,
263+
configure `NettyTransportSettings <{+core-api+}/connection/NettyTransportSettings.html>`__
264+
when you define your ``MongoClientSettings``.
265+
266+
Use ``MongoClientSettings.Builder.transportSettings()``
267+
and ``NettyTransportSettings.Builder.sslContext()`` to build your settings:
268+
269+
.. literalinclude:: /examples/generated/TlsTest.snippet.netty-tls-configuration.kt
270+
:language: kotlin
271+
228272
.. _tls-custom-sslContext:
229273

230274
Customize TLS/SSL Configuration through the Java SE SSLContext

0 commit comments

Comments
 (0)