Skip to content

Commit

Permalink
Ignore traps with invalid/missing ifIndex
Browse files Browse the repository at this point in the history
It's not entirely clear what Zino 1 does here, but it's also not clear
that we should attempt to do anything "useful" with crazy traps.  This
adds early ignores and tests for them.
  • Loading branch information
lunkwill42 committed Jun 6, 2024
1 parent 2655d23 commit 6cbd12e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/zino/trapobservers/link_traps.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ def handle_trap(self, trap: TrapMessage) -> Optional[bool]:
if "ifIndex" in trap.variables:
ifindex = trap.variables.get("ifIndex").value
else:
ifindex = -1
_logger.warning("%s: %s trap contained no ifIndex value, ignoring", trap.agent.device.name, trap.name)
return False
port = trap.agent.device.ports.get(ifindex) if ifindex > 0 else None
if not port:
_logger.warning(
"%s: %s trap referenced unknown port (ix %s), ignoring", trap.agent.device.name, trap.name, ifindex
)
return False

# TODO: The trap *might* contain an ifDescr value. If present, Zino uses that for trap processing.
# Otherwise, it fetches ifDescr from its own state and uses that for trap processing. Either way,
Expand Down
17 changes: 16 additions & 1 deletion tests/trapobservers/link_traps_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from unittest.mock import Mock
from unittest.mock import Mock, patch

import pytest

Expand Down Expand Up @@ -101,6 +101,21 @@ def test_when_link_trap_is_redundant_policy_should_ignore_trap(self, state_with_
localhost, localhost.ports[1], is_up=False
), "did not ignore redundant linkDown trap"

def test_when_link_trap_is_missing_ifindex_value_it_should_ignore_trap_early(self, state_with_localhost_with_port):
observer = LinkTrapObserver(state=state_with_localhost_with_port, polldevs=Mock())
trap = Mock(variables={})
with patch.object(observer, "handle_link_transition") as handle_link_transition:
assert not observer.handle_trap(trap)
assert not handle_link_transition.called, "handle_link_transition was called"

def test_when_link_trap_refers_to_unknown_port_it_should_ignore_trap_early(self, state_with_localhost_with_port):
observer = LinkTrapObserver(state=state_with_localhost_with_port, polldevs=Mock())
localhost = state_with_localhost_with_port.devices.devices["localhost"]
trap = Mock(agent=Mock(device=localhost), variables={"ifIndex": Mock(value=99)})
with patch.object(observer, "handle_link_transition") as handle_link_transition:
assert not observer.handle_trap(trap)
assert not handle_link_transition.called, "handle_link_transition was called"


@pytest.fixture
def state_with_localhost_with_port(state_with_localhost):
Expand Down

0 comments on commit 6cbd12e

Please sign in to comment.