added validation checks/conversion for valid values of mac addresses and ip addresses to addaddr and sethwaddr functions
This commit is contained in:
parent
de493c810a
commit
6042c6fed9
4 changed files with 51 additions and 6 deletions
|
@ -729,6 +729,7 @@ class CoreNode(CoreNodeBase):
|
||||||
:return: nothing
|
:return: nothing
|
||||||
:raises CoreCommandError: when a non-zero exit status occurs
|
:raises CoreCommandError: when a non-zero exit status occurs
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_mac(addr)
|
||||||
interface = self._netif[ifindex]
|
interface = self._netif[ifindex]
|
||||||
interface.sethwaddr(addr)
|
interface.sethwaddr(addr)
|
||||||
if self.up:
|
if self.up:
|
||||||
|
@ -742,6 +743,7 @@ class CoreNode(CoreNodeBase):
|
||||||
:param str addr: address to add to interface
|
:param str addr: address to add to interface
|
||||||
:return: nothing
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_ip(addr)
|
||||||
interface = self._netif[ifindex]
|
interface = self._netif[ifindex]
|
||||||
interface.addaddr(addr)
|
interface.addaddr(addr)
|
||||||
if self.up:
|
if self.up:
|
||||||
|
|
|
@ -114,7 +114,7 @@ class CoreInterface:
|
||||||
:param str addr: address to add
|
:param str addr: address to add
|
||||||
:return: nothing
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_ip(addr)
|
||||||
self.addrlist.append(addr)
|
self.addrlist.append(addr)
|
||||||
|
|
||||||
def deladdr(self, addr):
|
def deladdr(self, addr):
|
||||||
|
@ -133,6 +133,7 @@ class CoreInterface:
|
||||||
:param str addr: hardware address to set to.
|
:param str addr: hardware address to set to.
|
||||||
:return: nothing
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_mac(addr)
|
||||||
self.hwaddr = addr
|
self.hwaddr = addr
|
||||||
|
|
||||||
def getparam(self, key):
|
def getparam(self, key):
|
||||||
|
|
|
@ -59,19 +59,30 @@ class PhysicalNode(CoreNodeBase):
|
||||||
def sethwaddr(self, ifindex, addr):
|
def sethwaddr(self, ifindex, addr):
|
||||||
"""
|
"""
|
||||||
Set hardware address for an interface.
|
Set hardware address for an interface.
|
||||||
|
|
||||||
|
:param int ifindex: index of interface to set hardware address for
|
||||||
|
:param str addr: hardware address to set
|
||||||
|
:return: nothing
|
||||||
|
:raises CoreCommandError: when a non-zero exit status occurs
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_mac(addr)
|
||||||
interface = self._netif[ifindex]
|
interface = self._netif[ifindex]
|
||||||
interface.sethwaddr(addr)
|
interface.sethwaddr(addr)
|
||||||
if self.up:
|
if self.up:
|
||||||
self.net_client.device_mac(interface.name, str(addr))
|
self.net_client.device_mac(interface.name, addr)
|
||||||
|
|
||||||
def addaddr(self, ifindex, addr):
|
def addaddr(self, ifindex, addr):
|
||||||
"""
|
"""
|
||||||
Add an address to an interface.
|
Add an address to an interface.
|
||||||
|
|
||||||
|
:param int ifindex: index of interface to add address to
|
||||||
|
:param str addr: address to add
|
||||||
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_ip(addr)
|
||||||
interface = self._netif[ifindex]
|
interface = self._netif[ifindex]
|
||||||
if self.up:
|
if self.up:
|
||||||
self.net_client.create_address(interface.name, str(addr))
|
self.net_client.create_address(interface.name, addr)
|
||||||
interface.addaddr(addr)
|
interface.addaddr(addr)
|
||||||
|
|
||||||
def deladdr(self, ifindex, addr):
|
def deladdr(self, ifindex, addr):
|
||||||
|
@ -408,9 +419,9 @@ class Rj45Node(CoreNodeBase, CoreInterface):
|
||||||
:return: nothing
|
:return: nothing
|
||||||
:raises CoreCommandError: when there is a command exception
|
:raises CoreCommandError: when there is a command exception
|
||||||
"""
|
"""
|
||||||
|
addr = utils.validate_ip(addr)
|
||||||
if self.up:
|
if self.up:
|
||||||
self.net_client.create_address(self.name, str(addr))
|
self.net_client.create_address(self.name, addr)
|
||||||
|
|
||||||
CoreInterface.addaddr(self, addr)
|
CoreInterface.addaddr(self, addr)
|
||||||
|
|
||||||
def deladdr(self, addr):
|
def deladdr(self, addr):
|
||||||
|
|
|
@ -18,7 +18,7 @@ from subprocess import PIPE, STDOUT, Popen
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
from core.errors import CoreCommandError
|
from core.errors import CoreCommandError, CoreError
|
||||||
|
|
||||||
DEVNULL = open(os.devnull, "wb")
|
DEVNULL = open(os.devnull, "wb")
|
||||||
|
|
||||||
|
@ -425,3 +425,34 @@ def random_mac():
|
||||||
mac = netaddr.EUI(value)
|
mac = netaddr.EUI(value)
|
||||||
mac.dialect = netaddr.mac_unix
|
mac.dialect = netaddr.mac_unix
|
||||||
return str(mac)
|
return str(mac)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_mac(value):
|
||||||
|
"""
|
||||||
|
Validate mac and return unix formatted version.
|
||||||
|
|
||||||
|
:param str value: address to validate
|
||||||
|
:return: unix formatted mac
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
mac = netaddr.EUI(value)
|
||||||
|
mac.dialect = netaddr.mac_unix
|
||||||
|
return str(mac)
|
||||||
|
except netaddr.AddrFormatError as e:
|
||||||
|
raise CoreError(f"invalid mac address {value}: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_ip(value):
|
||||||
|
"""
|
||||||
|
Validate ip address with prefix and return formatted version.
|
||||||
|
|
||||||
|
:param str value: address to validate
|
||||||
|
:return: formatted ip address
|
||||||
|
:rtype: str
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
ip = netaddr.IPNetwork(value)
|
||||||
|
return str(ip)
|
||||||
|
except (ValueError, netaddr.AddrFormatError) as e:
|
||||||
|
raise CoreError(f"invalid ip address {value}: {e}")
|
||||||
|
|
Loading…
Add table
Reference in a new issue