summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Tantsur <divius.inside@gmail.com>2019-05-16 16:52:36 +0200
committerDmitry Tantsur <divius.inside@gmail.com>2019-05-17 09:39:43 +0200
commit266a700534cbd166c800999dd92556cb431d2055 (patch)
tree7dfd31f286a5e5ccfae62e8528368c2240960711
parent7217d7e8dc780002a7053d6c678dbe39665581fd (diff)
downloadpython-ironicclient-266a700534cbd166c800999dd92556cb431d2055.tar.gz
Do not try to use /v1/v1 when endpoint_override is used2.7.2
In one of the places where endpoint_override is used we did not strip the /v1 suffix, resulting in doube /v1/v1 sometimes. This patch also corrects the way we strip this suffix (str.rstrip accepts a set of symbols, not a substring; use regex instead). It remains unclear why the breakage passed the gate initially. I assume it was not on the active code path until some Nova change. Story: #2005723 Task: #31051 Change-Id: I3b25f4fb170aa93159ffa8074dc74fa6f50671b7 (cherry picked from commit 4565af8ccd8aaa35868fae6ae31216c3e57014f3)
-rw-r--r--ironicclient/common/http.py16
-rw-r--r--ironicclient/tests/unit/test_client.py6
-rw-r--r--releasenotes/notes/endpoint-strip-dea59ccb05628a35.yaml5
3 files changed, 18 insertions, 9 deletions
diff --git a/ironicclient/common/http.py b/ironicclient/common/http.py
index 96ec5a4..b78c5a6 100644
--- a/ironicclient/common/http.py
+++ b/ironicclient/common/http.py
@@ -18,6 +18,7 @@ from distutils.version import StrictVersion
import hashlib
import logging
import os
+import re
import socket
import ssl
import textwrap
@@ -50,7 +51,8 @@ LOG = logging.getLogger(__name__)
USER_AGENT = 'python-ironicclient'
CHUNKSIZE = 1024 * 64 # 64kB
-API_VERSION = '/v1'
+_MAJOR_VERSION = 1
+API_VERSION = '/v%d' % _MAJOR_VERSION
API_VERSION_SELECTED_STATES = ('user', 'negotiated', 'cached', 'default')
@@ -61,10 +63,12 @@ SENSITIVE_HEADERS = ('X-Auth-Token',)
SUPPORTED_ENDPOINT_SCHEME = ('http', 'https')
+_API_VERSION_RE = re.compile(r'/+(v%d)?/*$' % _MAJOR_VERSION)
+
def _trim_endpoint_api_version(url):
"""Trim API version and trailing slash from endpoint."""
- return url.rstrip('/').rstrip(API_VERSION).rstrip('/')
+ return re.sub(_API_VERSION_RE, '', url)
def _extract_error_json(body):
@@ -604,6 +608,9 @@ class SessionClient(VersionNegotiationMixin, adapter.LegacyJsonAdapter):
'removed in Stein. Please use "endpoint_override" '
'instead.')
self.endpoint = endpoint
+ if isinstance(kwargs.get('endpoint_override'), six.string_types):
+ kwargs['endpoint_override'] = _trim_endpoint_api_version(
+ kwargs['endpoint_override'])
super(SessionClient, self).__init__(**kwargs)
@@ -634,10 +641,7 @@ class SessionClient(VersionNegotiationMixin, adapter.LegacyJsonAdapter):
kwargs.setdefault('user_agent', USER_AGENT)
kwargs.setdefault('auth', self.auth)
if isinstance(self.endpoint_override, six.string_types):
- kwargs.setdefault(
- 'endpoint_override',
- _trim_endpoint_api_version(self.endpoint_override)
- )
+ kwargs.setdefault('endpoint_override', self.endpoint_override)
if getattr(self, 'os_ironic_api_version', None):
kwargs['headers'].setdefault('X-OpenStack-Ironic-API-Version',
diff --git a/ironicclient/tests/unit/test_client.py b/ironicclient/tests/unit/test_client.py
index 20f68a2..5238fb6 100644
--- a/ironicclient/tests/unit/test_client.py
+++ b/ironicclient/tests/unit/test_client.py
@@ -93,14 +93,14 @@ class ClientTest(utils.BaseTestCase):
client = self._test_get_client(auth='none',
warn_mock_call_count=1, **kwargs)
self.assertIsInstance(client.http_client, http.SessionClient)
- self.assertEqual('http://localhost:6385/v1',
+ self.assertEqual('http://localhost:6385',
client.http_client.endpoint_override)
def test_get_client_only_endpoint(self):
kwargs = {'endpoint': 'http://localhost:6385/v1'}
client = self._test_get_client(auth='none', **kwargs)
self.assertIsInstance(client.http_client, http.SessionClient)
- self.assertEqual('http://localhost:6385/v1',
+ self.assertEqual('http://localhost:6385',
client.http_client.endpoint_override)
def test_get_client_with_auth_token_ironic_url(self):
@@ -113,7 +113,7 @@ class ClientTest(utils.BaseTestCase):
warn_mock_call_count=2, **kwargs)
self.assertIsInstance(client.http_client, http.SessionClient)
- self.assertEqual('http://localhost:6385/v1',
+ self.assertEqual('http://localhost:6385',
client.http_client.endpoint_override)
def test_get_client_no_auth_token(self):
diff --git a/releasenotes/notes/endpoint-strip-dea59ccb05628a35.yaml b/releasenotes/notes/endpoint-strip-dea59ccb05628a35.yaml
new file mode 100644
index 0000000..6400da8
--- /dev/null
+++ b/releasenotes/notes/endpoint-strip-dea59ccb05628a35.yaml
@@ -0,0 +1,5 @@
+---
+fixes:
+ - |
+ Prevent trying to access endpoints with ``/v1/v1`` when using endpoint
+ overrides containing ``/v1``.