Protect Connector against empty auth object

Auth uses Connector, Connector uses Auth. This convoluted relationship
results in connector._auth being unset when an authentication request is
made. If it results in AccessError, sushy tries to access non-existing
connector._auth. This change protects against it and allows the actual
error to bubble up.

Change-Id: I4891b89a65567c9ba2057557462a5d3840808ae1
Story: #2008661
Task: #41935
(cherry picked from commit bf50d73638)
(cherry picked from commit efd47e3673)
(cherry picked from commit 781159e2f3)
This commit is contained in:
Dmitry Tantsur 2021-06-22 13:49:34 +02:00 committed by Julia Kreger
parent 84c8bb548a
commit 060c74c911
2 changed files with 14 additions and 1 deletions

View File

@ -31,6 +31,7 @@ class Connector(object):
self._verify = verify
self._session = requests.Session()
self._session.verify = self._verify
self._auth = None
# NOTE(etingof): field studies reveal that some BMCs choke at
# long-running persistent HTTP connections (or TCP connections).
@ -106,7 +107,7 @@ class Connector(object):
try:
exceptions.raise_for_response(method, url, response)
except exceptions.AccessError as e:
if self._auth.can_refresh_session():
if self._auth is not None and self._auth.can_refresh_session():
try:
self._auth.refresh_session()
except exceptions.AccessError as refresh_exc:

View File

@ -304,6 +304,18 @@ class ConnectorOpTestCase(base.TestCase):
exc = cm.exception
self.assertEqual(http_client.FORBIDDEN, exc.status_code)
def test_access_error_without_auth(self):
self.conn._auth = None
self.request.return_value.status_code = http_client.FORBIDDEN
self.request.return_value.json.side_effect = ValueError('no json')
with self.assertRaisesRegex(exceptions.AccessError,
'unknown error') as cm:
self.conn._op('GET', 'http://foo.bar')
exc = cm.exception
self.assertEqual(http_client.FORBIDDEN, exc.status_code)
@mock.patch.object(connector.LOG, 'debug', autospec=True)
def test_access_error_service_session(self, mock_log):
self.conn._auth.can_refresh_session.return_value = False