]> git.ipfire.org Git - thirdparty/suricata-update.git/commitdiff
Add "check-versions" subcommand
authorVagisha Gupta <vagishagupta23@gmail.com>
Fri, 12 Jul 2019 07:13:09 +0000 (12:43 +0530)
committerJason Ish <jason.ish@oisf.net>
Fri, 11 Oct 2019 15:17:04 +0000 (09:17 -0600)
Add a `suricata-update check-versions` subcommand that checks
the version of suricata and logs if the versions are up to
date, outdated or EOL.

Redmine issue:
    https://redmine.openinfosecfoundation.org/issues/2341

suricata/update/commands/__init__.py
suricata/update/commands/checkversions.py [new file with mode: 0644]
suricata/update/main.py
suricata/update/sources.py

index 0541a010c49afad558ec453ecb2f63a772a77654..5d36fc77dfa49c922de73ffd70b85aab8c4aee0c 100644 (file)
@@ -21,3 +21,4 @@ from suricata.update.commands import updatesources
 from suricata.update.commands import enablesource
 from suricata.update.commands import disablesource
 from suricata.update.commands import removesource
+from suricata.update.commands import checkversions
diff --git a/suricata/update/commands/checkversions.py b/suricata/update/commands/checkversions.py
new file mode 100644 (file)
index 0000000..c4f0bdb
--- /dev/null
@@ -0,0 +1,58 @@
+# Copyright (C) 2019 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.
+
+import os.path
+import logging
+from suricata.update import sources, engine
+
+logger = logging.getLogger()
+
+
+def register(parser):
+    parser.set_defaults(func=check_version)
+
+
+def check_version(suricata_version):
+    index_filename = sources.get_index_filename()
+    if not os.path.exists(index_filename):
+        logger.warning("No index exists, will use bundled index.")
+        logger.warning("Please run suricata-update update-sources.")
+    index = sources.Index(index_filename)
+    version = index.get_versions()
+    recommended = engine.parse_version(version['suricata']['recommended'])
+    if suricata_version.full in recommended.full:
+        logger.info("Suricata version %s is up to date", suricata_version.full)
+    elif suricata_version.short > recommended.short:
+        return None
+    elif "dev" in suricata_version.full:
+        if (suricata_version.short not in recommended.short or
+                suricata_version.short not in version['suricata']) and \
+                float(suricata_version.short) < float(recommended.short):
+            logger.warning(
+                "Suricata version %s has reached EOL. Please upgrade to %s.",
+                suricata_version.full, recommended.full)
+        elif float(suricata_version.short) >= float(recommended.short):
+            logger.info(
+                "Suricata version %s is up to date", suricata_version.full)
+    elif suricata_version.short in version['suricata'] and \
+            suricata_version.full not in recommended.full:
+        logger.warning(
+            "Suricata version %s is outdated. Please upgrade to %s.",
+            suricata_version.full, recommended.full)
+    else:
+        logger.warning(
+            "Suricata version %s has reached EOL. Please upgrade to %s.",
+            suricata_version.full, recommended.full)
index e1138436ef498580e8235a87882eb7cb5aca56e1..883afa7878ebefc3f19b70cd973c2fff3683bf6d 100644 (file)
@@ -1232,6 +1232,7 @@ def _main():
     remove-source              Remove an enabled or disabled source
     list-enabled-sources       List all enabled sources
     add-source                 Add a new source by URL
+    check-versions             Check version of suricata
 """
 
     # The Python 2.7 argparse module does prefix matching which can be
@@ -1259,6 +1260,8 @@ def _main():
         "disable-source", parents=[global_parser]))
     commands.removesource.register(subparsers.add_parser(
         "remove-source", parents=[global_parser]))
+    commands.checkversions.register(subparsers.add_parser(
+        "check-versions", parents=[global_parser]))
 
     args = parser.parse_args(rem)
 
@@ -1324,7 +1327,9 @@ def _main():
     suricata.update.net.set_user_agent_suricata_version(suricata_version.full)
 
     if args.subcommand:
-        if hasattr(args, "func"):
+        if args.subcommand == "check-versions" and hasattr(args, "func"):
+            return args.func(suricata_version)
+        elif hasattr(args, "func"):
             return args.func()
         elif args.subcommand != "update":
             logger.error("Unknown command: %s", args.subcommand)
index e87cb4a9e686efb0f8bf1999b87d5bf505d127d9..f0ce7088df739f52ed647121e9e22e44522b4d08 100644 (file)
@@ -130,6 +130,12 @@ class Index:
             return self.index["sources"][name]
         return None
 
+    def get_versions(self):
+        try:
+            return self.index["versions"]
+        except KeyError:
+            raise Exception("Version information not in index")
+
 def load_source_index(config):
     return Index(get_index_filename())