From 20b79b7ab04c74c80dcb2f876da52b33f3453ef9 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 15:56:26 +0000 Subject: [PATCH 1/6] Add new function to verify list value --- salt/sensoroni/files/analyzers/helpers.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/salt/sensoroni/files/analyzers/helpers.py b/salt/sensoroni/files/analyzers/helpers.py index 903e2373b..64e50b250 100644 --- a/salt/sensoroni/files/analyzers/helpers.py +++ b/salt/sensoroni/files/analyzers/helpers.py @@ -10,6 +10,11 @@ def checkSupportedType(meta, artifact_type): return True +def verifyNonEmptyListValue(conf, key): + if key not in conf or not isinstance(conf[key], list) or len(conf[key]) == 0: + sys.exit(126) + + def parseArtifact(artifact): data = json.loads(artifact) return data From d5ab45548543afef2eea7c1513e9786464508db0 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 15:56:58 +0000 Subject: [PATCH 2/6] Add new test for list value verification function --- salt/sensoroni/files/analyzers/helpers_test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/salt/sensoroni/files/analyzers/helpers_test.py b/salt/sensoroni/files/analyzers/helpers_test.py index c10ff00d5..fe6f10bf6 100644 --- a/salt/sensoroni/files/analyzers/helpers_test.py +++ b/salt/sensoroni/files/analyzers/helpers_test.py @@ -33,3 +33,8 @@ class TestHelpersMethods(unittest.TestCase): data = helpers.parseArtifact(input) self.assertEqual(data["artifactType"], "bar") self.assertEqual(data["value"], "foo") + + def test_verifyNonEmptyListValue(self): + conf = {"file_path": ['testfile.csv']} + path = 'file_path' + self.assertTrue(conf, path) From df5dd5fe283db90475fe27e56f3fc897924832f8 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 15:57:43 +0000 Subject: [PATCH 3/6] Use new list verification function for 'file_path' --- salt/sensoroni/files/analyzers/localfile/localfile.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/salt/sensoroni/files/analyzers/localfile/localfile.py b/salt/sensoroni/files/analyzers/localfile/localfile.py index 5538d6a93..8dbc2b163 100755 --- a/salt/sensoroni/files/analyzers/localfile/localfile.py +++ b/salt/sensoroni/files/analyzers/localfile/localfile.py @@ -1,18 +1,10 @@ import json import helpers import os -import sys import argparse import csv -def checkConfigRequirements(conf): - if "file_path" not in conf or len(conf['file_path']) == 0: - sys.exit(126) - else: - return True - - def searchFile(artifact, csvfiles): dir = os.path.dirname(os.path.realpath(__file__)) found = [] @@ -54,7 +46,7 @@ def prepareResults(raw): def analyze(conf, input): - checkConfigRequirements(conf) + helpers.verifyNonEmptyListValue(conf, 'file_path') meta = helpers.loadMetadata(__file__) data = helpers.parseArtifact(input) helpers.checkSupportedType(meta, data["artifactType"]) From 90dedbb8411c48e705b7d7b2b7808bfd66f18ed2 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 15:58:35 +0000 Subject: [PATCH 4/6] Update tests to account for change in 'file_path' value verification --- .../analyzers/localfile/localfile_test.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/salt/sensoroni/files/analyzers/localfile/localfile_test.py b/salt/sensoroni/files/analyzers/localfile/localfile_test.py index 154b74cd7..f2becac1f 100644 --- a/salt/sensoroni/files/analyzers/localfile/localfile_test.py +++ b/salt/sensoroni/files/analyzers/localfile/localfile_test.py @@ -28,22 +28,6 @@ class TestLocalfileMethods(unittest.TestCase): mock.assert_called_once() lcmock.assert_called_once() - def test_checkConfigRequirements_present(self): - conf = {"file_path": "['intel.csv']"} - self.assertTrue(localfile.checkConfigRequirements(conf)) - - def test_checkConfigRequirements_not_present(self): - conf = {"not_a_file_path": "blahblah"} - with self.assertRaises(SystemExit) as cm: - localfile.checkConfigRequirements(conf) - self.assertEqual(cm.exception.code, 126) - - def test_checkConfigRequirements_empty(self): - conf = {"file_path": ""} - with self.assertRaises(SystemExit) as cm: - localfile.checkConfigRequirements(conf) - self.assertEqual(cm.exception.code, 126) - def test_searchFile_multiple_found(self): artifact = "abcd1234" results = localfile.searchFile(artifact, ["localfile_test.csv"]) @@ -115,7 +99,7 @@ class TestLocalfileMethods(unittest.TestCase): } ] artifactInput = '{"value":"foo","artifactType":"url"}' - conf = {"file_path": "/home/intel.csv"} + conf = {"file_path": ['/home/intel.csv']} with patch('localfile.localfile.searchFile', new=MagicMock(return_value=output)) as mock: results = localfile.analyze(conf, artifactInput) self.assertEqual(results["summary"], "suspicious") From 874bbd25807db49b8795a9304414afc141bc981f Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 16:02:46 +0000 Subject: [PATCH 5/6] Remove extra whitespace --- salt/sensoroni/files/analyzers/helpers_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/sensoroni/files/analyzers/helpers_test.py b/salt/sensoroni/files/analyzers/helpers_test.py index fe6f10bf6..0acb18d29 100644 --- a/salt/sensoroni/files/analyzers/helpers_test.py +++ b/salt/sensoroni/files/analyzers/helpers_test.py @@ -33,7 +33,7 @@ class TestHelpersMethods(unittest.TestCase): data = helpers.parseArtifact(input) self.assertEqual(data["artifactType"], "bar") self.assertEqual(data["value"], "foo") - + def test_verifyNonEmptyListValue(self): conf = {"file_path": ['testfile.csv']} path = 'file_path' From 98a1fb96c21c2f9cfefd4d0599c5a618393217b6 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 16:23:16 +0000 Subject: [PATCH 6/6] Add test coverage for empty list value --- salt/sensoroni/files/analyzers/helpers_test.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/salt/sensoroni/files/analyzers/helpers_test.py b/salt/sensoroni/files/analyzers/helpers_test.py index 0acb18d29..86d05a8bb 100644 --- a/salt/sensoroni/files/analyzers/helpers_test.py +++ b/salt/sensoroni/files/analyzers/helpers_test.py @@ -38,3 +38,9 @@ class TestHelpersMethods(unittest.TestCase): conf = {"file_path": ['testfile.csv']} path = 'file_path' self.assertTrue(conf, path) + + def test_verifyNonEmptyListValueIsEmpty(self): + conf = {"file_path": ""} + with self.assertRaises(SystemExit) as cm: + helpers.verifyNonEmptyListValue(conf, 'file_path') + self.assertEqual(cm.exception.code, 126)