module CertMunger::ClassMethods

Class methods provided by CertMunger module

Public Instance Methods

build_cert(raw_cert) click to toggle source

Creates a temporary cert and orchestrates certificate body reformatting

@param raw_cert [String] The string of text you wish to parse into a cert @return [String] reformatted and (hopefully) parseable certificate string

# 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() click to toggle source

logger method to return Rails logger if defined, else logging logger

# 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) click to toggle source

Attempts to reformat multi-line certificate bodies

@param raw_cert [String] The string of text you wish to parse into a cert @return [String] reformatted certificate body

# 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) click to toggle source

Attempts to reformat one-line certificate bodies

@param raw_cert [String] The string of text you wish to parse into a cert @return [String] reformatted certificate body

# 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) click to toggle source

Attempts to reformat one-line certificate bodies

@param raw_cert [String] The string of text you wish to parse into a cert @return [Array] reformatted certificate content in Array format

# 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) click to toggle source

Attempts to munge a string into a valid X509 certificate

@param raw_cert [String] The string of text you wish to parse into a cert @return [OpenSSL::X509::Certificate]

# 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