summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schwede <cschwede@redhat.com>2017-06-13 10:51:33 +0200
committerChristian Schwede <cschwede@redhat.com>2017-09-19 07:38:30 +0000
commitba6abe9b20923390383050c025c56f86088bc416 (patch)
tree8492fb014637584c80864779cd3d07611db1db1b
parent9a60bc1307f47682d45af2b96936eb7dcd77aa98 (diff)
downloadpython-swiftclient-ba6abe9b20923390383050c025c56f86088bc416.tar.gz
Add support for versionless endpoints
Newer deployments are using versionless Keystone endpoints, and most OpenStack clients already support this. This patch enables this for Swift: if an auth_url without any path component is found, it assumes a versionless endpoint will be used. In this case the v3 suffix will be appended to the path if none auth_version is set, and v2.0 is appended if auth_version requires v2. Closes-Bug: 1554885 Related-Bug: 1691106 Change-Id: If7ecb67776cb77828f93ad8278cc5040015216b7 (cherry picked from commit 2ff3102cf779a507af8fa39296154a17a60f540a)
-rw-r--r--swiftclient/client.py17
-rw-r--r--tests/unit/test_swiftclient.py20
2 files changed, 35 insertions, 2 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index 80bc4a3..7db75f0 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -25,7 +25,7 @@ from distutils.version import StrictVersion
from requests.exceptions import RequestException, SSLError
from six.moves import http_client
from six.moves.urllib.parse import quote as _quote, unquote
-from six.moves.urllib.parse import urlparse, urlunparse
+from six.moves.urllib.parse import urljoin, urlparse, urlunparse
from time import sleep, time
import six
@@ -550,9 +550,22 @@ def get_auth_keystone(auth_url, user, key, os_options, **kwargs):
insecure = kwargs.get('insecure', False)
timeout = kwargs.get('timeout', None)
- auth_version = kwargs.get('auth_version', '2.0')
+ auth_version = kwargs.get('auth_version', None)
debug = logger.isEnabledFor(logging.DEBUG)
+ # Add the version suffix in case of versionless Keystone endpoints. If
+ # auth_version is also unset it is likely that it is v3
+ if len(urlparse(auth_url).path) <= 1:
+ if auth_version and auth_version in AUTH_VERSIONS_V2:
+ auth_url = urljoin(auth_url, "v2.0")
+ else:
+ auth_url = urljoin(auth_url, "v3")
+ auth_version = '3'
+ logger.debug("Versionless auth_url - using %s as endpoint" % auth_url)
+
+ # Legacy default if not set
+ if auth_version is None:
+ auth_version = 'v2.0'
ksclient, exceptions = _import_keystone_client(auth_version)
try:
diff --git a/tests/unit/test_swiftclient.py b/tests/unit/test_swiftclient.py
index 5384ac7..3de5f02 100644
--- a/tests/unit/test_swiftclient.py
+++ b/tests/unit/test_swiftclient.py
@@ -575,6 +575,26 @@ class TestGetAuth(MockHttpTest):
self.assertTrue(url.startswith("http"))
self.assertTrue(token)
+ def test_get_auth_keystone_versionless(self):
+ fake_ks = FakeKeystone(endpoint='http://some_url', token='secret')
+
+ with mock.patch('swiftclient.client._import_keystone_client',
+ _make_fake_import_keystone_client(fake_ks)):
+ c.get_auth_keystone('http://authurl', 'user', 'key', {})
+ self.assertEqual(1, len(fake_ks.calls))
+ self.assertEqual('http://authurl/v3', fake_ks.calls[0].get('auth_url'))
+
+ def test_get_auth_keystone_versionless_auth_version_set(self):
+ fake_ks = FakeKeystone(endpoint='http://some_url', token='secret')
+
+ with mock.patch('swiftclient.client._import_keystone_client',
+ _make_fake_import_keystone_client(fake_ks)):
+ c.get_auth_keystone('http://auth_url', 'user', 'key',
+ {}, auth_version='2.0')
+ self.assertEqual(1, len(fake_ks.calls))
+ self.assertEqual('http://auth_url/v2.0',
+ fake_ks.calls[0].get('auth_url'))
+
def test_auth_with_session(self):
mock_session = mock.MagicMock()
mock_session.get_endpoint.return_value = 'http://storagehost/v1/acct'