Ejemplo n.º 1
0
def deploy(conf):
    """Assemble the middleware pipeline leading to the placement app."""
    if conf.api.auth_strategy == 'noauth2':
        auth_middleware = auth.NoAuthMiddleware
    else:
        # Do not use 'oslo_config_project' param here as the conf
        # location may have been overridden earlier in the deployment
        # process with OS_PLACEMENT_CONFIG_DIR in wsgi.py.
        auth_middleware = auth.filter_factory({}, oslo_config_config=conf)

    # Pass in our CORS config, if any, manually as that's a)
    # explicit, b) makes testing more straightfoward, c) let's
    # us control the use of cors by the presence of its config.
    conf.register_opts(cors.CORS_OPTS, 'cors')
    if conf.cors.allowed_origin:
        cors_middleware = oslo_middleware.CORS.factory({}, **conf.cors)
    else:
        cors_middleware = None

    context_middleware = auth.PlacementKeystoneContext
    req_id_middleware = oslo_middleware.RequestId
    microversion_middleware = mp_middleware.MicroversionMiddleware
    fault_middleware = fault_wrap.FaultWrapper
    request_log = requestlog.RequestLog

    application = handler.PlacementHandler(config=conf)
    # configure microversion middleware in the old school way
    application = microversion_middleware(
        application,
        microversion.SERVICE_TYPE,
        microversion.VERSIONS,
        json_error_formatter=util.json_error_formatter)

    # NOTE(cdent): The ordering here is important. The list is ordered
    # from the inside out. For a single request req_id_middleware is called
    # first and microversion_middleware last. Then the request is finally
    # passed to the application (the PlacementHandler). At that point
    # the response ascends the middleware in the reverse of the
    # order the request went in. This order ensures that log messages
    # all see the same contextual information including request id and
    # authentication information.
    for middleware in (
            fault_middleware,
            request_log,
            context_middleware,
            auth_middleware,
            cors_middleware,
            req_id_middleware,
    ):
        if middleware:
            application = middleware(application)

    # NOTE(mriedem): Ignore scope check UserWarnings from oslo.policy.
    if not conf.oslo_policy.enforce_scope:
        import warnings
        warnings.filterwarnings('ignore',
                                message="Policy .* failed scope check",
                                category=UserWarning)

    return application
Ejemplo n.º 2
0
 def test_404_no_error_log(self, mocked_log):
     environ = _environ(path='/hello', method='GET')
     context_mock = mock.Mock()
     context_mock.to_policy_values.return_value = {'roles': ['admin']}
     environ['placement.context'] = context_mock
     app = handler.PlacementHandler()
     self.assertRaises(webob.exc.HTTPNotFound, app, environ, start_response)
     mocked_log.error.assert_not_called()
     mocked_log.exception.assert_not_called()
Ejemplo n.º 3
0
def deploy(conf):
    """Assemble the middleware pipeline leading to the placement app."""
    if conf.api.auth_strategy == 'noauth2':
        auth_middleware = auth.NoAuthMiddleware
    else:
        # Do not use 'oslo_config_project' param here as the conf
        # location may have been overridden earlier in the deployment
        # process with OS_PLACEMENT_CONFIG_DIR in wsgi.py.
        auth_middleware = auth.filter_factory({}, oslo_config_config=conf)

    # Conditionally add CORS middleware based on setting 'allowed_origin'
    # in config.
    if conf.cors.allowed_origin:
        cors_middleware = oslo_middleware.CORS.factory({}, **conf.cors)
    else:
        cors_middleware = None

    context_middleware = auth.PlacementKeystoneContext
    microversion_middleware = mp_middleware.MicroversionMiddleware
    fault_middleware = fault_wrap.FaultWrapper
    request_log = requestlog.RequestLog

    if os_profiler_web and 'profiler' in conf and conf.profiler.enabled:
        osprofiler_middleware = os_profiler_web.WsgiMiddleware.factory(
            {}, **conf.profiler)
    else:
        osprofiler_middleware = None

    application = handler.PlacementHandler(config=conf)

    # If PROFILER_OUTPUT is set, generate per request profile reports
    # to the directory named therein.
    if PROFILER_OUTPUT:
        application = profiler.ProfilerMiddleware(application,
                                                  profile_dir=PROFILER_OUTPUT)

    # configure microversion middleware in the old school way
    application = microversion_middleware(
        application,
        microversion.SERVICE_TYPE,
        microversion.VERSIONS,
        json_error_formatter=util.json_error_formatter)

    # NOTE(cdent): The ordering here is important. The list is ordered from the
    # inside out. For a single request, request_log is called first (to extract
    # request context information and log the start of the request). If
    # osprofiler_middleware is present (see above), it is first.
    # fault_middleware is last in the stack described below, to wrap unexpected
    # exceptions in the placement application as valid HTTP 500 responses. Then
    # the request is passed to the microversion middleware (configured above)
    # and then finally to the application (the PlacementHandler, further
    # above). At that point the response ascends the middleware in the reverse
    # of the order the request went in. This order ensures that log messages
    # all see the same contextual information including request id and
    # authentication information. An individual piece of middleware is a
    # wrapper around the next and can do work on the way in, the way out, or
    # both. Which can be determined by looking at the `__call__` method in the
    # middleware. "In" activity is done prior to calling the next layer in the
    # stack (often `self.application`). "Out" activity is after, or in a
    # redefinition of the `start_response` method, commonly called
    # `replacement_start_response`.
    for middleware in (
            fault_middleware,
            context_middleware,
            auth_middleware,
            cors_middleware,
            request_log,
            osprofiler_middleware,
    ):
        if middleware:
            application = middleware(application)

    # NOTE(mriedem): Ignore scope check UserWarnings from oslo.policy.
    if not conf.oslo_policy.enforce_scope:
        import warnings
        warnings.filterwarnings('ignore',
                                message="Policy .* failed scope check",
                                category=UserWarning)

    return application
Ejemplo n.º 4
0
 def setUp(self):
     super(ContentHeadersTest, self).setUp()
     self.environ = _environ(path='/')
     config = mock.MagicMock()
     self.environ['placement.context'] = mock.MagicMock()
     self.app = handler.PlacementHandler(config=config)
Ejemplo n.º 5
0
 def setUp(self):
     super(ContentHeadersTest, self).setUp()
     self.environ = _environ(path='/')
     self.app = handler.PlacementHandler()
Ejemplo n.º 6
0
def deploy(conf):
    """Assemble the middleware pipeline leading to the placement app."""
    if conf.api.auth_strategy == 'noauth2':
        auth_middleware = auth.NoAuthMiddleware
    else:
        # Do not use 'oslo_config_project' param here as the conf
        # location may have been overridden earlier in the deployment
        # process with OS_PLACEMENT_CONFIG_DIR in wsgi.py.
        auth_middleware = auth.filter_factory(
            {}, oslo_config_config=conf)

    # Conditionally add CORS middleware based on setting 'allowed_origin'
    # in config.
    if conf.cors.allowed_origin:
        cors_middleware = oslo_middleware.CORS.factory(
            {}, **conf.cors)
    else:
        cors_middleware = None

    context_middleware = auth.PlacementKeystoneContext
    req_id_middleware = oslo_middleware.RequestId
    microversion_middleware = mp_middleware.MicroversionMiddleware
    fault_middleware = fault_wrap.FaultWrapper
    request_log = requestlog.RequestLog

    if os_profiler_web and 'profiler' in conf and conf.profiler.enabled:
        osprofiler_middleware = os_profiler_web.WsgiMiddleware.factory(
            {}, **conf.profiler)
    else:
        osprofiler_middleware = None

    application = handler.PlacementHandler(config=conf)

    # If PROFILER_OUTPUT is set, generate per request profile reports
    # to the directory named therein.
    if PROFILER_OUTPUT:
        application = profiler.ProfilerMiddleware(
            application, profile_dir=PROFILER_OUTPUT)

    # configure microversion middleware in the old school way
    application = microversion_middleware(
        application, microversion.SERVICE_TYPE, microversion.VERSIONS,
        json_error_formatter=util.json_error_formatter)

    # NOTE(cdent): The ordering here is important. The list is ordered
    # from the inside out. For a single request req_id_middleware is called
    # first and microversion_middleware last. Then the request is finally
    # passed to the application (the PlacementHandler). At that point
    # the response ascends the middleware in the reverse of the
    # order the request went in. This order ensures that log messages
    # all see the same contextual information including request id and
    # authentication information.
    for middleware in (fault_middleware,
                       request_log,
                       context_middleware,
                       auth_middleware,
                       cors_middleware,
                       req_id_middleware,
                       osprofiler_middleware,
                       ):
        if middleware:
            application = middleware(application)

    # NOTE(mriedem): Ignore scope check UserWarnings from oslo.policy.
    if not conf.oslo_policy.enforce_scope:
        import warnings
        warnings.filterwarnings('ignore',
                                message="Policy .* failed scope check",
                                category=UserWarning)

    return application