Esempio n. 1
0
def create_url_map():
    """Instantiate all WSGI Apps and put them into the URL-Map."""
    _api_app = CheckmkApiApp(
        __name__,
        specification_dir=openapi_spec_dir(),
    )
    # NOTE
    # The URL will always contain the most up to date major version number, so that clients
    # exploring the API (browsers, etc.) will have a structural stability guarantee. Within major
    # versions only additions of fields or endpoints are allowed, never field changes or removals.
    # If a new major version is created it should be ADDED here and not replace the older version.
    # NOTE: v0 means totally unstable until we hit v1.
    _api_app.add_api_blueprint(
        'checkmk.yaml',
        base_path='/%s/check_mk/api/v0/' % cmk_version.omd_site(),
    )

    wrapped_api_app = with_context_middleware(
        OverrideRequestMethod(_api_app).wsgi_app)
    cmk_app = CheckmkApp()

    return Map([
        Submount('/<string:site>', [
            Submount("/check_mk", [
                Rule("/", endpoint=cmk_app),
                Rule("/dump.py", endpoint=dump_environ_app),
                Rule("/form.py", endpoint=test_formdata),
                Submount('/api', [
                    Rule("/", endpoint=wrapped_api_app),
                    Rule("/<path:path>", endpoint=wrapped_api_app),
                ]),
                Rule("/<string:script>", endpoint=cmk_app),
            ]),
        ])
    ])
Esempio n. 2
0
def create_url_map(debug=False):
    """Instantiate all WSGI Apps and put them into the URL-Map."""
    debug_rules = [
        Rule("/dump.py", endpoint=dump_environ_app),
        Rule("/form.py", endpoint=test_formdata),
    ]

    cmk_app = CheckmkApp(debug=debug)
    api_app = CheckmkRESTAPI(debug=debug).wsgi_app

    return Map(
        [
            Submount(
                "/<string:site>",
                [
                    Submount(
                        "/check_mk",
                        [
                            Rule("/", endpoint=cmk_app),
                            *(debug_rules if debug else []),
                            Rule("/api/<string:version>/<path:path>", endpoint=api_app),
                            Rule("/<string:script>", endpoint=cmk_app),
                        ],
                    ),
                ],
            )
        ]
    )
Esempio n. 3
0
def make_router(debug: bool = False) -> WSGIApplication:
    """Route the requests to the correct application.

    This router uses Werkzeug's URL-Map system to dispatch to the correct application. The
    applications are stored as references within the URL-Map and can then be called directly,
    without the necessity of doing import-magic.
    """
    # We create the URL-map once and re-use it on every request.
    url_map = create_url_map(debug=debug)

    # Fix wsgi.url_scheme with werkzeug.middleware.proxy_fix.ProxyFix
    # would be always http instead
    cmk_app = ProxyFix(
        app=CheckmkApp(debug=debug).__call__)  # type: ignore[arg-type]
    api_app = ProxyFix(
        app=CheckmkRESTAPI(debug=debug).wsgi_app)  # type: ignore[arg-type]

    endpoints: Dict[str, WSGIApplication] = {
        "cmk": cmk_app,
        "rest-api": api_app,
        "debug-dump": dump_environ_app,
        "debug-form": test_formdata,
    }

    def router(environ: WSGIEnvironment,
               start_response: StartResponse) -> WSGIResponse:
        urls = url_map.bind_to_environ(environ)
        try:
            result: Tuple[str, Mapping[str,
                                       Any]] = urls.match(return_rule=False)
            endpoint_name, args = result  # pylint: disable=unpacking-non-sequence
            endpoint = endpoints[endpoint_name]
        except HTTPException as e:
            # HTTPExceptions are WSGI apps
            endpoint = e
            args = {}

        environ[WSGI_ENV_ARGS_NAME] = args
        return endpoint(environ, start_response)

    return router