Fix unreachable 'ImageSizeLimitExceeded' exception in image-upload

ImageSizeLimitExceeded exception block [1] is unreachable in upload because
it is caught at [2] and raised StorageQuotaFull exception from there. The
problem here is that we have nested usage of the limiting reader.

To make it correct changed the limiting reader to accept
exception class as parameter so that we can pass the StorageQuotaFull
in case LimitingReader is used for quota check and ImageSizeExceeded exception
if it is used for image size cap check.

[1] fd16fa4f25/glance/api/v2/image_data.py (L230)
[2] fd16fa4f25/glance/quota/__init__.py (L305)

Closes-Bug: #1734832
Change-Id: I5a419b763bee7f983c2a94c6f3a2245281e86743
This commit is contained in:
Abhishek Kekane 2017-11-28 09:51:02 +00:00
parent fd16fa4f25
commit ffc3923e93
2 changed files with 10 additions and 9 deletions

View File

@ -271,20 +271,23 @@ class LimitingReader(object):
Reader designed to fail when reading image data past the configured
allowable amount.
"""
def __init__(self, data, limit):
def __init__(self, data, limit,
exception_class=exception.ImageSizeLimitExceeded):
"""
:param data: Underlying image data object
:param limit: maximum number of bytes the reader should allow
:param exception_class: Type of exception to be raised
"""
self.data = data
self.limit = limit
self.bytes_read = 0
self.exception_class = exception_class
def __iter__(self):
for chunk in self.data:
self.bytes_read += len(chunk)
if self.bytes_read > self.limit:
raise exception.ImageSizeLimitExceeded()
raise self.exception_class()
else:
yield chunk
@ -292,7 +295,7 @@ class LimitingReader(object):
result = self.data.read(i)
self.bytes_read += len(result)
if self.bytes_read > self.limit:
raise exception.ImageSizeLimitExceeded()
raise self.exception_class()
return result

View File

@ -299,12 +299,10 @@ class ImageProxy(glance.domain.proxy.Image):
if remaining is not None:
# NOTE(jbresnah) we are trying to enforce a quota, put a limit
# reader on the data
data = utils.LimitingReader(data, remaining)
try:
self.image.set_data(data, size=size)
except exception.ImageSizeLimitExceeded:
raise exception.StorageQuotaFull(image_size=size,
remaining=remaining)
data = utils.LimitingReader(
data, remaining, exception_class=exception.StorageQuotaFull)
self.image.set_data(data, size=size)
# NOTE(jbresnah) If two uploads happen at the same time and neither
# properly sets the size attribute[1] then there is a race condition