Ejemplo n.º 1
0
def _process_request(environ, start_response) -> Response:  # pylint: disable=too-many-branches
    try:
        html.init_modes()

        # Make sure all plugins are available as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        page_handler = get_and_wrap_page(html.myfile)
        response = page_handler()
        # If page_handler didn't raise we assume everything is OK.
        response.status_code = http_client.OK
    except HTTPRedirect as e:
        # This can't be a new Response as it can have already cookies set/deleted by the pages.
        # We can't return the response because the Exception has been raised instead.
        # TODO: Remove all HTTPRedirect exceptions from all pages. Making the Exception a subclass
        #       of Response may also work as it can then be directly returned from here.
        response = html.response
        response.status_code = e.status
        response.headers["Location"] = e.url

    except FinalizeRequest as e:
        # TODO: Remove all FinalizeRequest exceptions from all pages and replace it with a `return`.
        #       It may be necessary to rewire the control-flow a bit as this exception could have
        #       been used to short-circuit some code and jump directly to the response. This
        #       needs to be changed as well.
        response = html.response
        response.status_code = e.status

    except livestatus.MKLivestatusNotFoundError as e:
        response = _render_exception(e, title=_("Data not found"))

    except MKUserError as e:
        response = _render_exception(e, title=_("Invalid user Input"))

    except MKAuthException as e:
        response = _render_exception(e, title=_("Permission denied"))

    except livestatus.MKLivestatusException as e:
        response = _render_exception(e, title=_("Livestatus problem"))
        response.status_code = http_client.BAD_GATEWAY

    except MKUnauthenticatedException as e:
        response = _render_exception(e, title=_("Not authenticated"))
        response.status_code = http_client.UNAUTHORIZED

    except MKConfigError as e:
        response = _render_exception(e, title=_("Configuration error"))
        logger.error("MKConfigError: %s", e)

    except MKGeneralException as e:
        response = _render_exception(e, title=_("General error"))
        logger.error("MKGeneralException: %s", e)

    except Exception:
        response = handle_unhandled_exception()

    return response(environ, start_response)
Ejemplo n.º 2
0
def _process_request(
    environ: WSGIEnvironment,
    start_response: StartResponse,
    debug: bool = False,
) -> WSGIResponse:  # pylint: disable=too-many-branches
    resp: Response
    try:
        page_handler = get_and_wrap_page(requested_file_name(request))
        resp = page_handler()
    except HTTPRedirect as e:
        # This can't be a new Response as it can have already cookies set/deleted by the pages.
        # We can't return the response because the Exception has been raised instead.
        # TODO: Remove all HTTPRedirect exceptions from all pages. Making the Exception a subclass
        #       of Response may also work as it can then be directly returned from here.
        resp = response
        resp.status_code = e.status
        resp.headers["Location"] = e.url

    except FinalizeRequest as e:
        # TODO: Remove all FinalizeRequest exceptions from all pages and replace it with a `return`.
        #       It may be necessary to rewire the control-flow a bit as this exception could have
        #       been used to short-circuit some code and jump directly to the response. This
        #       needs to be changed as well.
        resp = response
        resp.status_code = e.status

    except livestatus.MKLivestatusNotFoundError as e:
        resp = _render_exception(e, title=_("Data not found"))

    except MKUserError as e:
        resp = _render_exception(e, title=_("Invalid user input"))

    except MKAuthException as e:
        resp = _render_exception(e, title=_("Permission denied"))

    except livestatus.MKLivestatusException as e:
        resp = _render_exception(e, title=_("Livestatus problem"))
        resp.status_code = http_client.BAD_GATEWAY

    except MKUnauthenticatedException as e:
        resp = _render_exception(e, title=_("Not authenticated"))
        resp.status_code = http_client.UNAUTHORIZED

    except MKConfigError as e:
        resp = _render_exception(e, title=_("Configuration error"))
        logger.error("MKConfigError: %s", e)

    except (MKGeneralException, cmk.utils.store.MKConfigLockTimeout) as e:
        resp = _render_exception(e, title=_("General error"))
        logger.error("%s: %s", e.__class__.__name__, e)

    except Exception:
        resp = handle_unhandled_exception()
        if debug:
            raise

    return resp(environ, start_response)
Ejemplo n.º 3
0
def _process_request(environ, start_response) -> Response:
    try:
        if html.myfile != "ajax_search_setup":
            abort(
                http_client.NOT_FOUND,
                description=
                f"CheckmkSetupSearchApp is reserved exclusively for the Setup search "
                f"(ajax_search_setup), but it was called with the page {html.myfile}.",
            )

        page_handler = pages.get_page_handler(html.myfile)
        if not page_handler:
            raise KeyError(
                "The page_handler for ajax_search_setup is missing.")

        page_handler_auth = ensure_authentication(page_handler)
        response = page_handler_auth()

        if '"result_code": 0' not in response.get_data(as_text=True):
            abort(http_client.BAD_REQUEST)

        response.status_code = http_client.OK

    except HTTPException as http_excpt:
        # do not write crash report in this case
        response = html.response
        response.status_code = http_excpt.code or http_client.BAD_REQUEST

    except MKUnauthenticatedException:
        # do not write crash report in this case
        response = html.response
        response.status_code = http_client.UNAUTHORIZED

    except Exception:
        response = handle_unhandled_exception()
        response.status_code = http_client.INTERNAL_SERVER_ERROR

    return response(environ, start_response)