2
2
# SPDX-License-Identifier: GPL-3.0-or-later
3
3
4
4
from json import loads
5
+ from re import search , split
5
6
from typing import Dict
6
7
from urllib import error , request
7
8
@@ -30,23 +31,41 @@ def cve_info(
30
31
try :
31
32
if not json_data :
32
33
raise KeyError
34
+
33
35
package_state = json_data ["package_state" ]
34
- output = f'{ cve ["product" ]} : No known fix for { cve ["cve_number" ]} .'
36
+ affected_releases = json_data ["affected_release" ]
37
+
38
+ no_fix = True
39
+
40
+ for package in affected_releases :
41
+ if (
42
+ package ["product_name" ]
43
+ == f"Red Hat Enterprise Linux { self .distro_codename } "
44
+ ):
45
+ package_data = self .parse_package_data (package ["package" ])
46
+ LOGGER .info (
47
+ f'{ cve ["product" ]} : { cve ["cve_number" ]} - Status: Fixed - Fixed package: { package_data } '
48
+ )
49
+ no_fix = False
50
+
35
51
for package in package_state :
36
52
if (
37
53
package ["product_name" ]
38
54
== f"Red Hat Enterprise Linux { self .distro_codename } "
39
55
):
40
- output = f'{ cve ["product" ]} : { cve ["cve_number" ]} - Status: { package ["fix_state" ]} '
41
- if (
42
- package ["fix_state" ] == "Affected"
43
- and "upstream_fix" in json_data
44
- ):
45
- output += (
46
- f' - Fixed releases: { json_data ["upstream_fix" ]} '
47
- )
48
- break
49
- LOGGER .info (output )
56
+ package_data = self .parse_package_data (
57
+ package ["package_name" ]
58
+ )
59
+ LOGGER .info (
60
+ f'{ cve ["product" ]} : { cve ["cve_number" ]} - Status: { package ["fix_state" ]} - Related package: { package_data } '
61
+ )
62
+ no_fix = False
63
+
64
+ if no_fix :
65
+ LOGGER .info (
66
+ f'{ cve ["product" ]} : No known fix for { cve ["cve_number" ]} .'
67
+ )
68
+
50
69
except (KeyError , TypeError ):
51
70
if cve ["cve_number" ] != "UNKNOWN" :
52
71
LOGGER .info (
@@ -58,5 +77,35 @@ def get_data(self, cve_number: str, product: str):
58
77
full_query = f"{ RH_CVE_API } /{ cve_number } .json"
59
78
response = request .urlopen (full_query ).read ().decode ("utf-8" )
60
79
return loads (response )
61
- except error .HTTPError :
62
- LOGGER .info (f"{ product } : No known fix for { cve_number } ." )
80
+ except error .HTTPError as e :
81
+ LOGGER .debug (e )
82
+
83
+ def parse_package_data (self , package_data : str ) -> str :
84
+ """
85
+ Parses package name and version data from the package data provided by Red Hat.
86
+
87
+ Sample input:
88
+ nodejs:12-8040020210817133458.522a0ee4
89
+ edk2-0:20210527gite1999b264f1f-3.el8
90
+ dnsmasq-0:2.79-13.el8_3.1
91
+
92
+ Sample output:
93
+ nodejs v12
94
+ edk v2
95
+ dnsmasq v2.79
96
+
97
+ """
98
+ parsed_package_data = ""
99
+ package_name = split (r"-\d" , package_data , 1 )[0 ]
100
+ if ":" in package_name :
101
+ package_name , package_version = split (":" , package_name )
102
+ package_version = search (r"\d+" , package_version ).group (0 )
103
+ parsed_package_data = f"{ package_name } v{ package_version } "
104
+ else :
105
+ parsed_package_data = package_name
106
+ match = search (r"\d+\.\d+" , package_data )
107
+ if match :
108
+ package_version = match .group (0 )
109
+ parsed_package_data += f" v{ package_version } "
110
+
111
+ return parsed_package_data
0 commit comments