summaryrefslogtreecommitdiffstats
path: root/scripts/cve-json-to-text.py
blob: 5531ee5eb609e799cb316a446a845710ace967d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/env python3
# SPDX-FileCopyrightText: OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT

# CVE results conversion script: JSON format to text
# Derived from cve-report.py from Oniro (MIT, by Huawei Inc)

import sys
import getopt

infile = "in.json"
outfile = "out.txt"


def show_syntax_and_exit(code):
    """
    Show the program syntax and exit with an errror
    Arguments:
        code: the error code to return
    """
    print("Syntax: %s [-h] [-i inputJSONfile][-o outputfile]" % sys.argv[0])
    sys.exit(code)


def exit_error(code, message):
    """
    Show the error message and exit with an errror
    Arguments:
        code: the error code to return
        message: the message to show
    """
    print("Error: %s" % message)
    sys.exit(code)


def parse_args(argv):
    """
    Parse the program arguments, put options in global variables
    Arguments:
        argv: program arguments
    """
    global infile, outfile
    try:
        opts, args = getopt.getopt(
            argv, "hi:o:", ["help", "input", "output"]
        )
    except getopt.GetoptError:
        show_syntax_and_exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            show_syntax_and_exit(0)
        elif opt in ("-a", "--all"):
            show_all = True
            show_unknown = True
        elif opt in ("-i", "--input"):
            infile = arg

def load_json(filename):
    """
    Load the JSON file, return the resulting dictionary
    Arguments:
        filename: the file to open
    Returns:
        Parsed file as a dictionary
    """
    import json

    out = {}
    try:
        with open(filename, "r") as f:
            out = json.load(f)
    except FileNotFoundError:
        exit_error(1, "Input file (%s) not found" % (filename))
    except json.decoder.JSONDecodeError as error:
        exit_error(1, "Malformed JSON file: %s" % str(error))
    return out


def process_data(filename, data):
    """
    Write the resulting CSV with one line for each package
    Arguments:
        filename: the file to write to
        data: dictionary from parsing the JSON file
    Returns:
        None
    """
    if not "version" in data or data["version"] != "1":
        exit_error(1, "Unrecognized format version number")
    if not "package" in data:
        exit_error(1, "Mandatory 'package' key not found")

    lines = ""
    total_issue_count = 0
    for package in data["package"]:
        package_info = ""
        keys_in_package = {"name", "layer", "version", "issue"}
        if keys_in_package - package.keys():
            exit_error(
                1,
                "Missing a mandatory key in package: %s"
                % (keys_in_package - package.keys()),
            )

        package_info += "LAYER: %s\n" % package["layer"]
        package_info += "PACKAGE NAME: %s\n" % package["name"]
        package_info += "PACKAGE VERSION: %s\n" % package["version"]

        for issue in package["issue"]:
            keys_in_issue = {"id", "status", "detail"}
            if keys_in_issue - issue.keys():
                print("Warning: Missing keys %s in 'issue' for the package '%s'"
                    % (keys_in_issue - issue.keys(), package["name"]))

            lines += package_info
            lines += "CVE: %s\n" % issue["id"]
            lines += "CVE STATUS: %s\n" % issue["status"]
            lines += "CVE DETAIL: %s\n" % issue["detail"]
            if "description" in issue:
                lines += "CVE DESCRIPTION: %s\n" % issue["description"]
            if "summary" in issue:
                lines += "CVE SUMMARY: %s\n" % issue["summary"]
            if "scorev2" in issue:
                lines += "CVSS v2 BASE SCORE: %s\n" % issue["scorev2"]
            if "scorev3" in issue:
                lines += "CVSS v3 BASE SCORE: %s\n" % issue["scorev3"]
            if "vector" in issue:
                lines += "VECTOR: %s\n" % issue["vector"]
            if "vectorString" in issue:
                lines += "VECTORSTRING: %s\n" % issue["vectorString"]
            lines += "MORE INFORMATION: https://nvd.nist.gov/vuln/detail/%s\n" % issue["id"]
            lines += "\n"

    with open(filename, "w") as f:
        f.write(lines)

def main(argv):
    parse_args(argv)
    data = load_json(infile)
    process_data(outfile, data)


if __name__ == "__main__":
    main(sys.argv[1:])