From: Jason Ish Date: Thu, 14 Dec 2017 20:35:45 +0000 (-0600) Subject: update-sources: catch network errors and error out X-Git-Tag: 1.0.0b1~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a6cf55a01c698858a027a9fae7c60341980d71c;p=thirdparty%2Fsuricata-update.git update-sources: catch network errors and error out Issue: https://redmine.openinfosecfoundation.org/issues/2348 --- diff --git a/suricata/update/commands/updatesources.py b/suricata/update/commands/updatesources.py index 0b9e4b2..7f6bfed 100644 --- a/suricata/update/commands/updatesources.py +++ b/suricata/update/commands/updatesources.py @@ -23,6 +23,7 @@ import io from suricata.update import config from suricata.update import sources from suricata.update import net +from suricata.update import exceptions logger = logging.getLogger() @@ -32,12 +33,13 @@ def register(parser): def update_sources(): local_index_filename = sources.get_index_filename() with io.BytesIO() as fileobj: + url = sources.get_source_index_url() + logger.info("Downloading %s", url) try: - url = sources.get_source_index_url() - logger.info("Downloading %s", url) net.get(url, fileobj) except Exception as err: - raise Exception("Failed to download index: %s: %s" % (url, err)) + raise exceptions.ApplicationError( + "Failed to download index: %s: %s" % (url, err)) if not os.path.exists(config.get_cache_dir()): try: os.makedirs(config.get_cache_dir()) diff --git a/suricata/update/exceptions.py b/suricata/update/exceptions.py new file mode 100644 index 0000000..1f2c547 --- /dev/null +++ b/suricata/update/exceptions.py @@ -0,0 +1,21 @@ +# Copyright (C) 2017 Open Information Security Foundation +# +# You can copy, redistribute or modify this Program under the terms of +# the GNU General Public License version 2 as published by the Free +# Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# version 2 along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +class ApplicationError(Exception): + pass + +class InvalidConfigurationError(ApplicationError): + pass diff --git a/suricata/update/main.py b/suricata/update/main.py index d66726d..a46935d 100644 --- a/suricata/update/main.py +++ b/suricata/update/main.py @@ -59,6 +59,7 @@ from suricata.update import extract from suricata.update import util from suricata.update import sources from suricata.update import commands +from suricata.update import exceptions from suricata.update.version import version try: @@ -84,12 +85,6 @@ DEFAULT_SURICATA_VERSION = "4.0.0" # single file concatenating all input rule files together. DEFAULT_OUTPUT_RULE_FILENAME = "suricata.rules" -class ApplicationError(Exception): - pass - -class InvalidConfigurationError(ApplicationError): - pass - class AllRuleMatcher(object): """Matcher object to match all rules. """ @@ -881,7 +876,7 @@ def load_sources(suricata_version): url = source["url"] % params else: if not index: - raise ApplicationError( + raise exceptions.ApplicationError( "Source index is required for source %s; " "run suricata-update update-sources" % (source["source"])) url = index.resolve_url(name, params) @@ -891,7 +886,7 @@ def load_sources(suricata_version): if config.get("sources"): for url in config.get("sources"): if type(url) not in [type("")]: - raise InvalidConfigurationError( + raise exceptions.InvalidConfigurationError( "Invalid datatype for source URL: %s" % (str(url))) url = url % internal_params logger.debug("Adding source %s.", url) @@ -1363,7 +1358,7 @@ def _main(): def main(): try: sys.exit(_main()) - except ApplicationError as err: + except exceptions.ApplicationError as err: logger.error(err) sys.exit(1) diff --git a/suricata/update/sources.py b/suricata/update/sources.py index ac64ce1..9368d6c 100644 --- a/suricata/update/sources.py +++ b/suricata/update/sources.py @@ -67,6 +67,9 @@ def get_source_index_url(): return DEFAULT_SOURCE_INDEX_URL def save_source_config(source_config): + if not os.path.exists(get_source_directory()): + logger.info("Creating directory %s", get_source_directory()) + os.makedirs(get_source_directory()) with open(get_enabled_source_filename(source_config.name), "w") as fileobj: fileobj.write(yaml.safe_dump( source_config.dict(), default_flow_style=False))