misc_rbtools/rpm_to_cve_parser/rhel_rpm_to_cve.rb

48 lines
921 B
Ruby
Raw Normal View History

2020-02-04 17:15:20 -06:00
require 'ox'
class RhelRpmToCve
# filepath == /path/to/rpm-to-cve.xml
attr_accessor :filepath, :file, :xml
def initialize(filepath)
@filepath = filepath
@file = File.read(filepath)
@xml = Ox.parse(file)
2020-02-04 17:15:20 -06:00
end
def list_pkg_names
xml.rpms.locate("?/@rpm")
2020-02-04 17:15:20 -06:00
end
def pkg_exists?(pkg_name)
list_pkg_names.include? pkg_name
end
def cves_per_pkg_name(pkg_name)
if pkg_exists? pkg_name
results = find_pkg(pkg_name).locate('*/cve').map do |r|
r.text
2020-02-04 17:15:20 -06:00
end.compact
{
:rhel_package_name => pkg_name,
:cves => results.map {|cve| cve}
}
2020-10-25 04:13:21 -05:00
else
'Package not found.'
2020-02-04 17:15:20 -06:00
end
end
def find_pkg(pkg_name)
xml.rpms.locate("rpm[@rpm=#{pkg_name}]").first
2020-02-04 17:15:20 -06:00
end
def convert_to_json
pkgs = list_pkg_names
pkgs_and_cves = pkgs.map do |pkg_name|
cves_per_pkg_name(pkg_name)
end
pkgs_and_cves.to_json
end
2020-02-04 17:15:20 -06:00
end