Module: CertMunger::ClassMethods
- Defined in:
- lib/cert_munger/formatter.rb
Overview
Class methods provided by CertMunger module
Instance Method Summary collapse
-
#build_cert(raw_cert) ⇒ String
Creates a temporary cert and orchestrates certificate body reformatting.
-
#logger ⇒ Object
logger method to return Rails logger if defined, else logging logger.
-
#multi_line_contents(raw_cert) ⇒ String
Attempts to reformat multi-line certificate bodies.
-
#one_line_contents(raw_cert) ⇒ String
Attempts to reformat one-line certificate bodies.
-
#parse_space_delimited_cert(raw_cert) ⇒ Array
Attempts to reformat one-line certificate bodies.
-
#to_cert(raw_cert) ⇒ OpenSSL::X509::Certificate
Attempts to munge a string into a valid X509 certificate.
Instance Method Details
#build_cert(raw_cert) ⇒ String
Creates a temporary cert and orchestrates certificate body reformatting
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cert_munger/formatter.rb', line 49 def build_cert(raw_cert) tmp_cert = ['-----BEGIN CERTIFICATE-----'] cert_contents = if raw_cert.lines.count == 1 one_line_contents(raw_cert) else multi_line_contents(raw_cert) end tmp_cert << cert_contents.flatten # put mixed space lines as own elements tmp_cert << '-----END CERTIFICATE-----' tmp_cert.join("\n").rstrip end |
#logger ⇒ Object
logger method to return Rails logger if defined, else logging logger
26 27 28 29 30 |
# File 'lib/cert_munger/formatter.rb', line 26 def logger return @logger if @logger logger = Logging.logger[self] unless @logger @logger ||= Kernel.const_defined?('Rails') ? Rails.logger : logger end |
#multi_line_contents(raw_cert) ⇒ String
Attempts to reformat multi-line certificate bodies
96 97 98 99 100 101 |
# File 'lib/cert_munger/formatter.rb', line 96 def multi_line_contents(raw_cert) cert_contents = raw_cert.split(/[-](.*)[-]/)[2] cert_contents.lines.map do |line| line.lstrip.squeeze(' ').split(' ') end end |
#one_line_contents(raw_cert) ⇒ String
Attempts to reformat one-line certificate bodies
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cert_munger/formatter.rb', line 65 def one_line_contents(raw_cert) # Detect if we have newlines, if not, split on spaces cert_contents = if raw_cert.include?('\n') raw_cert.split('\n') else parse_space_delimited_cert(raw_cert) end cert_contents.pop # Remove -----BEGIN CERTIFICATE----- cert_contents.shift # Remove -----END CERTIFICATE----- cert_contents.map! { |el| el.match('\W+[t|n|r](.*)')[1] } end |
#parse_space_delimited_cert(raw_cert) ⇒ Array
Attempts to reformat one-line certificate bodies
82 83 84 85 86 87 88 89 90 |
# File 'lib/cert_munger/formatter.rb', line 82 def parse_space_delimited_cert(raw_cert) cert_contents = raw_cert.split(' ') # "-----BEGIN CERTIFICATE------" fix cert_contents[0] += " #{cert_contents.delete_at(1)}" # "-----END CERTIFICATE------" fix cert_contents[-1] = "#{cert_contents[-2]} #{cert_contents.delete_at(-1)}" cert_contents.map { |el| "\\t#{el}" } # Hack it to match expected syntax end |
#to_cert(raw_cert) ⇒ OpenSSL::X509::Certificate
Attempts to munge a string into a valid X509 certificate
36 37 38 39 40 41 42 43 |
# File 'lib/cert_munger/formatter.rb', line 36 def to_cert(raw_cert) logger.debug "CertMunger raw_cert:\n#{raw_cert}" new_cert = build_cert(raw_cert) logger.debug "CertMunger reformatted_cert:\n#{new_cert}" logger.debug 'Returning OpenSSL::X509::Certificate.new on new_cert' OpenSSL::X509::Certificate.new new_cert end |