Skip to content

Instantly share code, notes, and snippets.

@sshastri
Last active September 6, 2018 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshastri/00b727bf57b67626029c2d9d099946ac to your computer and use it in GitHub Desktop.
Save sshastri/00b727bf57b67626029c2d9d099946ac to your computer and use it in GitHub Desktop.
require 'date'
Facter.add(:cert_expire) do
confine :kernel => 'Linux'
setcode do
hostname = Facter.value(:hostname)
info = Facter::Core::Execution.execute("echo | openssl s_client -showcerts -connect #{hostname}:443 2>/dev/null | openssl x509 -noout -dates")
expire_info = {}
dates = info.split("\n")
if !dates[0].nil?
start_date = dates[0].gsub('notBefore=','')
end_date = dates[1].gsub('notAfter=','')
expire_info['start_date'] = start_date
expire_info['end_date'] = end_date
expire_info['days_to_expire'] = (Date.parse(end_date) - Date.today).to_i
else
expire_info = ''
end
expire_info
end
end
@natemccurdy
Copy link

natemccurdy commented Sep 6, 2018

Here ya go @sshastri

require 'date'
Facter.add(:cert_expire) do
  confine kernel: :Linux

  setcode do
    hostname = Facter.value(:hostname)

    cert_info = Facter::Core::Execution.execute("echo | openssl s_client -showcerts -connect #{hostname}:443 2>/dev/null | openssl x509 -noout -dates")

    expire_info = {}
    dates = cert_info.split("\n")

    unless dates[0].nil?
      start_date = dates[0].gsub('notBefore=', '')
      end_date = dates[1].gsub('notAfter=', '')

      expire_info['start_date'] = start_date
      expire_info['end_date'] = end_date
      expire_info['days_to_expire'] = (Date.parse(end_date) - Date.today).to_i
    end

    expire_info
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment