Configure Transport Layer Security (TLS)
On this page
- Overview
- Enable TLS
- Configure Certificates
- Configure the JVM Trust Store
- Configure the JVM Key Store
- Configure a Client-Specific Trust Store and Key Store
- Disable Hostname Verification
- Restrict Connections to TLS 1.2
- Customize Configuration with SSLContext
- Online Certificate Status Protocol (OCSP)
- Client-Driven OCSP
- OCSP Stapling
- API Documentation
Overview
In this guide, you can learn how to use the Transport Layer Security (TLS) protocol to secure your connection to a MongoDB deployment.
When you enable TLS for a connection, the Scala driver performs the following actions:
Uses TLS to connect to the MongoDB deployment
Verifies the deployment's certificate
To learn how to configure your MongoDB deployment for TLS, see the TLS configuration guide in the MongoDB Server manual.
Note
This page assumes prior knowledge of TLS/SSL and access to valid certificates. A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, and Certificate Authorities (CAs) is beyond the scope of this documentation. To learn more about TLS, see the Wikipedia entry for Transport Layer Security.
Enable TLS
You can enable TLS for the connection to your MongoDB deployment in the following ways:
Use the
enabled()
method from theSslSettings.Builder
class when creating aMongoClientSettings
instanceSet the
tls
parameter in your connection URI
Select the MongoClientSettings or Connection URI tab to see corresponding code that enables TLS:
val tlsUri = "mongodb://localhost:27017/" val tlsSettings = MongoClientSettings.builder() .applyConnectionString(ConnectionString(tlsUri)) .applyToSslSettings(builder => builder.enabled(true)) .build() val tlsClient1 = MongoClient(tlsSettings)
val tlsClient2 = MongoClient("mongodb://localhost:27017/?tls=true")
Tip
If your connection string includes the +srv
modification, which specifies the
SRV connection format, TLS is enabled on your connection by default.
To learn more about the SRV connection format, see SRV Connection Format in the MongoDB Server documentation.
Configure Certificates
Note
The instructions in these sections are based on the documentation for Oracle JDK. They might not apply to your JDK or to your custom TLS/SSL implementation.
Scala applications that initiate TLS requests require access to cryptographic certificates that prove the application's identity and verify other applications with which the Scala application interacts. You can configure access to these certificates in your application in the following ways:
Use a JVM trust store and JVM key store
Use a client-specific trust store and key store
Configure the JVM Trust Store
The JVM trust store saves certificates that securely identify other applications with which your Scala application interacts. By using these certificates, your application can prove that the connection to another application is genuine and secure from tampering by third parties.
The Java Runtime Environment (JRE) provides a default certificate store, which includes commonly used public certificates from signing authorities. If your MongoDB deployment uses a certificate signed by an authority that is not present in the JRE's default certificate store, your application must configure the following system properties to initiate TLS requests:
javax.net.ssl.trustStore
: The path to a trust store containing the certificate of the signing authorityjavax.net.ssl.trustStorePassword
: The password to access the trust store defined by thejavax.net.ssl.trustStore
property
You can use the keytool
command line tool to define the preceding properties. The following example runs the keytool
command to specify the certificate authority file path, the trust store path, and the trust store password:
keytool -importcert -trustcacerts -file <path to certificate authority file> -keystore <path to trust store> -storepass <trust store password>
Configure the JVM Key Store
Note
By default, MongoDB instances do not perform client certificate validation. You must configure the key store if you configured your MongoDB instance to validate client certificates.
An application that initiates TLS requests must set the following JVM system properties to ensure that the client presents a TLS certificate to the MongoDB server:
javax.net.ssl.keyStore
: The path to a key store containing the client's TLS/SSL certificatesjavax.net.ssl.keyStorePassword
: The password to access the key store defined injavax.net.ssl.keyStore
You can create a key store by using the keytool or openssl command line tool.
To learn more about configuring a Scala application to use TLS, see the JSSE Reference Guide in the Java language documentation.
Configure a Client-Specific Trust Store and Key Store
You can configure a client-specific trust store and key store by using the
init()
method of the SSLContext
class.
To view an example that configures a client to use an SSLContext
instance, see the Customize Configuration with SSLContext section of this guide.
Disable Hostname Verification
By default, the driver ensures that the hostname included in the server's
TLS certificates matches the hostnames provided when constructing
a MongoClient
. You can disable hostname verification in the following
ways:
Use the
invalidHostNameAllowed()
method from theSslSettings.Builder
class when creating aMongoClientSettings
instanceSet the
tlsAllowInvalidHostnames
parameter in your connection URI
Select the MongoClientSettings or Connection URI tab to see corresponding code that disables hostname verification:
val invalidHostUri = "mongodb://localhost:27017/" val invalidHostSettings = MongoClientSettings.builder() .applyConnectionString(ConnectionString(invalidHostUri)) .applyToSslSettings(builder => builder .enabled(true) .invalidHostNameAllowed(true) ) .build() val invalidHostClient1 = MongoClient(invalidHostSettings)
val invalidHostClient2 = MongoClient("mongodb://localhost:27017/?tls=true&tlsAllowInvalidHostnames=true")
Warning
Disabling hostname verification makes your application insecure and potentially vulnerable to expired certificates and foreign processes posing as valid client instances.
Restrict Connections to TLS 1.2
To restrict your application to use only the TLS 1.2 protocol, set the
jdk.tls.client.protocols
system property to "TLSv1.2"
.
Note
Java Runtime Environments (JREs) before Java 8 enabled only the TLS 1.2 protocol in update releases. If your JRE has not enabled the TLS 1.2 protocol, upgrade to a later release to use TLS 1.2.
Customize Configuration with SSLContext
If your TLS configuration requires customization, you can
set the sslContext
property of your MongoClient
object. Pass
an SSLContext
object to the context()
method builder in the
applyToSslSettings()
block, as shown in the following code:
val sslContext = SSLContext.getDefault() val contextSettings = MongoClientSettings.builder() .applyToSslSettings(builder => builder .enabled(true) .context(sslContext) ) .build() val mongoClient = MongoClient(contextSettings);
For more information on the SSLContext
class, see the API
documentation for SSL Context.
Online Certificate Status Protocol (OCSP)
OCSP is a standard used to check whether X.509 certificates have been
revoked. A certificate authority (CA) can add an X.509 certificate to the
Certificate Revocation List (CRL) before the expiry time to invalidate
the certificate. When a client sends an X.509 certificate during the TLS
handshake, the CA's revocation server checks the CRL and returns a status
of good
, revoked
, or unknown
.
The driver supports the following variations of OCSP:
Client-Driven OCSP
OCSP Stapling
The following sections describe these variations and show how to enable them for your application.
Note
The Scala driver uses the JVM arguments configured for the application,
which cannot be overridden for a specific MongoClient
instance.
Client-Driven OCSP
In client-driven OCSP, the client receives the certificate from the server and sends this certificate in an OCSP request to an OCSP responder. The OCSP responder checks the status of the certificate with a CA and sends a report about its validity to the client.
To enable client-driven OCSP for your application, set the following JVM system properties:
Property | Description |
---|---|
| Set this property to |
| Set this property to |
Warning
If the OCSP responder is unavailable, the TLS support provided by the JDK reports a "hard fail". This differs from the "soft fail" behavior of the MongoDB Shell and some other drivers.
OCSP Stapling
OCSP stapling is a mechanism in which the server must obtain the signed certificate from the CA and include it in a time-stamped OCSP response to the client.
To enable OCSP stapling for your application, set the following JVM system properties:
Property | Description |
---|---|
| Set this property to |
| Set this property to true to enable OCSP stapling.If unset or set to false , the connection can proceed regardless of the presence or status of the certificate revocation response. |
To learn more about OCSP, view the following resources:
Client-Driven OCSP and OCSP Stapling in the Oracle JDK 8 documentation
API Documentation
For more information about any of the types discussed in this guide, see the following API documentation: