def setup_class(cls): cls.messaging_service = MessagingService() cls.messaging_service.service_start() cls.messaging_service.wait_for_start(15) cls.conn = WeaveConnection.local() cls.conn.connect() cls.env_service = DummyEnvService(cls.messaging_service.test_token, cls.conn) rpc_info = find_rpc(cls.env_service, MESSAGING_PLUGIN_URL, "app_manager") appmgr_client = RPCClient(cls.env_service.get_connection(), rpc_info, cls.env_service.get_auth_token()) appmgr_client.start() http_token = appmgr_client["register_plugin"]("http", WEAVE_HTTP_URL, _block=True) dummy_token = appmgr_client["register_plugin"]("x", "y", _block=True) cls.http = ThreadedWeaveHTTPService(auth_token=http_token, plugin_dir="x", venv_dir="y", conn=cls.conn, started_token="") cls.http.service_start() cls.http.wait_for_start(15) cls.dummy_service = DummyService(cls.conn, dummy_token) cls.dummy_service.service_start() cls.dummy_service.wait_for_start(15)
def setup_method(self): self.dummy_service = DummyMessagingService(MESSAGING_APP_TOKEN, WeaveConnection.local()) message_server_started = Event() app_registry = ApplicationRegistry([ ("Test", TEST_URL, TEST_APP_TOKEN), ("MessagingServer", MESSAGING_SERVER_URL, MESSAGING_APP_TOKEN), ]) channel_registry = ChannelRegistry(app_registry) synonym_registry = SynonymRegistry() self.message_server = MessageServer(PORT, app_registry, channel_registry, synonym_registry, message_server_started.set) self.message_server_thread = Thread(target=self.message_server.run) rpc_hub = MessagingRPCHub(self.dummy_service, channel_registry, app_registry, synonym_registry) self.message_server_thread.start() message_server_started.wait() self.dummy_service.start() rpc_hub.start() self.weave_service = DummyMessagingService(TEST_APP_TOKEN, WeaveConnection.local()) self.weave_service.start() self.appmgr_rpc_info = find_rpc(self.weave_service, MESSAGING_SERVER_URL, "app_manager")
def handle_rpc(): class FakeService(MessagingEnabled): def __init__(self, auth_token, conn): super(FakeService, self).__init__(auth_token=auth_token, conn=conn) def start(self): self.get_connection().connect() app_url = sys.argv[1] rpc_name = sys.argv[2] api_name = sys.argv[3] json_args = sys.argv[4] plugins_db = PluginsDatabase(os.path.join(get_config_path(), "db")) plugins_db.start() conn = WeaveConnection.discover() conn.connect() instance_data = get_instance_data() token = instance_data.app_token rpc_info = find_rpc(FakeService(token, conn), app_url, rpc_name) client = RPCClient(conn, rpc_info, token) client.start() print( json.dumps(client[api_name](*json.loads(json_args), _block=True), indent=2))
def load(self, plugin_state: PluginState): rpc_info = find_rpc(self.service, MESSAGING_PLUGIN_URL, "app_manager") self.client = RPCClient(self.service.get_connection(), rpc_info, self.service.get_auth_token()) self.client.start() plugin_state.app_manager_client = self.client if plugin_state.enabled: self.on_activate(plugin_state)
def register_plugin(service, plugin): conn = service.get_connection() token = service.get_auth_token() # TODO: Find a find to get find_rpc(..) work for core system RPC too. rpc_info = find_rpc(service, None) client = RPCClient(conn, rpc_info, token) # TODO: Make InstalledPlugin, and RunnablePlugin have GitHub url in them. return client["register_plugin"](plugin.plugin_id(), plugin.name, plugin.src, _block=True)
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())
def setup_class(cls): cls.messaging_service = MessagingService() cls.messaging_service.service_start() cls.messaging_service.wait_for_start(15) cls.conn = WeaveConnection.local() cls.conn.connect() cls.env_service = DummyEnvService(cls.messaging_service.test_token, cls.conn) rpc_info = find_rpc(cls.env_service, MESSAGING_PLUGIN_URL, "app_manager") appmgr_client = RPCClient(cls.env_service.get_connection(), rpc_info, cls.env_service.get_auth_token()) appmgr_client.start() # Register the DummyService used in the test cases. cls.test_token = appmgr_client["register_plugin"]("x", "y", _block=True) appmgr_client.stop()
def handle_rpc(self, body): app_url = get_required_argument(body, 'app_url') rpc_name = get_required_argument(body, 'rpc_name') api_name = get_required_argument(body, 'api_name') args = get_required_argument(body, 'args') with self.clients_lock: client = self.clients.get((app_url, rpc_name), None) if not client: rpc_info = find_rpc(self.service, app_url, rpc_name) with self.clients_lock: client = self.clients.get((app_url, rpc_name), None) if not client: client = RPCClient(self.service.get_connection(), rpc_info, self.service.get_auth_token()) client.start() self.clients[(app_url, rpc_name)] = client response = client[api_name](*args, _block=True) return response
def start(self): rpc_info = find_rpc(self.service, WEAVE_HTTP_URL, "static_files") self.rpc_client = RPCClient(self.service.get_connection(), rpc_info, self.service.get_auth_token()) self.rpc_client.start()
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()