Merge "Cleaned-up request.context code"
This commit is contained in:
commit
eeb89e3efe
@ -10,34 +10,19 @@
|
||||
# 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 oslo.config import cfg
|
||||
# under the License.
|
||||
|
||||
|
||||
import json
|
||||
from oslo.config import cfg
|
||||
|
||||
import webob.exc
|
||||
|
||||
from muranoapi.openstack.common import wsgi
|
||||
import muranoapi.context
|
||||
import muranoapi.openstack.common.wsgi as wsgi
|
||||
import muranoapi.openstack.common.log as logging
|
||||
|
||||
CONF = cfg.CONF
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BaseContextMiddleware(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):
|
||||
class ContextMiddleware(wsgi.Middleware):
|
||||
def process_request(self, req):
|
||||
"""Convert authentication information into a request context
|
||||
|
||||
@ -46,38 +31,14 @@ class ContextMiddleware(BaseContextMiddleware):
|
||||
of the req 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':
|
||||
roles_header = req.headers.get('X-Roles', '')
|
||||
roles = [r.strip().lower() for r in roles_header.split(',')]
|
||||
|
||||
#NOTE(bcwaldon): This header is deprecated in favor of X-Auth-Token
|
||||
deprecated_token = req.headers.get('X-Storage-Token')
|
||||
|
||||
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,
|
||||
'auth_token': req.headers.get('X-Auth-Token'),
|
||||
'session': req.headers.get('X-Configuration-Session')
|
||||
}
|
||||
req.context = muranoapi.context.RequestContext(**kwargs)
|
||||
else:
|
||||
raise webob.exc.HTTPUnauthorized()
|
||||
|
||||
@classmethod
|
||||
def factory(cls, global_conf, **local_conf):
|
||||
|
@ -12,8 +12,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from muranoapi.openstack.common import uuidutils
|
||||
|
||||
|
||||
class RequestContext(object):
|
||||
"""
|
||||
@ -21,33 +19,16 @@ class RequestContext(object):
|
||||
accesses the system, as well as additional request information.
|
||||
"""
|
||||
|
||||
def __init__(self, auth_token=None, user=None, tenant=None,
|
||||
roles=None, service_catalog=None, session=None):
|
||||
|
||||
def __init__(self, auth_token=None, user=None, tenant=None, session=None):
|
||||
self.auth_token = auth_token
|
||||
self.user = user
|
||||
self.tenant = tenant
|
||||
self.roles = roles or []
|
||||
self.request_id = uuidutils.generate_uuid()
|
||||
self.service_catalog = service_catalog
|
||||
self.session = session
|
||||
|
||||
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 {
|
||||
'request_id': self.request_id,
|
||||
|
||||
#NOTE(bcwaldon): openstack-common logging expects 'user'
|
||||
'user': self.user,
|
||||
'user_id': self.user,
|
||||
|
||||
#NOTE(bcwaldon): openstack-common logging expects 'tenant'
|
||||
'tenant': self.tenant,
|
||||
'tenant_id': self.tenant,
|
||||
'project_id': self.tenant,
|
||||
|
||||
'roles': self.roles,
|
||||
'auth_token': self.auth_token,
|
||||
'session': self.session
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user