-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored usm.py and security.py modules into a
usm
package.
- Loading branch information
1 parent
2042209
commit 88b5a1b
Showing
13 changed files
with
663 additions
and
609 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,43 @@ | ||
from __future__ import absolute_import | ||
|
||
from .auth import Authentication | ||
from .community import Community | ||
from .priv import Privacy | ||
from .user import User | ||
from .protocols import ( | ||
AUTH_MD5, | ||
AUTH_NOAUTH, | ||
auth_protocols, | ||
AUTH_SHA, | ||
AUTH_SHA_224, | ||
AUTH_SHA_256, | ||
AUTH_SHA_384, | ||
AUTH_SHA_512, | ||
PRIV_AES, | ||
PRIV_AES_192, | ||
PRIV_AES_256, | ||
PRIV_DES, | ||
PRIV_NOPRIV, | ||
priv_protocols, | ||
) | ||
|
||
__all__ = ( | ||
"Authentication", | ||
"AUTH_MD5", | ||
"AUTH_NOAUTH", | ||
"auth_protocols", | ||
"AUTH_SHA", | ||
"AUTH_SHA_224", | ||
"AUTH_SHA_256", | ||
"AUTH_SHA_384", | ||
"AUTH_SHA_512", | ||
"Community", | ||
"Privacy", | ||
"PRIV_AES", | ||
"PRIV_AES_192", | ||
"PRIV_AES_256", | ||
"PRIV_DES", | ||
"PRIV_NOPRIV", | ||
"priv_protocols", | ||
"User", | ||
) |
This file contains 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,50 @@ | ||
from __future__ import absolute_import | ||
|
||
from .protocols import AUTH_NOAUTH, auth_protocols | ||
|
||
|
||
class Authentication(object): | ||
""" | ||
Provides the authentication data for User objects. | ||
""" | ||
|
||
__slots__ = ("protocol", "passphrase") | ||
|
||
@classmethod | ||
def new_noauth(cls): | ||
return cls(None, None) | ||
|
||
def __init__(self, protocol, passphrase): | ||
if ( | ||
not protocol | ||
or protocol is AUTH_NOAUTH | ||
or protocol == "AUTH_NOAUTH" | ||
): | ||
self.protocol = AUTH_NOAUTH | ||
self.passphrase = None | ||
else: | ||
self.protocol = auth_protocols[protocol] | ||
if not passphrase: | ||
raise ValueError( | ||
"Authentication protocol requires a passphrase" | ||
) | ||
self.passphrase = passphrase | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Authentication): | ||
return NotImplemented | ||
return ( | ||
self.protocol == other.protocol | ||
and self.passphrase == other.passphrase | ||
) | ||
|
||
def __nonzero__(self): | ||
return self.protocol is not AUTH_NOAUTH | ||
|
||
def __repr__(self): | ||
return ( | ||
"<{0.__module__}.{0.__class__.__name__} protocol={0.protocol}>" | ||
).format(self) | ||
|
||
def __str__(self): | ||
return "{0.__class__.__name__}(protocol={0.protocol})".format(self) |
This file contains 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,19 @@ | ||
from __future__ import absolute_import | ||
|
||
from ..CONSTANTS import ( | ||
SNMP_VERSION_1 as _V1, | ||
SNMP_VERSION_2c as _V2C, | ||
SNMP_VERSION_3 as _V3, | ||
) | ||
|
||
version_map = { | ||
"1": "1", | ||
"2c": "2c", | ||
"3": "3", | ||
_V1: "1", | ||
_V2C: "2c", | ||
_V3: "3", | ||
"v1": "1", | ||
"v2c": "2c", | ||
"v3": "3", | ||
} |
This file contains 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,23 @@ | ||
from __future__ import absolute_import | ||
|
||
from ..CONSTANTS import SNMP_VERSION_2c as _V2C | ||
from .common import version_map | ||
|
||
|
||
class Community(object): | ||
""" | ||
Provides the community based security model for SNMP v1/V2c. | ||
""" | ||
|
||
def __init__(self, name, version=_V2C): | ||
mapped = version_map.get(version) | ||
if mapped is None or mapped == "3": | ||
raise ValueError( | ||
"SNMP version '{}' not supported for Community".format(version) | ||
) | ||
self.name = name | ||
self.version = mapped | ||
|
||
def getArguments(self): | ||
community = ("-c", str(self.name)) if self.name else () | ||
return ("-v", self.version) + community |
This file contains 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,48 @@ | ||
from __future__ import absolute_import | ||
|
||
from .protocols import PRIV_NOPRIV, priv_protocols | ||
|
||
|
||
class Privacy(object): | ||
""" | ||
Provides the privacy data for User objects. | ||
""" | ||
|
||
__slots__ = ("protocol", "passphrase") | ||
|
||
@classmethod | ||
def new_nopriv(cls): | ||
return cls(None, None) | ||
|
||
def __init__(self, protocol, passphrase): | ||
if ( | ||
not protocol | ||
or protocol is PRIV_NOPRIV | ||
or protocol == "PRIV_NOPRIV" | ||
): | ||
self.protocol = PRIV_NOPRIV | ||
self.passphrase = None | ||
else: | ||
self.protocol = priv_protocols[protocol] | ||
if not passphrase: | ||
raise ValueError("Privacy protocol requires a passphrase") | ||
self.passphrase = passphrase | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Privacy): | ||
return NotImplemented | ||
return ( | ||
self.protocol == other.protocol | ||
and self.passphrase == other.passphrase | ||
) | ||
|
||
def __nonzero__(self): | ||
return self.protocol is not PRIV_NOPRIV | ||
|
||
def __repr__(self): | ||
return ( | ||
"<{0.__module__}.{0.__class__.__name__} protocol={0.protocol}>" | ||
).format(self) | ||
|
||
def __str__(self): | ||
return "{0.__class__.__name__}(protocol={0.protocol})".format(self) |
Oops, something went wrong.