Skip to content

Commit

Permalink
Merge branch 'feature/snmpv3-management-profile' into snmpv3
Browse files Browse the repository at this point in the history
  • Loading branch information
lunkwill42 committed Nov 9, 2023
2 parents f936740 + bca69ae commit ca5a45c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
9 changes: 7 additions & 2 deletions python/nav/models/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ def get_preferred_snmp_management_profile(self, writeable=None):
Returns the snmp management profile with the highest available
SNMP version.
"""
query = Q(protocol=ManagementProfile.PROTOCOL_SNMP)
query = Q(
protocol__in=(
ManagementProfile.PROTOCOL_SNMP,
ManagementProfile.PROTOCOL_SNMPV3,
)
)
if writeable:
query = query & Q(configuration__write=True)
elif writeable is not None:
Expand All @@ -372,7 +377,7 @@ def get_preferred_snmp_management_profile(self, writeable=None):
)
profiles = sorted(
self.profiles.filter(query),
key=lambda p: str(p.configuration.get('version') or 0),
key=lambda p: p.snmp_version or 0,
reverse=True,
)
if profiles:
Expand Down
7 changes: 7 additions & 0 deletions python/nav/web/seeddb/page/management_profile/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Meta(object):
"auth_password",
"priv_protocol",
"priv_password",
"write",
]
fields = []

Expand Down Expand Up @@ -167,6 +168,12 @@ class Meta(object):
),
required=False,
)
write = forms.BooleanField(
initial=False,
required=False,
label="Enables write access",
help_text="Check this if this profile enables write access",
)

def clean_auth_password(self):
level = self.cleaned_data.get("sec_level")
Expand Down
48 changes: 48 additions & 0 deletions tests/unittests/seeddb/management_profile_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from nav.web.seeddb.page.management_profile.forms import SnmpV3Form


class TestSnmpv3Form:
def test_when_seclevel_is_noauth_then_it_should_not_require_auth_password(self):
form = SnmpV3Form(
dict(
sec_level="noAuthNoPriv",
auth_protocol="MD5",
sec_name="foo",
auth_password="",
)
)
assert form.is_valid()

def test_when_seclevel_is_auth_then_it_should_require_auth_password(self):
form = SnmpV3Form(
dict(
sec_level="authNoPriv",
auth_protocol="MD5",
sec_name="foo",
auth_password="",
)
)
assert not form.is_valid()

def test_when_seclevel_is_priv_then_it_should_require_priv_password(self):
form = SnmpV3Form(
dict(
sec_level="authPriv",
auth_protocol="MD5",
sec_name="foo",
auth_password="bar",
)
)
assert not form.is_valid()

def test_when_seclevel_is_priv_then_it_should_accept_priv_password(self):
form = SnmpV3Form(
dict(
sec_level="authPriv",
auth_protocol="MD5",
sec_name="foo",
auth_password="bar",
priv_password="cromulent",
)
)
assert form.is_valid()

0 comments on commit ca5a45c

Please sign in to comment.