Docs Menu
Docs Home
/ / /
Ruby MongoDB Driver
/

Configure Transport Layer Security (TLS)

On this page

  • Overview
  • Enable TLS
  • Specify Client TLS Certificates
  • TLS Certificate
  • TLS Private Key
  • TLS CA Certificate
  • Modify the TLS Context
  • OCSP Verification
  • Allow Insecure TLS
  • API Documentation

In this guide, you can learn how to use the Transport Layer Security (TLS) protocol to secure your connection to a MongoDB deployment.

To connect to a MongoDB deployment using TLS, you must perform the following steps:

  • Enable a TLS connection in Mongo::Client.

  • Specify the client TLS certificate.

  • Specify the Certificate Authority (CA) certificate to verify the server's TLS 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 CAs is beyond the scope of this documentation. To learn more about TLS, see the Wikipedia entry for Transport Layer Security.

You can enable TLS for the connection to your MongoDB deployment in the following ways:

  • Set the ssl option to true in a new Mongo:Client object.

  • Set the tls option to true in your connection string.

Note

SSL Naming Convention

All MongoDB Server versions supported by the Ruby driver v2.6 and later implement only TLS. 2.6 and do not use SSL.

For historical reasons, the Ruby driver prefixes TLS options with ssl instead of tls. Ruby driver version 3.0 and later will use the tls prefix for Ruby option names.

To configure certificates, you must specify the following options:

  • ssl_cert: The certificate file used to verify the connection to a MongoDB deployment.

  • ssl_key: The private keyfile used to verify the connection to a MongoDB deployment.

  • ssl_ca_cert: The file containing the concatenated CA certificates used to validate certificates passed from the MongoDB deployment to the client. If you don't specify a value for this option, the driver uses the default system root certificate store as the trust anchor.

In the following example, the TLS certificate and corresponding private key are provided in separate files:

client = Mongo::Client.new(["<hostname>:<port>"],
ssl: true,
ssl_cert: 'path/to/client.crt',
ssl_key: 'path/to/client.key',
ssl_ca_cert: 'path/to/ca.crt'
)

You can specify both the TLS certificate and private key in a single file, but both the certificate and private key options must still be specified. In the following example, the TLS certificate and the private key are both defined in the same client.pem file:

client = Mongo::Client.new(["<hostname>:<port>"],
ssl: true,
ssl_cert: 'path/to/client.pem',
ssl_key: 'path/to/client.pem',
ssl_ca_cert: 'path/to/ca.crt',
)

To configure certificates, you must specify the following URI options:

  • tlsCertificateKeyFile: The file that contains the certificate and keyfile used to verify the connection to a MongoDB deployment.

  • tlsCAFile: The file containing the concatenated CA certificates used to validate certificates passed from the MongoDB deployment to the client. If you don't specify a value for this option, the driver uses the default system root certificate store as the trust anchor.

client = Mongo::Client.new(
"mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=path%2fto%2fclient.pem&tlsCAFile=path%2fto%2fca.crt")

The file containing the certificate and key usually has a``.crt`` or .pem extension.

URI option values must be percent-encoded. This applies, for example, to slashes (/) in the paths, which are encoded as %2f.

The Ruby driver provides multiple options for you to specify the TLS certificate, key, and CA certificate with different data or object types.

You can provide one of the following options to specify the TLS certificate:

Option Name
Data/Object Type Accepted
Description

:ssl_cert

String

The certificate file path used to verify the connection to a MongoDB deployment.

:ssl_cert_object

OpenSSL::X509::Certificate

The certificate object used to verify the connection to a MongoDB deployment.

:ssl_cert_string

String

A string containing the PEM-encoded certificate used to verify the connection to a MongoDB deployment.

You can provide one of the following options to specify the TLS private key:

Option Name
Data/Object Type Accepted
Description

:ssl_key

String

The private keyfile path used to verify the connection to a MongoDB deployment.

:ssl_key_object

OpenSSL::PKey

The private key object used to verify the connection to a MongoDB deployment.

:ssl_key_pass_phrase

String

A passphrase for the private key.

:ssl_key_string

String

A string containing the PEM-encoded private key used to verify the connection to a MongoDB deployment.

You can provide one of the following options to specify the TLS CA certificate:

Option Name
Data/Object Type Accepted
Description

:ssl_ca_cert

String

The file path containing concatenated CA certificates used to validate certificates passed from the MongoDB deployment to the client.

:ssl_ca_cert_object

Array<OpenSSL::X509::Certificate>

An array of objects representing the CA certificates used to validate certificates passed from the MongoDB deployment to the client.

:ssl_ca_cert_string

String

A string containing one PEM-encoded CA certificate used to validate certificates passed from the MongoDB deployment to the client.

If your TLS configuration requires customization, you can set TLS context hooks by adding a native Ruby Proc object to the Mongo.tls_context_hooks array. Add the Proc object to the array before you create any Mongo::Client instances.

The following code example enables the AES256-SHA cipher as the only cipher to be used for TLS:

Mongo.tls_context_hooks.push(
Proc.new { |context|
context.ciphers = ["AES256-SHA"]
}
)

The Ruby driver TLS context options are based on native Ruby handling of SSL. To learn more about the TLS context options available, see the Ruby documentation for SSLContext.

If the certificate provided by the server contains an OCSP endpoint URI, the driver issues an Online Certificate Status Protocol (OCSP) request to the specified endpoint by default to verify the validity of the certificate.

To disable the OCSP endpoint check, set the :ssl_verify_ocsp_endpoint Ruby option to false or set the tlsDisableOCSPEndpointCheck URI option to true when creating a client.

Note

JRuby Does Not Support OCSP Verification

Because JRuby does not correctly expose OCSP endpoint URIs, the driver does not check OCSP endpoints when the underlying application runs on JRuby.

When TLS is enabled, the Ruby driver automatically verifies the certificate that the server presents. When testing your code, you can disable this verification. This is known as insecure TLS.

When insecure TLS is enabled, the driver requires only that the server present an X.509 certificate. The driver accepts a certificate even if any of the following are true:

  • The hostname of the server and the subject name (or subject alternative name) on the certificate don't match.

  • The certificate is expired or not yet valid.

  • The certificate doesn't have a trusted root certificate in the chain.

  • The certificate purpose isn't valid for server identification.

Note

Even when insecure TLS is enabled, communication between the client and server is encrypted with TLS.

To enable insecure TLS, set the sslVerify client option or the tlsInsecure URI option to true:

client = Mongo::Client.new(["<hostname>:<port>"],
ssl: true,
ssl_verify: false
)
client = Mongo::Client.new('mongodb://<hostname>:<port>/?tls=true&tlsInsecure=true')

You can similarly set the following options to disable verification for the certificate or hostname:

  • ssl_verify_certificate: Disable certificate validation by setting the option to false.

  • ssl_verify_hostname: Disable hostname verification by setting the option to false.

  • tlsAllowInvalidCertificates: Disable certificate validation by setting the option to true.

  • tlsAllowInvalidHostnames: Disable hostname validation by setting the option to true.

Warning

Don't Use Insecure TLS in Production

Always disable insecure TLS in production.

Enabling insecure TLS in a production environment makes your application insecure and potentially vulnerable to expired certificates and foreign processes posing as valid client instances.

For more information about any of the types and methods discussed in this guide, see the following API documentation:

  • Mongo::Client

  • tls_context_hooks

Back

Connection Option