Class: SyslogTls::SSLTransport
- Inherits:
-
Object
- Object
- SyslogTls::SSLTransport
- Defined in:
- lib/syslog_tls/ssl_transport.rb
Overview
Supports SSL connection to remote host
Instance Attribute Summary collapse
-
#ca_cert ⇒ Object
readonly
Returns the value of attribute ca_cert.
-
#cert ⇒ Object
readonly
Returns the value of attribute cert.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#retries ⇒ Object
writeonly
Sets the attribute retries.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#ssl_version ⇒ Object
readonly
Returns the value of attribute ssl_version.
Instance Method Summary collapse
- #connect ⇒ Object
- #get_ssl_connection ⇒ Object
-
#initialize(host, port, ca_cert: 'system', cert: nil, key: nil, ssl_version: :TLSv1_2, max_retries: 1) ⇒ SSLTransport
constructor
A new instance of SSLTransport.
-
#method_missing(method_sym, *arguments, &block) ⇒ Object
Forward any methods directly to SSLSocket.
-
#write(s) ⇒ Object
Allow to retry on failed writes.
Constructor Details
#initialize(host, port, ca_cert: 'system', cert: nil, key: nil, ssl_version: :TLSv1_2, max_retries: 1) ⇒ SSLTransport
Returns a new instance of SSLTransport.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/syslog_tls/ssl_transport.rb', line 27 def initialize(host, port, ca_cert: 'system', cert: nil, key: nil, ssl_version: :TLSv1_2, max_retries: 1) @ca_cert = ca_cert @host = host @port = port @cert = cert @key = key @ssl_version = ssl_version @retries = max_retries connect end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
Forward any methods directly to SSLSocket
86 87 88 |
# File 'lib/syslog_tls/ssl_transport.rb', line 86 def method_missing(method_sym, *arguments, &block) @socket.send(method_sym, *arguments, &block) end |
Instance Attribute Details
#ca_cert ⇒ Object (readonly)
Returns the value of attribute ca_cert.
23 24 25 |
# File 'lib/syslog_tls/ssl_transport.rb', line 23 def ca_cert @ca_cert end |
#cert ⇒ Object (readonly)
Returns the value of attribute cert.
23 24 25 |
# File 'lib/syslog_tls/ssl_transport.rb', line 23 def cert @cert end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
23 24 25 |
# File 'lib/syslog_tls/ssl_transport.rb', line 23 def host @host end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
23 24 25 |
# File 'lib/syslog_tls/ssl_transport.rb', line 23 def key @key end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
23 24 25 |
# File 'lib/syslog_tls/ssl_transport.rb', line 23 def port @port end |
#retries=(value) ⇒ Object (writeonly)
Sets the attribute retries
25 26 27 |
# File 'lib/syslog_tls/ssl_transport.rb', line 25 def retries=(value) @retries = value end |
#socket ⇒ Object
Returns the value of attribute socket.
21 22 23 |
# File 'lib/syslog_tls/ssl_transport.rb', line 21 def socket @socket end |
#ssl_version ⇒ Object (readonly)
Returns the value of attribute ssl_version.
23 24 25 |
# File 'lib/syslog_tls/ssl_transport.rb', line 23 def ssl_version @ssl_version end |
Instance Method Details
#connect ⇒ Object
38 39 40 41 |
# File 'lib/syslog_tls/ssl_transport.rb', line 38 def connect @socket = get_ssl_connection @socket.connect end |
#get_ssl_connection ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/syslog_tls/ssl_transport.rb', line 43 def get_ssl_connection tcp = TCPSocket.new(host, port) ctx = OpenSSL::SSL::SSLContext.new ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER) ctx.ssl_version = ssl_version case ca_cert when true, 'true', 'system' # use system certs, same as openssl cli ctx.cert_store = OpenSSL::X509::Store.new ctx.cert_store.set_default_paths when false, 'false' ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE when %r{/$} # ends in / ctx.ca_path = ca_cert when String ctx.ca_file = ca_cert end ctx.cert = OpenSSL::X509::Certificate.new(File.read(cert)) if cert ctx.key = OpenSSL::PKey::read(File.read(key)) if key socket = OpenSSL::SSL::SSLSocket.new(tcp, ctx) socket.sync_close = true socket end |
#write(s) ⇒ Object
Allow to retry on failed writes
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/syslog_tls/ssl_transport.rb', line 71 def write(s) begin retry_id ||= 0 @socket.send(:write, s) rescue => e if (retry_id += 1) < @retries connect retry else raise e end end end |