Skip to content

Commit fd78782

Browse files
committed
Modify parameters to reflect set_params hash
By default, we don't set any parameters (empty or no :tls_options). If the hash is non-empty, we pass it directly to set_params and use the resulting context for creating the TLS socket.
1 parent d7d2c52 commit fd78782

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

lib/net/ldap.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -560,20 +560,29 @@ def authenticate(username, password)
560560
# communcations with the LDAP server. With the exception that it operates
561561
# over the standard TCP port.
562562
#
563-
# In order to allow verification of server certificates and other TLS-related
564-
# options, the keys :cafile and :ssl_context can be used.
565-
#
566-
# The :cafile option is a single filename that points to one or more
567-
# PEM-encoded certificates. These certificates are used as a certificate auhority
568-
# to verify the server certificates.
569-
#
570-
# For fine-grained control of the TLS settings, it is also possible to use the
571-
# :ssl_context option to pass a custom OpenSSL::SSL::SSLContext. Consult the
572-
# OpenSSL documentation for more information on the available options.
563+
# In order to verify certificates and enable other TLS options, the
564+
# :tls_options hash can be passed alongside :simple_tls or :start_tls.
565+
# This hash contains any options that can be passed to
566+
# OpenSSL::SSL::SSLContext#set_params(). The most common options passed
567+
# should be OpenSSL::SSL::SSLContext::DEFAULT_PARAMS, or the :ca_file option,
568+
# which contains a path to a Certificate Authority file (PEM-encoded).
569+
#
570+
# Example for a default setup without custom settings:
571+
# {
572+
# :method => :simple_tls,
573+
# :tls_options => OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
574+
# }
575+
#
576+
# Example for specifying a CA-File and only allowing TLSv1.1 connections:
577+
#
578+
# {
579+
# :method => :start_tls,
580+
# :tls_options => { :ca_file => "/etc/cafile.pem", :ssl_version => "TLSv1_1" }
581+
# }
573582
def encryption(args)
574583
case args
575584
when :simple_tls, :start_tls
576-
args = { :method => args }
585+
args = { :method => args, :tls_options => {} }
577586
end
578587
@encryption = args
579588
end

lib/net/ldap/connection.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,11 @@ def close
4141
end
4242
end
4343

44-
def self.wrap_with_ssl(io, ssl_context = nil, cafile = nil)
44+
def self.wrap_with_ssl(io, tls_options = {})
4545
raise Net::LDAP::LdapError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL
46-
if (ssl_context && cafile)
47-
raise Net::LDAP::LdapError, "Please specify only one of ssl_context or cafile"
48-
end
49-
50-
ctx = ssl_context ? ssl_context : OpenSSL::SSL::SSLContext.new
5146

52-
# OpenSSL automatically merges the given parameters with the default parameters
53-
# These include verification and some common workarounds
54-
if cafile
55-
ctx.set_params({:ca_file => cafile})
56-
end
47+
ctx = OpenSSL::SSL::SSLContext.new
48+
ctx.set_params(tls_options) unless tls_options.empty?
5749

5850
conn = OpenSSL::SSL::SSLSocket.new(io, ctx)
5951
conn.connect
@@ -96,7 +88,7 @@ def self.wrap_with_ssl(io, ssl_context = nil, cafile = nil)
9688
def setup_encryption(args)
9789
case args[:method]
9890
when :simple_tls
99-
@conn = self.class.wrap_with_ssl(@conn, args[:ssl_context], args[:cafile])
91+
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
10092
# additional branches requiring server validation and peer certs, etc.
10193
# go here.
10294
when :start_tls
@@ -113,7 +105,7 @@ def setup_encryption(args)
113105
end
114106

115107
if pdu.result_code.zero?
116-
@conn = self.class.wrap_with_ssl(@conn, args[:ssl_context], args[:cafile])
108+
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
117109
else
118110
raise Net::LDAP::LdapError, "start_tls failed: #{pdu.result_code}"
119111
end

test/test_ldap_connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_queued_read_setup_encryption_with_start_tls
202202
and_return(result2)
203203
mock.should_receive(:write)
204204
conn = Net::LDAP::Connection.new(:socket => mock)
205-
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, nil, nil).
205+
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, nil).
206206
and_return(mock)
207207

208208
conn.next_msgid # simulates ongoing query

0 commit comments

Comments
 (0)