Catch InvalidURL when requesting store size

If location URL looks like scp address:
http://some_address:~/some_image then glance accepts this address
and creates a malformed image.
The problem here is because python http_client lib is trying to
recognize '~' as port but it is not acceptable port name.
The lib generates an InvalidURL exception but this exception is
hidden in one of universal catchers(except Exception).
We need to validate port name (is present before passing it to
http_client lib.

Change-Id: I36dddf7059711fb15ecb1fec4e438daf2bffcb60
Closes-bug: #1485792
This commit is contained in:
Kairat Kushaev 2015-08-18 01:34:58 +03:00
parent 4253600356
commit caf646af81
2 changed files with 30 additions and 0 deletions

View File

@ -90,6 +90,15 @@ class StoreLocation(glance_store.location.StoreLocation):
if netloc == '':
LOG.info(_("No address specified in HTTP URL"))
raise exceptions.BadStoreUri(uri=uri)
else:
# IPv6 address has the following format [1223:0:0:..]:<some_port>
# we need to be sure that we are validating port in both IPv4,IPv6
delimiter = "]:" if netloc.count(":") > 1 else ":"
host, dlm, port = netloc.partition(delimiter)
# if port is present in location then validate port format
if port and not port.isdigit():
raise exceptions.BadStoreUri(uri=uri)
self.netloc = netloc
self.path = path

View File

@ -152,6 +152,27 @@ class TestHttpStore(base.StoreBaseTest,
loc = location.get_location_from_uri(uri, conf=self.conf)
self.assertRaises(exceptions.NotFound, self.store.get_size, loc)
def test_http_store_location_initialization(self):
"""Test store location initialization from valid uris"""
uris = [
"http://127.0.0.1:8000/ubuntu.iso",
"http://openstack.com:80/ubuntu.iso",
"http://[1080::8:800:200C:417A]:80/ubuntu.iso"
]
for uri in uris:
location.get_location_from_uri(uri)
def test_http_store_location_initialization_with_invalid_url(self):
"""Test store location initialization from incorrect uris."""
incorrect_uris = [
"http://127.0.0.1:~/ubuntu.iso",
"http://openstack.com:some_text/ubuntu.iso",
"http://[1080::8:800:200C:417A]:some_text/ubuntu.iso"
]
for uri in incorrect_uris:
self.assertRaises(exceptions.BadStoreUri,
location.get_location_from_uri, uri)
def test_http_get_raises_remote_service_unavailable(self):
"""Test http store raises RemoteServiceUnavailable."""
uri = "http://netloc/path/to/file.tar.gz"