Cleaned-up request.context code

Change-Id: I47011d30461e0d42836a882ac07896404a6cb058
This commit is contained in:
Serg Melikyan
2013-05-21 17:16:10 +04:00
parent e6da2a43a5
commit f94b3ef683
2 changed files with 11 additions and 69 deletions

View File

@@ -10,34 +10,19 @@
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License.from oslo.config import cfg # under the License.
import json
from oslo.config import cfg from oslo.config import cfg
import webob.exc
from muranoapi.openstack.common import wsgi
import muranoapi.context import muranoapi.context
import muranoapi.openstack.common.wsgi as wsgi
import muranoapi.openstack.common.log as logging import muranoapi.openstack.common.log as logging
CONF = cfg.CONF CONF = cfg.CONF
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class BaseContextMiddleware(wsgi.Middleware): class ContextMiddleware(wsgi.Middleware):
def process_response(self, resp):
try:
request_id = resp.request.context.request_id
except AttributeError:
LOG.warn(_('Unable to retrieve request id from context'))
else:
resp.headers['x-openstack-request-id'] = 'req-%s' % request_id
return resp
class ContextMiddleware(BaseContextMiddleware):
def process_request(self, req): def process_request(self, req):
"""Convert authentication information into a request context """Convert authentication information into a request context
@@ -46,38 +31,14 @@ class ContextMiddleware(BaseContextMiddleware):
of the req object. of the req object.
:param req: wsgi request object that will be given the context object :param req: wsgi request object that will be given the context object
:raises webob.exc.HTTPUnauthorized: when value of the X-Identity-Status
header is not 'Confirmed' and
anonymous access is disallowed
""" """
if req.headers.get('X-Identity-Status') == 'Confirmed': kwargs = {
roles_header = req.headers.get('X-Roles', '') 'user': req.headers.get('X-User-Id'),
roles = [r.strip().lower() for r in roles_header.split(',')] 'tenant': req.headers.get('X-Tenant-Id'),
'auth_token': req.headers.get('X-Auth-Token'),
#NOTE(bcwaldon): This header is deprecated in favor of X-Auth-Token 'session': req.headers.get('X-Configuration-Session')
deprecated_token = req.headers.get('X-Storage-Token') }
req.context = muranoapi.context.RequestContext(**kwargs)
service_catalog = None
if req.headers.get('X-Service-Catalog') is not None:
try:
catalog_header = req.headers.get('X-Service-Catalog')
service_catalog = json.loads(catalog_header)
except ValueError:
raise webob.exc.HTTPInternalServerError(
_('Invalid service catalog json.'))
kwargs = {
'user': req.headers.get('X-User-Id'),
'tenant': req.headers.get('X-Tenant-Id'),
'roles': roles,
'auth_token': req.headers.get('X-Auth-Token',
deprecated_token),
'service_catalog': service_catalog,
'session': req.headers.get('X-Configuration-Session')
}
req.context = muranoapi.context.RequestContext(**kwargs)
else:
raise webob.exc.HTTPUnauthorized()
@classmethod @classmethod
def factory(cls, global_conf, **local_conf): def factory(cls, global_conf, **local_conf):

View File

@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from muranoapi.openstack.common import uuidutils
class RequestContext(object): class RequestContext(object):
""" """
@@ -21,33 +19,16 @@ class RequestContext(object):
accesses the system, as well as additional request information. accesses the system, as well as additional request information.
""" """
def __init__(self, auth_token=None, user=None, tenant=None, def __init__(self, auth_token=None, user=None, tenant=None, session=None):
roles=None, service_catalog=None, session=None):
self.auth_token = auth_token self.auth_token = auth_token
self.user = user self.user = user
self.tenant = tenant self.tenant = tenant
self.roles = roles or []
self.request_id = uuidutils.generate_uuid()
self.service_catalog = service_catalog
self.session = session self.session = session
def to_dict(self): def to_dict(self):
# NOTE(ameade): These keys are named to correspond with the default
# format string for logging the context in openstack common
return { return {
'request_id': self.request_id,
#NOTE(bcwaldon): openstack-common logging expects 'user'
'user': self.user, 'user': self.user,
'user_id': self.user,
#NOTE(bcwaldon): openstack-common logging expects 'tenant'
'tenant': self.tenant, 'tenant': self.tenant,
'tenant_id': self.tenant,
'project_id': self.tenant,
'roles': self.roles,
'auth_token': self.auth_token, 'auth_token': self.auth_token,
'session': self.session 'session': self.session
} }