From 15b444141f1bb305e39a1343f41d46f25d60141b Mon Sep 17 00:00:00 2001 From: Sean Whalen Date: Sun, 19 May 2019 13:21:06 -0400 Subject: [PATCH] 6.4.1 Raise utils.DownloadError exception when a GeoIP database or Public Suffix List (PSL) download fails (closes issue #73) --- CHANGELOG.md | 6 ++++++ parsedmarc/__init__.py | 2 +- parsedmarc/utils.py | 27 ++++++++++++++++----------- setup.py | 2 +- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f918d68..7b0375a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +6.4.1 +----- + +- Raise `utils.DownloadError` exception when a GeoIP database or Public + Suffix List (PSL) download fails (closes issue #73) + 6.4.0 ----- diff --git a/parsedmarc/__init__.py b/parsedmarc/__init__.py index f0569ce9..8656ba01 100644 --- a/parsedmarc/__init__.py +++ b/parsedmarc/__init__.py @@ -38,7 +38,7 @@ from parsedmarc.utils import is_outlook_msg, convert_outlook_msg from parsedmarc.utils import timestamp_to_human, human_timestamp_to_datetime from parsedmarc.utils import parse_email -__version__ = "6.4.0" +__version__ = "6.4.1" logging.basicConfig( format='%(levelname)8s:%(filename)s:%(lineno)d:' diff --git a/parsedmarc/utils.py b/parsedmarc/utils.py index a4dfb548..97cde01d 100644 --- a/parsedmarc/utils.py +++ b/parsedmarc/utils.py @@ -51,6 +51,10 @@ class EmailParserError(RuntimeError): """Raised when an error parsing the email occurs""" +class DownloadError(RuntimeError): + """Rasied when an error occurs when downloading a file""" + + def decode_base64(data): """ Decodes a base64 string, with padding being optional @@ -91,9 +95,13 @@ def get_base_domain(domain, use_fresh_psl=False): url = "https://publicsuffix.org/list/public_suffix_list.dat" # Use a browser-like user agent string to bypass some proxy blocks headers = {"User-Agent": USER_AGENT} - fresh_psl = requests.get(url, headers=headers).text - with open(psl_path, "w", encoding="utf-8") as fresh_psl_file: - fresh_psl_file.write(fresh_psl) + try: + fresh_psl = requests.get(url, headers=headers).text + with open(psl_path, "w", encoding="utf-8") as fresh_psl_file: + fresh_psl_file.write(fresh_psl) + except Exception as error: + raise DownloadError( + "Failed to download an updated PSL {0}".format(error)) if use_fresh_psl: if not os.path.exists(psl_path): @@ -102,11 +110,8 @@ def get_base_domain(domain, use_fresh_psl=False): psl_age = datetime.now() - datetime.fromtimestamp( os.stat(psl_path).st_mtime) if psl_age > timedelta(hours=24): - try: - download_psl() - except Exception as error: - logger.warning( - "Failed to download an updated PSL {0}".format(error)) + download_psl() + with open(psl_path, encoding="utf-8") as psl_file: psl = publicsuffix2.PublicSuffixList(psl_file) @@ -289,9 +294,9 @@ def get_ip_address_country(ip_address, parallel=False): shutil.move(tar_path, location) shutil.rmtree(tar_dir) except Exception as e: - logger.warning("Error downloading {0}: {1}".format(url, - e.__str__())) - + raise DownloadError("Error downloading {0}: {1}".format( + url, + e.__str__())) system_paths = [ "GeoLite2-Country.mmdb", "/usr/local/share/GeoIP/GeoLite2-Country.mmdb", diff --git a/setup.py b/setup.py index 7d24ba85..f73e670e 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ from setuptools import setup from codecs import open from os import path -__version__ = "6.4.0" +__version__ = "6.4.1" description = "A Python package and CLI for parsing aggregate and " \ "forensic DMARC reports"