Skip to content

Commit

Permalink
Add check for cidr overrapping for adding external gateway
Browse files Browse the repository at this point in the history
Fixes bug 1053633
Also add check for cidr overrapping between external gateway and
interfaces

Change-Id: I5bfb2fd96ea467b63e940893979a912caf550deb
  • Loading branch information
Nachi Ueno authored and openstack-gerrit committed Nov 14, 2012
1 parent 4546a18 commit 26b383f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
9 changes: 7 additions & 2 deletions quantum/db/l3_db.py
Expand Up @@ -193,6 +193,12 @@ def _update_router_gw_info(self, context, router_id, info):

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':
Expand Down Expand Up @@ -250,8 +256,7 @@ def _check_for_dup_router_subnet(self, context, router_id,
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']
Expand Down
40 changes: 37 additions & 3 deletions quantum/tests/unit/test_l3_plugin.py
Expand Up @@ -561,6 +561,40 @@ def test_router_add_interface_dup_subnet2(self):
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:
Expand Down Expand Up @@ -763,7 +797,7 @@ def _validate_floating_ip(self, fip):

@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:
Expand Down Expand Up @@ -794,7 +828,7 @@ def floatingip_with_assoc(self, port_id=None, fmt='json'):

@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(
Expand Down Expand Up @@ -831,7 +865,7 @@ def test_floatingip_crd_ops(self):

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:
Expand Down

0 comments on commit 26b383f

Please sign in to comment.