Merge "Restrict container name in REST API layer"

This commit is contained in:
Jenkins 2016-08-17 14:25:35 +00:00 committed by Gerrit Code Review
commit a4dee54194
3 changed files with 32 additions and 5 deletions

View File

@ -11,6 +11,7 @@
# under the License.
import logging
import re
import six
from oslo_utils import strutils
@ -21,6 +22,7 @@ from zun.common.i18n import _
from zun.common.i18n import _LE
LOG = logging.getLogger(__name__)
container_pattern = re.compile(r'[a-zA-Z0-9][a-zA-Z0-9_.-]')
class Text(object):
@ -56,6 +58,26 @@ class String(object):
return value
class ContainerName(String):
type_name = 'ContainerName'
# Container name allows to be None or a string matches pattern
# `[a-zA-Z0-9][a-zA-Z0-9_.-].` with minimum length is 2 and maximum length
# 255 string type.
@classmethod
def validate(cls, value):
if value is None:
return value
super(ContainerName, cls).validate(value, min_length=2, max_length=255)
match = container_pattern.match(value)
if match:
return value
else:
message = _('%s does not match [a-zA-Z0-9][a-zA-Z0-9_.-].') % value
raise exception.InvalidValue(message)
class Integer(object):
type_name = 'Integer'

View File

@ -59,11 +59,7 @@ class Container(base.APIBase):
'validate': types.Uuid.validate,
},
'name': {
'validate': types.String.validate,
'validate_args': {
'min_length': 1,
'max_length': 255,
},
'validate': types.ContainerName.validate,
},
'image': {
'validate': types.String.validate,

View File

@ -138,3 +138,12 @@ class TestTypes(test_base.BaseTestCase):
self.assertIsInstance(value, list)
self.assertIsInstance(value[0], TestAPI)
self.assertEqual({'test': 'test_value'}, value[0].as_dict())
def test_container_name_type(self):
test_value = '***'
self.assertRaises(exception.InvalidValue,
types.ContainerName.validate, test_value)
test_value = '*' * 256
self.assertRaises(exception.InvalidValue,
types.ContainerName.validate, test_value)