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 <dtantsur@protonmail.com>
Change-Id: Iead4b95c1f5b8d84cec705da32f41049e2eea641
This commit is contained in:
Monty Taylor 2020-07-27 08:25:41 -05:00 committed by Dmitry Tantsur
parent dab8e1057a
commit b95a89e3ff
4 changed files with 79 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)