Skip to content

Commit

Permalink
Make received MIB objects resolution more forgiving
Browse files Browse the repository at this point in the history
Previously, MIB resolution errors were ignored (whenever possible)
for objects we were sending and receiving. This change tightens
outgoing objects MIB compliance (send will fail), but tolerate
non quite compliant objects we receive.

Also, extend the same policy onto `NotificationOriginator`.
  • Loading branch information
etingof committed Jul 30, 2019
1 parent 9dd4bcc commit e8fa401
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Revision 4.4.10, released 2019-07-29

- Rebased MIB importing code onto `importlib` because `imp` is long
deprecated
- MIB objects resolution made more forgiving to errors, added optional
`ignoreErrors` parameter to `ObjectType.resolveWithMib()` to control
that behaviour.
- Received MIB objects resolution made more forgiving to errors, added
optional `ignoreErrors` parameter to `ObjectType.resolveWithMib()` to
control that behaviour.
- Fixed asyncore main loop to respect non-default timer resolution
- Fixed `.setTimerResolution()` behaviour of abstract main loop dispatcher
to update call intervals of the existing periodic dispatcher jobs
Expand Down
14 changes: 9 additions & 5 deletions pysnmp/hlapi/varbinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ def makeVarBinds(self, snmpEngine, varBinds):
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])

__varBinds.append(varBind.resolveWithMib(mibViewController))
__varBinds.append(varBind.resolveWithMib(mibViewController, ignoreErrors=False))

return __varBinds

def unmakeVarBinds(self, snmpEngine, varBinds, lookupMib=True):
if lookupMib:
mibViewController = self.getMibViewController(snmpEngine)
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds]
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(
mibViewController) for x in varBinds]

return varBinds

Expand All @@ -52,7 +53,8 @@ class NotificationOriginatorVarBinds(AbstractVarBinds):
def makeVarBinds(self, snmpEngine, varBinds):
mibViewController = self.getMibViewController(snmpEngine)
if isinstance(varBinds, NotificationType):
varBinds.resolveWithMib(mibViewController)
varBinds.resolveWithMib(
mibViewController, ignoreErrors=False)
__varBinds = []
for varBind in varBinds:
if isinstance(varBind, ObjectType):
Expand All @@ -61,11 +63,13 @@ def makeVarBinds(self, snmpEngine, varBinds):
varBind = ObjectType(*varBind)
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
__varBinds.append(varBind.resolveWithMib(mibViewController))
__varBinds.append(varBind.resolveWithMib(
mibViewController, ignoreErrors=False))
return __varBinds

def unmakeVarBinds(self, snmpEngine, varBinds, lookupMib=False):
if lookupMib:
mibViewController = self.getMibViewController(snmpEngine)
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds]
varBinds = [ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(
mibViewController) for x in varBinds]
return varBinds
16 changes: 11 additions & 5 deletions pysnmp/smi/rfc1902.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,14 +1113,20 @@ def loadMibs(self, *modNames):
def isFullyResolved(self):
return self.__state & self.stClean

def resolveWithMib(self, mibViewController):
def resolveWithMib(self, mibViewController, ignoreErrors=True):
"""Perform MIB variable ID conversion and notification objects expansion.
Parameters
----------
mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
class instance representing MIB browsing functionality.
Other Parameters
----------------
ignoreErrors: :py:class:`bool`
If `True` (default), ignore MIB object name or value casting
failures if possible.
Returns
-------
: :py:class:`~pysnmp.smi.rfc1902.NotificationType`
Expand Down Expand Up @@ -1158,7 +1164,7 @@ class instance representing MIB browsing functionality.

self.__varBinds.append(
ObjectType(ObjectIdentity(v2c.apiTrapPDU.snmpTrapOID),
self.__objectIdentity).resolveWithMib(mibViewController)
self.__objectIdentity).resolveWithMib(mibViewController, ignoreErrors)

This comment has been minimized.

Copy link
@eminjin

eminjin Mar 24, 2020

ObjectIdentity class function resolveWithMib has not added ignoreErrors flag

This comment has been minimized.

Copy link
@etingof

etingof Mar 24, 2020

Author Owner

That's right, but I think this problem has been fixed already in both master and release candidate branches. WDYT?

This comment has been minimized.

Copy link
@eminjin

eminjin Mar 25, 2020

Sorry, marked in wrong line. Check my comments on line 1173(1179).

)

SmiNotificationType, = mibViewController.mibBuilder.importSymbols('SNMPv2-SMI', 'NotificationType')
Expand All @@ -1170,11 +1176,11 @@ class instance representing MIB browsing functionality.
if isinstance(mibNode, SmiNotificationType):
for notificationObject in mibNode.getObjects():
objectIdentity = ObjectIdentity(*notificationObject + self.__instanceIndex).resolveWithMib(
mibViewController)
mibViewController, ignoreErrors)

This comment has been minimized.

Copy link
@eminjin

eminjin Mar 25, 2020

Problem "resolveWithMib() takes exactly 2 arguments (3 given)" was caused by this line, becasue ObjectIdentity class function resolveWithMib has not added ignoreErrors flag.
On master branch, another commit 86790dd with same purpose doesn't include this problem.
But in release candidate branches release-4.4.10, 4.4.11, ..., it exists.
So we met this problem in 4.4.10 and later release.

self.__varBinds.append(
ObjectType(objectIdentity,
self.__objects.get(notificationObject, rfc1905.unSpecified)).resolveWithMib(
mibViewController)
mibViewController, ignoreErrors)
)
varBindsLocation[objectIdentity] = len(self.__varBinds) - 1
else:
Expand All @@ -1184,7 +1190,7 @@ class instance representing MIB browsing functionality.
for varBinds in self.__additionalVarBinds:
if not isinstance(varBinds, ObjectType):
varBinds = ObjectType(ObjectIdentity(varBinds[0]), varBinds[1])
varBinds.resolveWithMib(mibViewController)
varBinds.resolveWithMib(mibViewController, ignoreErrors)
if varBinds[0] in varBindsLocation:
self.__varBinds[varBindsLocation[varBinds[0]]] = varBinds
else:
Expand Down

0 comments on commit e8fa401

Please sign in to comment.