From fdeb100129321c7010d24efd6311cd8f52d847bf Mon Sep 17 00:00:00 2001 From: Ferran Tufan Date: Thu, 30 Nov 2023 14:55:53 +0100 Subject: [PATCH 1/2] Recognise and add tests for various types of invalid MAC addresses The YAML 1.1 specification allows PyYAML to parse certain MAC addresses as sexagesimal integers. Wrapping the MAC address between quotes is a valid workaround for that behavior, but because most users aren't aware of this YAML quirk, we should inform them explicitly. Finally, I have added several tests for MAC address validation. --- vnet_manager/config/validate.py | 6 ++++++ vnet_manager/tests/config/test_validate.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/vnet_manager/config/validate.py b/vnet_manager/config/validate.py index 056b26e..72d6931 100644 --- a/vnet_manager/config/validate.py +++ b/vnet_manager/config/validate.py @@ -244,6 +244,12 @@ def validate_interface_config(self, machine: str): if "mac" not in int_vals: logger.debug(f"MAC not found for interface {int_name} on machine {machine}, generating a random one") self._new_config["machines"][machine]["interfaces"][int_name]["mac"] = random_mac_generator() + elif isinstance(int_vals["mac"], int): + logger.error( + f"MAC {int_vals['mac']} for interface {int_name} on machine {machine} was parsed as a sexagesimal integer. " + "Please wrap the MAC address between 'quotes'" + ) + self._all_ok = False # From: https://stackoverflow.com/a/7629690/8632038 elif not fullmatch(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", int_vals["mac"]): logger.error( diff --git a/vnet_manager/tests/config/test_validate.py b/vnet_manager/tests/config/test_validate.py index 8470473..a81ad7b 100644 --- a/vnet_manager/tests/config/test_validate.py +++ b/vnet_manager/tests/config/test_validate.py @@ -197,6 +197,24 @@ def test_validate_machine_config_fails_if_bridge_config_is_not_a_dict(self): self.assertFalse(self.validator.config_validation_successful) self.logger.error.assert_called_once() + def test_validate_interface_config_sexagesimal_mac_address(self): + # 0d33212743741 is the sexagesimal representation of 42:42:42:42:09:01 + # cf. https://github.com/Erik-Lamers1/vnet-manager/issues/62 + self.validator.config["machines"]["router100"]["interfaces"]["eth12"]["mac"] = 33212743741 + self.validator.validate_interface_config("router100") + self.assertFalse(self.validator.config_validation_successful) + self.logger.error.assert_called_once_with( + f"MAC 33212743741 for interface eth12 on machine router100 was parsed as a sexagesimal integer. Please wrap the MAC address between 'quotes'" + ) + + def test_validate_interface_config_invalid_mac_address(self): + self.validator.config["machines"]["router100"]["interfaces"]["eth12"]["mac"] = "gg:hh:ii:jj:kk:ll" + self.validator.validate_interface_config("router100") + self.assertFalse(self.validator.config_validation_successful) + self.logger.error.assert_called_once_with( + f"MAC gg:hh:ii:jj:kk:ll for interface eth12 on machine router100, does not seem to be valid. Please check your settings" + ) + class TestValidateConfigValidateMachineFilesParameters(VNetTestCase): def setUp(self) -> None: From 5974e86d2f4287cd7b6a2bc3ff7729cbd0d53d0b Mon Sep 17 00:00:00 2001 From: Ferran Tufan Date: Thu, 30 Nov 2023 18:18:21 +0100 Subject: [PATCH 2/2] Adjust wording --- vnet_manager/config/validate.py | 2 +- vnet_manager/tests/config/test_validate.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vnet_manager/config/validate.py b/vnet_manager/config/validate.py index 72d6931..9258e8b 100644 --- a/vnet_manager/config/validate.py +++ b/vnet_manager/config/validate.py @@ -247,7 +247,7 @@ def validate_interface_config(self, machine: str): elif isinstance(int_vals["mac"], int): logger.error( f"MAC {int_vals['mac']} for interface {int_name} on machine {machine} was parsed as a sexagesimal integer. " - "Please wrap the MAC address between 'quotes'" + "Please wrap the MAC address in 'quotes'" ) self._all_ok = False # From: https://stackoverflow.com/a/7629690/8632038 diff --git a/vnet_manager/tests/config/test_validate.py b/vnet_manager/tests/config/test_validate.py index a81ad7b..161163a 100644 --- a/vnet_manager/tests/config/test_validate.py +++ b/vnet_manager/tests/config/test_validate.py @@ -204,7 +204,7 @@ def test_validate_interface_config_sexagesimal_mac_address(self): self.validator.validate_interface_config("router100") self.assertFalse(self.validator.config_validation_successful) self.logger.error.assert_called_once_with( - f"MAC 33212743741 for interface eth12 on machine router100 was parsed as a sexagesimal integer. Please wrap the MAC address between 'quotes'" + f"MAC 33212743741 for interface eth12 on machine router100 was parsed as a sexagesimal integer. Please wrap the MAC address in 'quotes'" ) def test_validate_interface_config_invalid_mac_address(self):