From f0e56e2515c538f6ceae79d0c76531624a745bc0 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Wed, 1 Nov 2023 11:05:44 -0400 Subject: [PATCH] use new http wrapper in amazon provider Signed-off-by: Will Murphy --- src/vunnel/providers/amazon/parser.py | 29 +++++++--------------- tests/unit/providers/amazon/test_amazon.py | 3 ++- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/vunnel/providers/amazon/parser.py b/src/vunnel/providers/amazon/parser.py index fbbcd9b5..95399fb5 100644 --- a/src/vunnel/providers/amazon/parser.py +++ b/src/vunnel/providers/amazon/parser.py @@ -7,10 +7,8 @@ from html.parser import HTMLParser import defusedxml.ElementTree as ET -import requests -from vunnel import utils -from vunnel.utils import rpm +from vunnel.utils import http, rpm namespace = "amzn" @@ -48,17 +46,13 @@ def __init__(self, workspace, download_timeout=125, security_advisories=None, lo logger = logging.getLogger(self.__class__.__name__) self.logger = logger - @utils.retry_with_backoff() def _download_rss(self, rss_url, rss_file): try: self.logger.info(f"downloading amazon security advisory from {rss_url}") self.urls.append(rss_url) - r = requests.get(rss_url, timeout=self.download_timeout) - if r.status_code == 200: - with open(rss_file, "w", encoding="utf-8") as fp: - fp.write(r.text) - else: - raise Exception(f"GET {rss_url} failed with HTTP error {r.status_code}") + r = http.get(rss_url, self.logger, timeout=self.download_timeout) + with open(rss_file, "w", encoding="utf-8") as fp: + fp.write(r.text) except Exception: self.logger.exception("error downloading amazon linux vulnerability feeds") raise @@ -91,23 +85,18 @@ def _parse_rss(self, file_path): return sorted(alas_summaries) - @utils.retry_with_backoff() def _get_alas_html(self, alas_url, alas_file, skip_if_exists=True): if skip_if_exists and os.path.exists(alas_file): # read alas from disk if its available self.logger.debug(f"loading existing ALAS from {alas_file}") with open(alas_file, encoding="utf-8") as fp: content = fp.read() return content # noqa: RET504 - try: - self.logger.debug(f"downloading ALAS from {alas_url}") - r = requests.get(alas_url, timeout=self.download_timeout) - if r.status_code == 200: - content = r.text - with open(alas_file, "w", encoding="utf-8") as fp: - fp.write(content) - return content - raise Exception(f"GET {alas_url} failed with HTTP error {r.status_code}") + r = http.get(alas_url, self.logger, timeout=self.download_timeout) + content = r.text + with open(alas_file, "w", encoding="utf-8") as fp: + fp.write(content) + return content except Exception: self.logger.exception(f"error downloading data from {alas_url}") raise diff --git a/tests/unit/providers/amazon/test_amazon.py b/tests/unit/providers/amazon/test_amazon.py index 4437df6d..f0ed1c59 100644 --- a/tests/unit/providers/amazon/test_amazon.py +++ b/tests/unit/providers/amazon/test_amazon.py @@ -4,6 +4,7 @@ import pytest from vunnel import result, workspace +from vunnel.utils import http from vunnel.providers.amazon import Config, Provider, parser @@ -81,7 +82,7 @@ def disable_get_requests(monkeypatch): def disabled(*args, **kwargs): raise RuntimeError("requests disabled but HTTP GET attempted") - monkeypatch.setattr(parser.requests, "get", disabled) + monkeypatch.setattr(http, "get", disabled) def test_provider_schema(helpers, disable_get_requests, monkeypatch):