From b95a89e3ff65f175b0b4b422ffa968d676adacbf Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 27 Jul 2020 08:25:41 -0500 Subject: [PATCH] Fix get_endpoint_data for non-keystone plugins We expect endpoint_override, but these plugins won't necessary have it, they have endpoint instead. Co-Authored-By: Dmitry Tantsur Change-Id: Iead4b95c1f5b8d84cec705da32f41049e2eea641 --- keystoneauth1/http_basic.py | 14 +------ keystoneauth1/noauth.py | 15 +------- keystoneauth1/plugin.py | 45 ++++++++++++++++++++++ keystoneauth1/tests/unit/test_discovery.py | 31 +++++++++++++++ 4 files changed, 79 insertions(+), 26 deletions(-) diff --git a/keystoneauth1/http_basic.py b/keystoneauth1/http_basic.py index 9f164373..63f97cbf 100644 --- a/keystoneauth1/http_basic.py +++ b/keystoneauth1/http_basic.py @@ -17,7 +17,7 @@ from keystoneauth1 import plugin AUTH_HEADER_NAME = 'Authorization' -class HTTPBasicAuth(plugin.BaseAuthPlugin): +class HTTPBasicAuth(plugin.FixedEndpointPlugin): """A provider that will always use HTTP Basic authentication. This is useful to unify session/adapter loading for services @@ -25,8 +25,7 @@ class HTTPBasicAuth(plugin.BaseAuthPlugin): """ def __init__(self, endpoint=None, username=None, password=None): - super(HTTPBasicAuth, self).__init__() - self.endpoint = endpoint + super(HTTPBasicAuth, self).__init__(endpoint) self.username = username self.password = password @@ -44,12 +43,3 @@ class HTTPBasicAuth(plugin.BaseAuthPlugin): return None auth = 'Basic %s' % token return {AUTH_HEADER_NAME: auth} - - def get_endpoint(self, session, **kwargs): - """Return the supplied endpoint. - - Using this plugin the same endpoint is returned regardless of the - parameters passed to the plugin. endpoint_override overrides the - endpoint specified when constructing the plugin. - """ - return kwargs.get('endpoint_override') or self.endpoint diff --git a/keystoneauth1/noauth.py b/keystoneauth1/noauth.py index 5d3b2cd9..c9ff51b1 100644 --- a/keystoneauth1/noauth.py +++ b/keystoneauth1/noauth.py @@ -13,25 +13,12 @@ from keystoneauth1 import plugin -class NoAuth(plugin.BaseAuthPlugin): +class NoAuth(plugin.FixedEndpointPlugin): """A provider that will always use no auth. This is useful to unify session/adapter loading for services that might be deployed in standalone/noauth mode. """ - def __init__(self, endpoint=None): - super(NoAuth, self).__init__() - self.endpoint = endpoint - def get_token(self, session, **kwargs): return 'notused' - - def get_endpoint(self, session, **kwargs): - """Return the supplied endpoint. - - Using this plugin the same endpoint is returned regardless of the - parameters passed to the plugin. endpoint_override overrides the - endpoint specified when constructing the plugin. - """ - return kwargs.get('endpoint_override') or self.endpoint diff --git a/keystoneauth1/plugin.py b/keystoneauth1/plugin.py index f27a9e1a..31719918 100644 --- a/keystoneauth1/plugin.py +++ b/keystoneauth1/plugin.py @@ -313,3 +313,48 @@ class BaseAuthPlugin(object): feature. """ raise NotImplementedError() + + +class FixedEndpointPlugin(BaseAuthPlugin): + """A base class for plugins that have one fixed endpoint.""" + + def __init__(self, endpoint=None): + super(FixedEndpointPlugin, self).__init__() + self.endpoint = endpoint + + def get_endpoint(self, session, **kwargs): + """Return the supplied endpoint. + + Using this plugin the same endpoint is returned regardless of the + parameters passed to the plugin. endpoint_override overrides the + endpoint specified when constructing the plugin. + """ + return kwargs.get('endpoint_override') or self.endpoint + + def get_endpoint_data(self, session, + endpoint_override=None, + discover_versions=True, + **kwargs): + """Return a valid endpoint data for a the service. + + :param session: A session object that can be used for communication. + :type session: keystoneauth1.session.Session + :param str endpoint_override: URL to use for version discovery. + :param bool discover_versions: Whether to get version metadata from + the version discovery document even + if it major api version info can be + inferred from the url. + (optional, defaults to True) + :param kwargs: Ignored. + + :raises keystoneauth1.exceptions.http.HttpError: An error from an + invalid HTTP response. + + :return: Valid EndpointData or None if not available. + :rtype: `keystoneauth1.discover.EndpointData` or None + """ + return super(FixedEndpointPlugin, self).get_endpoint_data( + session, + endpoint_override=endpoint_override or self.endpoint, + discover_versions=discover_versions, + **kwargs) diff --git a/keystoneauth1/tests/unit/test_discovery.py b/keystoneauth1/tests/unit/test_discovery.py index 9fe6cd94..c8468923 100644 --- a/keystoneauth1/tests/unit/test_discovery.py +++ b/keystoneauth1/tests/unit/test_discovery.py @@ -20,6 +20,7 @@ from keystoneauth1 import adapter from keystoneauth1 import discover from keystoneauth1 import exceptions from keystoneauth1 import fixture +from keystoneauth1 import http_basic from keystoneauth1 import noauth from keystoneauth1 import session from keystoneauth1.tests.unit import utils @@ -778,6 +779,36 @@ class VersionDataTests(utils.TestCase): plugin.get_endpoint(self.session, endpoint_override=V3_URL), V3_URL) + def test_endpoint_data_http_basic_no_discover(self): + plugin = http_basic.HTTPBasicAuth(endpoint=V3_URL) + data = plugin.get_endpoint_data( + self.session, discover_versions=False) + + self.assertEqual(data.api_version, (3, 0)) + self.assertEqual(data.url, V3_URL) + self.assertEqual( + plugin.get_api_major_version( + self.session, endpoint_override=V3_URL), + (3, 0)) + self.assertEqual( + plugin.get_endpoint(self.session, endpoint_override=V3_URL), + V3_URL) + + def test_endpoint_data_http_basic_override_no_discover(self): + plugin = http_basic.HTTPBasicAuth() + data = plugin.get_endpoint_data( + self.session, endpoint_override=V3_URL, discover_versions=False) + + self.assertEqual(data.api_version, (3, 0)) + self.assertEqual(data.url, V3_URL) + self.assertEqual( + plugin.get_api_major_version( + self.session, endpoint_override=V3_URL), + (3, 0)) + self.assertEqual( + plugin.get_endpoint(self.session, endpoint_override=V3_URL), + V3_URL) + def test_endpoint_data_noauth_adapter(self): mock = self.requests_mock.get( V3_URL, status_code=200, json=V3_VERSION_ENTRY)