diff options
author | elie <elie> | 2013-06-01 10:08:33 +0000 |
---|---|---|
committer | elie <elie> | 2013-06-01 10:08:33 +0000 |
commit | a8bd7e3f06c5c84363764de284ef4e7024534969 (patch) | |
tree | e4ab90ab91fae02b8abc4c03704277bf49492c4d | |
parent | 0200c5022de3e1e224ed73eba357e12b56e05b79 (diff) | |
download | pysnmp-a8bd7e3f06c5c84363764de284ef4e7024534969.tar.gz |
Oneliner UsmUserData() and CommunityData() classes now support clone()'ing
to facilitate authentication data management in user applications.
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | pysnmp/entity/rfc3413/oneliner/auth.py | 53 |
2 files changed, 46 insertions, 9 deletions
@@ -43,6 +43,8 @@ Revision 4.2.5rc2 addV3User() functions as well as to their oneliner's wrappers. - The contextEngineId parameter of config.addV3User() and auth.UsmUserData() renamed into securityEngineId as it's semantically correct +- Oneliner UsmUserData() and CommunityData() classes now support clone()'ing + to facilitate authentication data management in user applications. - Oneliner transport target classes now support the getTransportInfo() method that returns network addresses used on protocol level. - The SnmpV3MessageProcessingModel.getPeerEngineInfo() method is implemented diff --git a/pysnmp/entity/rfc3413/oneliner/auth.py b/pysnmp/entity/rfc3413/oneliner/auth.py index a2ab5f3..8c7e5f6 100644 --- a/pysnmp/entity/rfc3413/oneliner/auth.py +++ b/pysnmp/entity/rfc3413/oneliner/auth.py @@ -19,23 +19,28 @@ class CommunityData: self.contextName = contextName if tag is not None: self.tag = tag - # Autogenerate securityName if not specified + # a single arg is considered as a community name if communityName is None: - self.communityName = communityIndex + communityName, communityIndex = communityIndex, None + self.communityName = communityName + # Autogenerate communityIndex if not specified + if communityIndex is None: self.communityIndex = self.securityName = 's%s' % hash( - ( communityIndex, self.mpModel, - self.contextEngineId, self.contextName, self.tag ) - ) + ( self.communityName, + self.mpModel, + self.contextEngineId, + self.contextName, + self.tag ) + ) else: self.communityIndex = communityIndex - self.communityName = communityName self.securityName = securityName is not None and securityName or communityIndex def __hash__(self): raise TypeError('%s is not hashable' % self.__class__.__name__) def __repr__(self): - return '%s("%s", <COMMUNITY>, %r, %r, %r, %r, %r)' % ( + return '%s(communityIndex=%r, communityName=<COMMUNITY>, mpModel=%r, contextEngineId=%r, contextName=%r, tag=%r, securityName=%r)' % ( self.__class__.__name__, self.communityIndex, self.mpModel, @@ -45,6 +50,22 @@ class CommunityData: self.securityName ) + def clone(self, communityIndex=None, communityName=None, + mpModel=None, contextEngineId=None, + contextName=None, tag=None, securityName=None): + # a single arg is considered as a community name + if communityName is None: + communityName, communityIndex = communityIndex, None + return self.__class__( + communityIndex, + communityName is None and self.communityName or communityName, + mpModel is None and self.mpModel or mpModel, + contextEngineId is None and self.contextEngineId or contextEngineId, + contextName is None and self.contextName or contextName, + tag is None and self.tag or tag, + securityName is None and self.securityName or securityName + ) + class UsmUserData: authKey = privKey = None authProtocol = config.usmNoAuthProtocol @@ -102,11 +123,25 @@ class UsmUserData: raise TypeError('%s is not hashable' % self.__class__.__name__) def __repr__(self): - return '%s("%s", <AUTHKEY>, <PRIVKEY>, %r, %r, %r, securityName=%r)'%( + return '%s(userName=%r, authKey=<AUTHKEY>, privKey=<PRIVKEY>, authProtocol=%r, privProtocol=%r, securityEngineId=%r, securityName=%r)'%( self.__class__.__name__, self.userName, self.authProtocol, self.privProtocol, - self.securityEngineId, + self.securityEngineId is None and '<DEFAULT>' or self.securityEngineId, self.securityName ) + + def clone(self, userName=None, + authKey=None, privKey=None, + authProtocol=None, privProtocol=None, + securityEngineId=None, securityName=None): + return self.__class__( + userName is None and self.userName or userName, + authKey is None and self.authKey or authKey, + privKey is None and self.privKey or privKey, + authProtocol is None and self.authProtocol or authProtocol, + privProtocol is None and self.privProtocol or privProtocol, + securityEngineId is None and self.securityEngineId or securityEngineId, + securityName=securityName is None and self.securityName or securityName + ) |