Skip to content

Commit

Permalink
Merge pull request #2768 from lunkwill42/bugfix/allow-v2c-profiles
Browse files Browse the repository at this point in the history
Parse SNMP profile version numbers using existing `ManagementProfile.snmp_version` property
  • Loading branch information
lunkwill42 authored Nov 28, 2023
2 parents 427276e + 98b67c8 commit cce6311
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 2 additions & 4 deletions python/nav/ipdevpoll/snmp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,11 @@ def factory(
if netbox:
profile = netbox.get_preferred_snmp_management_profile()
if profile:
if profile.protocol == profile.PROTOCOL_SNMPV3:
kwargs["version"] = 3
kwargs_out.update(
{k: v for k, v in profile.configuration.items() if hasattr(cls, k)}
)
# Sometimes profiles store the version number as a string
kwargs_out["version"] = int(kwargs_out["version"])
# Let profile object parse its own version to an int
kwargs_out["version"] = profile.snmp_version
else:
_logger.debug("%r has no snmp profile", netbox)
return None
Expand Down
28 changes: 28 additions & 0 deletions tests/unittests/ipdevpoll/snmp/common_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from unittest.mock import Mock

import pytest

from nav.ipdevpoll.snmp.common import SNMPParameters
from nav.Snmp.defines import AuthenticationProtocol, PrivacyProtocol, SecurityLevel
from nav.models.manage import ManagementProfile


class TestSNMPParameters:
Expand Down Expand Up @@ -41,6 +44,18 @@ def test_should_contain_sec_name_cmdline_argument(self, snmpv3_params):
assert "-u foobar" in args


class TestSNMPParametersFactory:
@pytest.mark.parametrize("version_value", (2, "2", "2c"))
def test_snmp_v2_profile_should_be_parsed_without_error(
self, snmpv2c_profile, version_value
):
mock_netbox = Mock()
mock_netbox.get_preferred_snmp_management_profile.return_value = snmpv2c_profile
snmpv2c_profile.configuration["version"] = version_value
params = SNMPParameters.factory(mock_netbox)
assert params.version == 2


@pytest.fixture
def snmpv3_params():
param = SNMPParameters(
Expand All @@ -53,3 +68,16 @@ def snmpv3_params():
priv_password="secret2",
)
yield param


@pytest.fixture
def snmpv2c_profile():
profile = ManagementProfile(
protocol=ManagementProfile.PROTOCOL_SNMP,
configuration={
"version": "2c",
"community": "private",
"write": True,
},
)
yield profile

0 comments on commit cce6311

Please sign in to comment.