auto convert cer to cert

This commit is contained in:
邱博亞 2023-03-25 14:07:05 +08:00
parent bb9c7ce6cb
commit 1a2618319a
2 changed files with 20 additions and 11 deletions

View File

@ -156,7 +156,7 @@ class Admin::SitePanelController < OrbitAdminController
render :layout => false render :layout => false
end end
def cert_management def cert_management
@site_certs = SiteCert.all.page(params[:page]).per(10) @site_certs = SiteCert.all.order_by(id: :desc).page(params[:page]).per(10)
@filter_fields = {} @filter_fields = {}
params[:keywords] = params[:keywords].to_s.gsub("*","\\*") params[:keywords] = params[:keywords].to_s.gsub("*","\\*")
@site_certs = search_data(@site_certs,[:domain_names]) @site_certs = search_data(@site_certs,[:domain_names])

View File

@ -32,14 +32,25 @@ class SiteCert
end end
def change_data def change_data
if !@skip_callback if !@skip_callback
cert_file_md5 = `openssl x509 -noout -modulus -in #{self.cert_file.file.file} | openssl md5` org_cert_file_name = self.cert_file.file.file.to_s
cert_file_name = org_cert_file_name.sub(/.cer$/, '.crt')
if org_cert_file_name != cert_file_name
if File.open(org_cert_file_name, 'r').read().match(/\A\s*---/)
new_sf = CarrierWave::SanitizedFile.new(self.cert_file.file.move_to(cert_file_name))
else
`openssl x509 --inform DER -in #{org_cert_file_name} --out #{cert_file_name}`
new_sf = CarrierWave::SanitizedFile.new(cert_file_name)
end
self.cert_file.cache!(new_sf)
end
cert_file_md5 = `openssl x509 -noout -modulus -in #{cert_file_name} | openssl md5`
private_key_md5 = `openssl rsa -noout -modulus -in #{self.private_key.file.file} | openssl md5` private_key_md5 = `openssl rsa -noout -modulus -in #{self.private_key.file.file} | openssl md5`
is_valid = (cert_file_md5 == private_key_md5) is_valid = (cert_file_md5 == private_key_md5)
domain_names = `openssl x509 -text < #{self.cert_file.file.file} | grep 'DNS:' | sed 's/\s*DNS:\([a-z0-9.\-]*\)[,\s]\?/\1 /g'`.split('DNS:').map{|s| s.sub(',','').strip}.select{|s| s.present?} rescue [] domain_names = `openssl x509 -text < #{cert_file_name} | grep 'DNS:' | sed 's/\s*DNS:\([a-z0-9.\-]*\)[,\s]\?/\1 /g'`.split('DNS:').map{|s| s.sub(',','').strip}.select{|s| s.present?} rescue []
if domain_names.length == 0 if domain_names.length == 0
domain_names = [`openssl x509 -text < #{self.cert_file.file.file} | grep 'Subject' | grep 'CN =' | grep 'Subject' | grep 'CN =' |sed 's/\s*Subject: //g'`[0...-1].split(/, | = /).each_slice(2).to_h['CN']] rescue [] domain_names = [`openssl x509 -text < #{cert_file_name} | grep 'Subject' | grep 'CN =' | grep 'Subject' | grep 'CN =' |sed 's/\s*Subject: //g'`[0...-1].split(/, | = /).each_slice(2).to_h['CN']] rescue []
end end
sign_algo_valid = `openssl x509 -text < #{self.cert_file.file.file} | grep 'Signature Algorithm: sha1'`[0...-1].blank? rescue false sign_algo_valid = `openssl x509 -text < #{cert_file_name} | grep 'Signature Algorithm: sha1'`[0...-1].blank? rescue false
invalid_messages = [] invalid_messages = []
if !is_valid if !is_valid
invalid_messages << 'cert and key not match' invalid_messages << 'cert and key not match'
@ -56,18 +67,16 @@ class SiteCert
end end
if domain_names.blank? if domain_names.blank?
self.is_valid = false self.is_valid = false
@skip_callback = true
self.save(:validate=>false)
else else
start_date_text = `openssl x509 -text < #{self.cert_file.file.file} -startdate -noout`.split('=').last.strip start_date_text = `openssl x509 -text < #{cert_file_name} -startdate -noout`.split('=').last.strip
end_date_text = `openssl x509 -text < #{self.cert_file.file.file} -enddate -noout`.split('=').last.strip end_date_text = `openssl x509 -text < #{cert_file_name} -enddate -noout`.split('=').last.strip
self.start_date = DateTime.parse(start_date_text) rescue nil self.start_date = DateTime.parse(start_date_text) rescue nil
self.end_date = DateTime.parse(end_date_text) rescue nil self.end_date = DateTime.parse(end_date_text) rescue nil
self.is_valid = is_valid self.is_valid = is_valid
self.domain_names = domain_names self.domain_names = domain_names
@skip_callback = true
self.save(:validate=>false)
end end
@skip_callback = true
self.save(:validate=>false)
@skip_callback = false @skip_callback = false
false false
end end