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

Respect connect_timeout when establishing SSL connections #273

Merged
merged 5 commits into from
Jul 13, 2016
Merged
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
38 changes: 29 additions & 9 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,7 +87,26 @@ 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

begin
if timeout
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:
# conn.sync_close = true
Expand Down Expand Up @@ -123,11 +143,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 +164,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).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmm1 Doesn't this mocking cover your changes on wrap_with_ssl, do ti? If you tested your changes work as you expected, could you share how to do it?

I know it's really hard to reproduce the situation timeout or something network issues happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the change by setting a very low timeout on LDAPS connection and
make sure an exception is raised.
On Mon, Jun 20, 2016 at 10:15 PM Tatsuya Sato [email protected]
wrote:

In test/test_ldap_connection.rb
#273 (comment)
:

@@ -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).

@tmm1 https://github.com/tmm1 Doesn't this mocking cover your changes
on wrap_with_ssl, do ti? If you tested your changes work as you expected,
could you share how to do it?

I know it's really hard to reproduce the situation timeout or something
network issues happens.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ruby-ldap/ruby-net-ldap/pull/273/files/749c22b4e5514ead10c92bcaec1c5a1eb49db455#r67809160,
or mute the thread
https://github.com/notifications/unsubscribe/AAAKB01CFZ0ewSejsl4qpinzGhwx48U5ks5qN3N4gaJpZM4I25cW
.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks!

and_return(mock)

conn.next_msgid # simulates ongoing query
Expand Down