Class: TLSChecker::CertificateChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/tls_checker/certificate_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, address, port, starttls) ⇒ CertificateChecker

Returns a new instance of CertificateChecker.



9
10
11
12
13
14
15
16
17
18
# File 'lib/tls_checker/certificate_checker.rb', line 9

def initialize(hostname, address, port, starttls)
  @hostname = hostname
  @address = address
  @port = port
  @starttls = starttls

  @certificate = nil
  @certificate_failure = nil
  @tls_socket = nil
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



20
21
22
# File 'lib/tls_checker/certificate_checker.rb', line 20

def address
  @address
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



20
21
22
# File 'lib/tls_checker/certificate_checker.rb', line 20

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



20
21
22
# File 'lib/tls_checker/certificate_checker.rb', line 20

def port
  @port
end

#starttlsObject (readonly)

Returns the value of attribute starttls.



20
21
22
# File 'lib/tls_checker/certificate_checker.rb', line 20

def starttls
  @starttls
end

Instance Method Details

#certificateObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tls_checker/certificate_checker.rb', line 49

def certificate
  return @certificate unless @certificate.nil?

  if tls_socket.peer_cert
    @certificate = OpenSSL::X509::Certificate.new(tls_socket.peer_cert)
  else
    @certificate_failure = 'No peer certificate (TLS handshake failed?)'
    @certificate = false
  end
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketRecvTimeout, IO::TimeoutError => e
  @certificate_failure = "#{e.class.name}: #{e.message}"
  @certificate = false
end

#checkObject



41
42
43
# File 'lib/tls_checker/certificate_checker.rb', line 41

def check
  !!certificate
end

#humanized_addressObject



67
68
69
70
71
72
73
# File 'lib/tls_checker/certificate_checker.rb', line 67

def humanized_address
  if @address.is_a?(Resolv::IPv6)
    "[#{@address}]"
  else
    @address.to_s
  end
end

#serviceObject



63
64
65
# File 'lib/tls_checker/certificate_checker.rb', line 63

def service
  "X.509/#{hostname}/#{humanized_address}:#{port}"
end

#to_eObject

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tls_checker/certificate_checker.rb', line 22

def to_e # rubocop:disable Metrics/MethodLength
  if certificate
    InternetSecurityEvent::TLSStatus.build(hostname, certificate)
  else
    {
      state:       'critical',
      description: @certificate_failure || "#{hostname} does not have a valid certificate",
    }
  end.merge(
    service:  service,
    af:       af,
    hostname: hostname,
    address:  address.to_s,
    port:     port,
    ttl:      12.hours.to_i,
    tags:     ['tls-checker'],
  )
end

#to_sObject



45
46
47
# File 'lib/tls_checker/certificate_checker.rb', line 45

def to_s
  description
end