added validation checks/conversion for valid values of mac addresses and ip addresses to addaddr and sethwaddr functions

This commit is contained in:
Blake Harnden 2020-01-08 20:44:15 -08:00
parent de493c810a
commit 6042c6fed9
4 changed files with 51 additions and 6 deletions

View file

@ -729,6 +729,7 @@ class CoreNode(CoreNodeBase):
:return: nothing
:raises CoreCommandError: when a non-zero exit status occurs
"""
addr = utils.validate_mac(addr)
interface = self._netif[ifindex]
interface.sethwaddr(addr)
if self.up:
@ -742,6 +743,7 @@ class CoreNode(CoreNodeBase):
:param str addr: address to add to interface
:return: nothing
"""
addr = utils.validate_ip(addr)
interface = self._netif[ifindex]
interface.addaddr(addr)
if self.up:

View file

@ -114,7 +114,7 @@ class CoreInterface:
:param str addr: address to add
:return: nothing
"""
addr = utils.validate_ip(addr)
self.addrlist.append(addr)
def deladdr(self, addr):
@ -133,6 +133,7 @@ class CoreInterface:
:param str addr: hardware address to set to.
:return: nothing
"""
addr = utils.validate_mac(addr)
self.hwaddr = addr
def getparam(self, key):

View file

@ -59,19 +59,30 @@ class PhysicalNode(CoreNodeBase):
def sethwaddr(self, ifindex, addr):
"""
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.sethwaddr(addr)
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):
"""
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]
if self.up:
self.net_client.create_address(interface.name, str(addr))
self.net_client.create_address(interface.name, addr)
interface.addaddr(addr)
def deladdr(self, ifindex, addr):
@ -408,9 +419,9 @@ class Rj45Node(CoreNodeBase, CoreInterface):
:return: nothing
:raises CoreCommandError: when there is a command exception
"""
addr = utils.validate_ip(addr)
if self.up:
self.net_client.create_address(self.name, str(addr))
self.net_client.create_address(self.name, addr)
CoreInterface.addaddr(self, addr)
def deladdr(self, addr):

View file

@ -18,7 +18,7 @@ from subprocess import PIPE, STDOUT, Popen
import netaddr
from core.errors import CoreCommandError
from core.errors import CoreCommandError, CoreError
DEVNULL = open(os.devnull, "wb")
@ -425,3 +425,34 @@ def random_mac():
mac = netaddr.EUI(value)
mac.dialect = netaddr.mac_unix
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}")