Skip to content
Snippets Groups Projects
Commit e7730752 authored by Marta Rybczynska's avatar Marta Rybczynska
Browse files

scripts/cve-report.py: rename from cve-json-to-csv and add summary


Make the report generation more customizable. Apart from the CSV report,
add also a summary report including the number of CVEs in each package.

Signed-off-by: default avatarMarta Rybczynska <marta.rybczynska@huawei.com>
parent 684973b7
No related branches found
No related tags found
1 merge request!149scripts/cve-report.py: rename from cve-json-to-csv and add summary
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# CVE-check example script # CVE-check example script
# Convert the cve-check JSON output to a CSV file # Reporting based on the cve-check JSON format: conversion to CSV and summary
import sys import sys
import getopt import getopt
...@@ -11,6 +11,8 @@ import getopt ...@@ -11,6 +11,8 @@ import getopt
infile = "in.json" infile = "in.json"
outfile = "out.csv" outfile = "out.csv"
show_all = False show_all = False
show_summary = False
to_csv = False
def show_syntax_and_exit(code): def show_syntax_and_exit(code):
...@@ -19,9 +21,13 @@ def show_syntax_and_exit(code): ...@@ -19,9 +21,13 @@ def show_syntax_and_exit(code):
Arguments: Arguments:
code: the error code to return code: the error code to return
""" """
print("Syntax: %s [-h][-i inputfile][-o outputfile] [-a]" % __name__) print("Syntax: %s [-h] [-a] [-s] [-c] [-i inputfile][-o outputfile]" % sys.argv[0])
print("Default files: in.json and out.csv") print("Default files: in.json and out.csv")
print("Use -a or -all to list all issues, otherwise we filter only unpatched ones") print(
"Use -c or --to-csv to generate a CSV report, output file is then needed, out.csv by default"
)
print("Use -a or --all to list all issues, otherwise we filter only unpatched ones")
print("Use -s or --summary to show a summary of the issues")
sys.exit(code) sys.exit(code)
...@@ -42,20 +48,26 @@ def parse_args(argv): ...@@ -42,20 +48,26 @@ def parse_args(argv):
Arguments: Arguments:
argv: program arguments argv: program arguments
""" """
global infile, outfile, show_all global infile, outfile, show_all, show_summary, to_csv
try: try:
opts, args = getopt.getopt(argv, "hi:o:a", ["input", "output"]) opts, args = getopt.getopt(
argv, "hi:o:asc", ["help", "input", "output", "summary", "to-csv"]
)
except getopt.GetoptError: except getopt.GetoptError:
show_syntax_and_exit(1) show_syntax_and_exit(1)
for opt, arg in opts: for opt, arg in opts:
if opt == "-h": if opt in ("-h", "--help"):
show_syntax_and_exit(0) show_syntax_and_exit(0)
elif opt in ("-a", "--all"):
show_all = True
elif opt in ("-i", "--input"): elif opt in ("-i", "--input"):
infile = arg infile = arg
elif opt in ("-c", "--to-csv"):
to_csv = True
elif opt in ("-o", "--output"): elif opt in ("-o", "--output"):
outfile = arg outfile = arg
elif opt in ("-a", "--all"): elif opt in ("-s", "--summary"):
show_all = True show_summary = True
def load_json(filename): def load_json(filename):
...@@ -79,7 +91,7 @@ def load_json(filename): ...@@ -79,7 +91,7 @@ def load_json(filename):
return out return out
def write_csv(filename, data, unpatched_only): def process_data(filename, data, unpatched_only, do_summary, do_csv):
""" """
Write the resulting CSV with one line for each package Write the resulting CSV with one line for each package
Arguments: Arguments:
...@@ -95,6 +107,7 @@ def write_csv(filename, data, unpatched_only): ...@@ -95,6 +107,7 @@ def write_csv(filename, data, unpatched_only):
exit_error(1, "Mandatory 'package' key not found") exit_error(1, "Mandatory 'package' key not found")
lines = "" lines = ""
total_issue_count = 0
for package in data["package"]: for package in data["package"]:
keys_in_package = {"name", "layer", "version", "issue"} keys_in_package = {"name", "layer", "version", "issue"}
if keys_in_package - package.keys(): if keys_in_package - package.keys():
...@@ -107,6 +120,11 @@ def write_csv(filename, data, unpatched_only): ...@@ -107,6 +120,11 @@ def write_csv(filename, data, unpatched_only):
package_name = package["name"] package_name = package["name"]
layer = package["layer"] layer = package["layer"]
package_version = package["version"] package_version = package["version"]
package_summary = "Issues for package %s (version %s):\n\t" % (
package_name,
package_version,
)
issue_count = 0
for issue in package["issue"]: for issue in package["issue"]:
keys_in_issue = {"id", "scorev2", "scorev3", "vector", "status"} keys_in_issue = {"id", "scorev2", "scorev3", "vector", "status"}
...@@ -132,15 +150,27 @@ def write_csv(filename, data, unpatched_only): ...@@ -132,15 +150,27 @@ def write_csv(filename, data, unpatched_only):
scorev3, scorev3,
vector, vector,
) )
package_summary += "%s " % (cve_id)
issue_count += 1
if do_summary and issue_count > 0:
package_summary += "\n\tCount: %d\n" % (issue_count)
print(package_summary)
total_issue_count += issue_count
if do_csv:
with open(filename, "w") as f:
f.write(lines)
with open(filename, "w") as f: if do_summary:
f.write(lines) print("Global issue count: %d" % (total_issue_count))
def main(argv): def main(argv):
parse_args(argv) parse_args(argv)
data = load_json(infile) data = load_json(infile)
write_csv(outfile, data, not show_all) process_data(outfile, data, not show_all, show_summary, to_csv)
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment