61 lines
1.3 KiB
Ruby
61 lines
1.3 KiB
Ruby
require 'json'
|
|
require 'rest-client'
|
|
require 'bulk_insert'
|
|
|
|
class CnaImporter
|
|
attr_accessor :url
|
|
def initialize
|
|
@url = 'https://raw.githubusercontent.com/CVEProject/cve-website/dev/src/assets/data/CNAsList.json'
|
|
end
|
|
|
|
def send_request_rest
|
|
RestClient::Request.execute(
|
|
method: :get,
|
|
url: url
|
|
)
|
|
end
|
|
|
|
def parse_res(response)
|
|
JSON.parse(response.body)
|
|
end
|
|
|
|
def get_json
|
|
res = send_request_rest
|
|
if res.code == 200
|
|
parse_res(res)
|
|
else
|
|
"HTTP Status: #{res.code}"
|
|
end
|
|
end
|
|
|
|
def json_to_hash(json)
|
|
data_hash = {}
|
|
data_hash[:short_name] = json['shortName']
|
|
data_hash[:cna_id] = json['cnaID']
|
|
data_hash[:organization_name] = json['organizationName']
|
|
data_hash[:scope] = json['scope']
|
|
data_hash[:contact] = json['contact']
|
|
data_hash[:disclosure_policy] = json['disclosurePolicy']
|
|
data_hash[:security_advisories] = json['securityAdvisories']
|
|
data_hash[:resources] = json['resources']
|
|
data_hash[:cna] = json['CNA']
|
|
data_hash[:country] = json['country']
|
|
data_hash
|
|
end
|
|
|
|
def bulk_insert(cves)
|
|
Cna.bulk_insert do |worker|
|
|
cves.each do |attrs|
|
|
worker.add(attrs)
|
|
end
|
|
end
|
|
end
|
|
|
|
def import
|
|
json = get_json
|
|
attrs = json.map {|j| json_to_hash(j) }
|
|
puts "Now importing CNAs."
|
|
bulk_insert(attrs)
|
|
end
|
|
|
|
end
|