|
| 1 | +# Copyright 2012 OpenStack Foundation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +from oslo_config import cfg |
| 16 | +from oslo_log import log as logging |
| 17 | +from oslo_middleware import request_id |
| 18 | +import webob.dec |
| 19 | +import webob.exc |
| 20 | + |
| 21 | +from bigbang.common import context |
| 22 | +from bigbang import wsgi |
| 23 | + |
| 24 | +LOG = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +class BigbangKeystoneContext(wsgi.Middleware): |
| 28 | + """Make a request context from keystone headers.""" |
| 29 | + |
| 30 | + @webob.dec.wsgify |
| 31 | + def __call__(self, req): |
| 32 | + # Determine the user ID |
| 33 | + user_id = req.headers.get('X_USER_ID') |
| 34 | + if not user_id: |
| 35 | + LOG.debug(_("X_USER_ID is not found in request")) |
| 36 | + return webob.exc.HTTPUnauthorized() |
| 37 | + |
| 38 | + # Determine the tenant |
| 39 | + tenant_id = req.headers.get('X_PROJECT_ID') |
| 40 | + |
| 41 | + # Suck out the roles |
| 42 | + roles = [r.strip() for r in req.headers.get('X_ROLES', '').split(',')] |
| 43 | + |
| 44 | + # Human-friendly names |
| 45 | + tenant_name = req.headers.get('X_PROJECT_NAME') |
| 46 | + user_name = req.headers.get('X_USER_NAME') |
| 47 | + |
| 48 | + # Use request_id if already set |
| 49 | + req_id = req.environ.get(request_id.ENV_REQUEST_ID) |
| 50 | + |
| 51 | + # Get the auth token |
| 52 | + auth_token = req.headers.get('X_AUTH_TOKEN', |
| 53 | + req.headers.get('X_STORAGE_TOKEN')) |
| 54 | + |
| 55 | + # Create a context with the authentication data |
| 56 | + ctx = context.Context(user_id, tenant_id, roles=roles, |
| 57 | + user_name=user_name, tenant_name=tenant_name, |
| 58 | + request_id=req_id, auth_token=auth_token) |
| 59 | + |
| 60 | + # Inject the context... |
| 61 | + req.environ['tacker.context'] = ctx |
| 62 | + |
| 63 | + return self.application |
| 64 | + |
| 65 | + |
| 66 | +def pipeline_factory(loader, global_conf, **local_conf): |
| 67 | + """Create a paste pipeline based on the 'auth_strategy' config option.""" |
| 68 | + pipeline = local_conf[cfg.CONF.auth_strategy] |
| 69 | + pipeline = pipeline.split() |
| 70 | + filters = [loader.get_filter(n) for n in pipeline[:-1]] |
| 71 | + app = loader.get_app(pipeline[-1]) |
| 72 | + filters.reverse() |
| 73 | + for f in filters: |
| 74 | + app = f(app) |
| 75 | + return app |
0 commit comments