Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SNMPv3 support to Arnold #2733

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions python/nav/arnold.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from subprocess import Popen, PIPE
from collections import namedtuple
from smtplib import SMTPException
from typing import Callable

from IPy import IP
from django.db import connection
Expand All @@ -40,6 +41,7 @@
from nav.Snmp.errors import AgentError
import nav.bitvector
import nav.buildconf
from nav.Snmp.profile import get_snmp_session_for_profile
from nav.config import find_config_file
from nav.errors import GeneralException
from nav.models.arnold import Identity, Event
Expand Down Expand Up @@ -443,7 +445,11 @@ def open_port(identity, username, eventcomment=""):
_logger.info("openPort: Port successfully opened")


def change_port_status(action, identity):
def change_port_status(
action,
identity,
agent_getter: Callable = get_snmp_session_for_profile,
):
"""Use SNMP to change status on an interface.

We use ifadminstatus to enable and disable ports
Expand All @@ -463,14 +469,8 @@ def change_port_status(action, identity):

if not profile:
raise NoReadWriteManagementProfileError
if not hasattr(profile, "snmp_community") or not hasattr(profile, "snmp_version"):
raise InvalidManagementProfileError

agent = nav.Snmp.Snmp(
host=netbox.ip,
community=profile.snmp_community,
version=profile.snmp_version,
)
agent = agent_getter(profile)(host=netbox.ip)

# Disable or enable based on input
try:
Expand Down
26 changes: 14 additions & 12 deletions tests/unittests/arnold/arnold_snmp_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tests for Arnold using snmp objects"""
from nav.arnold import change_port_status
from mock import Mock, patch
from unittest.mock import Mock, patch
import unittest

from nav.models.manage import ManagementProfile


@patch('nav.Snmp.Snmp', autospec=True)
class TestArnoldSnmp(unittest.TestCase):
Expand All @@ -28,9 +30,13 @@ def create_interface_mock(self):

def create_management_profile_mock(self):
"""Create management profile model mock object"""
profile = Mock()
profile.snmp_version = 1
profile.snmp_community = "public"
profile = ManagementProfile(
protocol=ManagementProfile.PROTOCOL_SNMP,
configuration={
"version": 1,
"community": "public",
},
)
return profile

def create_netbox_mock(self):
Expand All @@ -46,11 +52,9 @@ def test_change_port_status_enable(self, snmp):
instance = snmp.return_value
identity = Mock()
identity.interface = self.interface
change_port_status('enable', identity)
change_port_status('enable', identity, agent_getter=lambda profile: snmp)

snmp.assert_called_once_with(
self.ip, self.profile.snmp_community, self.profile.snmp_version
)
snmp.assert_called_once_with(self.ip)
instance.set.assert_called_once_with(
self.port_status_oid + '.' + str(self.ifindex), 'i', 1
)
Expand All @@ -60,11 +64,9 @@ def test_change_port_status_disable(self, snmp):
instance = snmp.return_value
identity = Mock()
identity.interface = self.interface
change_port_status('disable', identity)
change_port_status('disable', identity, agent_getter=lambda profile: snmp)

snmp.assert_called_once_with(
self.ip, self.profile.snmp_community, self.profile.snmp_version
)
snmp.assert_called_once_with(self.ip)
instance.set.assert_called_once_with(
self.port_status_oid + '.' + str(self.ifindex), 'i', 2
)
Loading