Merge "Handle invalid image name"
This commit is contained in:
commit
3d48bc6b6d
|
@ -12,6 +12,7 @@
|
|||
|
||||
from zun.common.docker_image import digest as digest_
|
||||
from zun.common.docker_image import regexp
|
||||
from zun.common import exception
|
||||
|
||||
|
||||
ImageRegexps = regexp.ImageRegexps
|
||||
|
@ -19,43 +20,6 @@ ImageRegexps = regexp.ImageRegexps
|
|||
NAME_TOTAL_LENGTH_MAX = 255
|
||||
|
||||
|
||||
class InvalidReference(Exception):
|
||||
@classmethod
|
||||
def default(cls):
|
||||
return cls("invalid reference")
|
||||
|
||||
|
||||
class ReferenceInvalidFormat(InvalidReference):
|
||||
@classmethod
|
||||
def default(cls):
|
||||
return cls("invalid reference format")
|
||||
|
||||
|
||||
class TagInvalidFormat(InvalidReference):
|
||||
@classmethod
|
||||
def default(cls):
|
||||
return cls("invalid tag format")
|
||||
|
||||
|
||||
class DigestInvalidFormat(InvalidReference):
|
||||
@classmethod
|
||||
def default(cls):
|
||||
return cls("invalid digest format")
|
||||
|
||||
|
||||
class NameEmpty(InvalidReference):
|
||||
@classmethod
|
||||
def default(cls):
|
||||
return cls("repository name must have at least one component")
|
||||
|
||||
|
||||
class NameTooLong(InvalidReference):
|
||||
@classmethod
|
||||
def default(cls):
|
||||
return cls("repository name must not be more than {} "
|
||||
"characters".format(NAME_TOTAL_LENGTH_MAX))
|
||||
|
||||
|
||||
class Reference(dict):
|
||||
def __init__(self, name=None, tag=None, digest=None):
|
||||
super(Reference, self).__init__()
|
||||
|
@ -104,13 +68,13 @@ class Reference(dict):
|
|||
def parse(cls, s):
|
||||
matched = ImageRegexps.REFERENCE_REGEXP.match(s)
|
||||
if not matched and not s:
|
||||
raise NameEmpty.default()
|
||||
raise exception.NameEmpty()
|
||||
if not matched:
|
||||
raise ReferenceInvalidFormat.default()
|
||||
raise exception.ReferenceInvalidFormat()
|
||||
|
||||
matches = matched.groups()
|
||||
if len(matches[0]) > NAME_TOTAL_LENGTH_MAX:
|
||||
raise NameTooLong.default()
|
||||
raise exception.NameTooLong(length_max=NAME_TOTAL_LENGTH_MAX)
|
||||
|
||||
ref = cls(name=matches[0], tag=matches[1])
|
||||
if matches[2]:
|
||||
|
@ -119,7 +83,7 @@ class Reference(dict):
|
|||
|
||||
r = ref.best_reference()
|
||||
if not r:
|
||||
raise NameEmpty.default()
|
||||
raise exception.NameEmpty()
|
||||
|
||||
return r
|
||||
|
||||
|
|
|
@ -759,3 +759,20 @@ class Base64Exception(Invalid):
|
|||
class InvalidEncryptionKey(ZunException):
|
||||
message = _('Can not decrypt data with the auth_encryption_key '
|
||||
'in zun config.')
|
||||
|
||||
|
||||
class InvalidReference(ZunException):
|
||||
message = _('invalid reference')
|
||||
|
||||
|
||||
class ReferenceInvalidFormat(InvalidReference):
|
||||
message = _('invalid reference format')
|
||||
|
||||
|
||||
class NameEmpty(InvalidReference):
|
||||
message = _('repository name must have at least one component')
|
||||
|
||||
|
||||
class NameTooLong(InvalidReference):
|
||||
message = _('repository name must not be more than %(length_max)s '
|
||||
'characters')
|
||||
|
|
|
@ -81,6 +81,10 @@ class API(object):
|
|||
"image '%(image)s' and image driver '%(driver)s'.",
|
||||
{'image': new_container.image,
|
||||
'driver': new_container.image_driver})
|
||||
except exception.ReferenceInvalidFormat:
|
||||
raise exception.InvalidValue(_("The format of image name '%s' "
|
||||
"is invalid.")
|
||||
% new_container.image)
|
||||
except Exception as e:
|
||||
new_container.status = consts.ERROR
|
||||
new_container.status_reason = str(e)
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
from zun.common.docker_image import digest
|
||||
from zun.common.docker_image import reference
|
||||
from zun.common import exception
|
||||
from zun.tests import base
|
||||
|
||||
|
||||
|
@ -59,25 +60,25 @@ class TestReference(base.BaseTestCase):
|
|||
create_test_case(input_='test:5000/repo',
|
||||
repository='test:5000/repo',
|
||||
hostname='test:5000'),
|
||||
create_test_case(input_='', err=reference.NameEmpty),
|
||||
create_test_case(input_='', err=exception.NameEmpty),
|
||||
create_test_case(input_=':justtag',
|
||||
err=reference.ReferenceInvalidFormat),
|
||||
err=exception.ReferenceInvalidFormat),
|
||||
create_test_case(input_='@sha256:{}'.format('f' * 64),
|
||||
err=reference.ReferenceInvalidFormat),
|
||||
err=exception.ReferenceInvalidFormat),
|
||||
create_test_case(input_='repo@sha256:{}'.format('f' * 34),
|
||||
err=digest.DigestInvalidLength),
|
||||
create_test_case(input_='validname@invaliddigest:{}'.
|
||||
format('f' * 64),
|
||||
err=digest.DigestUnsupported),
|
||||
create_test_case(input_='{}a:tag'.format('a/' * 128),
|
||||
err=reference.NameTooLong),
|
||||
err=exception.NameTooLong),
|
||||
create_test_case(input_='{}a:tag-puts-this-over-max'.
|
||||
format('a/' * 127),
|
||||
repository='{}a'.format('a/' * 127),
|
||||
hostname='',
|
||||
tag='tag-puts-this-over-max'),
|
||||
create_test_case(input_='aa/asdf$$^/aa',
|
||||
err=reference.ReferenceInvalidFormat),
|
||||
err=exception.ReferenceInvalidFormat),
|
||||
create_test_case(input_='sub-dom1.foo.com/bar/baz/quux',
|
||||
repository='sub-dom1.foo.com/bar/baz/quux',
|
||||
hostname='sub-dom1.foo.com'),
|
||||
|
|
Loading…
Reference in New Issue