Created
July 10, 2019 14:42
-
-
Save jcward/370b642705c588e863abd2ad6b833bcb to your computer and use it in GitHub Desktop.
CI test for SSL certificate expiration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Checks each server's certificate, and fails (exit 1) if it is etiher | |
# unreachable, or the certificate expires within 7 days. | |
require 'time' | |
min_days_left = 7 | |
servers = [ | |
"one.example.com", | |
"two.example.com", | |
] | |
$errors = [] | |
puts "SSL certificate expiration check:" | |
servers.each { |server| | |
cmd = "echo | openssl s_client -servername #{ server } -connect #{ server }:443 2>/dev/null | openssl x509 -noout -dates" | |
result = `#{ cmd }` | |
if (result.match(/notBefore=(.*)/)) then | |
notBefore = Time.parse($1) | |
if (Time.now.to_i < notBefore.to_i) then | |
$errors.push("SSL ERROR: #{ server } cert is not yet valid!") | |
end | |
end | |
if (result.match(/notAfter=(.*)/)) then | |
notAfter = Time.parse($1) | |
puts " - #{ server } - #{ notAfter.to_s }" | |
if (Time.now.to_i > notAfter.to_i) then | |
$errors.push("SSL ERROR: #{ server } cert is EXPIRED !!!") | |
elsif (Time.now.to_i > notAfter.to_i - min_days_left*24*3600) then | |
$errors.push("SSL WARNING: #{ server } cert is expiring in less than #{ min_days_left } days: #{ notAfter.to_s } !!!") | |
end | |
else | |
$errors.push("SSL ERROR: #{ server } no response or unknown failure") | |
end | |
} | |
if ($errors.length>0) then | |
puts "" | |
puts " = = = = SSL ERRORS = = = = = = = = = = = = = = = = = =" | |
puts $errors.join("\n") | |
puts " = = = = = = = = = = = = = = = = = = = = = = = = = = =" | |
exit 1 | |
else | |
exit 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment