From 1006b33c64df3d800c28cd39f3e7fa1c44baddbe Mon Sep 17 00:00:00 2001 From: booboy Date: Sun, 12 May 2019 23:56:12 -0500 Subject: [PATCH] added more to nvd downloader and made a cve reporter that will query it --- classes/cve_info.rb | 16 ++++++++++++++-- classes/nvd_client.rb | 17 +++++++++++++++++ classes/nvd_downloader.rb | 30 ++++++++++++++++++------------ 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 classes/nvd_client.rb diff --git a/classes/cve_info.rb b/classes/cve_info.rb index b49c441..c585a16 100644 --- a/classes/cve_info.rb +++ b/classes/cve_info.rb @@ -1,4 +1,16 @@ -class CveInfo - def initialize +class CveReport + + def cves + # return a list of cve data from the given filename in an array. + self.from_file(filename) + end + + def from_file(filename) + # lookup info from the given json.gz filename of cve information. + 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 end end \ No newline at end of file diff --git a/classes/nvd_client.rb b/classes/nvd_client.rb new file mode 100644 index 0000000..3316749 --- /dev/null +++ b/classes/nvd_client.rb @@ -0,0 +1,17 @@ +require '../modules/nvd_tools' +require 'rest-client' + +module NvdTools + class NvdClient + attr_accessor :version, :base_url, :rest_client + def initialize(rest_client: RestClient) + @version = "1.0" + @base_url = "https://nvd.nist.gov/feeds/json/cve/#{version}/" + @rest_client = rest_client + end + + def get(url) + rest_client.get "#{base_url}#{url}" + end + end +end diff --git a/classes/nvd_downloader.rb b/classes/nvd_downloader.rb index df3cdfc..3342d3e 100644 --- a/classes/nvd_downloader.rb +++ b/classes/nvd_downloader.rb @@ -5,19 +5,19 @@ require '../modules/nvd_tools' module NvdTools class NvdDownloader - attr_accessor :version, :base_url, :base_filename, :years, :filenames_json + attr_accessor :version, :base_url, :base_filename, :years, :filenames_json, :client MIN_YEAR = '2002' MAX_YEAR = '2019' AVAILABLE_YEARS = (MIN_YEAR..MAX_YEAR).to_a - JSON = 'json.gz' + GZIPPED_JSON_EXTENSION = 'json.gz' - def initialize - @version = "1.0" - @base_url = "https://nvd.nist.gov/feeds/json/cve/#{version}/" - @base_filename = "nvdcve-#{version}-" + def initialize(client: NvdClient.new) + + @base_filename = "nvdcve-#{client.version}-" @years = self.years - @filenames_json = self.filenames(JSON) + @filenames_json = self.filenames(GZIPPED_JSON_EXTENSION) + @client = client end def years @@ -39,11 +39,6 @@ module NvdTools year_filenames + other_filenames end - def get(url) - r = RestClient.get "#{base_url}#{url}" - r.body if r.code == 200 - end - def read_gzip_stream(gzip_stream) io_stream = StringIO.new(gzip_stream) gz = Zlib::GzipReader.new(io_stream) @@ -59,6 +54,17 @@ module NvdTools end def one_time_import + # 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) + + gzip_stream = r.body + json_string = read_gzip_stream(gzip_stream) + parse_json(json_string) + 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 end