Fix hlapi LCD to include `contextName` (#217)

Fixed hlapi LCD configurator to include `contextName`.
Prior to this fix sending SNMPv3 TRAP with non-default
`contextName` would fail.

This change modifies the signature of the internal
LCD methods.
master
Ilya Etingof 2018-11-03 12:51:35 +01:00
parent 9ebd0c0fab
commit 4994111b2b
9 changed files with 52 additions and 34 deletions

View File

@ -66,6 +66,8 @@ Revision 4.4.7, released 2018-11-XX
the stack frames (ultimately shown in traceback/debugger) the stack frames (ultimately shown in traceback/debugger)
- Fixed hlapi/v3arch transport target caching to ensure transport targets - Fixed hlapi/v3arch transport target caching to ensure transport targets
are different even if just timeout/retries options differ are different even if just timeout/retries options differ
- Fixed hlapi LCD configurator to include `contextName`. Prior to this fix
sending SNMPv3 TRAP with non-default `contextName` would fail.
Revision 4.4.6, released 2018-09-13 Revision 4.4.6, released 2018-09-13
----------------------------------- -----------------------------------

View File

@ -578,12 +578,13 @@ def addVacmUser(snmpEngine, securityModel, securityName, securityLevel,
def delVacmUser(snmpEngine, securityModel, securityName, securityLevel, def delVacmUser(snmpEngine, securityModel, securityName, securityLevel,
readSubTree=(), writeSubTree=(), notifySubTree=()): readSubTree=(), writeSubTree=(), notifySubTree=(),
contextName=null):
(groupName, securityLevel, readView, writeView, (groupName, securityLevel, readView, writeView,
notifyView) = __cookVacmUserInfo(snmpEngine, securityModel, notifyView) = __cookVacmUserInfo(snmpEngine, securityModel,
securityName, securityLevel) securityName, securityLevel)
delVacmGroup(snmpEngine, securityModel, securityName) delVacmGroup(snmpEngine, securityModel, securityName)
delVacmAccess(snmpEngine, groupName, null, securityModel, securityLevel) delVacmAccess(snmpEngine, groupName, contextName, securityModel, securityLevel)
if readSubTree: if readSubTree:
delVacmView( delVacmView(
snmpEngine, readView, readSubTree snmpEngine, readView, readSubTree
@ -597,7 +598,6 @@ def delVacmUser(snmpEngine, securityModel, securityName, securityLevel,
snmpEngine, notifyView, notifySubTree snmpEngine, notifyView, notifySubTree
) )
# Notification target setup # Notification target setup
def __cookNotificationTargetInfo(snmpEngine, notificationName, paramsName, def __cookNotificationTargetInfo(snmpEngine, notificationName, paramsName,

View File

@ -145,7 +145,8 @@ def getCmd(snmpEngine, authData, transportTarget, contextData,
(errorIndication, errorStatus, errorIndex, varBindsUnmade) (errorIndication, errorStatus, errorIndex, varBindsUnmade)
) )
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
future = asyncio.Future() future = asyncio.Future()
@ -250,7 +251,8 @@ def setCmd(snmpEngine, authData, transportTarget, contextData,
(errorIndication, errorStatus, errorIndex, varBindsUnmade) (errorIndication, errorStatus, errorIndex, varBindsUnmade)
) )
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
future = asyncio.Future() future = asyncio.Future()
@ -361,7 +363,8 @@ def nextCmd(snmpEngine, authData, transportTarget, contextData,
(errorIndication, errorStatus, errorIndex, varBindsUnmade) (errorIndication, errorStatus, errorIndex, varBindsUnmade)
) )
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
future = asyncio.Future() future = asyncio.Future()
@ -501,7 +504,8 @@ def bulkCmd(snmpEngine, authData, transportTarget, contextData,
(errorIndication, errorStatus, errorIndex, varBindsUnmade) (errorIndication, errorStatus, errorIndex, varBindsUnmade)
) )
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
future = asyncio.Future() future = asyncio.Future()

View File

@ -152,8 +152,8 @@ def sendNotification(snmpEngine, authData, transportTarget, contextData,
) )
notifyName = lcd.configure( notifyName = lcd.configure(
snmpEngine, authData, transportTarget, notifyType snmpEngine, authData, transportTarget, notifyType,
) contextData.contextName)
future = asyncio.Future() future = asyncio.Future()

View File

@ -121,7 +121,8 @@ def getCmd(snmpEngine, authData, transportTarget, contextData,
snmpEngine.cache, varBinds, lookupMib snmpEngine.cache, varBinds, lookupMib
), cbCtx) ), cbCtx)
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
return cmdgen.GetCommandGenerator().sendVarBinds( return cmdgen.GetCommandGenerator().sendVarBinds(
snmpEngine, addrName, contextData.contextEngineId, snmpEngine, addrName, contextData.contextEngineId,
@ -232,7 +233,8 @@ def setCmd(snmpEngine, authData, transportTarget, contextData,
snmpEngine.cache, varBinds, lookupMib snmpEngine.cache, varBinds, lookupMib
), cbCtx) ), cbCtx)
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
return cmdgen.SetCommandGenerator().sendVarBinds( return cmdgen.SetCommandGenerator().sendVarBinds(
snmpEngine, addrName, contextData.contextEngineId, snmpEngine, addrName, contextData.contextEngineId,
@ -343,7 +345,9 @@ def nextCmd(snmpEngine, authData, transportTarget, contextData,
varBindTable], varBindTable],
cbCtx) cbCtx)
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
return cmdgen.NextCommandGenerator().sendVarBinds( return cmdgen.NextCommandGenerator().sendVarBinds(
snmpEngine, addrName, snmpEngine, addrName,
contextData.contextEngineId, contextData.contextName, contextData.contextEngineId, contextData.contextName,
@ -483,7 +487,8 @@ def bulkCmd(snmpEngine, authData, transportTarget, contextData,
[vbProcessor.unmakeVarBinds(snmpEngine.cache, varBindTableRow, lookupMib) for varBindTableRow in [vbProcessor.unmakeVarBinds(snmpEngine.cache, varBindTableRow, lookupMib) for varBindTableRow in
varBindTable], cbCtx) varBindTable], cbCtx)
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
return cmdgen.BulkCommandGenerator().sendVarBinds( return cmdgen.BulkCommandGenerator().sendVarBinds(
snmpEngine, addrName, contextData.contextEngineId, snmpEngine, addrName, contextData.contextEngineId,

View File

@ -162,7 +162,7 @@ def sendNotification(snmpEngine, authData, transportTarget, contextData,
) )
notifyName = lcd.configure(snmpEngine, authData, transportTarget, notifyName = lcd.configure(snmpEngine, authData, transportTarget,
notifyType) notifyType, contextData.contextName)
return ntforg.NotificationOriginator().sendVarBinds( return ntforg.NotificationOriginator().sendVarBinds(
snmpEngine, notifyName, snmpEngine, notifyName,

View File

@ -8,6 +8,8 @@ from pysnmp.entity import config
from pysnmp import nextid, error from pysnmp import nextid, error
from pysnmp.hlapi.v3arch.auth import * from pysnmp.hlapi.v3arch.auth import *
from pyasn1.compat.octets import null
__all__ = ['CommandGeneratorLcdConfigurator', __all__ = ['CommandGeneratorLcdConfigurator',
'NotificationOriginatorLcdConfigurator'] 'NotificationOriginatorLcdConfigurator']
@ -24,17 +26,17 @@ class AbstractLcdConfigurator(object):
snmpEngine.setUserContext(**{cacheId: cache}) snmpEngine.setUserContext(**{cacheId: cache})
return cache return cache
def configure(self, snmpEngine, authData, transportTarget, *options): def configure(self, snmpEngine, *args, **kwargs):
pass pass
def unconfigure(self, snmpEngine, authData=None): def unconfigure(self, snmpEngine, *args, **kwargs):
pass pass
class CommandGeneratorLcdConfigurator(AbstractLcdConfigurator): class CommandGeneratorLcdConfigurator(AbstractLcdConfigurator):
cacheKeys = ['auth', 'parm', 'tran', 'addr'] cacheKeys = ['auth', 'parm', 'tran', 'addr']
def configure(self, snmpEngine, authData, transportTarget, *options): def configure(self, snmpEngine, authData, transportTarget, contextName, **options):
cache = self._getCache(snmpEngine) cache = self._getCache(snmpEngine)
if isinstance(authData, CommunityData): if isinstance(authData, CommunityData):
if authData.communityIndex not in cache['auth']: if authData.communityIndex not in cache['auth']:
@ -117,7 +119,7 @@ class CommandGeneratorLcdConfigurator(AbstractLcdConfigurator):
return addrName, paramsName return addrName, paramsName
def unconfigure(self, snmpEngine, authData=None): def unconfigure(self, snmpEngine, authData=None, contextName=null, **options):
cache = self._getCache(snmpEngine) cache = self._getCache(snmpEngine)
if authData: if authData:
if isinstance(authData, CommunityData): if isinstance(authData, CommunityData):
@ -198,9 +200,9 @@ class NotificationOriginatorLcdConfigurator(AbstractLcdConfigurator):
cacheKeys = ['auth', 'name'] cacheKeys = ['auth', 'name']
_cmdGenLcdCfg = CommandGeneratorLcdConfigurator() _cmdGenLcdCfg = CommandGeneratorLcdConfigurator()
def configure(self, snmpEngine, authData, transportTarget, *options): def configure(self, snmpEngine, authData, transportTarget, notifyType,
contextName, **options):
cache = self._getCache(snmpEngine) cache = self._getCache(snmpEngine)
notifyType = options and options[0] or 'trap'
notifyName = None notifyName = None
# Create matching transport tags if not given by user. Not good! # Create matching transport tags if not given by user. Not good!
@ -211,7 +213,8 @@ class NotificationOriginatorLcdConfigurator(AbstractLcdConfigurator):
if isinstance(authData, CommunityData) and not authData.tag: if isinstance(authData, CommunityData) and not authData.tag:
authData.tag = transportTarget.tagList.split()[0] authData.tag = transportTarget.tagList.split()[0]
addrName, paramsName = self._cmdGenLcdCfg.configure(snmpEngine, authData, transportTarget) addrName, paramsName = self._cmdGenLcdCfg.configure(
snmpEngine, authData, transportTarget, contextName, **options)
tagList = transportTarget.tagList.split() tagList = transportTarget.tagList.split()
if not tagList: if not tagList:
tagList = [''] tagList = ['']
@ -230,7 +233,7 @@ class NotificationOriginatorLcdConfigurator(AbstractLcdConfigurator):
notifyType notifyType
) )
cache['name'][notifyNameKey] = notifyName, paramsName, 1 cache['name'][notifyNameKey] = notifyName, paramsName, 1
authDataKey = authData.securityName, authData.securityModel authDataKey = authData.securityName, authData.securityModel, authData.securityLevel, contextName
if authDataKey in cache['auth']: if authDataKey in cache['auth']:
authDataX, subTree, useCount = cache['auth'][authDataKey] authDataX, subTree, useCount = cache['auth'][authDataKey]
cache['auth'][authDataKey] = authDataX, subTree, useCount + 1 cache['auth'][authDataKey] = authDataX, subTree, useCount + 1
@ -240,23 +243,24 @@ class NotificationOriginatorLcdConfigurator(AbstractLcdConfigurator):
authData.securityModel, authData.securityModel,
authData.securityName, authData.securityName,
authData.securityLevel, authData.securityLevel,
(), (), subTree) (), (), subTree, contextName=contextName)
cache['auth'][authDataKey] = authData, subTree, 1 cache['auth'][authDataKey] = authData, subTree, 1
return notifyName return notifyName
def unconfigure(self, snmpEngine, authData=None): def unconfigure(self, snmpEngine, authData=None, contextName=null, **options):
cache = self._getCache(snmpEngine) cache = self._getCache(snmpEngine)
if authData: if authData:
authDataKey = authData.securityName, authData.securityModel authDataKey = authData.securityName, authData.securityModel, authData.securityLevel, contextName
if authDataKey in cache['auth']: if authDataKey in cache['auth']:
authDataKeys = (authDataKey,) authDataKeys = (authDataKey,)
else: else:
raise error.PySnmpError('Unknown authData %s' % (authData,)) raise error.PySnmpError('Unknown authData %s' % (authData,))
else: else:
authDataKeys = tuple(cache['auth'].keys()) authDataKeys = tuple(cache['auth'])
addrNames, paramsNames = self._cmdGenLcdCfg.unconfigure(snmpEngine, authData) addrNames, paramsNames = self._cmdGenLcdCfg.unconfigure(
snmpEngine, authData, contextName, **options)
notifyAndParamsNames = [(cache['name'][x], x) for x in cache['name'].keys() if x[0] in paramsNames] notifyAndParamsNames = [(cache['name'][x], x) for x in cache['name'].keys() if x[0] in paramsNames]

View File

@ -128,7 +128,8 @@ def getCmd(snmpEngine, authData, transportTarget, contextData,
else: else:
deferred.callback((errorStatus, errorIndex, varBindsUnmade)) deferred.callback((errorStatus, errorIndex, varBindsUnmade))
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
deferred = Deferred() deferred = Deferred()
@ -244,7 +245,8 @@ def setCmd(snmpEngine, authData, transportTarget, contextData,
else: else:
deferred.callback((errorStatus, errorIndex, varBindsUnmade)) deferred.callback((errorStatus, errorIndex, varBindsUnmade))
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
deferred = Deferred() deferred = Deferred()
@ -374,7 +376,8 @@ def nextCmd(snmpEngine, authData, transportTarget, contextData,
else: else:
deferred.callback((errorStatus, errorIndex, varBindsUnmade)) deferred.callback((errorStatus, errorIndex, varBindsUnmade))
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
deferred = Deferred() deferred = Deferred()
@ -532,7 +535,8 @@ def bulkCmd(snmpEngine, authData, transportTarget, contextData,
else: else:
deferred.callback((errorStatus, errorIndex, varBindsUnmade)) deferred.callback((errorStatus, errorIndex, varBindsUnmade))
addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget) addrName, paramsName = lcd.configure(
snmpEngine, authData, transportTarget, contextData.contextName)
deferred = Deferred() deferred = Deferred()

View File

@ -166,9 +166,8 @@ def sendNotification(snmpEngine, authData, transportTarget, contextData,
else: else:
deferred.callback((errorStatus, errorIndex, varBindsUnmade)) deferred.callback((errorStatus, errorIndex, varBindsUnmade))
notifyName = lcd.configure( notifyName = lcd.configure(snmpEngine, authData, transportTarget,
snmpEngine, authData, transportTarget, notifyType notifyType, contextData.contextName)
)
def __trapFun(deferred): def __trapFun(deferred):
deferred.callback((0, 0, [])) deferred.callback((0, 0, []))