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.



59
60
61
62
63
64
# File 'lib/certutil/certificate_parser.rb', line 59

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



51
52
53
54
55
56
57
# File 'lib/certutil/certificate_parser.rb', line 51

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

  @decoded
end

#flipit!Object



47
48
49
# File 'lib/certutil/certificate_parser.rb', line 47

def flipit!
  @certs = certs.reverse
end

#write_crt_file!Object



75
76
77
78
79
80
# File 'lib/certutil/certificate_parser.rb', line 75

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



66
67
68
69
70
71
72
73
# File 'lib/certutil/certificate_parser.rb', line 66

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



90
91
92
93
94
95
# File 'lib/certutil/certificate_parser.rb', line 90

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



82
83
84
85
86
87
88
# File 'lib/certutil/certificate_parser.rb', line 82

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