]> git.ipfire.org Git - thirdparty/suricata-update.git/commitdiff
fix: fix --no-merge option 270/head
authorJason <jason.ish@oisf.net>
Mon, 15 Feb 2021 22:35:27 +0000 (16:35 -0600)
committerJason <jason.ish@oisf.net>
Mon, 15 Feb 2021 22:35:27 +0000 (16:35 -0600)
The no-merge handling was not updated when the file storage
was converted to a list causing it to fail.

Also add a --no-merge test to our integration test.

Fixes issue:
https://redmine.openinfosecfoundation.org/issues/4324

suricata/update/main.py
tests/integration_tests.py

index 41cf0cb699ea51060b355e1f4125075f2a67d691..b1f08191cb1beaab84f319f2965f3ac78dc5aaaa 100644 (file)
@@ -508,9 +508,9 @@ def write_to_directory(directory, files, rulemap, dep_files):
 
     oldset = {}
     if not args.quiet:
-        for filename in files:
+        for file in files:
             outpath = os.path.join(
-                directory, os.path.basename(filename))
+                directory, os.path.basename(file.filename))
 
             if os.path.exists(outpath):
                 for rule in rule_mod.parse_file(outpath):
@@ -533,15 +533,15 @@ def write_to_directory(directory, files, rulemap, dep_files):
                         len(removed),
                         len(modified)))
 
-    for filename in sorted(files):
+    for file in sorted(files):
         outpath = os.path.join(
-            directory, os.path.basename(filename))
+            directory, os.path.basename(file.filename))
         logger.debug("Writing %s." % outpath)
-        if not filename.endswith(".rules"):
-            open(outpath, "wb").write(files[filename])
+        if not file.filename.endswith(".rules"):
+            open(outpath, "wb").write(file.content)
         else:
             content = []
-            for line in io.StringIO(files[filename].decode("utf-8")):
+            for line in io.StringIO(file.content.decode("utf-8")):
                 rule = rule_mod.parse(line)
                 if not rule:
                     content.append(line.strip())
@@ -552,7 +552,13 @@ def write_to_directory(directory, files, rulemap, dep_files):
                                 handle_dataset_files(rule, dep_files)
                             else:
                                 handle_filehash_files(rule, dep_files, kw)
-                    content.append(rulemap[rule.id].format())
+                    if rule.id in rulemap:
+                        content.append(rulemap[rule.id].format())
+                    else:
+                        # Just pass the input through. Most likey a
+                        # rule from a file that was ignored, but we'll
+                        # still pass it through.
+                        content.append(line.strip())
             tmp_filename = ".".join([outpath, "tmp"])
             io.open(tmp_filename, encoding="utf-8", mode="w").write(
                 u"\n".join(content))
@@ -1234,10 +1240,10 @@ def _main():
         file_tracker.add(output_filename)
         write_merged(os.path.join(output_filename), rulemap, dep_files)
     else:
-        for filename in files:
+        for file in files:
             file_tracker.add(
                 os.path.join(
-                    config.get_output_dir(), os.path.basename(filename)))
+                    config.get_output_dir(), os.path.basename(file.filename)))
         write_to_directory(config.get_output_dir(), files, rulemap, dep_files)
 
     manage_classification(suriconf, classification_files)
index 770575337b026087e9aa6e7a45a4b707e2862ef5..a421ebff3772d21b9d8cccf3e3cf20fbf6f1260a 100755 (executable)
@@ -51,6 +51,13 @@ assert(os.path.exists(DATA_DIR))
 assert(os.path.exists(os.path.join(DATA_DIR, "update", "cache")))
 assert(os.path.exists(os.path.join(DATA_DIR, "rules", "suricata.rules")))
 
+# Default run with data directory and --no-merge
+run(common_args + common_update_args + ["--no-merge"])
+assert(os.path.exists(DATA_DIR))
+assert(os.path.exists(os.path.join(DATA_DIR, "update", "cache")))
+assert(os.path.exists(os.path.join(DATA_DIR, "rules", "emerging-deleted.rules")))
+assert(os.path.exists(os.path.join(DATA_DIR, "rules", "emerging-current_events.rules")))
+
 # Still a default run, but set --output to an alternate location."
 run(common_args + common_update_args + ["--output", "./tests/tmp/_rules"])
 assert(os.path.exists(os.path.join(DATA_DIR, "_rules")))