Exemplo n.º 1
0
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)
Exemplo n.º 2
0
 def is_permitted(self, url: str) -> bool:
     file_name, query_vars = file_name_and_query_vars_from_url(url)
     self._set_query_vars(query_vars)
     try:
         with RequestContext(html_obj=html(self._request),
                             req=self._request):
             with UserContext(self._user_id):
                 page_handler = get_page_handler(file_name)
                 if page_handler:
                     page_handler()
         return True
     except MKAuthException:
         return False
Exemplo 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)
Exemplo n.º 4
0
 def _try_page(file_name: str) -> None:
     page_handler = get_page_handler(file_name)
     if page_handler:
         page_handler()
Exemplo n.º 5
0
 def _try_page(file_name: str) -> None:
     page_handler = get_page_handler(file_name)
     if page_handler:
         with output_funnel.plugged():
             page_handler()
             output_funnel.drain()