Created
November 5, 2013 12:23
-
-
Save thomaswitt/7318277 to your computer and use it in GitHub Desktop.
Rename PEM certificates to CN and date
This file contains 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/ruby | |
require 'openssl' | |
require 'date' | |
Dir.chdir('/Users/thomas/Desktop/certs') | |
Dir.glob('./*.pem') do |file| | |
next if file =~ /.k.pem$/ | |
puts file | |
raw = File.read file | |
cert = OpenSSL::X509::Certificate.new(raw) | |
valid_until = cert.not_after.to_date.xmlschema | |
subject = OpenSSL::X509::Name.new(cert.subject).to_a | |
subject_hash = Hash[*subject.transpose[0..-2].transpose.flatten] | |
cn = subject_hash['CN'].gsub(/\*/,'STAR') | |
filename = "#{cn}_until_#{valid_until}" | |
puts "#{file} => #{filename}.pem" | |
File.rename(file, "#{filename}.pem") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment