Fix 500 on missing "cidr" key from POST to subnets

If cidr is missing from request body on a POST to subnets,
neutron will throw a 500. Quark uses netaddr, so the Sentinel
passed in for a missing attribute cannot be handled properly.
Wrap the netaddr.IPSet create call in a try/catch to check
for TypeError and throw a BadRequest.

Change-Id: Iee4d874551b859db8239ba6fe9743662755198dc
JIRA:NCP-1990
Closes-Bug: #1598956
This commit is contained in:
Kyle Haley
2016-07-04 15:21:06 -07:00
parent aa060e2154
commit 22af0f684f
2 changed files with 17 additions and 1 deletions

View File

@@ -64,7 +64,12 @@ def _validate_subnet_cidr(context, network_id, new_subnet_cidr):
if neutron_cfg.cfg.CONF.allow_overlapping_ips:
return
new_subnet_ipset = netaddr.IPSet([new_subnet_cidr])
try:
new_subnet_ipset = netaddr.IPSet([new_subnet_cidr])
except TypeError:
LOG.exception("Invalid or missing cidr: %s" % new_subnet_cidr)
raise n_exc.BadRequest(resource="subnet",
msg="Invalid or missing cidr")
# Using admin context here, in case we actually share networks later
subnet_list = db_api.subnet_find(context.elevated(), None, None, None,

View File

@@ -15,6 +15,7 @@
import mock
import netaddr
from neutron.api.v2 import attributes as neutron_attrs
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
@@ -198,6 +199,16 @@ class QuarkCreateSubnets(BaseFunctionalTest):
with self._stubs(network, subnet) as (net, sub1):
self.assertEqual(sub1["allocation_pools"], pools)
def test_create_cidr_not_specified_raises(self):
cidr = neutron_attrs.ATTR_NOT_SPECIFIED
network = dict(name="public", tenant_id="fake", network_plugin="BASE")
network = {"network": network}
subnet = dict(ip_version=4, cidr=cidr, tenant_id="fake")
subnet = {"subnet": subnet}
with self.assertRaises(n_exc.BadRequest):
with self._stubs(network, subnet) as (net, sub1):
pass
class QuarkUpdateSubnets(BaseFunctionalTest):