Skip to content

Commit

Permalink
Merge branch '5.8.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
lunkwill42 committed Nov 28, 2023
2 parents 61c3677 + d96ff4d commit 06f47de
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This changelog format was introduced in NAV 5.4.0. Older changelogs can be
found in the [HISTORY](HISTORY) file.

## [Unreleased]

### Fixed

- Fix ipdevpoll crash error from using SNMP v2c profile example that came with NAV ([#2767](https://github.com/Uninett/nav/issues/2767), [#2768](https://github.com/Uninett/nav/pull/2768))
- Gracefully handle encoding errors in invalid sysname/IP input in SeedDB IP Device form ([#2764](https://github.com/Uninett/nav/pull/2764))
- Gracefully handle errors from invalid profiles list input in SeedDB IP Device form ([#2765](https://github.com/Uninett/nav/pull/2765))

## [5.8.0] - 2023-11-24

### Added
Expand All @@ -26,6 +34,7 @@ found in the [HISTORY](HISTORY) file.
- Add subcommand to `navuser` command line program for deleting users ([#2705](https://github.com/Uninett/nav/pull/2705))
- Add toggle in `webfront.conf` for automatic creation of remote users ([#2698](https://github.com/Uninett/nav/issue/2698), [#2707](https://github.com/Uninett/nav/pull/2707))
- Add proper documentation index page for all howto guides ([#2716](https://github.com/Uninett/nav/pull/2716))
- Add description to threshold alarms ([#2691](https://github.com/Uninett/nav/issue/2691), [#2709](https://github.com/Uninett/nav/pull/2709))


#### Developer-centric additions
Expand Down
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
2 changes: 1 addition & 1 deletion python/nav/web/seeddb/page/netbox/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def get_sysname(ip_address):
try:
_, sysname = resolve_ip_and_sysname(ip_address)
return sysname
except SocketError:
except (SocketError, UnicodeError):
return None


Expand Down
4 changes: 2 additions & 2 deletions python/nav/web/seeddb/page/netbox/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def clean_ip(self):
name = self.cleaned_data['ip'].strip()
try:
ip, _ = resolve_ip_and_sysname(name)
except SocketError:
except (SocketError, UnicodeError):
raise forms.ValidationError("Could not resolve name %s" % name)
return str(ip)

Expand Down Expand Up @@ -235,7 +235,7 @@ def clean(self):
self._errors['profiles'] = self.error_class(
["Category %s requires a management profile." % cat.id]
)
del cleaned_data['profiles']
cleaned_data.pop('profiles', None)

return cleaned_data

Expand Down
42 changes: 42 additions & 0 deletions tests/integration/seeddb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test.client import RequestFactory
from mock import MagicMock

from nav.compatibility import smart_str
from nav.models.manage import Netbox, Room
from nav.web.seeddb.page.netbox.edit import netbox_edit, log_netbox_change
from nav.web.seeddb.utils.delete import dependencies
Expand All @@ -28,6 +29,47 @@ def test_editing_deleted_netboxes_should_raise_404(admin_account):
netbox_edit(request, netboxid)


def test_adding_netbox_with_invalid_ip_should_fail(db, client):
url = reverse('seeddb-netbox-edit')
ip = "195.88.54.16'))) OR 2121=(SELECT COUNT(*) FROM GENERATE_SERIES(1,15000000)) AND ((('FRyc' LIKE 'FRyc"

response = client.post(
url,
follow=True,
data={
"ip": ip,
"room": "myroom",
"category": "GW",
"organization": "myorg",
},
)

assert response.status_code == 200
assert 'Form was not valid' in smart_str(response.content)
assert 'Could not resolve name' in smart_str(response.content)


def test_adding_netbox_with_invalid_profiles_should_fail(db, client):
url = reverse('seeddb-netbox-edit')
ip = "10.254.254.253"

response = client.post(
url,
follow=True,
data={
"ip": ip,
"room": "myroom",
"category": "GW",
"organization": "myorg",
"profiles": "-5785')) ORDER BY 1-- qAPu",
},
)

assert response.status_code == 200
assert 'Form was not valid' in smart_str(response.content)
assert not Netbox.objects.filter(ip=ip).exists()


@pytest.fixture()
def netbox(management_profile):
box = Netbox(
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 06f47de

Please sign in to comment.