Skip to content

Commit a288752

Browse files
committed
Put network calls in try/except block for redhat importer
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent d1686db commit a288752

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

vulnerabilities/importers/redhat.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
requests_session = requests_with_5xx_retry(max_retries=5, backoff_factor=1)
4545

4646

47+
def get_response(url):
48+
try:
49+
return requests_session.get(url)
50+
except Exception as e:
51+
logger.error(f"Failed to fetch results from {url} {e}")
52+
return None
53+
54+
4755
def fetch_list_of_cves() -> Iterable[List[Dict]]:
4856
page_no = 1
4957
cve_data = None
@@ -64,14 +72,12 @@ def fetch_list_of_cves() -> Iterable[List[Dict]]:
6472
yield cve_data
6573

6674

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()
75+
def get_data_from_url(url):
76+
try:
77+
return requests_session.get(url).json()
78+
except Exception as e:
79+
logger.error(f"Failed to fetch results from {url} {e!r}")
80+
return
7581

7682

7783
class RedhatImporter(Importer):
@@ -112,9 +118,11 @@ def to_advisory(advisory_data):
112118
bugzilla = advisory_data.get("bugzilla")
113119
if bugzilla:
114120
url = "https://bugzilla.redhat.com/show_bug.cgi?id={}".format(bugzilla)
115-
bugzilla_data = get_bugzilla_data(bugzilla)
121+
bugzilla_url = f"https://bugzilla.redhat.com/rest/bug/{bugzilla}"
122+
bugzilla_data = get_data_from_url(bugzilla_url)
116123
if (
117-
bugzilla_data.get("bugs")
124+
bugzilla_data
125+
and bugzilla_data.get("bugs")
118126
and len(bugzilla_data["bugs"])
119127
and bugzilla_data["bugs"][0].get("severity")
120128
):
@@ -141,8 +149,10 @@ def to_advisory(advisory_data):
141149
continue
142150

143151
if "RHSA" in rh_adv.upper():
144-
rhsa_data = get_rhsa_data(rh_adv)
145-
152+
rhsa_url = f"https://access.redhat.com/hydra/rest/securitydata/cvrf/{rh_adv}.json"
153+
rhsa_data = get_data_from_url(rhsa_url)
154+
if not rhsa_data:
155+
continue
146156
rhsa_aggregate_severities = []
147157
if rhsa_data.get("cvrfdoc"):
148158
# not all RHSA errata have a corresponding CVRF document

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, caplog):
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)