update rpm pkg audit stuff

This commit is contained in:
kenna-bmcdevitt 2020-10-31 01:57:58 -05:00
parent 70403b23dd
commit a3a8fb0c1e
3 changed files with 43 additions and 42 deletions

View file

@ -1,13 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
# this is meant to parse and query data in redhat linux rpm-to-cve.xml file
# that is posted on their security API.
# Reference: https://www.redhat.com/security/data/metrics/
require 'ox' require 'ox'
require 'json' require 'json'
require 'rest-client' require 'rest-client'
# This is meant to parse and query data in redhat linux rpm-to-cve.xml file
# That is posted on their security API.
# Documentation Reference: https://www.redhat.com/security/data/metrics/
class RhelRpmToCve class RhelRpmToCve
# filepath == /path/to/rpm-to-cve.xml # filepath == /path/to/rpm-to-cve.xml
attr_accessor :filepath, :file, :xml attr_accessor :filepath, :file, :xml
@ -49,8 +48,7 @@ class RhelRpmToCve
cves: cves, cves: cves,
cve_count: cves.count cve_count: cves.count
} }
else else 'Package not found.'
'Package not found.'
end end
end end

View file

@ -1,12 +1,11 @@
# Documentation link:
# https://access.redhat.com/documentation/en-us/red_hat_security_data_api/1.0/html/red_hat_security_data_api/overview
# frozen_string_literal: true # frozen_string_literal: true
require 'rest-client' require 'rest-client'
require 'json' require 'json'
require 'active_support/core_ext/array/grouping.rb' require 'active_support/core_ext/array/grouping.rb'
# Documentation link:
# https://access.redhat.com/documentation/en-us/red_hat_security_data_api/1.0/html/red_hat_security_data_api/overview
class RhelSecurityApiClient class RhelSecurityApiClient
attr_accessor :base_url attr_accessor :base_url
@ -14,8 +13,7 @@ class RhelSecurityApiClient
@base_url = 'https://access.redhat.com/hydra/rest/securitydata' @base_url = 'https://access.redhat.com/hydra/rest/securitydata'
end end
# params is a hash that looks like # params = {:params => {:key => value}}
# {:params => {:key => value}}
def request(path, params) def request(path, params)
r = RestClient::Request.execute( r = RestClient::Request.execute(
method: :get, method: :get,
@ -48,7 +46,7 @@ class RhelSecurityApiClient
request("/cve/#{cve_id}.json", params) request("/cve/#{cve_id}.json", params)
end end
def cves(cve_ids) def cve_ids(cve_ids)
params = {} params = {}
responses = [] responses = []
cve_ids.in_groups_of(500, false) do |cve_id_batch| cve_ids.in_groups_of(500, false) do |cve_id_batch|

View file

@ -19,59 +19,64 @@ def read_cves_file(filepath)
CSV.read(filepath).flatten CSV.read(filepath).flatten
end end
parser = OptionParser.new do |parser| def json_pp(json)
puts JSON.pretty_generate(json)
end
parserr = OptionParser.new do |parser|
parser.banner = 'Usage: rpm_pkg_audit.rb [options]' parser.banner = 'Usage: rpm_pkg_audit.rb [options]'
parser.on('-p', '--pkg PKGNAME', 'Takes a base pkg name and returns cves from redhats security API.') do |pkg| parser.on('-p', '--pkg PKGNAME',
'Takes a base pkg name and returns cves from redhats security API.') do |pkg|
options[:pkg] = pkg options[:pkg] = pkg
end end
parser.on('-l', '--list', 'List packages in the XML datafile.') do |list| parser.on('-l', '--list',
'List packages in the XML datafile.') do |list|
options[:list] = list options[:list] = list
end end
parser.on('-x', '--xmlpkg PKGNAME', 'The pkg name you want to audit from xml file rpm-to-cve.xml') do |xmlpkg| parser.on('-x', '--xmlpkg PKGNAME',
'The pkg name you want to audit from xml file rpm-to-cve.xml') do |xmlpkg|
options[:xmlpkg] = xmlpkg options[:xmlpkg] = xmlpkg
end end
parser.on('-r', '--refresh', 'Refresh rpm-to-cve.xml file with latest pkgs and cves') do |_refresh| parser.on('-r', '--refresh',
'Refresh rpm-to-cve.xml file with latest pkgs and cves') do |_refresh|
options[:refresh] = true options[:refresh] = true
end end
parser.on('-c', '--cve CVE_ID', 'Takes a cve id and returns cve json from redhats security API.') do |cve| parser.on('-c', '--cve CVE_ID',
'Takes a cve id and returns cve json from redhats security API.') do |cve|
options[:cve] = cve options[:cve] = cve
end end
parser.on('-f', '--cves-from-file CVE_FILE', 'Takes a file one cve id per line and sends a batch request to redhat security API') do |file| parser.on('-f', '--cves-from-file CVE_FILE',
'Takes a file one cve id per line and sends a batch request to redhat security API') do |file|
options[:file] = file options[:file] = file
end end
end end
parser.parse! parserr.parse!
if options[:pkg]
pkg_name = options[:pkg]
elsif options[:xmlpkg]
xmlpkg_name = options[:xmlpkg]
end
rpm_auditer = RhelRpmToCve.new(filepath: data_file) rpm_auditer = RhelRpmToCve.new(filepath: data_file)
rhel_api_client = RhelSecurityApiClient.new rhel_api_client = RhelSecurityApiClient.new
# this is getting long and nasty probably change this to a case statment if options[:xmlpkg_name]
if xmlpkg_name pkg = option[:xmlpkg_name]
json = rpm_auditer.cves_per_pkg_name(xmlpkg_name).to_json json = rpm_auditer.cves_per_pkg_name(pkg).to_json
puts JSON.pretty_generate(JSON.parse(json)) json_pp(JSON.parse(json))
elsif pkg_name elsif options[:pkg]
params = { params: { package: pkg_name } } pkg = options[:pkg]
json_response = rhel_api_client.request('/cve.json', params) params = { params: { package: pkg } }
cve_pkgs_and_adv = rhel_api_client.cve_pkg_adv(json_response) json = rhel_api_client.request('/cve.json', params)
puts JSON.pretty_generate(cve_pkgs_and_adv) cve_pkgs_and_adv = rhel_api_client.cve_pkg_adv(json)
json_pp(cve_pkgs_and_adv)
elsif options[:refresh] elsif options[:refresh]
rpm_auditer.refresh_rpm_to_cve_file('./data/rpm-to-cve.xml') rpm_auditer.refresh_rpm_to_cve_file('./data/rpm-to-cve.xml')
elsif options[:cve] elsif options[:cve]
cve = options[:cve] id = options[:cve]
r = rhel_api_client.cve_id(cve) json = rhel_api_client.cve_id(id)
puts JSON.pretty_generate(r) json_pp(json)
elsif options[:file] elsif options[:file]
filepath = options[:file] filepath = options[:file]
cve_ids = read_cves_file(filepath) cve_ids = read_cves_file(filepath)
r = rhel_api_client.cves(cve_ids) json = rhel_api_client.cve_ids(cve_ids)
puts JSON.pretty_generate(r) json_pp(json)
else options.key?(:list) elsif options[:list]
puts rpm_auditer.list_pkg_names.sort puts rpm_auditer.list_pkg_names.sort
end end