Skip to content

Commit de04efe

Browse files
p-mongop
authored andcommitted
RUBY-1794 Verify that driver can connect to server with full TLS verification (#1380)
* RUBY-1794 documentation for running tests with TLS verification * RUBY-1794 refactor TLS option handling
1 parent 9b7e100 commit de04efe

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

lib/mongo/server/connectable.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ module Connectable
2323
# The ssl option prefix.
2424
#
2525
# @since 2.1.0
26+
# @deprecated
2627
SSL = 'ssl'.freeze
2728

2829
# The default time in seconds to timeout an operation executed on a socket.
2930
#
3031
# @since 2.0.0
3132
#
32-
# @deprecated Timeouts on Ruby sockets aren't effective so this default option is
33-
# no longer used.
34-
# Will be removed in driver version 3.0.
33+
# @deprecated Timeouts on Ruby sockets aren't effective so this default
34+
# option is no longer used. Will be removed in driver version 3.0.
3535
TIMEOUT = 5.freeze
3636

3737
# @return [ Integer ] pid The process id when the connection was created.
@@ -69,7 +69,11 @@ def connected?
6969
private
7070

7171
def ssl_options
72-
@ssl_options[:ssl] == true ? @ssl_options : {}
72+
@ssl_options ||= if options[:ssl]
73+
options.select { |k, v| k.to_s.start_with?('ssl') }
74+
else
75+
{}
76+
end.freeze
7377
end
7478

7579
def ensure_connected

lib/mongo/server/connection.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def initialize(server, options = {})
9393
@monitoring = server.monitoring
9494
@options = options.freeze
9595
@server = server
96-
@ssl_options = options.select { |k, v| k.to_s.start_with?(SSL) }.freeze
9796
@socket = nil
9897
@last_checkin = nil
9998
@auth_mechanism = nil

lib/mongo/server/monitor/connection.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def initialize(address, options = {})
114114
@address = address
115115
@options = options.freeze
116116
@app_metadata = options[:app_metadata]
117-
@ssl_options = options.reject { |k, v| !k.to_s.start_with?(SSL) }
118117
@socket = nil
119118
@pid = Process.pid
120119
@compressor = nil

lib/mongo/socket/ssl.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,16 @@ def create_context(options)
149149
end
150150

151151
def set_cert(context, options)
152+
# Since we clear cert_text during processing, we need to examine
153+
# ssl_cert_object here to avoid considering it if we have also
154+
# processed the text.
152155
if options[:ssl_cert]
153156
cert_text = File.read(options[:ssl_cert])
157+
cert_object = nil
158+
elsif cert_text = options[:ssl_cert_string]
159+
cert_object = nil
154160
else
155-
cert_text = options[:ssl_cert_string]
161+
cert_object = options[:ssl_cert_object]
156162
end
157163

158164
# The client certificate may be a single certificate or a bundle
@@ -185,8 +191,8 @@ def set_cert(context, options)
185191

186192
if cert_text
187193
context.cert = OpenSSL::X509::Certificate.new(cert_text)
188-
elsif options[:ssl_cert_object]
189-
context.cert = options[:ssl_cert_object]
194+
elsif cert_object
195+
context.cert = cert_object
190196
end
191197
end
192198

spec/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,40 @@ cluster topology.
8787
## TLS With Verification
8888

8989
The test suite includes a set of TLS certificates for configuring a server
90-
and a client to perform full TLS verification. The server can be started as
91-
follows, if the current directory is the top of the driver source tree:
90+
and a client to perform full TLS verification in the `spec/support/certificates`
91+
directory. The server can be started as follows, if the current directory is
92+
the top of the driver source tree:
9293

9394
mlaunch init --single --dir /tmp/mdb-ssl --sslMode requireSSL \
9495
--sslPEMKeyFile `pwd`/spec/support/certificates/server.pem \
9596
--sslCAFile `pwd`/spec/support/certificates/ca.pem \
9697
--sslClientCertificate `pwd`/spec/support/certificates/client.pem
9798

99+
To test that the driver works when the server's certificate is signed by an
100+
intermediate certificate (i.e. uses certificate chaining), use the chained
101+
server certificate bundle:
102+
103+
mlaunch init --single --dir /tmp/mdb-ssl --sslMode requireSSL \
104+
--sslPEMKeyFile `pwd`/spec/support/certificates/server-second-level-bundle.pem \
105+
--sslCAFile `pwd`/spec/support/certificates/ca.pem \
106+
--sslClientCertificate `pwd`/spec/support/certificates/client.pem
107+
108+
The driver's test suite is configured to verify certificates by default.
109+
If the server is launched with the certificates from the driver's test suite,
110+
the test suite can be run simply by specifying `tls=true` URI option:
111+
112+
MONGODB_URI='mongodb://localhost:27017/?tls=true' rake
113+
114+
The driver's test suite can also be executed against a server launched with
115+
any other certificates. In this case the certificates need to be explicitly
116+
specified in the URI, for example as follows:
117+
118+
MONGODB_URI='mongodb://localhost:27017/?tls=true&tlsCAFile=path/to/ca.crt&tlsCertificateKeyFile=path/to/client.pem' rake
119+
120+
Note that some tests (specifically testing TLS verification) expect the server
121+
to be launched using the certificates in the driver's test suite, and will
122+
fail when run against a server using other certificates.
123+
98124
## TLS Without Verification
99125

100126
It is also possible to enable TLS but omit certificate verification. In this
@@ -111,6 +137,9 @@ verification, run:
111137

112138
MONGODB_URI='mongodb://localhost:27017/?tls=true&tlsInsecure=true' rake
113139

140+
Note that there are tests in the test suite that cover TLS verification, and
141+
they may fail if the test suite is run in this way.
142+
114143
## Authentication
115144

116145
mlaunch can configure authentication on the server:

0 commit comments

Comments
 (0)