From 5656523ad69b70fb3e0748e37c2eae539d727e4a Mon Sep 17 00:00:00 2001 From: dandds Date: Mon, 14 Jan 2019 13:14:56 -0500 Subject: [PATCH 1/2] catch additional CRL download exception --- atst/domain/authnid/crl/util.py | 8 ++++++-- tests/domain/authnid/test_crl.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/atst/domain/authnid/crl/util.py b/atst/domain/authnid/crl/util.py index a26835c5..3934ed50 100644 --- a/atst/domain/authnid/crl/util.py +++ b/atst/domain/authnid/crl/util.py @@ -72,7 +72,8 @@ def write_crl(out_dir, target_dir, crl_location): def remove_bad_crl(out_dir, crl_location): crl = crl_local_path(out_dir, crl_location) - os.remove(crl) + if os.path.isfile(crl): + os.remove(crl) def refresh_crls(out_dir, target_dir, logger): @@ -85,7 +86,10 @@ def refresh_crls(out_dir, target_dir, logger): logger.info("successfully synced CRL from {}".format(crl_location)) else: logger.info("no updates for CRL from {}".format(crl_location)) - except requests.exceptions.ChunkedEncodingError: + except ( + requests.exceptions.ChunkedEncodingError, + requests.exceptions.ConnectionError, + ): if logger: logger.error( "Error downloading {}, removing file and continuing anyway".format( diff --git a/tests/domain/authnid/test_crl.py b/tests/domain/authnid/test_crl.py index c8cf5cfd..805aab5a 100644 --- a/tests/domain/authnid/test_crl.py +++ b/tests/domain/authnid/test_crl.py @@ -127,3 +127,37 @@ def test_skips_crl_if_it_has_not_been_modified(tmpdir, monkeypatch): "requests.get", lambda u, **kwargs: MockStreamingResponse([b"it worked"], 304) ) assert not util.write_crl(tmpdir, "random_target_dir", "crl_file_name") + + +class FakeLogger: + def __init__(self): + self.messages = [] + + def info(self, msg): + self.messages.append(msg) + + def warning(self, msg): + self.messages.append(msg) + + def error(self, msg): + self.messages.append(msg) + + +def test_refresh_crls_with_error(tmpdir, monkeypatch): + def _mock_create_connection(*args, **kwargs): + raise TimeoutError + + fake_crl = "https://fakecrl.com/fake.crl" + + monkeypatch.setattr( + "urllib3.util.connection.create_connection", _mock_create_connection + ) + monkeypatch.setattr("atst.domain.authnid.crl.util.fetch_disa", lambda *args: None) + monkeypatch.setattr( + "atst.domain.authnid.crl.util.crl_list_from_disa_html", lambda *args: [fake_crl] + ) + + logger = FakeLogger() + util.refresh_crls(tmpdir, tmpdir, logger) + + assert "Error downloading {}".format(fake_crl) in logger.messages[-1] From 93a54d7b9d06b8c19fcd88d399a3d046c6820fc0 Mon Sep 17 00:00:00 2001 From: dandds Date: Mon, 14 Jan 2019 15:52:41 -0500 Subject: [PATCH 2/2] catch all request exceptions when downloading CRLs --- atst/domain/authnid/crl/util.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/atst/domain/authnid/crl/util.py b/atst/domain/authnid/crl/util.py index 3934ed50..f5a702fd 100644 --- a/atst/domain/authnid/crl/util.py +++ b/atst/domain/authnid/crl/util.py @@ -86,10 +86,7 @@ def refresh_crls(out_dir, target_dir, logger): logger.info("successfully synced CRL from {}".format(crl_location)) else: logger.info("no updates for CRL from {}".format(crl_location)) - except ( - requests.exceptions.ChunkedEncodingError, - requests.exceptions.ConnectionError, - ): + except requests.exceptions.RequestException: if logger: logger.error( "Error downloading {}, removing file and continuing anyway".format(