From 4960c48aec24e7d1b0f436a704d63b9ec9b6b805 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Fri, 30 Nov 2018 12:39:58 -0600 Subject: [PATCH] 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 --- keystoneauth1/discover.py | 6 ++++++ keystoneauth1/tests/unit/test_discovery.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/keystoneauth1/discover.py b/keystoneauth1/discover.py index eb7ebf94..97321a14 100644 --- a/keystoneauth1/discover.py +++ b/keystoneauth1/discover.py @@ -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 diff --git a/keystoneauth1/tests/unit/test_discovery.py b/keystoneauth1/tests/unit/test_discovery.py index 024efc62..9972eb3d 100644 --- a/keystoneauth1/tests/unit/test_discovery.py +++ b/keystoneauth1/tests/unit/test_discovery.py @@ -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)