Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
fix: changed order of validation (#147)
Browse files Browse the repository at this point in the history
* fix: changed order of validation

* feat: added validation for profile in inventory

* fix: build fix
  • Loading branch information
weliasz committed Oct 14, 2021
1 parent 3c690a5 commit 8dd65e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
31 changes: 15 additions & 16 deletions splunk_connect_for_snmp_poller/manager/poller_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)


Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -111,19 +114,19 @@ 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 = (
is_valid_host(host.strip())
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(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_inventory_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))

0 comments on commit 8dd65e4

Please sign in to comment.