Skip to content

Instantly share code, notes, and snippets.

@thomaswitt
Created November 5, 2013 12:23
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 thomaswitt/7318277 to your computer and use it in GitHub Desktop.
Save thomaswitt/7318277 to your computer and use it in GitHub Desktop.
Rename PEM certificates to CN and date
#!/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