f240d55ac5
This change implements the sahara.service.sessions module with the basic session cache object. It also adds the authentication plugin from the keystonemiddleware into the context for rest transactions. It also migrates the keystone client utility functions to use the new session methodology. The trust module has been accordingly fixed to make greater use of authentication objects instead of client objects. * adding auth plugin to context * adding sessions module * adding test for sessions * adding keystonemiddleware base auth plugin object to context on api call * adding keystone session to sessions module * refactoring keystone client to use sessions * adding keystone methods to retrieve auth plugins, tokens, and service catalog * changing sahara.service.trusts to use new keystone methods * fixing trust tests to fit new authentication methodologies Change-Id: I65ed4b4dcee8752bf4e66ef9e47305ff408d8d5d Partial-Implements: bp keystone-sessions
107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
# Copyright (c) 2015 Red Hat, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from keystoneclient import session as keystone
|
|
from oslo_config import cfg
|
|
from oslo_log import log as logging
|
|
|
|
from sahara import exceptions as ex
|
|
from sahara.i18n import _
|
|
from sahara.i18n import _LE
|
|
|
|
|
|
CONF = cfg.CONF
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
_SESSION_CACHE = None
|
|
|
|
SESSION_TYPE_GENERIC = 'generic'
|
|
SESSION_TYPE_KEYSTONE = 'keystone'
|
|
|
|
|
|
def cache():
|
|
global _SESSION_CACHE
|
|
if not _SESSION_CACHE:
|
|
_SESSION_CACHE = SessionCache()
|
|
return _SESSION_CACHE
|
|
|
|
|
|
class SessionCache(object):
|
|
'''A cache of keystone Session objects
|
|
|
|
When a requested Session is not currently cached, it will be
|
|
acquired from specific information in this module. Sessions should
|
|
be referenced by their OpenStack project name and not the service
|
|
name, this is to allow for multiple service implementations
|
|
while retaining the ability to generate Session objects. In all
|
|
cases, the constant values in this module should be used to
|
|
communicate the session type.
|
|
|
|
'''
|
|
def __init__(self):
|
|
'''create a new SessionCache'''
|
|
self._sessions = {}
|
|
self._session_funcs = {
|
|
SESSION_TYPE_GENERIC: self.get_generic_session,
|
|
SESSION_TYPE_KEYSTONE: self.get_keystone_session,
|
|
}
|
|
|
|
def _set_session(self, session_type, session):
|
|
'''Set the session for a given type.
|
|
|
|
:param session_type: the type of session to set.
|
|
|
|
:param session: the session to associate with the type
|
|
'''
|
|
self._sessions[session_type] = session
|
|
|
|
def get_session(self, session_type=SESSION_TYPE_GENERIC):
|
|
'''Return a Session for the requested type
|
|
|
|
:param session_type: the type of Session to get, if None a generic
|
|
session will be returned.
|
|
|
|
:raises SaharaException: if the requested session type is not
|
|
found.
|
|
'''
|
|
session_function = self._session_funcs.get(session_type)
|
|
if session_function:
|
|
return session_function()
|
|
else:
|
|
LOG.error(
|
|
_LE('Requesting an unknown session type (type: {type})').
|
|
format(type=session_type))
|
|
raise ex.SaharaException(
|
|
_('Session type {type} not recognized').
|
|
format(type=session_type))
|
|
|
|
def get_generic_session(self):
|
|
session = self._sessions.get(SESSION_TYPE_GENERIC)
|
|
if not session:
|
|
session = keystone.Session()
|
|
self._set_session(SESSION_TYPE_GENERIC, session)
|
|
return session
|
|
|
|
def get_keystone_session(self):
|
|
session = self._sessions.get(SESSION_TYPE_KEYSTONE)
|
|
if not session:
|
|
if CONF.keystone.ca_file:
|
|
session = keystone.Session(cert=CONF.keystone.ca_file,
|
|
verify=CONF.keystone.api_insecure)
|
|
else:
|
|
session = self.get_generic_session()
|
|
self._set_session(SESSION_TYPE_KEYSTONE, session)
|
|
return session
|