2019-05-11 17:33:04 -05:00
|
|
|
require 'rest-client'
|
2019-05-11 18:07:09 -05:00
|
|
|
require 'zlib'
|
|
|
|
require 'json'
|
2019-05-11 17:33:04 -05:00
|
|
|
|
|
|
|
class NvdDownloader
|
2019-05-12 01:02:48 -05:00
|
|
|
attr_accessor :version, :base_url, :base_filename, :years, :filenames_json
|
2019-05-11 17:33:04 -05:00
|
|
|
|
|
|
|
MIN_YEAR = '2002'
|
|
|
|
MAX_YEAR = '2019'
|
|
|
|
AVAILABLE_YEARS = (MIN_YEAR..MAX_YEAR).to_a
|
2019-05-12 01:02:48 -05:00
|
|
|
JSON = 'json.gz'
|
2019-05-11 17:33:04 -05:00
|
|
|
|
|
|
|
def initialize
|
2019-05-12 00:41:20 -05:00
|
|
|
@version = "1.0"
|
|
|
|
@base_url = "https://nvd.nist.gov/feeds/json/cve/#{version}/"
|
|
|
|
@base_filename = "nvdcve-#{version}-"
|
2019-05-11 17:33:04 -05:00
|
|
|
@years = self.years
|
2019-05-12 01:02:48 -05:00
|
|
|
@filenames_json = self.filenames(JSON)
|
2019-05-11 17:33:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def years
|
|
|
|
year = NvdDownloader::AVAILABLE_YEARS.map do |year|
|
|
|
|
[year.to_i, year]
|
|
|
|
end.to_h
|
|
|
|
end
|
|
|
|
|
2019-05-12 00:41:20 -05:00
|
|
|
def filenames(extension)
|
|
|
|
year_filenames = years.map do |k, year|
|
|
|
|
"#{base_filename}#{year}.#{extension}"
|
|
|
|
end
|
|
|
|
|
|
|
|
other_filenames = [
|
|
|
|
"#{base_filename}recent.#{extension}",
|
|
|
|
"#{base_filename}modified.#{extension}"
|
|
|
|
]
|
|
|
|
|
|
|
|
year_filenames + other_filenames
|
2019-05-11 17:33:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(url)
|
2019-05-11 18:07:09 -05:00
|
|
|
r = RestClient.get "#{base_url}#{url}"
|
|
|
|
r.body if r.code == 200
|
|
|
|
end
|
2019-05-11 17:33:04 -05:00
|
|
|
|
2019-05-11 18:07:09 -05:00
|
|
|
def read_gzip_stream(gzip_stream)
|
|
|
|
io_stream = StringIO.new(gzip_stream)
|
|
|
|
gz = Zlib::GzipReader.new(io_stream)
|
|
|
|
gz.read
|
2019-05-11 17:33:04 -05:00
|
|
|
end
|
|
|
|
|
2019-05-12 00:41:20 -05:00
|
|
|
def write_to_file(parsed_json, file_path)
|
|
|
|
File.write(file_path, parsed_json)
|
|
|
|
end
|
|
|
|
|
2019-05-11 18:07:09 -05:00
|
|
|
def parse_json(json_string)
|
|
|
|
JSON.parse(json_string)
|
|
|
|
end
|
2019-05-12 00:41:20 -05:00
|
|
|
|
|
|
|
def one_time_import
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def modified_meta
|
|
|
|
# this should be a method that builds
|
|
|
|
# the modified filename with the meta file extension included
|
|
|
|
end
|
2019-05-12 00:47:10 -05:00
|
|
|
|
2019-05-12 00:54:45 -05:00
|
|
|
def check_metafile(metafile)
|
|
|
|
# open the metafile, build a hash of k/v pairs of the data inside of the file
|
|
|
|
# check each k/v pair against the file on disk
|
|
|
|
# return a new hash with the same k as before, but the value being a boolean true or false if the value from the k/v pair
|
|
|
|
end
|
|
|
|
|
2019-05-12 00:47:10 -05:00
|
|
|
def detect_changes(metafile)
|
|
|
|
# this should be a method that detects changes in the metafile.
|
2019-05-12 00:54:45 -05:00
|
|
|
# run the check_metafile method against the current metafile on disk.
|
|
|
|
# if there is a change, return true, if not return false
|
2019-05-12 00:47:10 -05:00
|
|
|
end
|
2019-05-11 17:33:04 -05:00
|
|
|
end
|