Class: Riemann::Client::SSLSocket

Inherits:
TcpSocket
  • Object
show all
Defined in:
lib/riemann/client/ssl_socket.rb

Overview

Socket: A specialized socket that has been configure

Instance Attribute Summary

Attributes inherited from TcpSocket

#connect_timeout, #host, #keepalive_count, #keepalive_idle, #keepalive_interval, #port, #read_timeout, #write_timeout

Instance Method Summary collapse

Methods inherited from TcpSocket

#close, #closed?, connect, #connect, #connect_nonblock_finalize, #connect_or_error, #keepalive_active?, #read, #socket, #socket_factory, #using_keepalive?, #wait_readable, #wait_writable

Constructor Details

#initialize(options = {}) ⇒ SSLSocket

Returns a new instance of SSLSocket.



10
11
12
13
14
15
16
# File 'lib/riemann/client/ssl_socket.rb', line 10

def initialize(options = {})
  super(options)
  @key_file = options[:key_file]
  @cert_file = options[:cert_file]
  @ca_file = options[:ca_file]
  @ssl_verify = options[:ssl_verify]
end

Instance Method Details

#connect_nonblock(addr, timeout) ⇒ Object

Internal: Connect to the give address within the timeout.

Make an attempt to connect to a single address within the given timeout.

Return the ::Socket when it is connected, or raise an Error if no connection was possible.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/riemann/client/ssl_socket.rb', line 34

def connect_nonblock(addr, timeout)
  sock = super(addr, timeout)
  ssl_socket = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
  ssl_socket.sync = true

  begin
    ssl_socket.connect_nonblock
  rescue IO::WaitReadable
    unless IO.select([ssl_socket], nil, nil, timeout)
      raise Timeout, "Could not read from #{host}:#{port} in #{timeout} seconds"
    end

    retry
  rescue IO::WaitWritable
    unless IO.select(nil, [ssl_socket], nil, timeout)
      raise Timeout, "Could not write to #{host}:#{port} in #{timeout} seconds"
    end

    retry
  end
  ssl_socket
end

#readpartial(maxlen, outbuf = nil) ⇒ Object

Internal: Read up to a maxlen of data from the socket and store it in outbuf

maxlen - the maximum number of bytes to read from the socket outbuf - the buffer in which to store the bytes.

Returns the bytes read



63
64
65
66
67
68
69
70
71
# File 'lib/riemann/client/ssl_socket.rb', line 63

def readpartial(maxlen, outbuf = nil)
  super(maxlen, outbuf)
rescue OpenSSL::SSL::SSLErrorWaitReadable
  unless wait_readable(read_timeout)
    raise Timeout, "Could not read from #{host}:#{port} in #{read_timeout} seconds"
  end

  retry
end

#ssl_contextObject



18
19
20
21
22
23
24
25
26
# File 'lib/riemann/client/ssl_socket.rb', line 18

def ssl_context
  @ssl_context ||= OpenSSL::SSL::SSLContext.new.tap do |ctx|
    ctx.key = OpenSSL::PKey::RSA.new(File.read(@key_file))
    ctx.cert = OpenSSL::X509::Certificate.new(File.read(@cert_file))
    ctx.ca_file = @ca_file if @ca_file
    ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
    ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER if @ssl_verify
  end
end

#write(buf) ⇒ Object

Internal: Write the given data to the socket

buf - the data to write to the socket.

Raises an error if it is unable to write the data to the socket within the write_timeout.

returns nothing



81
82
83
84
85
86
87
88
89
# File 'lib/riemann/client/ssl_socket.rb', line 81

def write(buf)
  super(buf)
rescue OpenSSL::SSL::SSLErrorWaitWritable
  unless wait_writable(write_timeout)
    raise Timeout, "Could not write to #{host}:#{port} in #{write_timeout} seconds"
  end

  retry
end