]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix quota limit range validator
authorElena Ezhova <eezhova@mirantis.com>
Mon, 25 Aug 2014 14:44:32 +0000 (18:44 +0400)
committerElena Ezhova <eezhova@mirantis.com>
Thu, 25 Sep 2014 06:30:14 +0000 (10:30 +0400)
In Quota model limit is defined with Integer type which in case of
MySQL and PostgreSQL is always stored in 4 bytes. At the same time,
the current validator checks that the value does not exceed sys.maxsize
which depends on whether the system is 32-bit or 64-bit based.

In SQLite Integer can be stored in 1, 2, 3, 4, 6, or 8 bytes depending
on the magnitude of the value. Nevertheless, assume that it can not
exceed 4 bytes for consistency.

Limited the upper bound of the validator with 2**31 - 1.

Added a unit test.

Closes-Bug: #1338479
Change-Id: Icefa2fc228e4255a022d586cab4590607953d1ee

neutron/common/constants.py
neutron/extensions/quotasv2.py
neutron/tests/unit/test_quota_ext.py

index 158e34245ee87f7602c293341da903909ba17e56..59066903b11366300e388ea9f893cbe94d02ba31 100644 (file)
@@ -152,3 +152,8 @@ DEVICE_NAME_MAX_LEN = 15
 TAP_DEVICE_PREFIX = 'tap'
 
 ATTRIBUTES_TO_UPDATE = 'attributes_to_update'
+
+# Maximum value integer can take in MySQL and PostgreSQL
+# In SQLite integer can be stored in 1, 2, 3, 4, 6, or 8 bytes,
+# but here it will be limited by this value for consistency.
+DB_INTEGER_MAX_VALUE = 2 ** 31 - 1
index 4fa9bf28056df71770eb36fee7828b794ff90d59..19fa85dbff26230b6d7d10d81f0a5c7db2e423c3 100644 (file)
@@ -13,8 +13,6 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import sys
-
 from oslo.config import cfg
 import webob
 
@@ -22,6 +20,7 @@ from neutron.api import extensions
 from neutron.api.v2 import attributes
 from neutron.api.v2 import base
 from neutron.api.v2 import resource
+from neutron.common import constants as const
 from neutron.common import exceptions as n_exc
 from neutron import manager
 from neutron.openstack.common import importutils
@@ -55,7 +54,7 @@ class QuotaSetsController(wsgi.Controller):
                 'allow_post': False,
                 'allow_put': True,
                 'convert_to': attributes.convert_to_int,
-                'validate': {'type:range': [-1, sys.maxsize]},
+                'validate': {'type:range': [-1, const.DB_INTEGER_MAX_VALUE]},
                 'is_visible': True}
         self._update_extended_attributes = False
 
index 262fb04411596a809809bedd6dacfd3126f59722..d330b5f67d99c4f17519ac4a4c58ae963611b146 100644 (file)
@@ -18,11 +18,13 @@ import sys
 import mock
 from oslo.config import cfg
 import testtools
+from webob import exc
 import webtest
 
 from neutron.api import extensions
 from neutron.api.v2 import attributes
 from neutron.common import config
+from neutron.common import constants
 from neutron.common import exceptions
 from neutron import context
 from neutron.db import quota_db
@@ -188,6 +190,16 @@ class QuotaExtensionDbTestCase(QuotaExtensionTestCase):
                            expect_errors=True)
         self.assertEqual(400, res.status_int)
 
+    def test_update_quotas_with_out_of_range_integer_returns_400(self):
+        tenant_id = 'tenant_id1'
+        env = {'neutron.context': context.Context('', tenant_id,
+                                                  is_admin=True)}
+        quotas = {'quota': {'network': constants.DB_INTEGER_MAX_VALUE + 1}}
+        res = self.api.put(_get_path('quotas', id=tenant_id, fmt=self.fmt),
+                           self.serialize(quotas), extra_environ=env,
+                           expect_errors=True)
+        self.assertEqual(exc.HTTPBadRequest.code, res.status_int)
+
     def test_update_quotas_to_unlimited(self):
         tenant_id = 'tenant_id1'
         env = {'neutron.context': context.Context('', tenant_id,