From 6042c6fed9914d9a074429b793c517484f67d07d Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Wed, 8 Jan 2020 20:44:15 -0800 Subject: [PATCH] added validation checks/conversion for valid values of mac addresses and ip addresses to addaddr and sethwaddr functions --- daemon/core/nodes/base.py | 2 ++ daemon/core/nodes/interface.py | 3 ++- daemon/core/nodes/physical.py | 19 +++++++++++++++---- daemon/core/utils.py | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index c3c1524d..95d1b379 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -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: diff --git a/daemon/core/nodes/interface.py b/daemon/core/nodes/interface.py index 884ce4e9..236bdd5c 100644 --- a/daemon/core/nodes/interface.py +++ b/daemon/core/nodes/interface.py @@ -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): diff --git a/daemon/core/nodes/physical.py b/daemon/core/nodes/physical.py index 4ed38470..1d470b98 100644 --- a/daemon/core/nodes/physical.py +++ b/daemon/core/nodes/physical.py @@ -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): diff --git a/daemon/core/utils.py b/daemon/core/utils.py index 13d75887..cf394a15 100644 --- a/daemon/core/utils.py +++ b/daemon/core/utils.py @@ -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}")