Skip to content

Commit ea7b70c

Browse files
author
Emily Giurleo
authored
RUBY-1387 Add global TLS context hooks (#2123)
1 parent b1b8cc6 commit ea7b70c

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

source/tutorials/ruby-driver-create-client.txt

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ to connect to; if no database name is specified, the client will use the
2929
.. code-block:: ruby
3030

3131
Mongo::Client.new([ '127.0.0.1:27017' ], database: 'mydb')
32-
32+
3333
# Or using the URI syntax:
3434
Mongo::Client.new("mongodb://127.0.0.1:27017/mydb")
3535

@@ -82,7 +82,7 @@ The database can be specified during ``Client`` construction:
8282

8383
# Using Ruby client options:
8484
client = Mongo::Client.new(['localhost'], database: 'mydb')
85-
85+
8686
# Using a MongoDB URI:
8787
client = Mongo::Client.new('mongodb://localhost/mydb')
8888

@@ -92,9 +92,9 @@ new ``Client`` instance configured with the specified database:
9292
.. code-block:: ruby
9393

9494
client = Mongo::Client.new(['localhost'], database: 'mydb')
95-
95+
9696
admin_client = client.use('admin')
97-
97+
9898
# Issue an administrative command
9999
admin_client.database.command(replSetGetConfig: 1).documents.first
100100

@@ -125,7 +125,7 @@ To force all operations to be performed on the designated server, specify the
125125
.. code-block:: ruby
126126

127127
Mongo::Client.new([ '1.2.3.4:27017' ], database: 'mydb', direct_connection: true)
128-
128+
129129
# Or using the URI syntax:
130130
Mongo::Client.new("mongodb://1.2.3.4:27017/mydb?directConnection=true")
131131

@@ -153,7 +153,7 @@ connect to the replica set.
153153

154154
Mongo::Client.new([ '127.0.0.1:27017', '127.0.0.1:27018' ],
155155
:database => 'mydb', replica_set: 'myapp')
156-
156+
157157
# Or using the URI syntax:
158158
Mongo::Client.new("mongodb://127.0.0.1:27017,127.0.0.1:27018/mydb?replicaSet=myapp")
159159

@@ -186,7 +186,7 @@ spread the operation load accordingly.
186186
.. code-block:: ruby
187187

188188
Mongo::Client.new([ '1.2.3.4:27017', '1.2.3.5:27017' ], :database => 'mydb')
189-
189+
190190
Mongo::Client.new("mongodb://1.2.3.4:27017,1.2.3.5:27017/mydb")
191191

192192

@@ -270,7 +270,7 @@ Ruby Options
270270

271271
* - ``:auth_mech_properties``
272272
- Provides additional authentication mechanism properties.
273-
273+
274274
The keys in properties are interpreted case-insensitively.
275275
When the client is created, keys are lowercased.
276276

@@ -436,11 +436,11 @@ Ruby Options
436436
max_staleness: 5,
437437
}
438438
}
439-
439+
440440
If tag sets are provided, they must be an array of hashes. A server
441441
satisfies the read preference if its tags match any one hash in the
442442
provided tag sets.
443-
443+
444444
Each tag set must be a hash, and will be converted internally to
445445
a ``BSON::Document`` instance prior to being used for server selection.
446446
Hash keys can be strings or symbols. The keys are case sensitive.
@@ -480,12 +480,12 @@ Ruby Options
480480
subscriber not receiving some of the SDAM events. The ``:sdam_proc``
481481
option permits adding event subscribers on the client being constructed
482482
before any SDAM events are published.
483-
483+
484484
Pass a ``Proc`` which will be called with the ``Client`` as the argument
485485
after the client's event subscription mechanism has been initialized
486486
but before any of the servers are added to the client. Use this
487487
``Proc`` to set up SDAM event subscribers on the client.
488-
488+
489489
Note: the client is not fully constructed when the ``Proc`` provided in
490490
``:sdam_proc is invoked, in particular the cluster is nil at this time.
491491
``:sdam_proc`` procedure should limit itself to calling
@@ -1090,6 +1090,50 @@ file and both must be stored in the same file. Example usage:
10901090
URI option values must be properly URI escaped. This applies, for example, to
10911091
slashes in the paths.
10921092

1093+
Modifying ``SSLContext``
1094+
------------------------
1095+
It may be desirable to further configure TLS options in the driver, for example
1096+
by enabling or disabling certain ciphers. Currently, the Ruby driver does not
1097+
provide a way to do this when initializing a ``Mongo::Client``.
1098+
1099+
However, the Ruby driver provides a way to set global "TLS context hooks" --
1100+
these are user-provided ``Proc``s that will be invoked before any TLS socket
1101+
connection and can be used to modify the underlying ``OpenSSL::SSL::SSLContext``
1102+
object used by the socket.
1103+
1104+
To set the TLS context hooks, add ``Proc``s to the ``Mongo.tls_context_hooks``
1105+
array. This should be done before creating any Mongo::Client instances.
1106+
For example, in a Rails application this code could be placed in an initializer.
1107+
1108+
.. code-block:: ruby
1109+
1110+
Mongo.tls_context_hooks.push(
1111+
Proc.new { |context|
1112+
context.ciphers = ["AES256-SHA"]
1113+
}
1114+
)
1115+
1116+
# Only the AES256-SHA cipher will be enabled from this point forward
1117+
1118+
Every ``Proc`` in ``Mongo.tls_context_hooks`` will be passed an
1119+
``OpenSSL::SSL::SSLContext`` object as its sole argument. These ``Proc``s will
1120+
be executed sequentially during the creation of every ``Mongo::Socket::SSL`` object.
1121+
1122+
It is possible to assign the entire array of hooks calling ``Mongo.tls_context_hooks=``,
1123+
but doing so will remove any previously assigned hooks. It is recommended to use
1124+
the ``Array#push`` or ``Array#unshift`` methods to add new hooks.
1125+
1126+
It is also possible to remove hooks from ``Mongo.tls_context_hooks`` by storing
1127+
a reference to the ``Proc``s somewhere else in the application, and then using
1128+
``Array#delete_if`` to remove the desired hooks.
1129+
1130+
..warning ::
1131+
1132+
TLS context hooks are global and will affect every instance of ``Mongo::Client``.
1133+
Any library that allows applications to enable these hooks should expose methods to
1134+
modify the hooks (which can be called by the application) rather than
1135+
automatically enabling the hooks when the library is loaded.
1136+
10931137
Further information on configuring MongoDB server for TLS is available in the
10941138
:manual:`MongoDB manual </tutorial/configure-ssl/>`.
10951139

@@ -1162,7 +1206,7 @@ The ``:ssl_ca_cert_string`` option supports specifying only one CA certificate.
11621206
CA certificate options. Doing so would elevate the intermediate certificates
11631207
to the status of root certificates, rather than verifying intermediate
11641208
certificates against the root certificates.
1165-
1209+
11661210
If intermediate certificates need to be used, specify them as part of the
11671211
client or server TLS certificate files.
11681212

@@ -1300,7 +1344,7 @@ Usage with Forking Servers
13001344
==========================
13011345

13021346
.. note::
1303-
1347+
13041348
Applications using Mongoid should follow `Mongoid's forking guidance
13051349
<https://docs.mongodb.com/mongoid/current/tutorials/mongoid-configuration/#usage-with-forking-servers>`_.
13061350
The guidance and sample code below is provided for applications using the
@@ -1316,7 +1360,7 @@ This is because:
13161360
2. File descriptors like network sockets are shared between parent and
13171361
child processes.
13181362

1319-
The driver attempts to detect client use from forked processes and
1363+
The driver attempts to detect client use from forked processes and
13201364
reestablish network connections when such use is detected, alleviating
13211365
the issue of file descriptor sharing.
13221366

0 commit comments

Comments
 (0)