From: Shivani Bhardwaj Date: Mon, 27 Dec 2021 17:27:50 +0000 (+0530) Subject: main: add fail option and handle it X-Git-Tag: 1.3.0rc1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9b8b7174d2e00bf80a4b350f875b390ce3f86a6;p=thirdparty%2Fsuricata-update.git main: add fail option and handle it Ticket 4579 --- diff --git a/suricata/update/main.py b/suricata/update/main.py index c319940..9b2b177 100644 --- a/suricata/update/main.py +++ b/suricata/update/main.py @@ -99,6 +99,11 @@ INDEX_EXPIRATION_TIME = 60 * 60 * 24 * 14 # Rule keywords that come with files file_kw = ["filemd5", "filesha1", "filesha256", "dataset"] +def strict_error(msg): + logger.error(msg) + if config.args().fail: + sys.exit(1) + class Fetch: def __init__(self): @@ -193,9 +198,10 @@ class Fetch: tmp_fileobj.close() except URLError as err: if os.path.exists(tmp_filename): - logger.warning( - "Failed to fetch %s, " - "will use latest cached version: %s", url, err) + msg = "Failed to fetch {}, will use latest cached version: {}".format(url, err) + if conf.args().fail: + strict_error(msg) + logger.warning(msg) return self.extract_files(tmp_filename) raise err except IOError as err: @@ -216,7 +222,7 @@ class Fetch: files.update(fetched) except URLError as err: url = url[0] if isinstance(url, tuple) else url - logger.error("Failed to fetch {}: {}".format(url, err)) + strict_error("Failed to fetch {}: {}".format(url, err)) else: for url in self.args.url: files.update(self.fetch(url)) diff --git a/suricata/update/parsers.py b/suricata/update/parsers.py index adda46b..185205c 100644 --- a/suricata/update/parsers.py +++ b/suricata/update/parsers.py @@ -165,6 +165,9 @@ update_arg = [ (("--offline",), {'action': 'store_true', 'help': "Run offline using most recent cached rules"}), + (("--fail",), + {'action': 'store_true', + 'help': "Strictly fail and exit in case of an error"}), # Hidden argument, --now to bypass the timebased bypass of # updating a ruleset.