-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssl_checker.rb
44 lines (31 loc) · 903 Bytes
/
ssl_checker.rb
1
2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
require "net/http"
class SslChecker
def initialize(host:)
@host = host
end
def expires_in(reference_time: Time.now)
cert.not_after - reference_time
end
def cert
@cert ||= fetch_certificate
end
private
def fetch_certificate
http = Net::HTTP.new(@host, 443)
http.use_ssl = true
# Explicitly setting cert_store like this is not needed in most cases but it
# seems necessary in edge cases such as when using `verify_callback` in some
# combination of Ruby + OpenSSL versions.
http.cert_store = OpenSSL::X509::Store.new
http.cert_store.set_default_paths
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
cert = nil
http.verify_callback = lambda { |verify_ok, store_context|
cert = store_context.current_cert.dup
true
}
req = Net::HTTP::Head.new('/')
res = http.start { http.request(req) }
cert
end
end