84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
# -*- encoding: utf-8 -*-
|
|
#
|
|
# Copyright © 2012 New Dream Network, LLC (DreamHost)
|
|
#
|
|
# Author: Doug Hellmann <doug.hellmann@dreamhost.com>
|
|
#
|
|
# 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 oslo.config import cfg
|
|
from pecan import hooks
|
|
|
|
from ironic.conductor import rpcapi
|
|
from ironic.db import api as dbapi
|
|
from ironic.openstack.common import context
|
|
from ironic.openstack.common import policy
|
|
|
|
|
|
class ConfigHook(hooks.PecanHook):
|
|
"""Attach the configuration object to the request
|
|
so controllers can get to it.
|
|
"""
|
|
|
|
def before(self, state):
|
|
state.request.cfg = cfg.CONF
|
|
|
|
|
|
class DBHook(hooks.PecanHook):
|
|
|
|
def before(self, state):
|
|
state.request.dbapi = dbapi.get_instance()
|
|
|
|
|
|
class ContextHook(hooks.PecanHook):
|
|
"""Configures a request context and attaches it to the request.
|
|
|
|
The following HTTP request headers are used:
|
|
|
|
X-User-Id or X-User:
|
|
Used for context.user_id.
|
|
|
|
X-Tenant-Id or X-Tenant:
|
|
Used for context.tenant.
|
|
|
|
X-Auth-Token:
|
|
Used for context.auth_token.
|
|
|
|
X-Roles:
|
|
Used for setting context.is_admin flag to either True or False.
|
|
The flag is set to True, if X-Roles contains either an administrator
|
|
or admin substring. Otherwise it is set to False.
|
|
|
|
"""
|
|
def before(self, state):
|
|
user_id = state.request.headers.get('X-User-Id')
|
|
user_id = state.request.headers.get('X-User', user_id)
|
|
tenant = state.request.headers.get('X-Tenant-Id')
|
|
tenant = state.request.headers.get('X-Tenant', tenant)
|
|
auth_token = state.request.headers.get('X-Auth-Token')
|
|
creds = {'roles': state.request.headers.get('X-Roles').split(',')}
|
|
is_admin = policy.check('is_admin', state.request.headers, creds)
|
|
|
|
state.request.context = context.RequestContext(
|
|
auth_token=auth_token,
|
|
user=user_id,
|
|
tenant=tenant,
|
|
request_id=context.generate_request_id(),
|
|
is_admin=is_admin)
|
|
|
|
|
|
class RPCHook(hooks.PecanHook):
|
|
|
|
def before(self, state):
|
|
state.request.rpcapi = rpcapi.ConductorAPI()
|