From 9343c44818ba8b472903973bfd190e478062ef83 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 21 Sep 2012 20:52:16 +0000 Subject: [PATCH] Add check for cidr overrapping for adding external gateway Fixes bug 1053633 Also add check for cidr overrapping between external gateway and interfaces Change-Id: I5bfb2fd96ea467b63e940893979a912caf550deb --- quantum/db/l3_db.py | 9 +++++-- quantum/tests/unit/test_l3_plugin.py | 40 +++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/quantum/db/l3_db.py b/quantum/db/l3_db.py index 8b78bf1e2..21c340955 100644 --- a/quantum/db/l3_db.py +++ b/quantum/db/l3_db.py @@ -193,6 +193,12 @@ class L3_NAT_db_mixin(l3.RouterPluginBase): if network_id is not None and (gw_port is None or gw_port['network_id'] != network_id): + subnets = self._get_subnets_by_network(context, + network_id) + for subnet in subnets: + self._check_for_dup_router_subnet(context, router_id, + network_id, subnet['id']) + # Port has no 'tenant-id', as it is hidden from user gw_port = self.create_port(context.elevated(), { 'port': @@ -250,8 +256,7 @@ class L3_NAT_db_mixin(l3.RouterPluginBase): try: rport_qry = context.session.query(models_v2.Port) rports = rport_qry.filter_by( - device_id=router_id, - device_owner=DEVICE_OWNER_ROUTER_INTF,).all() + device_id=router_id).all() # its possible these ports on on the same network, but # different subnet new_cidr = self._get_subnet(context, subnet_id)['cidr'] diff --git a/quantum/tests/unit/test_l3_plugin.py b/quantum/tests/unit/test_l3_plugin.py index c40499256..eae343dea 100644 --- a/quantum/tests/unit/test_l3_plugin.py +++ b/quantum/tests/unit/test_l3_plugin.py @@ -561,6 +561,40 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase): None, p1['port']['id']) + def test_router_add_gateway_dup_subnet1(self): + with self.router() as r: + with self.subnet() as s: + body = self._router_interface_action('add', + r['router']['id'], + s['subnet']['id'], + None) + self._set_net_external(s['subnet']['network_id']) + self._add_external_gateway_to_router( + r['router']['id'], + s['subnet']['network_id'], + expected_code=exc.HTTPBadRequest.code) + body = self._router_interface_action('remove', + r['router']['id'], + s['subnet']['id'], + None) + + def test_router_add_gateway_dup_subnet2(self): + with self.router() as r: + with self.subnet() as s: + self._set_net_external(s['subnet']['network_id']) + self._add_external_gateway_to_router( + r['router']['id'], + s['subnet']['network_id']) + self._router_interface_action('add', + r['router']['id'], + s['subnet']['id'], + None, + expected_code=exc. + HTTPBadRequest.code) + self._remove_external_gateway_from_router( + r['router']['id'], + s['subnet']['network_id']) + def test_router_add_interface_overlapped_cidr(self): with self.router() as r: with self.subnet(cidr='10.0.1.0/24') as s1: @@ -763,7 +797,7 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase): @contextlib.contextmanager def floatingip_with_assoc(self, port_id=None, fmt='json'): - with self.subnet() as public_sub: + with self.subnet(cidr='11.0.0.0/24') as public_sub: self._set_net_external(public_sub['subnet']['network_id']) with self.port() as private_port: with self.router() as r: @@ -794,7 +828,7 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase): @contextlib.contextmanager def floatingip_no_assoc(self, private_sub, fmt='json'): - with self.subnet() as public_sub: + with self.subnet(cidr='12.0.0.0/24') as public_sub: self._set_net_external(public_sub['subnet']['network_id']) with self.router() as r: self._add_external_gateway_to_router( @@ -831,7 +865,7 @@ class L3NatDBTestCase(test_db_plugin.QuantumDbPluginV2TestCase): def test_floatingip_with_assoc_fails(self): fmt = 'json' - with self.subnet() as public_sub: + with self.subnet(cidr='200.0.0.1/24') as public_sub: self._set_net_external(public_sub['subnet']['network_id']) with self.port() as private_port: with self.router() as r: -- 2.45.2