From: Jason Ish Date: Sun, 3 Dec 2017 20:57:20 +0000 (-0600) Subject: update-sources: move to own source file X-Git-Tag: 1.0.0a1~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2bbe96f0ab06a50fbd63e28b3798719a69d82439;p=thirdparty%2Fsuricata-update.git update-sources: move to own source file Also make the verbose logging info. --- diff --git a/suricata/update/commands/__init__.py b/suricata/update/commands/__init__.py index 78af72d..a090177 100644 --- a/suricata/update/commands/__init__.py +++ b/suricata/update/commands/__init__.py @@ -17,3 +17,4 @@ from suricata.update.commands import listenabledsources from suricata.update.commands import addsource from suricata.update.commands import listsources +from suricata.update.commands import updatesources diff --git a/suricata/update/commands/updatesources.py b/suricata/update/commands/updatesources.py new file mode 100644 index 0000000..0e6c984 --- /dev/null +++ b/suricata/update/commands/updatesources.py @@ -0,0 +1,49 @@ +# 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. + +from __future__ import print_function + +import os +import logging +import io + +from suricata.update import sources +from suricata.update import net + +logger = logging.getLogger() + +def register(parser): + parser.set_defaults(func=update_sources) + +def update_sources(config): + local_index_filename = sources.get_index_filename(config) + with io.BytesIO() as fileobj: + try: + url = sources.get_source_index_url(config) + logger.info("Downloading %s", url) + net.get(url, fileobj) + except Exception as err: + raise Exception("Failed to download index: %s: %s" % (url, err)) + if not os.path.exists(config.get_cache_dir()): + try: + os.makedirs(config.get_cache_dir()) + except Exception as err: + logger.error("Failed to create directory %s: %s", + config.get_cache_dir(), err) + return 1 + with open(local_index_filename, "w") as outobj: + outobj.write(fileobj.getvalue()) + logger.info("Saved %s", local_index_filename) diff --git a/suricata/update/main.py b/suricata/update/main.py index 4ce6913..59e78e6 100644 --- a/suricata/update/main.py +++ b/suricata/update/main.py @@ -1145,15 +1145,14 @@ def _main(): enable_source_parser.add_argument("name") enable_source_parser.add_argument("params", nargs="*", metavar="param=val") - update_sources_parser = subparsers.add_parser( - "update-sources", parents=[common_parser]) - commands.listsources.register(subparsers.add_parser( "list-sources", parents=[common_parser])) commands.listenabledsources.register(subparsers.add_parser( "list-enabled-sources", parents=[common_parser])) commands.addsource.register(subparsers.add_parser( "add-source", parents=[common_parser])) + commands.updatesources.register(subparsers.add_parser( + "update-sources", parents=[common_parser])) args = parser.parse_args() @@ -1185,9 +1184,7 @@ def _main(): return 1 if args.subcommand: - if args.subcommand == "update-sources": - return sources.update_sources(config) - elif args.subcommand == "enable-source": + if args.subcommand == "enable-source": return sources.enable_source(config) elif args.subcommand == "disable-source": return sources.disable_source(config) diff --git a/suricata/update/sources.py b/suricata/update/sources.py index 68f20b9..edb889c 100644 --- a/suricata/update/sources.py +++ b/suricata/update/sources.py @@ -62,6 +62,11 @@ def source_index_exists(config): """Return True if the source index file exists.""" return os.path.exists(get_index_filename(config)) +def get_source_index_url(config): + if os.getenv("SOURCE_INDEX_URL"): + return os.getenv("SOURCE_INDEX_URL") + return DEFAULT_SOURCE_INDEX_URL + def save_source_config(source_config): with open(get_enabled_source_filename(source_config.name), "wb") as fileobj: fileobj.write(yaml.safe_dump( @@ -129,33 +134,6 @@ def get_enabled_sources(): return sources -def get_source_index_url(config): - if os.getenv("SOURCE_INDEX_URL"): - return os.getenv("SOURCE_INDEX_URL") - return DEFAULT_SOURCE_INDEX_URL - -def update_sources(config): - source_cache_filename = os.path.join( - config.get_cache_dir(), SOURCE_INDEX_FILENAME) - source_templates = {} - with io.BytesIO() as fileobj: - try: - url = get_source_index_url(config) - logger.debug("Downloading %s", url) - net.get(get_source_index_url(config), fileobj) - except Exception as err: - raise Exception("Failed to download index: %s: %s" % (url, err)) - if not os.path.exists(config.get_cache_dir()): - try: - os.makedirs(config.get_cache_dir()) - except Exception as err: - logger.error("Failed to create directory %s: %s", - config.get_cache_dir(), err) - return 1 - with open(source_cache_filename, "w") as outobj: - outobj.write(fileobj.getvalue()) - logger.debug("Saved %s", source_cache_filename) - def load_sources(config): sources_cache_filename = os.path.join( config.get_cache_dir(), SOURCE_INDEX_FILENAME)