class Thrift::SSLSocket
Attributes
Public Class Methods
Source
# File lib/thrift/transport/ssl_socket.rb 26 def initialize(host = "localhost", port = 9090, timeout = nil, ssl_context = nil, server_hostname: host) 27 super(host, port, timeout) 28 @ssl_context = ssl_context 29 @server_hostname = server_hostname 30 end
Calls superclass method
Thrift::Socket::new
Public Instance Methods
Source
# File lib/thrift/transport/ssl_socket.rb 35 def open 36 deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout unless @timeout.nil? || @timeout == 0 37 socket = connect_socket(deadline) 38 39 begin 40 ssl_context = configured_ssl_context 41 ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context) 42 ssl_socket.hostname = @server_hostname if send_server_hostname? 43 ssl_socket.sync_close = true 44 @handle = ssl_socket 45 46 if deadline 47 loop do 48 result = @handle.connect_nonblock(exception: false) 49 50 case result 51 when @handle 52 break 53 when :wait_readable 54 remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) 55 if remaining <= 0 || !@handle.to_io.wait_readable(remaining) 56 raise TransportException.new(TransportException::TIMED_OUT, "SSL socket: Timed out establishing session with #{@desc}") 57 end 58 when :wait_writable 59 remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) 60 if remaining <= 0 || !@handle.to_io.wait_writable(remaining) 61 raise TransportException.new(TransportException::TIMED_OUT, "SSL socket: Timed out establishing session with #{@desc}") 62 end 63 else 64 raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}: unexpected SSL connect result #{result.inspect}") 65 end 66 end 67 else 68 @handle.connect 69 end 70 71 @handle.post_connection_check(server_hostname_for_verification) 72 @handle 73 rescue TransportException 74 close_socket(@handle) 75 @handle = nil 76 raise 77 rescue StandardError 78 close_socket(@handle || socket) 79 @handle = nil 80 raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}") 81 end 82 end
Private Instance Methods
Source
# File lib/thrift/transport/ssl_socket.rb 90 def configured_ssl_context 91 @ssl_context ||= OpenSSL::SSL::SSLContext.new.tap do |context| 92 context.verify_mode = OpenSSL::SSL::VERIFY_PEER 93 context.cert_store = OpenSSL::X509::Store.new.tap(&:set_default_paths) 94 end 95 end
Source
# File lib/thrift/transport/ssl_socket.rb 97 def send_server_hostname? 98 return false if @server_hostname.nil? || @server_hostname.empty? 99 100 IPAddr.new(@server_hostname) 101 false 102 rescue IPAddr::InvalidAddressError 103 true 104 end
Source
# File lib/thrift/transport/ssl_socket.rb 106 def server_hostname_for_verification 107 @server_hostname.nil? || @server_hostname.empty? ? @host : @server_hostname 108 end