data_importer/lib/oval_parser.rb

54 lines
1.6 KiB
Ruby
Raw Normal View History

require 'nokogiri'
class OvalParser
attr_accessor :data, :doc, :root
def initialize(data)
@data = data
@doc = Nokogiri::XML(data)
@root = doc.root
end
def get_definitions
root.xpath("//xmlns:definition")
end
def get_def_ids
definitions = get_definitions
definitions.map { |d| d.xpath(".//@id").text }
end
def oval_defs_for_id(id)
definitions = get_definitions
definitions.select do |definition|
definition if definition.attributes.dig('id').value == id
end
end
def get_cve_def_info
# largerly used this code here as a guide:
# https://github.com/OpenSCAP/openscap-daemon/blob/1b9e9d4849573e1ce09728cc61c4564e5d605a8e/openscap_daemon/cve_scanner/generate_summary.py#L83-L104
def_ids = get_def_ids
def_ids.map do |id|
oval_defs = oval_defs_for_id(id)
oval_defs.map do |oval_def|
title = oval_def.xpath(".//xmlns:title").text
cve_meta = oval_def.xpath(".//xmlns:reference").select { |n| n.attributes.dig('source').value == 'CVE' }
cve_ids = cve_meta.map { |cve_m| cve_m.attributes.dig('ref_id').value || 'None' }
cve_urls = cve_meta.map { |cve_m| cve_m.attributes.dig('ref_url').value || 'None' }
description = oval_def.xpath(".//xmlns:description").text
severity = oval_def.xpath(".//xmlns:severity").text
my_sample_data_hash = {
:title => title,
:cve_ids => cve_ids,
:cve_urls => cve_urls,
:description => description,
:severity => severity
}
binding.pry
my_sample_data_hash
{}
end
end
end
end