diff --git a/classes/cve_info.rb b/classes/cve_info.rb index e35f7b4..e1d49ec 100644 --- a/classes/cve_info.rb +++ b/classes/cve_info.rb @@ -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 \ No newline at end of file diff --git a/classes/nvd_downloader.rb b/classes/nvd_downloader.rb index 7b2923c..b7d6e3b 100644 --- a/classes/nvd_downloader.rb +++ b/classes/nvd_downloader.rb @@ -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