Skip to content

Commit d7627ef

Browse files
authored
Merge pull request #757 from TG1999/fix_redhat_importer
Put network calls in try/except block for redhat importer
2 parents d1686db + 31ba98a commit d7627ef

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

vulnerabilities/importers/redhat.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def fetch_list_of_cves() -> Iterable[List[Dict]]:
4848
page_no = 1
4949
cve_data = None
5050
while True:
51-
current_url = f"https://access.redhat.com/hydra/rest/securitydata/cve.json?per_page=10000&page={page_no}" # nopep8
51+
current_url = f"https://access.redhat.com/hydra/rest/securitydata/cve.json?per_page=1000&page={page_no}" # nopep8
5252
try:
5353
response = requests_session.get(current_url)
5454
if response.status_code != requests.codes.ok:
@@ -64,14 +64,12 @@ def fetch_list_of_cves() -> Iterable[List[Dict]]:
6464
yield cve_data
6565

6666

67-
def get_bugzilla_data(bugzilla):
68-
return requests_session.get(f"https://bugzilla.redhat.com/rest/bug/{bugzilla}").json()
69-
70-
71-
def get_rhsa_data(rh_adv):
72-
return requests_session.get(
73-
f"https://access.redhat.com/hydra/rest/securitydata/cvrf/{rh_adv}.json"
74-
).json()
67+
def get_data_from_url(url):
68+
try:
69+
return requests_session.get(url).json()
70+
except Exception as e:
71+
logger.error(f"Failed to fetch results from {url} {e!r}")
72+
return {}
7573

7674

7775
class RedhatImporter(Importer):
@@ -112,25 +110,24 @@ def to_advisory(advisory_data):
112110
bugzilla = advisory_data.get("bugzilla")
113111
if bugzilla:
114112
url = "https://bugzilla.redhat.com/show_bug.cgi?id={}".format(bugzilla)
115-
bugzilla_data = get_bugzilla_data(bugzilla)
116-
if (
117-
bugzilla_data.get("bugs")
118-
and len(bugzilla_data["bugs"])
119-
and bugzilla_data["bugs"][0].get("severity")
120-
):
121-
bugzilla_severity_val = bugzilla_data["bugs"][0]["severity"]
122-
bugzilla_severity = VulnerabilitySeverity(
123-
system=severity_systems.REDHAT_BUGZILLA,
124-
value=bugzilla_severity_val,
125-
)
126-
127-
references.append(
128-
Reference(
129-
severities=[bugzilla_severity],
130-
url=url,
131-
reference_id=bugzilla,
113+
bugzilla_url = f"https://bugzilla.redhat.com/rest/bug/{bugzilla}"
114+
bugzilla_data = get_data_from_url(bugzilla_url)
115+
bugs = bugzilla_data.get("bugs") or []
116+
if bugs:
117+
# why [0] only here?
118+
severity = bugs[0].get("severity")
119+
if severity:
120+
bugzilla_severity = VulnerabilitySeverity(
121+
system=severity_systems.REDHAT_BUGZILLA,
122+
value=severity,
123+
)
124+
references.append(
125+
Reference(
126+
severities=[bugzilla_severity],
127+
url=url,
128+
reference_id=bugzilla,
129+
)
132130
)
133-
)
134131

135132
for rh_adv in advisory_data.get("advisories") or []:
136133
# RH provides 3 types of advisories RHSA, RHBA, RHEA. Only RHSA's contain severity score.
@@ -141,8 +138,10 @@ def to_advisory(advisory_data):
141138
continue
142139

143140
if "RHSA" in rh_adv.upper():
144-
rhsa_data = get_rhsa_data(rh_adv)
145-
141+
rhsa_url = f"https://access.redhat.com/hydra/rest/securitydata/cvrf/{rh_adv}.json"
142+
rhsa_data = get_data_from_url(rhsa_url)
143+
if not rhsa_data:
144+
continue
146145
rhsa_aggregate_severities = []
147146
if rhsa_data.get("cvrfdoc"):
148147
# not all RHSA errata have a corresponding CVRF document
@@ -189,7 +188,9 @@ def to_advisory(advisory_data):
189188
alias = advisory_data.get("CVE")
190189
if alias:
191190
aliases.append(alias)
192-
references.append(Reference(severities=redhat_scores, url=advisory_data["resource_url"]))
191+
resource_url = advisory_data.get("resource_url")
192+
if resource_url:
193+
references.append(Reference(severities=redhat_scores, url=resource_url))
193194
return AdvisoryData(
194195
aliases=aliases,
195196
summary=advisory_data.get("bugzilla_description") or "",

vulnerabilities/tests/test_data/redhat/redhat-input.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"public_date": "2022-04-19T20:00:00Z",
2121
"advisories": [
2222
"RHSA-2022:1439",
23-
"RHSA-2022:1437"
23+
"RHSA-2022:1437",
24+
"RHSA-2022:1436"
2425
],
2526
"bugzilla": 2075788,
2627
"bugzilla_description": "CVE-2022-21426 OpenJDK: Unbounded memory allocation when compiling crafted XPath expressions (JAXP, 8270504)",

vulnerabilities/tests/test_redhat_importer.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,23 @@ def test_rpm_to_purl():
4646

4747

4848
@patch("vulnerabilities.importers.redhat.fetch_list_of_cves")
49-
@patch("vulnerabilities.importers.redhat.get_rhsa_data")
50-
@patch("vulnerabilities.importers.redhat.get_bugzilla_data")
51-
def test_redhat_importer(bugzilla, rhsa, fetcher):
49+
@patch("vulnerabilities.importers.redhat.get_data_from_url")
50+
def test_redhat_importer(get_data_from_url, fetcher):
5251
redhat_importer = redhat.RedhatImporter()
5352
response_file = os.path.join(TEST_DATA, f"redhat-input.json")
5453

5554
with open(response_file) as f:
5655
fetcher.return_value = [json.load(f)]
5756
bugzilla_2075788_response_file = os.path.join(TEST_DATA, f"bugzilla-2075788.json")
5857
bugzilla_2077736_response_file = os.path.join(TEST_DATA, f"bugzilla-2077736.json")
59-
bugzilla.side_effect = [
60-
json.load(open(bugzilla_2075788_response_file)),
61-
json.load(open(bugzilla_2077736_response_file)),
62-
]
6358
rhsa_1437 = os.path.join(TEST_DATA, f"RHSA-2022:1437.json")
6459
rhsa_1439 = os.path.join(TEST_DATA, f"RHSA-2022:1439.json")
65-
rhsa.side_effect = [
66-
json.load(open(rhsa_1437)),
60+
get_data_from_url.side_effect = [
61+
json.load(open(bugzilla_2075788_response_file)),
62+
json.load(open(bugzilla_2077736_response_file)),
6763
json.load(open(rhsa_1439)),
64+
json.load(open(rhsa_1437)),
65+
None,
6866
]
6967
expected_file = os.path.join(TEST_DATA, f"redhat-expected.json")
7068
imported_data = list(redhat_importer.advisory_data())

0 commit comments

Comments
 (0)