def get_and_wrap_page(script_name: str) -> Callable[[], Response]: """Get the page handler and wrap authentication logic when needed. For all "noauth" page handlers the wrapping part is skipped. In the `ensure_authentication` wrapper everything needed to make a logged-in request is listed. """ _handler = pages.get_page_handler(script_name) if _handler is None: # Some pages do skip authentication. This is done by adding # noauth: to the page handler, e.g. "noauth:run_cron" : ... # TODO: Eliminate those "noauth:" pages. Eventually replace it by call using # the now existing default automation user. _handler = pages.get_page_handler("noauth:" + script_name) if _handler is not None: return _noauth(_handler) if _handler is None: return _page_not_found return ensure_authentication(_handler)
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)