Last active
December 22, 2017 16:01
-
-
Save dmikusa/1f54e899bbf604d793e19cad26450ff4 to your computer and use it in GitHub Desktop.
Inspect the certs listed in installation.yml for PCF
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 | |
require 'yaml' | |
require 'openssl' | |
def confirm | |
print " Enter (y/n)... " | |
begin | |
while input = STDIN.gets.downcase.strip | |
case input | |
when "y" | |
return true | |
when "n" | |
return false | |
else | |
puts " Invalid Choice #{input}" | |
end | |
end | |
rescue Interrupt | |
exit -1 | |
end | |
end | |
def traverse(parent, tag, obj, &blk) | |
case obj | |
when Hash | |
obj.each {|k,v| traverse(obj, k, v, &blk) } | |
when Array | |
obj.each {|v, i| traverse(obj, i, v, &blk) } | |
else | |
blk.call(parent, tag, obj) | |
end | |
end | |
def main(inputFile, outputFile) | |
installation_yml = YAML.load_file(inputFile) | |
traverse(nil, installation_yml, installation_yml) do |parent, tag, node| | |
if node.class == String && node.start_with?("-----BEGIN CERTIFICATE-----") then | |
begin | |
node.scan(/(-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----)/m).flat_map(&:compact).each {|cert| | |
x = OpenSSL::X509::Certificate.new(cert) | |
puts "Subject: #{x.subject}" | |
puts " Issuer: #{x.issuer}" | |
puts " Valid: #{x.not_before} to #{x.not_after}" | |
now = Time::now | |
age = (Time::now - x.not_before) / 60.0 / 60.0 / 24.0 | |
expires = (x.not_after - Time::now) / 60.0 / 60.0 / 24.0 | |
puts " Age: #{age}" | |
puts " Expires in: #{expires}" | |
if expires > 0.0 and expires < 30.0 then | |
puts " WARNING!! Cert will expire in less than 30 days" | |
elsif expires < 0.0 then | |
puts " ERROR!!! Cert has expired!!" | |
end | |
if confirm then | |
puts " Confirmed. Removing cert." | |
parent[tag] = "" | |
else | |
puts " Skipping Cert [#{x.subject}] at user's request" | |
end | |
} | |
rescue SystemExit, Interrupt | |
raise | |
rescue Exception => e | |
puts "error reading #{node}" | |
puts "message #{e.message}" | |
puts e.backtrace | |
end | |
end | |
end | |
File.open(outputFile, "w") { |f| YAML.dump(installation_yml, f) } | |
end | |
def usage(msg="") | |
puts "" | |
if msg != "" then | |
puts msg | |
puts "" | |
end | |
puts "USAGE:" | |
puts " ruby check-args input_yml output_yml" | |
puts "" | |
end | |
if ARGV.length != 2 then | |
usage() | |
exit -1 | |
end | |
if ARGV[0] == ARGV[1] then | |
usage("input and output file names cannot be the same") | |
exit -1 | |
end | |
if ! File.file?(ARGV[0]) then | |
usage("input file #{ARGV[0]} does not exist") | |
exit -1 | |
end | |
if File.file?(ARGV[1]) then | |
usage("output file #{ARGV[1]} already exists") | |
exit -1 | |
end | |
main(ARGV[0], ARGV[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment