From e96206d065a7cb7a23a671f5fab002b4c8d6e811 Mon Sep 17 00:00:00 2001 From: Wes Lambert Date: Tue, 5 Jul 2022 14:25:54 +0000 Subject: [PATCH 1/3] Strip quotes and ensure file_path is typed as a list --- salt/sensoroni/files/analyzers/localfile/localfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/sensoroni/files/analyzers/localfile/localfile.py b/salt/sensoroni/files/analyzers/localfile/localfile.py index 5538d6a93..0924a98cc 100755 --- a/salt/sensoroni/files/analyzers/localfile/localfile.py +++ b/salt/sensoroni/files/analyzers/localfile/localfile.py @@ -17,7 +17,7 @@ def searchFile(artifact, csvfiles): dir = os.path.dirname(os.path.realpath(__file__)) found = [] for f in csvfiles: - filename = dir + "/" + f + filename = dir + "/" + f.strip("'") with open(filename, "r") as csvfile: csvdata = csv.DictReader(csvfile) for row in csvdata: @@ -58,7 +58,7 @@ def analyze(conf, input): meta = helpers.loadMetadata(__file__) data = helpers.parseArtifact(input) helpers.checkSupportedType(meta, data["artifactType"]) - search = searchFile(data["value"], conf['file_path']) + search = searchFile(data["value"], conf['file_path'].strip("[]").split(', ')) results = prepareResults(search) return results From f6266b19cc293d28bd8578f97ac47e23f2d5321b Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Tue, 5 Jul 2022 16:20:15 -0400 Subject: [PATCH 2/3] Fix unit test issues --- salt/sensoroni/files/analyzers/build.sh | 3 ++- .../files/analyzers/localfile/localfile_test.py | 13 ++++++++----- .../malwarehashregistry/malwarehashregistry_test.py | 4 +++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/salt/sensoroni/files/analyzers/build.sh b/salt/sensoroni/files/analyzers/build.sh index cb7dcbc52..386cc92d5 100755 --- a/salt/sensoroni/files/analyzers/build.sh +++ b/salt/sensoroni/files/analyzers/build.sh @@ -15,8 +15,9 @@ function ci() { exit 1 fi + pip install pytest pytest-cov flake8 "$TARGET_DIR" "--config=${HOME_DIR}/pytest.ini" - pytest "$TARGET_DIR" "--cov-config=${HOME_DIR}/pytest.ini" "--cov=$TARGET_DIR" --doctest-modules --cov-report=term --cov-fail-under=100 + python3 -m pytest "--cov-config=${HOME_DIR}/pytest.ini" "--cov=$TARGET_DIR" --doctest-modules --cov-report=term --cov-fail-under=100 "$TARGET_DIR" } function download() { diff --git a/salt/sensoroni/files/analyzers/localfile/localfile_test.py b/salt/sensoroni/files/analyzers/localfile/localfile_test.py index 30b171f86..154b74cd7 100644 --- a/salt/sensoroni/files/analyzers/localfile/localfile_test.py +++ b/salt/sensoroni/files/analyzers/localfile/localfile_test.py @@ -17,13 +17,16 @@ class TestLocalfileMethods(unittest.TestCase): def test_main_success(self): output = {"foo": "bar"} + conf = {"file_path": ["somefile.csv"]} with patch('sys.stdout', new=StringIO()) as mock_stdout: with patch('localfile.localfile.analyze', new=MagicMock(return_value=output)) as mock: - sys.argv = ["cmd", "input"] - localfile.main() - expected = '{"foo": "bar"}\n' - self.assertEqual(mock_stdout.getvalue(), expected) - mock.assert_called_once() + with patch('helpers.loadConfig', new=MagicMock(return_value=conf)) as lcmock: + sys.argv = ["cmd", "input"] + localfile.main() + expected = '{"foo": "bar"}\n' + self.assertEqual(mock_stdout.getvalue(), expected) + mock.assert_called_once() + lcmock.assert_called_once() def test_checkConfigRequirements_present(self): conf = {"file_path": "['intel.csv']"} diff --git a/salt/sensoroni/files/analyzers/malwarehashregistry/malwarehashregistry_test.py b/salt/sensoroni/files/analyzers/malwarehashregistry/malwarehashregistry_test.py index 824949d8b..a4a7d2340 100644 --- a/salt/sensoroni/files/analyzers/malwarehashregistry/malwarehashregistry_test.py +++ b/salt/sensoroni/files/analyzers/malwarehashregistry/malwarehashregistry_test.py @@ -35,7 +35,9 @@ class TestMalwareHashRegistryMethods(unittest.TestCase): response = malwarehashregistry.sendReq(hash) mock.assert_called_once_with(options, hash, flags) self.assertIsNotNone(response) - self.assertEqual(response, {"hash": "84af04b8e69682782607a0c5796ca56999eda6b3", "last_seen": "2019-15-07 03:30:33", "av_detection_percentage": 35}) + self.assertEqual(response["hash"], "84af04b8e69682782607a0c5796ca56999eda6b3") + self.assertRegex(response["last_seen"], r'2019-..-07 ..:..:33') # host running this test won't always use UTC + self.assertEqual(response["av_detection_percentage"], 35) def test_sendReqNoData(self): output = "84af04b8e69682782607a0c5796ca5696b3 NO_DATA" From 9d43b7ec89eb2476838f919eee2f51ebd16d04db Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Tue, 5 Jul 2022 16:21:27 -0400 Subject: [PATCH 3/3] Rollback string manipulation in favor of fixed unit tests --- salt/sensoroni/files/analyzers/localfile/localfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/sensoroni/files/analyzers/localfile/localfile.py b/salt/sensoroni/files/analyzers/localfile/localfile.py index 0924a98cc..5538d6a93 100755 --- a/salt/sensoroni/files/analyzers/localfile/localfile.py +++ b/salt/sensoroni/files/analyzers/localfile/localfile.py @@ -17,7 +17,7 @@ def searchFile(artifact, csvfiles): dir = os.path.dirname(os.path.realpath(__file__)) found = [] for f in csvfiles: - filename = dir + "/" + f.strip("'") + filename = dir + "/" + f with open(filename, "r") as csvfile: csvdata = csv.DictReader(csvfile) for row in csvdata: @@ -58,7 +58,7 @@ def analyze(conf, input): meta = helpers.loadMetadata(__file__) data = helpers.parseArtifact(input) helpers.checkSupportedType(meta, data["artifactType"]) - search = searchFile(data["value"], conf['file_path'].strip("[]").split(', ')) + search = searchFile(data["value"], conf['file_path']) results = prepareResults(search) return results