]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Send 400 error if device specification contains unexpected attributes
authorSalvatore Orlando <salv.orlando@gmail.com>
Wed, 24 Apr 2013 00:11:07 +0000 (02:11 +0200)
committerSalvatore Orlando <salv.orlando@gmail.com>
Wed, 24 Apr 2013 22:02:23 +0000 (00:02 +0200)
Bug #1171926

This patch simply enhances the validator for the 'devices' attribute
by verifying each element in the device list does not contain
unexpected attributes.

Change-Id: Id3fd182a7f711b482ad371752983264ad73b100f

quantum/plugins/nicira/extensions/nvp_networkgw.py
quantum/tests/unit/nicira/test_networkgw.py

index cff405798b66b5fb9797cc87968f10bc68d18384..f9b2dc3ccff7f54ab9e35573a490dfc4fc6aa90e 100644 (file)
@@ -26,6 +26,7 @@ from quantum.api.v2 import base
 from quantum import manager
 from quantum import quota
 
+
 RESOURCE_NAME = "network-gateway"
 COLLECTION_NAME = "%ss" % RESOURCE_NAME
 EXT_ALIAS = RESOURCE_NAME
@@ -62,16 +63,21 @@ def _validate_device_list(data, valid_values=None):
         return msg
     try:
         for device in data:
+            key_specs = {DEVICE_ID_ATTR:
+                         {'type:regex': attributes.UUID_PATTERN,
+                          'required': True},
+                         IFACE_NAME_ATTR:
+                         {'type:string': None,
+                          'required': False}}
             err_msg = attributes._validate_dict(
-                device,
-                key_specs={DEVICE_ID_ATTR:
-                           {'type:regex': attributes.UUID_PATTERN,
-                            'required': True},
-                           IFACE_NAME_ATTR:
-                           {'type:string': None,
-                            'required': False}})
+                device, key_specs=key_specs)
             if err_msg:
                 return err_msg
+            unexpected_keys = [key for key in device if key not in key_specs]
+            if unexpected_keys:
+                err_msg = ("Unexpected keys found in device description:%s",
+                           ",".join(unexpected_keys))
+                return err_msg
     except TypeError:
         return (_("%s: provided data are not iterable") %
                 _validate_device_list.__name__)
index bc1e00c246a999e309426d2188d80ee01db7f85d..d5ffbca93d1bc5a649337a433df0cc0e96e2cd83 100644 (file)
@@ -103,6 +103,27 @@ class NetworkGatewayExtensionTestCase(base.BaseTestCase):
         nw_gw = res.json[self._resource]
         self.assertEqual(nw_gw['id'], nw_gw_id)
 
+    def _test_network_gateway_create_with_error(
+        self, data, error_code=exc.HTTPBadRequest.code):
+        res = self.api.post_json(_get_path(networkgw.COLLECTION_NAME), data,
+                                 expect_errors=True)
+        self.assertEqual(res.status_int, error_code)
+
+    def test_network_gateway_create_invalid_device_spec(self):
+        data = {self._resource: {'name': 'nw-gw',
+                                 'tenant_id': _uuid(),
+                                 'devices': [{'id': _uuid(),
+                                              'invalid': 'xxx'}]}}
+        self._test_network_gateway_create_with_error(data)
+
+    def test_network_gateway_create_extra_attr_in_device_spec(self):
+        data = {self._resource: {'name': 'nw-gw',
+                                 'tenant_id': _uuid(),
+                                 'devices': [{'id': _uuid(),
+                                              'interface_name': 'xxx',
+                                              'extra_attr': 'onetoomany'}]}}
+        self._test_network_gateway_create_with_error(data)
+
     def test_network_gateway_update(self):
         nw_gw_name = 'updated'
         data = {self._resource: {'name': nw_gw_name}}