Fix version discovery for clouds with int project_ids

On a cloud that has inaccessible version discovery documents AND uses
integer project ids, the discovery fallback logic can fail because the
project id parses as a (very large) version.

Check to see that the url segment in the fallback code begins with a v,
so that we're only attempting to parse versions from actual candidate
segments.

Closes-Bug: #1806109
Change-Id: Id90b3b9e4852494a4678b0a9bb67362babdc971c
This commit is contained in:
Monty Taylor 2018-11-30 12:39:58 -06:00
parent d8cee933fe
commit 4960c48aec
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
2 changed files with 11 additions and 0 deletions

View File

@ -473,6 +473,12 @@ def _version_from_url(url):
url = urllib.parse.urlparse(url)
for part in reversed(url.path.split('/')):
try:
# All integer project ids can parse as valid versions. In URLs
# all known instances of versions start with a v. So check to make
# sure the url part starts with 'v', then check that it parses
# as a valid version.
if part[0] != 'v':
continue
return normalize_version_number(part)
except Exception:
pass

View File

@ -1257,3 +1257,8 @@ class EndpointDataTests(utils.TestCase):
self.assertEqual(exp, str(epd))
# Works with implicit stringification
self.assertEqual(exp, "%s" % epd)
def test_project_id_int_fallback(self):
bad_url = "https://compute.example.com/v2/123456"
epd = discover.EndpointData(catalog_url=bad_url)
self.assertEqual((2, 0), epd.api_version)