Skip to content

Commit

Permalink
Force engine id discovery to use empty user name for MikroTik RouterO…
Browse files Browse the repository at this point in the history
…S compatibility
  • Loading branch information
dvolodin7 committed Oct 31, 2024
1 parent 821358e commit 36ee61a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/gufo/snmp/_fast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class SnmpV3ClientSocket(object):
) -> None: ...
def set_keys(
self: "SnmpV3ClientSocket",
user_name: str,
auth_alg: int,
auth_key: bytes,
priv_alg: int,
Expand Down
2 changes: 2 additions & 0 deletions src/gufo/snmp/async_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __init__(
if not engine_id:
# Defer authentication until engine id is discovered
self._deferred_user = user
user = User.default()
self._sock = SnmpV3ClientSocket(
f"{addr}:{port}",
engine_id if engine_id else b"",
Expand Down Expand Up @@ -328,6 +329,7 @@ async def refresh(self: "SnmpSession") -> None:
)
# Set and localize actual keys
self._sock.set_keys(
self._deferred_user.name,
self._deferred_user.get_auth_alg(),
self._deferred_user.get_auth_key(),
self._deferred_user.get_priv_alg(),
Expand Down
2 changes: 2 additions & 0 deletions src/gufo/snmp/sync_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(
if not engine_id:
# Defer authentication until engine id is discovered
self._deferred_user = user
user = User.default()
self._sock = SnmpV3ClientSocket(
f"{addr}:{port}",
engine_id if engine_id else b"",
Expand Down Expand Up @@ -313,6 +314,7 @@ def refresh(self: "SnmpSession") -> None:
self._sock.sync_refresh()
# Set and localize actual keys
self._sock.set_keys(
self._deferred_user.name,
self._deferred_user.get_auth_alg(),
self._deferred_user.get_auth_key(),
self._deferred_user.get_priv_alg(),
Expand Down
10 changes: 10 additions & 0 deletions src/gufo/snmp/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ def __init__(
):
self.priv_key._pad(self.auth_key.KEY_LENGTH)

@classmethod
def default(cls: Type["User"]) -> "User":
"""
Default user without name and keys.
Returns:
Default user instance.
"""
return User(name="")

def require_auth(self: "User") -> bool:
"""
Chech if user requires authentication.
Expand Down
3 changes: 3 additions & 0 deletions src/socket/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ impl SnmpV3ClientSocket {
/// Change keys
fn set_keys(
&mut self,
user_name: String,
auth_alg: u8,
auth_key: &[u8],
priv_alg: u8,
priv_key: &[u8],
) -> PyResult<()> {
// Replace user
self.user_name = user_name;
// Auth key
let mut auth = AuthKey::new(auth_alg)?;
auth.as_key_type(auth_alg, auth_key, &self.engine_id)?;
Expand Down

0 comments on commit 36ee61a

Please sign in to comment.