-
Notifications
You must be signed in to change notification settings - Fork 37
feat(anta): Added the test case to verify SNMP user #877
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
Merged
carl-baillargeon
merged 11 commits into
aristanetworks:main
from
vitthalmagadum:issue_852
Jan 14, 2025
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1119961
Added TC for SNMP user
4f4c60e
Merge branch 'main' into issue_852
vitthalmagadum 581acc2
Updated input model refactoring changes
vitthalmagadum bb12cc2
updated documentation apis
vitthalmagadum 1f610ee
added unit tests for the input models
vitthalmagadum 58e14db
addressed review comments: updated docstrings, input model
vitthalmagadum 9a79572
Merge branch 'main' into issue_852
vitthalmagadum 96a6569
updated field validator
vitthalmagadum 157751a
Merge branch 'main' into issue_852
vitthalmagadum f0af0c5
Addressed review comments: updated input model docstrings
vitthalmagadum 91fe14c
Remove unnecessary TypeVar
carl-baillargeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2023-2025 Arista Networks, Inc. | ||
# Use of this source code is governed by the Apache License 2.0 | ||
# that can be found in the LICENSE file. | ||
"""Module containing input models for SNMP tests.""" | ||
|
||
from __future__ import annotations | ||
|
||
from pydantic import BaseModel, ConfigDict | ||
|
||
from anta.custom_types import EncryptionAlgorithms, HashingAlgorithms, SnmpVersion | ||
|
||
|
||
class SnmpUser(BaseModel): | ||
"""Model for a SNMP User.""" | ||
|
||
model_config = ConfigDict(extra="forbid") | ||
username: str | ||
"""SNMP user name.""" | ||
group_name: str | None = None | ||
"""SNMP group for the user. Required field in the `VerifySnmpUser` test.""" | ||
security_model: SnmpVersion | None = None | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""SNMP protocol version. Required field in the `VerifySnmpUser` test.""" | ||
authentication_type: HashingAlgorithms | None = None | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""User authentication settings. Can be provided in the `VerifySnmpUser` test.""" | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
encryption: EncryptionAlgorithms | None = None | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""User privacy settings. Can be provided in the `VerifySnmpUser` test.""" | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __str__(self) -> str: | ||
"""Return a human-readable string representation of the SnmpUser for reporting. | ||
|
||
Examples | ||
-------- | ||
User: Test Group: Test_Group Version: v2c | ||
""" | ||
return f"User: {self.username} Version: {self.security_model}" | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,21 @@ | |
# mypy: disable-error-code=attr-defined | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, ClassVar, get_args | ||
from typing import TYPE_CHECKING, ClassVar, TypeVar, get_args | ||
|
||
from pydantic import field_validator | ||
|
||
from anta.custom_types import PositiveInteger, SnmpErrorCounter, SnmpPdu | ||
from anta.input_models.snmp import SnmpUser | ||
from anta.models import AntaCommand, AntaTest | ||
from anta.tools import get_value | ||
|
||
if TYPE_CHECKING: | ||
from anta.models import AntaTemplate | ||
|
||
# Using a TypeVar for the SnmpUser model since mypy thinks it's a ClassVar and not a valid type when used in field validators | ||
T = TypeVar("T", bound=SnmpUser) | ||
|
||
|
||
class VerifySnmpStatus(AntaTest): | ||
"""Verifies whether the SNMP agent is enabled in a specified VRF. | ||
|
@@ -339,3 +345,82 @@ def test(self) -> None: | |
self.result.is_success() | ||
else: | ||
self.result.is_failure(f"The following SNMP error counters are not found or have non-zero error counters:\n{error_counters_not_ok}") | ||
|
||
|
||
class VerifySnmpUser(AntaTest): | ||
"""Verifies the SNMP user configurations for specified version(s). | ||
|
||
This test performs the following checks for each specified user: | ||
|
||
1. Verifies that the valid user name and group name. | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
2. Ensures that the SNMP v3 security model, the user authentication and privacy settings aligning with version-specific requirements. | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Expected Results | ||
---------------- | ||
* Success: If all of the following conditions are met: | ||
- All specified users are found in the SNMP configuration with valid user group. | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- The SNMP v3 security model, the user authentication and privacy settings matches the required settings. | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Failure: If any of the following occur: | ||
- A specified user is not found in the SNMP configuration. | ||
- A user's group is not correct. | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- For SNMP v3 security model, the user authentication and privacy settings does not matches the required settings. | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples | ||
-------- | ||
```yaml | ||
anta.tests.snmp: | ||
- VerifySnmpUser: | ||
snmp_users: | ||
- username: test | ||
group_name: test_group | ||
security_model: v3 | ||
authentication_type: MD5 | ||
encryption: AES-128 | ||
``` | ||
""" | ||
|
||
categories: ClassVar[list[str]] = ["snmp"] | ||
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show snmp user", revision=1)] | ||
|
||
class Input(AntaTest.Input): | ||
"""Input model for the VerifySnmpUser test.""" | ||
|
||
snmp_users: list[SnmpUser] | ||
"""List of SNMP users.""" | ||
|
||
@field_validator("snmp_users") | ||
@classmethod | ||
def validate_snmp_user(cls, snmp_users: list[T]) -> list[T]: | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Validate that 'authentication_type' or 'encryption' field is provided in each SNMP user.""" | ||
for user in snmp_users: | ||
if user.security_model == "v3" and not (user.authentication_type or user.encryption): | ||
msg = f"{user}; At least one of 'authentication_type' or 'encryption' must be provided." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we still using |
||
raise ValueError(msg) | ||
return snmp_users | ||
|
||
@AntaTest.anta_test | ||
def test(self) -> None: | ||
"""Main test function for VerifySnmpUser.""" | ||
self.result.is_success() | ||
|
||
for user in self.inputs.snmp_users: | ||
username = user.username | ||
group_name = user.group_name | ||
security_model = user.security_model | ||
authentication_type = user.authentication_type | ||
encryption = user.encryption | ||
|
||
# Verify SNMP user details. | ||
if not (user_details := get_value(self.instance_commands[0].json_output, f"usersByVersion.{security_model}.users.{username}")): | ||
self.result.is_failure(f"{user} - Not found") | ||
continue | ||
|
||
if group_name != (act_group := user_details.get("groupName", "Not Found")): | ||
self.result.is_failure(f"{user} - Incorrect user group - Expected: {group_name} Actual: {act_group}") | ||
|
||
if security_model == "v3": | ||
if authentication_type and (act_auth_type := user_details.get("v3Params", {}).get("authType", "Not Found")) != authentication_type: | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.result.is_failure(f"{user} - Incorrect authentication type - Expected: {authentication_type} Actual: {act_auth_type}") | ||
|
||
if encryption and (act_encryption := user_details.get("v3Params", {}).get("privType", "Not Found")) != encryption: | ||
carl-baillargeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.result.is_failure(f"{user} - Incorrect privacy type - Expected: {encryption} Actual: {act_encryption}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.