From fa8fb27255c46c92d24a969518f4665c72d07dbd Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 15 Feb 2021 16:35:27 -0600 Subject: [PATCH] fix: fix --no-merge option 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 | 26 ++++++++++++++++---------- tests/integration_tests.py | 7 +++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/suricata/update/main.py b/suricata/update/main.py index 41cf0cb..b1f0819 100644 --- a/suricata/update/main.py +++ b/suricata/update/main.py @@ -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) diff --git a/tests/integration_tests.py b/tests/integration_tests.py index 7705753..a421ebf 100755 --- a/tests/integration_tests.py +++ b/tests/integration_tests.py @@ -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"))) -- 2.47.3