Class: CertificateParser

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::SH
Defined in:
lib/certutil/certificate_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ CertificateParser

Returns a new instance of CertificateParser.



55
56
57
58
59
60
# File 'lib/certutil/certificate_parser.rb', line 55

def initialize(attrs = {})
  @input = attrs[:string]
  @name = attrs[:name]
  @path = Dir.pwd
  info "Certificate loaded#{' from ' + attrs[:source].to_s if attrs[:source]}: #{@name}."
end

Class Method Details

.new_from_file(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/certutil/certificate_parser.rb', line 12

def self.new_from_file(path)
  # should return nil if it's not a valid cert

  debug "Parsing #{path}..."
  begin
    string = File.read(path)
    if string
      path_array = path.split(File::SEPARATOR)
      debug "Creating..."
      self.new(string: string, name: path_array.last.gsub(/(\.crt)|(.pem)/, ""), source: :file)
    end
  rescue 
    nil
  end
end

.new_from_gist(gist_link) ⇒ Object



35
36
37
# File 'lib/certutil/certificate_parser.rb', line 35

def self.new_from_gist(gist_link)
  # not yet
end

.new_from_hostname(hostname) ⇒ Object



28
29
30
31
32
33
# File 'lib/certutil/certificate_parser.rb', line 28

def self.new_from_hostname(hostname)
  debug "Fetching certificate from #{hostname}..."
  debug "Creating..."
  string = `openssl s_client -connect #{hostname}:443 -showcerts </dev/null`
  self.new(string: string, name: hostname, source: :hostname) unless string == ""
end

.new_from_input(input) ⇒ Object



8
9
10
# File 'lib/certutil/certificate_parser.rb', line 8

def self.new_from_input(input)
  new_from_file(File.expand_path(input)) || new_from_hostname(input)
end

.new_from_string(string, opts = {}) ⇒ Object



39
40
41
# File 'lib/certutil/certificate_parser.rb', line 39

def self.new_from_string(string, opts = {})
  self.new(string: string, name: (opts[:name] || "temp"))
end

Instance Method Details

#certsObject



43
44
45
# File 'lib/certutil/certificate_parser.rb', line 43

def certs
  @certs ||= split_input
end

#decodedObject



47
48
49
50
51
52
53
# File 'lib/certutil/certificate_parser.rb', line 47

def decoded
  debug "lazy loading decoded..."
  @decoded ||= decode
  debug "lazy loading done."

  @decoded
end

#write_crt_file!Object



71
72
73
74
75
76
# File 'lib/certutil/certificate_parser.rb', line 71

def write_crt_file!
  info "Writing .crt file..."
  joined = certs.join("\n") + "\n"
  File.open(File.join(@path, "#{@name}.crt"), "w") { |f| f.write(joined) }
  debug "--> Wrote #{@path} -- #{@name}.crt."
end

#write_crt_files!Object



62
63
64
65
66
67
68
69
# File 'lib/certutil/certificate_parser.rb', line 62

def write_crt_files!
  info "Writing separate .crt files..."
  certs.each_with_index do |cert, i|
    debug "right now, wd is #{Dir.pwd}"
    File.open(File.join(@path, "#{@name}-#{i}.crt"), "w") { |f| f.write(cert) }
    debug "--> Wrote #{@path} -- #{@name}-#{i}.crt."
  end
end

#write_txt_file!Object



86
87
88
89
90
91
# File 'lib/certutil/certificate_parser.rb', line 86

def write_txt_file!
  info "Writing .txt file..."
  joined = decoded.join("\n-----\n") + "\n"
  File.open(File.join(@path, "#{@name}-decoded.txt"), "w") { |f| f.write(joined) }
  debug "--> Wrote #{@path}/#{@name}-decoded.txt."
end

#write_txt_files!Object



78
79
80
81
82
83
84
# File 'lib/certutil/certificate_parser.rb', line 78

def write_txt_files!
  info "Writing separate .txt files..."
  decoded.each_with_index do |cert, i|
    File.open(File.join(@path, "#{@name}-#{i}-decoded.txt"), "w") { |f| f.write(cert) }
    debug "--> Wrote #{@path}/#{@name}-#{i}-decoded.txt."
  end
end