added some one time import stuff to nvd downloader and have cve_info searching through gzipped files now for cve id

This commit is contained in:
booboy 2019-06-12 01:20:44 -05:00
parent 8a0757c10e
commit 63a62fac79
2 changed files with 51 additions and 13 deletions

View file

@ -1,16 +1,48 @@
class CveReport
attr_accessor :filename, :file
# 1st way:
# expects a hash with the following key:values # {:filename => "nvdcve-1.0-2002.json.gz",
# :json => {"CVE_data_type"=>"CVE",
# "CVE_Items"= etc...etc...etc...}
# 2nd way:
# a json gz compressed file of cve metadata downloaded from nvd
# TO ADD: 1st way.
def cves
# return a list of cve data from the given filename in an array.
self.from_file(filename)
def initialize(filename)
@filename = filename
@file = from_file
end
def from_file(filename)
# lookup info from the given json.gz filename of cve information.
def cve_ids
# return a list of cve data from the given filename in an array.
file["CVE_Items"].map do |item|
item["cve"]["CVE_data_meta"]["ID"]
end
end
def cve_id?(cve_id)
cve_ids.include?(cve_id)
end
def cve(cve_id)
find(cve_id)
end
def cves
file["CVE_Items"].map do |cve|
cve
end
end
def from_file
Zlib::GzipReader.open(filename) do |gz|
JSON.parse(gz.read)
end
end
def find(cve_id)
# this should look in the returned array of cve information from self.cves
# and return the given information for the cve_id
cves.select do |cve|
cve["cve"]["CVE_data_meta"]["ID"] == cve_id
end
end
end
end

View file

@ -53,17 +53,23 @@ module NvdTools
JSON.parse(json_string)
end
def one_time_import
def one_time_import(to_file=false)
# experimenting with how i want to do this.
# right now its a loop through the filenames
# and parse all into a json string and store in an array
self.filenames_json.map do |filename|
r = client.get(filename)
sleep(1)
json_string = read_gzip_stream(r.body)
parsed_json = parse_json(json_string)
gzip_stream = r.body
json_string = read_gzip_stream(gzip_stream)
parse_json(json_string)
{ :filename => filename,
:json => parsed_json
}
if to_file
filepath = "../data/cve/#{filename}"
write_to_file(r.body, filepath)
end
end
# this should be a method that does a one-time import
# of all of the json.gz from each year + recent + modified json feeds