wrapped in nvd_tools module

This commit is contained in:
booboy 2019-05-12 20:34:52 -05:00
parent edb87d3148
commit 07ba0f4fc9
2 changed files with 53 additions and 48 deletions

View file

@ -1,8 +1,10 @@
require 'rest-client' require 'rest-client'
require 'zlib' require 'zlib'
require 'json' require 'json'
require '../modules/nvd_tools'
class NvdDownloader module NvdTools
class NvdDownloader
attr_accessor :version, :base_url, :base_filename, :years, :filenames_json attr_accessor :version, :base_url, :base_filename, :years, :filenames_json
MIN_YEAR = '2002' MIN_YEAR = '2002'
@ -11,70 +13,71 @@ class NvdDownloader
JSON = 'json.gz' JSON = 'json.gz'
def initialize def initialize
@version = "1.0" @version = "1.0"
@base_url = "https://nvd.nist.gov/feeds/json/cve/#{version}/" @base_url = "https://nvd.nist.gov/feeds/json/cve/#{version}/"
@base_filename = "nvdcve-#{version}-" @base_filename = "nvdcve-#{version}-"
@years = self.years @years = self.years
@filenames_json = self.filenames(JSON) @filenames_json = self.filenames(JSON)
end end
def years def years
year = NvdDownloader::AVAILABLE_YEARS.map do |year| year = NvdDownloader::AVAILABLE_YEARS.map do |year|
[year.to_i, year] [year.to_i, year]
end.to_h end.to_h
end end
def filenames(extension) def filenames(extension)
year_filenames = years.map do |k, year| year_filenames = years.map do |k, year|
"#{base_filename}#{year}.#{extension}" "#{base_filename}#{year}.#{extension}"
end end
other_filenames = [ other_filenames = [
"#{base_filename}recent.#{extension}", "#{base_filename}recent.#{extension}",
"#{base_filename}modified.#{extension}" "#{base_filename}modified.#{extension}"
] ]
year_filenames + other_filenames year_filenames + other_filenames
end end
def get(url) def get(url)
r = RestClient.get "#{base_url}#{url}" r = RestClient.get "#{base_url}#{url}"
r.body if r.code == 200 r.body if r.code == 200
end end
def read_gzip_stream(gzip_stream) def read_gzip_stream(gzip_stream)
io_stream = StringIO.new(gzip_stream) io_stream = StringIO.new(gzip_stream)
gz = Zlib::GzipReader.new(io_stream) gz = Zlib::GzipReader.new(io_stream)
gz.read gz.read
end end
def write_to_file(parsed_json, file_path) def write_to_file(parsed_json, file_path)
File.write(file_path, parsed_json) File.write(file_path, parsed_json)
end end
def parse_json(json_string) def parse_json(json_string)
JSON.parse(json_string) JSON.parse(json_string)
end end
def one_time_import def one_time_import
# this should be a method that does a 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 # of all of the json.gz from each year + recent + modified json feeds
end end
def modified_meta def modified_meta
# this should be a method that builds # this should be a method that builds
# the modified filename with the meta file extension included # the modified filename with the meta file extension included
end end
def check_metafile(metafile) def check_metafile(metafile)
# open the metafile, build a hash of k/v pairs of the data inside of the file # 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 # 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 # 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 end
def detect_changes(metafile) def detect_changes(metafile)
# this should be a method that detects changes in the metafile. # this should be a method that detects changes in the metafile.
# run the check_metafile method against the current metafile on disk. # run the check_metafile method against the current metafile on disk.
# if there is a change, return true, if not return false # if there is a change, return true, if not return false
end end
end
end end

2
modules/nvd_tools.rb Normal file
View file

@ -0,0 +1,2 @@
module NvdTools
end