Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nonblock_connect and ssl hanging #274

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ rvm:
- 2.0.0
- 2.1
- 2.2
- 2.3.0
# optional
- ruby-head
- jruby-19mode
Expand Down
44 changes: 33 additions & 11 deletions lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@ def socket_class=(socket_class)
@socket_class = socket_class
end

def prepare_socket(server)
def prepare_socket(server, timeout = nil)
socket = server[:socket]
encryption = server[:encryption]

@conn = socket
setup_encryption encryption if encryption
setup_encryption(encryption, timeout) if encryption
end

def open_connection(server)
hosts = server[:hosts]
encryption = server[:encryption]

timeout = server[:connect_timeout] || DefaultConnectTimeout
socket_opts = {
connect_timeout: server[:connect_timeout] || DefaultConnectTimeout,
connect_timeout: timeout,
}

errors = []
hosts.each do |host, port|
begin
prepare_socket(server.merge(socket: @socket_class.new(host, port, socket_opts)))
prepare_socket(server.merge(socket: @socket_class.new(host, port, socket_opts)), timeout)
return
rescue Net::LDAP::Error, SocketError, SystemCallError,
OpenSSL::SSL::SSLError => e
Expand All @@ -76,7 +77,7 @@ def close
end
end

def self.wrap_with_ssl(io, tls_options = {})
def self.wrap_with_ssl(io, tls_options = {}, timeout = nil)
raise Net::LDAP::NoOpenSSLError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL

ctx = OpenSSL::SSL::SSLContext.new
Expand All @@ -86,10 +87,31 @@ def self.wrap_with_ssl(io, tls_options = {})
ctx.set_params(tls_options) unless tls_options.empty?

conn = OpenSSL::SSL::SSLSocket.new(io, ctx)
conn.connect

# Doesn't work:
# conn.sync_close = true
use_nonblocking_connect = timeout && !ENV['FORCE_BLOCKING_LDAP_SOCKET'] == 'true'

begin
if use_nonblocking_connect
conn.connect_nonblock
else
conn.connect
end
rescue IO::WaitReadable
if IO.select([conn], nil, nil, timeout)
retry
else
raise Errno::ETIMEDOUT, "OpenSSL connection read timeout"
end
rescue IO::WaitWritable
if IO.select(nil, [conn], nil, timeout)
retry
else
raise Errno::ETIMEDOUT, "OpenSSL connection write timeout"
end
end

# Doesn't work? Or maybe it is needed if connect_nonblock?
conn.sync_close = use_nonblocking_connect && !ENV['DISABLE_SYNC_CLOSE_LDAP_SOCKET'] == 'true'

conn.extend(GetbyteForSSLSocket) unless conn.respond_to?(:getbyte)
conn.extend(FixSSLSocketSyncClose)
Expand Down Expand Up @@ -123,11 +145,11 @@ def self.wrap_with_ssl(io, tls_options = {})
# communications, as with simple_tls. Thanks for Kouhei Sutou for
# generously contributing the :start_tls path.
#++
def setup_encryption(args)
def setup_encryption(args, timeout = nil)
args[:tls_options] ||= {}
case args[:method]
when :simple_tls
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
# additional branches requiring server validation and peer certs, etc.
# go here.
when :start_tls
Expand All @@ -144,7 +166,7 @@ def setup_encryption(args)
end

if pdu.result_code.zero?
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
else
raise Net::LDAP::StartTLSError, "start_tls failed: #{pdu.result_code}"
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_queued_read_setup_encryption_with_start_tls
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}).
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil).
and_return(mock)

conn.next_msgid # simulates ongoing query
Expand Down