From 5422c5b3e2f02228ac439ce38e0ffac9227aadcc Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 16:27:58 +0000 Subject: [PATCH 1/4] 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 117d230b9d19d055472c9333ae6901e381d05001 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 16:28:22 +0000 Subject: [PATCH 2/4] Add new test for list value verification function --- salt/sensoroni/files/analyzers/helpers_test.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/salt/sensoroni/files/analyzers/helpers_test.py b/salt/sensoroni/files/analyzers/helpers_test.py index c10ff00d5..86d05a8bb 100644 --- a/salt/sensoroni/files/analyzers/helpers_test.py +++ b/salt/sensoroni/files/analyzers/helpers_test.py @@ -33,3 +33,14 @@ 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) + + def test_verifyNonEmptyListValueIsEmpty(self): + conf = {"file_path": ""} + with self.assertRaises(SystemExit) as cm: + helpers.verifyNonEmptyListValue(conf, 'file_path') + self.assertEqual(cm.exception.code, 126) From eae05e83e672a0fe34eecc47f3a4e3db646a6a77 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 16:28:50 +0000 Subject: [PATCH 3/4] 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 3ab8a0be6016badd6651c341e6ae4b088da84af8 Mon Sep 17 00:00:00 2001 From: Wes Date: Tue, 13 Dec 2022 16:29:18 +0000 Subject: [PATCH 4/4] 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")