Beispiel #1
0
def setup_studies_dispatcher(app: web.Application) -> bool:
    settings: StudiesDispatcherSettings = get_plugin_settings(app)

    _setup_studies_access(app, settings)

    # Redirects routes
    redirect_handler = get_redirection_to_viewer
    if settings.is_login_required():
        redirect_handler = login_required(get_redirection_to_viewer)

        logger.info(
            "'%s' config explicitly disables anonymous users from this feature",
            __name__,
        )

    app.router.add_routes(
        [web.get("/view", redirect_handler, name="get_redirection_to_viewer")])

    # Rest-API routes: maps handlers with routes tags with "viewer" based on OAS operation_id
    specs = app[APP_OPENAPI_SPECS_KEY]
    rest_routes = map_handlers_with_operations(
        rest_handler_functions,
        filter(lambda op: "viewer" in op.tags, iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(rest_routes)

    return True
Beispiel #2
0
def _create_routes(tag, specs, *handlers_module, disable_login: bool = False):
    """
    :param disable_login: Disables login_required decorator for testing purposes defaults to False
    :type disable_login: bool, optional
    """
    # TODO: Remove 'disable_login' and use instead a mock.patch on the decorator!

    handlers = {}
    for mod in handlers_module:
        handlers.update(get_handlers_from_namespace(mod))

    if disable_login:
        handlers = {name: hnds.__wrapped__ for name, hnds in handlers.items()}

    routes = map_handlers_with_operations(
        handlers,
        filter(
            lambda o: tag in o[3] and "snapshot" not in o[2],
            iter_path_operations(specs),
        ),
        strict=True,
    )

    if disable_login:
        logger.debug("%s:\n%s", "projects", pformat(routes))

    return routes
def setup_director_v2(app: web.Application):
    assert app[APP_SETTINGS_KEY].WEBSERVER_DIRECTOR_V2  # nosec

    # client to communicate with director-v2 service
    set_client(app, DirectorV2ApiClient(app))

    # routes at the web-server app
    setup_rest(app)

    if is_setup_completed(setup_rest.metadata()["module_name"], app):
        log.debug("Adding routes")

        set_project_run_policy(app, DefaultProjectRunPolicy())

        specs = app[APP_OPENAPI_SPECS_KEY]
        routes = map_handlers_with_operations(
            {
                "start_pipeline": director_v2_handlers.start_pipeline,
                "stop_pipeline": director_v2_handlers.stop_pipeline,
            },
            filter(lambda o: "computation" in o[1],
                   iter_path_operations(specs)),
            strict=True,
        )
        # TODO:  app.router.add_routes(director_v2_handlers.routes) and corresponding tests
        app.router.add_routes(routes)

    else:

        log.warning(
            "Skipping computation routes since WEBSERVER_REST plugin is disabled (i.e. service w/o http API)"
        )
Beispiel #4
0
def setup_publications(app: web.Application):

    # routes
    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        get_handlers_from_namespace(publication_handlers),
        filter(lambda o: "publication" in o[3], iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(routes)
Beispiel #5
0
def setup_users(app: web.Application):

    # routes related with users
    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        get_handlers_from_namespace(users_handlers),
        filter(lambda o: "me" in o[1].split("/"), iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(routes)
Beispiel #6
0
def setup_tags(app: web.Application):
    assert app[APP_SETTINGS_KEY].WEBSERVER_TAGS  # nosec
    # routes
    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        get_handlers_from_namespace(tags_handlers),
        filter(lambda o: "tag" in o[3], iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(routes)
def setup_publications(app: web.Application):
    assert app[APP_SETTINGS_KEY].WEBSERVER_PUBLICATIONS  # nosec

    setup_email(app)

    # routes
    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        get_handlers_from_namespace(publication_handlers),
        filter(lambda o: "publication" in o[3], iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(routes)
Beispiel #8
0
def setup_groups(app: web.Application):

    assert app[APP_SETTINGS_KEY].WEBSERVER_GROUPS  # nosec

    # routes
    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        get_handlers_from_namespace(groups_handlers),
        filter(lambda o: "groups" in o[1].split("/"),
               iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(routes)
Beispiel #9
0
def setup_exporter(app: web.Application) -> bool:

    # Rest-API routes: maps handlers with routes tags with "viewer" based on OAS operation_id

    specs = app[APP_OPENAPI_SPECS_KEY]
    rest_routes = map_handlers_with_operations(
        rest_handler_functions,
        filter(lambda op: "exporter" in op.tags, iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(rest_routes)

    return True
Beispiel #10
0
def setup_groups(app: web.Application):

    # prepares scicrunch api
    setup_scicrunch_submodule(app)

    # routes
    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        get_handlers_from_namespace(groups_handlers),
        filter(lambda o: "groups" in o[1].split("/"), iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(routes)
Beispiel #11
0
def test_filtered_routing(specs):
    handlers = Handlers()
    found = get_handlers_from_namespace(handlers)

    hdl_sel = {name: hdl for name, hdl in found.items() if "i" in name}
    opr_iter = ((mth, url, opname, _tags)
                for mth, url, opname, _tags in iter_path_operations(specs)
                if "i" in opname)

    routes = map_handlers_with_operations(hdl_sel, opr_iter, strict=True)

    for rdef in routes:
        assert rdef.method == "GET"
        assert rdef.handler in hdl_sel.values()
Beispiel #12
0
def setup_activity(app: web.Application):

    # setup routes ------------
    specs = app[APP_OPENAPI_SPECS_KEY]

    def include_path(tup_object):
        _method, path, _operation_id, _tags = tup_object
        return any(tail in path for tail in ["/activity/status"])

    handlers_dict = {"get_status": handlers.get_status}

    routes = map_handlers_with_operations(handlers_dict,
                                          filter(include_path,
                                                 iter_path_operations(specs)),
                                          strict=True)
    app.router.add_routes(routes)
Beispiel #13
0
def setup_director_v2(app: web.Application):
    # create settings and injects in app
    create_settings(app)

    if not APP_OPENAPI_SPECS_KEY in app:
        log.warning(
            "rest submodule not initialised? computation routes will not be defined!"
        )
        return

    specs = app[APP_OPENAPI_SPECS_KEY]
    # bind routes with handlers
    routes = map_handlers_with_operations(
        {
            "start_pipeline": director_v2_handlers.start_pipeline,
            "stop_pipeline": director_v2_handlers.stop_pipeline,
        },
        filter(lambda o: "computation" in o[1], iter_path_operations(specs)),
        strict=True,
    )
    # FIXME:  app.router.add_routes(director_v2_handlers.routes)
    app.router.add_routes(routes)
Beispiel #14
0
def create_routes(specs: openapi.Spec) -> List[web.RouteDef]:
    """Creates routes mapping operators_id with handler functions

    :param specs: validated oas
    :type specs: openapi.Spec
    :return: list of web routes for auth
    :rtype: List[web.RouteDef]
    """
    log.debug("Creating %s ", __name__)

    base_path = openapi.get_base_path(specs)

    def include_path(tuple_object):
        _method, path, _operation_id, _tags = tuple_object
        return path.startswith(base_path + "/auth/")

    handlers_map = {
        "auth_register": login_handlers.register,
        "auth_login": login_handlers.login,
        "auth_logout": login_handlers.logout,
        "auth_reset_password": login_handlers.reset_password,
        "auth_reset_password_allowed": login_handlers.reset_password_allowed,
        "auth_change_email": login_handlers.change_email,
        "auth_change_password": login_handlers.change_password,
        "auth_confirmation": login_handlers.email_confirmation,
        "create_api_key": api_keys_handlers.create_api_key,
        "delete_api_key": api_keys_handlers.delete_api_key,
        "list_api_keys": api_keys_handlers.list_api_keys,
    }

    routes = map_handlers_with_operations(handlers_map,
                                          filter(include_path,
                                                 iter_path_operations(specs)),
                                          strict=True)

    log.debug("Mapped auth routes: %s",
              "\n".join([pformat(r) for r in routes]))

    return routes
Beispiel #15
0
def setup_director(
    app: web.Application, *, disable_login: bool = False, disable_routes: bool = False
):
    """Sets up director's app module

    disable_* options are for testing purpuses
    """
    # ----------------------------------------------
    # TODO: temporary, just to check compatibility between
    # trafaret and pydantic schemas
    _, settings = assert_valid_config(app)
    # ---------------------------------------------

    # director service API base url, e.g. http://director:8081/v0
    app[APP_DIRECTOR_API_KEY] = str(settings.base_url)

    # setup routes ------------
    if not disable_routes:
        specs = app[APP_OPENAPI_SPECS_KEY]

        def include_path(tup_object):
            _method, path, _operation_id, _tags = tup_object
            return any(tail in path for tail in ["/running_interactive_services"])

        handlers_dict = {}

        # Disables login_required decorator for testing purposes
        if disable_login:
            for name, hnds in handlers_dict.items():
                if hasattr(hnds, "__wrapped__"):
                    handlers_dict[name] = hnds.__wrapped__

        routes = map_handlers_with_operations(
            handlers_dict,
            filter(include_path, iter_path_operations(specs)),
            strict=True,
        )
        app.router.add_routes(routes)
Beispiel #16
0
def setup_director_v2(app: web.Application):
    set_project_run_policy(app, DefaultProjectRunPolicy())

    set_client(app, DirectorV2ApiClient(app))

    if not APP_OPENAPI_SPECS_KEY in app:
        log.warning(
            "rest submodule not initialised? computation routes will not be defined!"
        )
        return

    specs = app[APP_OPENAPI_SPECS_KEY]
    # bind routes with handlers
    routes = map_handlers_with_operations(
        {
            "start_pipeline": director_v2_handlers.start_pipeline,
            "stop_pipeline": director_v2_handlers.stop_pipeline,
        },
        filter(lambda o: "computation" in o[1], iter_path_operations(specs)),
        strict=True,
    )
    # TODO:  app.router.add_routes(director_v2_handlers.routes) and corresponding tests
    app.router.add_routes(routes)
Beispiel #17
0
def setup_director_v2(app: web.Application):
    assert app[APP_SETTINGS_KEY].WEBSERVER_DIRECTOR_V2  # nosec

    # dependencies
    setup_rest(app)

    # lcietn
    set_client(app, DirectorV2ApiClient(app))

    # routes
    set_project_run_policy(app, DefaultProjectRunPolicy())

    specs = app[APP_OPENAPI_SPECS_KEY]
    routes = map_handlers_with_operations(
        {
            "start_pipeline": director_v2_handlers.start_pipeline,
            "stop_pipeline": director_v2_handlers.stop_pipeline,
        },
        filter(lambda o: "computation" in o[1], iter_path_operations(specs)),
        strict=True,
    )
    # TODO:  app.router.add_routes(director_v2_handlers.routes) and corresponding tests
    app.router.add_routes(routes)
Beispiel #18
0
def setup_activity(app: web.Application):

    # ----------------------------------------------
    # TODO: temporary, just to check compatibility between
    # trafaret and pydantic schemas
    assert_valid_config(app)
    # ---------------------------------------------

    # setup routes ------------
    specs = app[APP_OPENAPI_SPECS_KEY]

    def include_path(tup_object):
        _method, path, _operation_id, _tags = tup_object
        return any(tail in path for tail in ["/activity/status"])

    handlers_dict = {"get_status": handlers.get_status}

    routes = map_handlers_with_operations(handlers_dict,
                                          filter(include_path,
                                                 iter_path_operations(specs)),
                                          strict=True)
    app.router.add_routes(routes)

    celery_client.setup(app)
Beispiel #19
0
def setup_catalog(app: web.Application, *, disable_auth=False):
    # ----------------------------------------------
    # TODO: temporary, just to check compatibility between
    # trafaret and pydantic schemas
    cfg, _ = assert_valid_config(app)
    # ---------------------------------------------

    # resolve url
    app[KCATALOG_ORIGIN] = URL.build(scheme="http",
                                     host=cfg["host"],
                                     port=cfg["port"])
    app[KCATALOG_VERSION_PREFIX] = cfg["version"]

    specs = app[APP_OPENAPI_SPECS_KEY]  # validated openapi specs

    exclude: List[str] = []
    route_def: RouteDef
    for route_def in catalog_api_handlers.routes:
        route_def.kwargs["name"] = operation_id = route_def.handler.__name__
        exclude.append(operation_id)

    app.add_routes(catalog_api_handlers.routes)

    # bind the rest routes with the reverse-proxy-handler
    # FIXME: this would reroute **anything** to the catalog service!
    handler = (_reverse_proxy_handler.__wrapped__
               if disable_auth else _reverse_proxy_handler)
    routes = [
        web.route(method.upper(), path, handler, name=operation_id)
        for method, path, operation_id, tags in iter_path_operations(specs)
        if "catalog" in tags and operation_id not in exclude
    ]
    assert routes, "Got no paths tagged as catalog"  # nosec

    # reverse proxy to catalog's API
    app.router.add_routes(routes)
Beispiel #20
0
def setup_studies_dispatcher(app: web.Application) -> bool:
    cfg = app[APP_CONFIG_KEY]["main"]

    # Redirects routes
    redirect_handler = get_redirection_to_viewer
    if not cfg["studies_access_enabled"]:
        redirect_handler = login_required(get_redirection_to_viewer)
        logger.warning(
            "'%s' config explicitly disables anonymous users from this feature",
            __name__,
        )
    app.router.add_routes(
        [web.get("/view", redirect_handler, name="get_redirection_to_viewer")])

    # Rest-API routes: maps handlers with routes tags with "viewer" based on OAS operation_id
    specs = app[APP_OPENAPI_SPECS_KEY]
    rest_routes = map_handlers_with_operations(
        rest_handler_functions,
        filter(lambda op: "viewer" in op.tags, iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(rest_routes)

    return True