Class: WatchmonkeyCli::Checkers::SslExpiration

Inherits:
WatchmonkeyCli::Checker show all
Defined in:
lib/watchmonkey_cli/checkers/ssl_expiration.rb

Instance Attribute Summary

Attributes inherited from WatchmonkeyCli::Checker

#app

Instance Method Summary collapse

Methods inherited from WatchmonkeyCli::Checker

#_tolog, checker_name, checker_name=, #debug, descendants, #error, #info, inherited, #init, #initialize, #local, #rsafe, #safe, #spawn_sub, #start, #stop

Constructor Details

This class inherits a constructor from WatchmonkeyCli::Checker

Instance Method Details

#check!(result, page, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/watchmonkey_cli/checkers/ssl_expiration.rb', line 11

def check! result, page, opts = {}
  uri = URI.parse(page)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  cert = nil
  http.start do |h|
    cert = h.peer_cert
  end

  if cert.not_before > Time.current
    result.error! "Certificate is not yet valid (will in #{fseconds(cert.not_before - Time.current)}, #{cert.not_before})!"
    return
  end

  if cert.not_after <= Time.current
    result.error! "Certificate is EXPIRED (since #{fseconds(cert.not_after - Time.current)}, #{cert.not_after})!"
    return
  end

  if cert.not_after <= Time.current + opts[:threshold]
    result.error! "Certificate is about to expire within threshold (in #{fseconds(cert.not_after - Time.current)}, #{cert.not_after})!"
    return
  else
    result.info! "Certificate for `#{page}' expires in #{fseconds(cert.not_after - Time.current)} (#{cert.not_after})!"
  end
end

#enqueue(page, opts = {}) ⇒ Object



6
7
8
9
# File 'lib/watchmonkey_cli/checkers/ssl_expiration.rb', line 6

def enqueue page, opts = {}
  opts = { threshold: 1.months }.merge(opts)
  app.enqueue(self, page, opts)
end

#fseconds(secs) ⇒ Object



39
40
41
42
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
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/watchmonkey_cli/checkers/ssl_expiration.rb', line 39

def fseconds secs
  secs = secs.to_i
  t_minute = 60
  t_hour = t_minute * 60
  t_day = t_hour * 24
  t_week = t_day * 7
  t_month = t_day * 30
  t_year = t_month * 12
  "".tap do |r|
    if secs >= t_year
      r << "#{secs / t_year}y "
      secs = secs % t_year
    end

    if secs >= t_month
      r << "#{secs / t_month}m "
      secs = secs % t_month
    end

    if secs >= t_week
      r << "#{secs / t_week}w "
      secs = secs % t_week
    end

    if secs >= t_day || !r.blank?
      r << "#{secs / t_day}d "
      secs = secs % t_day
    end

    if secs >= t_hour || !r.blank?
      r << "#{secs / t_hour}h "
      secs = secs % t_hour
    end

    if secs >= t_minute || !r.blank?
      r << "#{secs / t_minute}m "
      secs = secs % t_minute
    end

    r << "#{secs}s" unless r.include?("d")
  end.strip
end