From f7c3fe572ba2314923b2d785ec1989ccebedbb42 Mon Sep 17 00:00:00 2001 From: dandds Date: Wed, 15 May 2019 14:38:33 -0400 Subject: [PATCH] Raise error if CRL download fails. The download method for Libcloud objects returns a boolean, which means that our CRL download could fail silently. The RackspaceCRLProvider would not raise an error until it tried to open the full path for the downloaded resource. This checks the return status of the download call and raises an error if the download failed. For reference: https://libcloud.readthedocs.io/en/latest/storage/api.html#libcloud.storage.base.StorageDriver.download_object --- atst/domain/csp/files.py | 17 +++++++++++++++-- tests/domain/csp/test_files.py | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/atst/domain/csp/files.py b/atst/domain/csp/files.py index 3e14068c..dfb48a3c 100644 --- a/atst/domain/csp/files.py +++ b/atst/domain/csp/files.py @@ -9,6 +9,10 @@ from libcloud.storage.providers import get_driver from atst.domain.exceptions import UploadError +class CSPFileError(Exception): + pass + + class FileProviderInterface: _PERMITTED_MIMETYPES = ["application/pdf", "image/png"] @@ -99,14 +103,23 @@ class RackspaceCRLProvider(CRLProviderInterface): ) self._crl_dir = app.config.get("CRL_STORAGE_CONTAINER") self._object_name = app.config.get("STORAGE_CRL_ARCHIVE_NAME") + self._object = None + + @property + def object(self): + if self._object is None: + self._object = self.container.get_object(object_name=self._object_name) + + return self._object def sync_crls(self): if not os.path.exists(self._crl_dir): os.mkdir(self._crl_dir) - obj = self.container.get_object(object_name=self._object_name) with TemporaryDirectory() as tempdir: dl_path = os.path.join(tempdir, self._object_name) - obj.download(dl_path, overwrite_existing=True) + success = self.object.download(dl_path, overwrite_existing=True) + if not success: + raise CSPFileError("The CRL package was not downloaded") archive = tarfile.open(dl_path, "r:bz2") archive.extractall(self._crl_dir) diff --git a/tests/domain/csp/test_files.py b/tests/domain/csp/test_files.py index 66830a63..aee804b0 100644 --- a/tests/domain/csp/test_files.py +++ b/tests/domain/csp/test_files.py @@ -1,8 +1,13 @@ import os import pytest from werkzeug.datastructures import FileStorage +from unittest.mock import Mock -from atst.domain.csp.files import RackspaceFileProvider +from atst.domain.csp.files import ( + CSPFileError, + RackspaceFileProvider, + RackspaceCRLProvider, +) from atst.domain.exceptions import UploadError from tests.mocks import PDF_FILENAME @@ -55,3 +60,16 @@ def test_downloading_uploaded_object(uploader, pdf_upload): pdf_content = pdf_upload.read() assert stream_content == pdf_content + + +def test_crl_download_fails(app, monkeypatch): + mock_object = Mock() + mock_object.download.return_value = False + monkeypatch.setattr( + "atst.domain.csp.files.RackspaceCRLProvider.object", mock_object + ) + + rs_crl_provider = RackspaceCRLProvider(app) + + with pytest.raises(CSPFileError): + rs_crl_provider.sync_crls()