From 037f52c83b87ae3ddfd0080691eb51ca69f6656c Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 25 Mar 2022 08:38:50 +0100 Subject: [PATCH 1/2] Use PRIVATE_NET_MASKS in EC2 --- IM/VirtualMachine.py | 2 +- IM/connectors/EC2.py | 10 ++++++++-- IM/connectors/OpenStack.py | 4 ++-- test/unit/connectors/Azure.py | 9 +++++++++ test/unit/connectors/EC2.py | 12 ++++++++++++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/IM/VirtualMachine.py b/IM/VirtualMachine.py index bda1a8bd7..caa3a3143 100644 --- a/IM/VirtualMachine.py +++ b/IM/VirtualMachine.py @@ -642,7 +642,7 @@ def add_public_net(radl): return public_net, num_net - def setIps(self, public_ips, private_ips, remove_old=False, ignore_nets=None): + def setIps(self, public_ips, private_ips, remove_old=True, ignore_nets=None): """ Set the specified IPs in the VM RADL info """ diff --git a/IM/connectors/EC2.py b/IM/connectors/EC2.py index dcfdd3d6b..a1c177293 100644 --- a/IM/connectors/EC2.py +++ b/IM/connectors/EC2.py @@ -16,7 +16,7 @@ import time import requests -from netaddr import IPNetwork, spanning_cidr +from netaddr import IPNetwork, IPAddress, spanning_cidr try: import boto.ec2 @@ -1031,7 +1031,13 @@ def setIPsFromInstance(self, vm, instance): num_nets += 1 num_pub_nets = 1 if instance.private_ip_address is not None and len(instance.private_ip_address) > 0: - private_ips = [instance.private_ip_address] + is_private = any([IPAddress(instance.private_ip_address) in IPNetwork(mask) + for mask in Config.PRIVATE_NET_MASKS]) + if is_private: + private_ips = [instance.private_ip_address] + else: + public_ips = [instance.private_ip_address] + num_pub_nets += 1 num_nets += 1 vm.setIps(public_ips, private_ips) diff --git a/IM/connectors/OpenStack.py b/IM/connectors/OpenStack.py index 71e113639..1183f3bf8 100644 --- a/IM/connectors/OpenStack.py +++ b/IM/connectors/OpenStack.py @@ -730,7 +730,7 @@ def setIPsFromInstance(self, vm, node): priv_ips.append(ipu) else: pub_ips.append(ipu) - vm.setIps(pub_ips, priv_ips) + vm.setIps(pub_ips, priv_ips, False) else: # if addresses are not available use the old method @@ -741,7 +741,7 @@ def setIPsFromInstance(self, vm, node): private_ips.append(ip) else: public_ips.append(ip) - vm.setIps(public_ips, private_ips, True) + vm.setIps(public_ips, private_ips) if vm.state == VirtualMachine.RUNNING: if self.add_public_ip_count < self.MAX_ADD_IP_COUNT: diff --git a/test/unit/connectors/Azure.py b/test/unit/connectors/Azure.py index 9818f2dc2..3d34dd565 100755 --- a/test/unit/connectors/Azure.py +++ b/test/unit/connectors/Azure.py @@ -410,6 +410,15 @@ def test_30_updateVMInfo(self, credentials, dns_client, compute_client, network_ {'arecords': [{'ipv4_address': '13.0.0.1'}], 'ttl': 300})]) self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) + # Test using PRIVATE_NET_MASKS setting 10.0.0.0/8 as public net + old_priv = Config.PRIVATE_NET_MASKS + Config.PRIVATE_NET_MASKS = ["172.16.0.0/12", "192.168.0.0/16"] + ip_conf.public_ip_address = None + success, vm = azure_cloud.updateVMInfo(vm, auth) + Config.PRIVATE_NET_MASKS = old_priv + self.assertEqual(vm.getPublicIP(), "10.0.0.1") + self.assertEqual(vm.getPrivateIP(), None) + @patch('IM.connectors.Azure.ComputeManagementClient') @patch('IM.connectors.Azure.ClientSecretCredential') def test_40_stop(self, credentials, compute_client): diff --git a/test/unit/connectors/EC2.py b/test/unit/connectors/EC2.py index 4e643c6f7..524e78f06 100755 --- a/test/unit/connectors/EC2.py +++ b/test/unit/connectors/EC2.py @@ -28,6 +28,7 @@ from IM.VirtualMachine import VirtualMachine from IM.InfrastructureInfo import InfrastructureInfo from IM.connectors.EC2 import EC2CloudConnector +from IM.config import Config from mock import patch, MagicMock, call @@ -406,6 +407,17 @@ def test_30_updateVMInfo(self, record_sets, connect_to_region, get_connection): self.assertEqual(change.add_value.call_args_list, [call('158.42.1.1')]) self.assertEqual(conn.create_route.call_args_list, [call('routet-id', '10.0.10.0/24', instance_id='int-id')]) + # Test using PRIVATE_NET_MASKS setting 10.0.0.0/8 as public net + old_priv = Config.PRIVATE_NET_MASKS + Config.PRIVATE_NET_MASKS = ["172.16.0.0/12", "192.168.0.0/16"] + instance.ip_address = None + instance.private_ip_address = "10.0.0.1" + conn.get_all_addresses.return_value = [] + success, vm = ec2_cloud.updateVMInfo(vm, auth) + Config.PRIVATE_NET_MASKS = old_priv + self.assertEqual(vm.getPublicIP(), "10.0.0.1") + self.assertEqual(vm.getPrivateIP(), None) + @patch('IM.connectors.EC2.EC2CloudConnector.get_connection') def test_30_updateVMInfo_spot(self, get_connection): radl_data = """ From 823ea988045d8e6ff0ec27596fdb835753061b9f Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 25 Mar 2022 08:40:03 +0100 Subject: [PATCH 2/2] Minor change --- IM/connectors/OCCI.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IM/connectors/OCCI.py b/IM/connectors/OCCI.py index 5835082b6..32879a777 100644 --- a/IM/connectors/OCCI.py +++ b/IM/connectors/OCCI.py @@ -278,7 +278,7 @@ def setIPs(self, vm, occi_res, auth_data, auth_header): not public_ips and vm.requested_radl.hasPublicNet(vm.info.systems[0].name)): self.manage_public_ips(vm, auth_data, auth_header) - vm.setIps(public_ips, private_ips, remove_old=True) + vm.setIps(public_ips, private_ips) @staticmethod def get_property_from_category(occi_res, category, prop_name):