diff --git a/splunk_connect_for_snmp_poller/manager/poller_utilities.py b/splunk_connect_for_snmp_poller/manager/poller_utilities.py index 688d7cb..a5a699c 100644 --- a/splunk_connect_for_snmp_poller/manager/poller_utilities.py +++ b/splunk_connect_for_snmp_poller/manager/poller_utilities.py @@ -37,15 +37,15 @@ logger = logging.getLogger(__name__) -def _should_process_current_line(inventory_record): +def _should_process_current_line(inventory_record: dict): return should_process_inventory_line( - inventory_record.host + inventory_record.get("host") ) and is_valid_inventory_line_from_dict( - inventory_record.host, - inventory_record.version, - inventory_record.community, - inventory_record.profile, - inventory_record.frequency_str, + inventory_record.get("host"), + inventory_record.get("version"), + inventory_record.get("community"), + inventory_record.get("profile"), + inventory_record.get("frequency_str"), ) @@ -71,15 +71,14 @@ def refresh_inventory(inventory_file_path): def parse_inventory_file(inventory_file_path, profiles): with open(inventory_file_path, newline="") as inventory_file: for agent in csv.DictReader(inventory_file, delimiter=","): - inventory_record = InventoryRecord( - agent["host"], - agent["version"], - agent["community"], - agent["profile"], - get_frequency(agent, profiles, 60), - ) - if _should_process_current_line(inventory_record): - yield inventory_record + if _should_process_current_line(agent): + yield InventoryRecord( + agent["host"], + agent["version"], + agent["community"], + agent["profile"], + get_frequency(agent, profiles, 60), + ) def get_frequency(agent, profiles, default_frequency): diff --git a/splunk_connect_for_snmp_poller/manager/validator/inventory_validator.py b/splunk_connect_for_snmp_poller/manager/validator/inventory_validator.py index 0402a8c..1ae6507 100644 --- a/splunk_connect_for_snmp_poller/manager/validator/inventory_validator.py +++ b/splunk_connect_for_snmp_poller/manager/validator/inventory_validator.py @@ -32,9 +32,12 @@ # under the License. import logging +import re logger = logging.getLogger(__name__) +profile_pattern = re.compile("^[A-Za-z0-9_-]*$") + SNMP_VERSION_1 = "1" SNMP_VERSION_2C = "2c" SNMP_VERSION_3 = "3" @@ -111,11 +114,11 @@ def is_valid_community(community_string): def is_valid_profile(profile): - return True if profile.strip() else False + return profile_pattern.match(profile.strip()) def is_valid_inventory_line_from_dict(host, version, community, profile, seconds): - if None in [host, version, community, profile, seconds]: + if None in [host, version, community, profile]: return False valid_inventory_line = ( @@ -123,7 +126,7 @@ def is_valid_inventory_line_from_dict(host, version, community, profile, seconds and is_valid_version(version.strip()) and is_valid_community(community.strip()) and is_valid_profile(profile.strip()) - and is_valid_second_quantity(seconds) + and (seconds is None or is_valid_second_quantity(seconds)) ) if not valid_inventory_line: logger.error( diff --git a/tests/test_inventory_validator.py b/tests/test_inventory_validator.py index 46a0ee5..2cb27ba 100644 --- a/tests/test_inventory_validator.py +++ b/tests/test_inventory_validator.py @@ -18,6 +18,7 @@ from splunk_connect_for_snmp_poller.manager.validator.inventory_validator import ( is_valid_inventory_line_from_dict, + is_valid_profile, should_process_inventory_line, ) from tests.static_inventory_test_data import InventoryLineBuilder @@ -62,3 +63,8 @@ def test_invalid_snmp_version(self): for line in InventoryLineBuilder().invalid_snmp_versions(): logger.info(f"Invalid SNMP protocol version: {line}") self.assertFalse(is_valid_inventory_line(line)) + + def test_invalid_profile_name(self): + self.assertFalse(is_valid_profile("asd sa")) + self.assertFalse(is_valid_profile("&asd")) + self.assertTrue(is_valid_profile("1_asd-asds"))