Skip to content

Commit b173a6f

Browse files
author
Emily Giurleo
authored
RUBY-1387 TLS context hooks documentation (#4909)
1 parent 20427c4 commit b173a6f

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

source/tutorials/mongoid-configuration.txt

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ the configuration file path as its argument, as follows:
7373

7474
# Use automatically detected environment name
7575
Mongoid.load!("path/to/your/mongoid.yml")
76-
76+
7777
# Specify environment name manually
7878
Mongoid.load!("path/to/your/mongoid.yml", :production)
7979

@@ -97,7 +97,7 @@ current environment - but it does support defining multiple clients.
9797
hosts: ['localhost:27017'],
9898
database: 'my_db',
9999
}
100-
100+
101101
config.log_level = :warn
102102
end
103103

@@ -228,7 +228,7 @@ can be configured.
228228
# The file containing a set of concatenated certification authority certifications
229229
# used to validate certs passed from the other end of the connection.
230230
ssl_ca_cert: /path/to/ca.cert
231-
231+
232232
# Compressors to use. (default is to not use compression)
233233
compressors: [zlib]
234234

@@ -239,21 +239,21 @@ can be configured.
239239
# exceed 128 bytes. It is also used as the database name if the
240240
# database name is not explicitly defined. (default: nil)
241241
app_name: MyApplicationName
242-
242+
243243
# Create indexes in background by default. (default: false)
244244
background_indexing: false
245-
245+
246246
# Mark belongs_to associations as required by default, so that saving a
247247
# model with a missing belongs_to association will trigger a validation
248248
# error. (default: true)
249249
belongs_to_required_by_default: true
250-
250+
251251
# Set the global discriminator key. (default: "_type")
252252
discriminator_key: "_type"
253253

254254
# Raise an exception when a field is redefined. (default: false)
255255
duplicate_fields_exception: false
256-
256+
257257
# Include the root model name in json serialization. (default: false)
258258
include_root_in_json: false
259259

@@ -267,7 +267,7 @@ can be configured.
267267
# Set the Mongoid and Ruby driver log levels when Mongoid is not using
268268
# Ruby on Rails logger instance. (default: :info)
269269
log_level: :info
270-
270+
271271
# Preload all models in development, needed when models use
272272
# inheritance. (default: false)
273273
preload_models: false
@@ -290,7 +290,7 @@ can be configured.
290290
# (default: false)
291291
use_utc: false
292292

293-
The Ruby driver options may be found in
293+
The Ruby driver options may be found in
294294
`the driver documentation <https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-create-client/>`_.
295295

296296
ERb Preprocessing
@@ -351,7 +351,7 @@ to the same logger instance:
351351

352352
Rails.logger === Mongoid.logger
353353
# => true
354-
354+
355355
Mongoid.logger === Mongo::Logger.logger
356356
# => true
357357

@@ -383,7 +383,7 @@ use an initializer as follows:
383383
Mongoid.logger = Logger.new(STDERR).tap do |logger|
384384
logger.level = Logger::DEBUG
385385
end
386-
386+
387387
# Change driver log destination and/or level
388388
Mongo::Logger.logger = Logger.new(STDERR).tap do |logger|
389389
logger.level = Logger::DEBUG
@@ -484,12 +484,12 @@ time zone handling is as follows:
484484

485485
.. code-block:: bash
486486

487-
cp /usr/share/zoneinfo/UTC /etc/localtime
487+
cp /usr/share/zoneinfo/UTC /etc/localtime
488488

489489
2. Set ActiveSupport's time zone to UTC:
490490

491491
.. code-block:: ruby
492-
492+
493493
# If using Rails, in application.rb:
494494
class Application < Rails::Application
495495
config.time_zone = 'UTC'
@@ -525,33 +525,33 @@ Mongoid offers the following time zone-related configuration options:
525525
When parsing times without time zone information (such as when
526526
mongoizing strings or arrays to time), assume the times are specified
527527
in ActiveSupport's time zone. This is the default.
528-
528+
529529
If false, prefer to work with times using Ruby standard library ``Time`` class.
530530
Values in fields of type ``Time`` will be returned as ``Time`` instances.
531531
When parsing times without time zone information, assume the times
532532
are specified in the Ruby time zone.
533-
533+
534534
Note that the ``use_activesupport_time_zone`` setting does not affect
535535
fields of types ``Date`` or ``DateTime``, which use ``Date`` and
536536
``DateTime`` classes for their values, respectively.
537-
537+
538538
Also note that Mongoid may still utilize both ``Time`` and
539539
``ActiveSupport::TimeWithZone`` classes internally, as appropriate,
540540
regardless of the ``use_activesupport_time_zone`` setting.
541-
- ``use_utc``:
541+
- ``use_utc``:
542542
If true, times stored in MongoDB will be returned in UTC.
543543
If false, times stored in MongoDB will be returned in local time
544544
(as instances of either ``Time`` or ``ActiveSupport::TimeWithZone``,
545545
respectively in the Ruby default time zone or the ActiveSupport time zone,
546546
based on the value of ``use_activesupport_time_zone`` setting).
547547
The default is false.
548-
548+
549549
The ``use_utc`` setting does not affect how times are parsed - parsing
550550
is always done in local time when the input being parsed does not
551551
include time zone information. To parse dates in UTC, set the
552552
system/Ruby or ActiveSupport time zone to UTC (as mentioned above,
553553
setting all three to UTC leads to the fewest headaches).
554-
554+
555555
Setting ``use_activesupport_time_zone`` to true and ``Time.zone`` to
556556
UTC (and using ActiveSupport time machinery for all time-related
557557
operations) is recommended over setting ``use_utc`` to true.
@@ -562,6 +562,42 @@ instance does have an associated time zone, and this time zone will be used
562562
even if it is different from ActiveSupport's configured time zone when
563563
``use_activesupport_time_zone`` is true.
564564

565+
Configuring ``SSLContext``
566+
==========================
567+
It may be desirable to further configure TLS options in your application, for
568+
example by enabling or disabling certain ciphers.
569+
570+
This can be done by setting TLS context hooks on the Ruby driver -- TLS context
571+
hooks are user-provided ``Proc``s that will be invoked before any TLS socket
572+
connection in the driver and can be used to modify the underlying
573+
``OpenSSL::SSL::SSLContext`` object used by the socket.
574+
575+
To set TLS context hooks, add ``Proc``s to the ``Mongo.tls_context_hooks``
576+
array. This can be done in an initializer. The example below adds a hook
577+
that only enables the "AES256-SHA" cipher.
578+
579+
.. code-block:: ruby
580+
581+
Mongo.tls_context_hooks.push(
582+
Proc.new { |context|
583+
context.ciphers = ["AES256-SHA"]
584+
}
585+
)
586+
587+
# Only the AES256-SHA cipher will be enabled from this point forward
588+
589+
Every ``Proc`` in ``Mongo.tls_context_hooks`` will be passed an
590+
``OpenSSL::SSL::SSLContext`` object as its sole argument. These ``Proc``s will
591+
be executed sequentially during socket creation.
592+
593+
..warning ::
594+
595+
TLS context hooks are global and will affect all ``Mongo::Client`` instances
596+
in an application.
597+
598+
For more information about TLS context hooks, including best practices for
599+
assigning and removing them, see `the Ruby driver documentation <https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-create-client/#modifying-sslcontext>`_.
600+
565601
Usage with Forking Servers
566602
==========================
567603

0 commit comments

Comments
 (0)