Use Sessions URL from root service if it's provided

If the root endpoint provides the link to Sessions, we should just
use it.

Change-Id: I825930623c84d3b6e5994e29f963b4f5af7afe4d
Story: 2008059
Task: 40742
This commit is contained in:
Riccardo Pittau
2020-08-26 16:40:28 +02:00
parent fc5b22a568
commit e730987a18
5 changed files with 49 additions and 9 deletions

View File

@@ -0,0 +1,8 @@
---
fixes:
- |
Instead of trying to GET /redfish/v1/SessionService, which is usually
reachable via authentication, fail, and then guess
/redfish/v1/SessionService/Sessions as Sessions URL, we try first to use
directly the Sessions URL provided by the root service, if available.

View File

@@ -149,10 +149,18 @@ class SessionAuth(AuthBase):
:raises: AccessError
:raises: HTTPError
"""
target_uri = None
try:
target_uri = self._root_resource.get_sessions_path()
except exceptions.MissingAttributeError:
LOG.debug('Missing Sessions attribute under Links in Root '
'Service, we\'ll try to determine it from Session '
'Service')
session_service = self._root_resource.get_session_service()
session_auth_token, session_uri = (
session_service.create_session(self._username,
self._password))
self._password,
target_uri=target_uri))
self._session_key = session_auth_token
self._session_resource_id = session_uri
self._connector.set_http_session_auth(session_auth_token)

View File

@@ -365,6 +365,16 @@ class Sushy(base.ResourceBase):
self._conn, self._session_service_path,
redfish_version=self.redfish_version)
def get_sessions_path(self):
"""Returns the Sessions url"""
try:
links_url = self.json.get('Links')
return links_url['Sessions']['@odata.id']
except (TypeError, KeyError):
raise exceptions.MissingAttributeError(
attribute='Links/Sessions/@data.id', resource=self.path)
def get_session(self, identity):
"""Given the identity return a Session object

View File

@@ -91,20 +91,26 @@ class SessionService(base.ResourceBase):
"""
self._conn.delete(session_uri)
def create_session(self, username, password):
def create_session(self, username, password, target_uri=None):
"""This function will try to create a session.
:param username: the username of the user requesting a new session
:param password: the password associated to the user requesting
a new session
:param target_uri: the "Sessions" uri, usually in the form:
'/redfish/v1/SessionService/Sessions'
:returns: A session key and uri in the form of a tuple
:raises: MissingXAuthToken
:raises: ConnectionError
:raises: AccessError
:raises: HTTPError
"""
try:
target_uri = self._get_sessions_collection_path()
except Exception:
# Defaulting to /Sessions
target_uri = self.path + '/Sessions'
if not target_uri:
try:
target_uri = self._get_sessions_collection_path()
except Exception:
# Defaulting to /Sessions
target_uri = self.path + '/Sessions'
data = {'UserName': username, 'Password': password}
LOG.debug("Requesting new session from %s.", target_uri)

View File

@@ -390,6 +390,10 @@ class MainTestCase(base.TestCase):
registries[1]
self.assertEqual(1, mock_registries.__getitem__.call_count)
def test_get_sessions_path(self):
expected = '/redfish/v1/SessionService/Sessions'
self.assertEqual(expected, self.root.get_sessions_path())
class BareMinimumMainTestCase(base.TestCase):
@@ -432,11 +436,15 @@ class BareMinimumMainTestCase(base.TestCase):
exceptions.MissingAttributeError,
'UpdateService/@odata.id', self.root.get_update_service)
def test_get_composition_service_when_compositionservice_attr_absent(
self):
def test_get_composition_service_when_compositionservice_attr_absent(self):
self.assertRaisesRegex(
exceptions.MissingAttributeError,
'CompositionService/@odata.id', self.root.get_composition_service)
def test__get_registry_collection_when_registries_attr_absent(self):
self.assertIsNone(self.root._get_registry_collection())
def test_get_sessions_path_fail(self):
self.assertRaisesRegex(
exceptions.MissingAttributeError,
'Links/Sessions/@data.id', self.root.get_sessions_path)