Exemplo n.º 1
0
 def __init__(self, service):
     self.base_dir = tempfile.mkdtemp()
     self.rpc = RPCServer("static_files", "HTTP Registry", [
         ServerAPI("register", "Register a resource.", [
             ArgParameter("filename", "File name.", str),
             ArgParameter("content", "Base64 content", str),
         ], self.register),
         ServerAPI("unregister", "Unregister a resource",
                   [ArgParameter("filename", "File name", str)],
                   self.unregister),
     ], service)
     self.pseudo_app_id_map = defaultdict(lambda: str(uuid4()))
     self.pseudo_app_id_map_lock = Lock()
Exemplo n.º 2
0
 def __init__(self, plugin_manager: PluginManager):
     self.plugin_manager = plugin_manager
     self.rpc_server = RPCServer("PluginManager", "WeaveInstance Manager", [
         ServerAPI("list_plugins", "List plugins.", [],
                   self.plugin_manager.list_plugins),
         ServerAPI("activate_plugin", "Activate a plugin", [
             ArgParameter("plugin_url", "Plugin URL to activate", str),
         ], self.plugin_manager.activate),
         ServerAPI("deactivate_plugin", "Deactivate a plugin", [
             ArgParameter("plugin_url", "Plugin URL to deactivate", str),
         ], self.plugin_manager.deactivate),
         ServerAPI("enable_plugin", "Enable a plugin", [
             ArgParameter("plugin_url", "Plugin URL to deactivate", str),
         ], self.plugin_manager.enable_plugin),
         ServerAPI("disable_plugin", "Disable a plugin", [
             ArgParameter("plugin_url", "Plugin URL to deactivate", str),
         ], self.plugin_manager.disable_plugin),
         ServerAPI("install_plugin", "Install a plugin", [
             ArgParameter("plugin_url", "URL ending with .git.", str),
         ], self.plugin_manager.install),
         ServerAPI("uninstall_plugin", "Uninstall a plugin", [
             ArgParameter("plugin_url", "Plugin URL to uninstall", str),
         ], self.plugin_manager.uninstall),
         ServerAPI("plugin_info", "Get Plugin Info", [
             ArgParameter("plugin_url", "Get plugin info", str),
         ], self.plugin_manager.info),
     ], service)
Exemplo n.º 3
0
 def __init__(self, conn, token):
     super(DummyService, self).__init__(auth_token=token, conn=conn)
     apis = [
         ServerAPI("api", "desc1", [
             ArgParameter("param", "d1", str),
         ], self.api),
         ServerAPI("number", "desc1", [
             ArgParameter("param", "d1", int),
         ], self.number),
         ServerAPI("exception", "desc1", [], self.exception),
     ]
     self.rpc_server = RPCServer("name", "desc", apis, self)
     dashboard_rpc_info = find_rpc(self, "b", "static_files")
     self.http_client = RPCClient(self.get_connection(), dashboard_rpc_info,
                                  self.get_auth_token())
Exemplo n.º 4
0
 def __init__(self, service, channel_registry, app_registry,
              synonym_registry):
     owner_app = app_registry.get_app_by_url(MESSAGING_SERVER_URL)
     self.rpc = RootRPCServer("app_manager", "Application Manager", [
         ServerAPI("register_rpc", "Register new RPC", [
             ArgParameter("name", "Name of the RPC", str),
             ArgParameter("description", "Description of RPC", str),
             ArgParameter("apis", "Maps of all APIs", self.APIS_SCHEMA),
             ArgParameter(
                 "allowed_requestors",
                 "List of app_urls that can call this RPC. Empty " +
                 "list to allow everyone.", {
                     "type": "array",
                     "items": {
                         "type": "string"
                     }
                 })
         ], self.register_rpc),
         ServerAPI("update_rpc", "Update RPC with latest schema.", [
             ArgParameter("name", "Name of the RPC", str),
             ArgParameter("apis", "Maps of all APIs", self.APIS_SCHEMA),
         ], self.update_rpc),
         ServerAPI("unregister_rpc", "Unregister an RPC", [
             ArgParameter("name", "Name of the RPC", str),
         ], self.unregister_rpc),
         ServerAPI("register_queue", "Register a new queue", [
             ArgParameter("channel_name", "Basename of the queue", str),
             ArgParameter("queue_type", "Type of the queue",
                          OneOf("fifo", "sessionized", "multicast")),
             ArgParameter("schema", "JSONSchema of the messages pushed",
                          {}),
             ArgParameter("push_whitelist", "Whitelisted plugin URLs array",
                          ListOf(Type(str))),
             ArgParameter("pop_whitelist", "Whitelisted plugin URLs array",
                          ListOf(Type(str))),
         ], self.register_queue),
         ServerAPI("register_plugin", "Register Plugin", [
             ArgParameter("name", "Plugin Name", str),
             ArgParameter("url", "Plugin URL (GitHub)", str),
         ], self.register_plugin),
         ServerAPI("unregister_plugin", "Unregister Plugin",
                   [ArgParameter("url", "Plugin URL (GitHub)", str)],
                   self.unregister_plugin),
         ServerAPI("rpc_info", "Get RPCInfo object.", [
             ArgParameter("app_url", "Plugin URL", str),
             ArgParameter("rpc_name", "RPC Name", str),
         ], self.rpc_info),
         ServerAPI(
             "register_synonym", "Register a synonym for a channel.", [
                 ArgParameter("synonym", "Name of requested synonym", str),
                 ArgParameter("target", "Name of channel to map to", str),
             ], self.register_synonym),
     ], service, channel_registry, owner_app)
     self.channel_registry = channel_registry
     self.app_registry = app_registry
     self.synonym_registry = synonym_registry
     self.rpc_registry = {}
Exemplo n.º 5
0
    def start(self):
        # Insert basic data into the DB such as command-line access Data and
        # current machine data.
        # Check if the messaging plugin is installed any machine.
        try:
            messaging_db_plugin = get_plugin_by_url(MESSAGING_PLUGIN_URL)
        except ObjectNotFound:
            print("No messaging plugin installed.")
            sys.exit(1)

        auth_token = self.instance_data.app_token
        if (messaging_db_plugin.machine.machine_id !=
                self.instance_data.machine_id):
            conn = WeaveConnection.discover()
        else:
            messaging_plugin = load_plugin(messaging_db_plugin,
                                           self.plugin_manager,
                                           auth_token,
                                           ignore_hierarchy=True)
            self.plugin_manager.activate(messaging_plugin)
            conn = WeaveConnection.local()

        conn.connect()

        service = MessagingEnabled(auth_token=auth_token, conn=conn)
        self.rpc_server = RPCServer("WeaveEnv", "WeaveInstance Manager", [
            ServerAPI("list_plugins", "List plugins.", [], self.list_plugins),
            ServerAPI("activate_plugin", "Activate a plugin", [
                ArgParameter("plugin_id", "PluginID to activate", str),
            ], self.activate),
            ServerAPI("deactivate_plugin", "Deactivate a plugin", [
                ArgParameter("plugin_id", "PluginID to deactivate", str),
            ], self.deactivate),
            ServerAPI("install_plugin", "Install a plugin", [
                ArgParameter("plugin_url", "URL ending with .git.", str),
            ], self.install),
            ServerAPI("uninstall_plugin", "Uninstall a plugin", [
                ArgParameter("plugin_id", "PluginID to uninstall", str),
            ], self.uninstall),
        ], service)

        installed_plugins = load_installed_plugins(self.instance_data.plugins,
                                                   service,
                                                   self.plugin_manager)
        self.plugin_manager.start(installed_plugins)
        self.rpc_server.start()
Exemplo n.º 6
0
 def get_request_schema_from_apis(self, apis):
     return {
         "type": "object",
         "properties": {
             "invocation": {
                 "anyOf":
                 [ServerAPI.from_info(x).schema for x in apis.values()]
             }
         }
     }
Exemplo n.º 7
0
 def __init__(self, conn, token):
     super(DummyService, self).__init__(auth_token=token, conn=conn)
     apis = [
         ServerAPI("api1", "desc1", [
             ArgParameter("p1", "d1", str),
             ArgParameter("p2", "d2", int),
             KeywordParameter("k3", "d3", bool)
         ], self.api1),
         ServerAPI("api2", "desc2", [], self.api2),
         ServerAPI("api3", "desc3", [], self.api3),
         ServerAPI("callback", "c",
                   [ArgParameter("param", "d", self.get_param)],
                   self.callback_api),
         ServerAPI("change_param", "c", [ArgParameter("param", "d", str)],
                   self.change_param),
         ServerAPI("exception", "desc2", [], self.exception)
     ]
     self.rpc_server = RPCServer("name", "desc", apis, self)
     self.paused = False
     self.available_params = ["1", "2"]
Exemplo n.º 8
0
    def test_register_rpc_with_whitelists(self):
        conn = WeaveConnection.local()
        conn.connect()
        client = RPCClient(conn, self.appmgr_rpc_info, TEST_APP_TOKEN)
        client.start()

        data = {
            "1": {
                "name": "name1",
                "url": "url1",
                "rpc_name": "rpc1",
                "allowed_requestors": []
            },
            "2": {
                "name": "name2",
                "url": "url2",
                "rpc_name": "rpc2",
                "allowed_requestors": ["url1"]
            },
            "3": {
                "name": "name3",
                "url": "url3",
                "rpc_name": "rpc3",
                "allowed_requestors": ["diff-url"]
            },
        }

        for info in data.values():
            info["token"] = client["register_plugin"](info["name"],
                                                      info["url"],
                                                      _block=True)

            service = DummyMessagingService(info["token"], conn)
            info["server"] = RPCServer(info["rpc_name"], "desc", [
                ServerAPI("name", "desc", [
                    ArgParameter("param", "desc", str),
                ], lambda x: x),
            ], service, info["allowed_requestors"])
            info["server"].start()

            info["rpc_info"] = find_rpc(service, info["url"], info["rpc_name"])

        allowed_requestors = [
            ("1", "2"),
            ("1", "1"),
            ("2", "1"),
            ("3", "1"),
        ]

        disallowed_requestors = [("1", "3"), ("2", "2"), ("2", "3"),
                                 ("3", "2"), ("3", "3")]

        for source, target in allowed_requestors:
            plugin_client = RPCClient(conn, data[target]["rpc_info"],
                                      data[source]["token"])
            plugin_client.start()
            assert plugin_client["name"]("x", _block=True) == "x"
            plugin_client.stop()

        for source, target in disallowed_requestors:
            plugin_client = RPCClient(conn, data[target]["rpc_info"],
                                      data[source]["token"])
            plugin_client.start()
            with pytest.raises(Unauthorized):
                plugin_client["name"]("x", _block=True)
            plugin_client.stop()

        for info in data.values():
            info["server"].stop()

        client.stop()
        conn.close()