예제 #1
0
 def test_schema(self):
     assert {"type": "string"} == ArgParameter("", "", str).schema
     assert {"type": "number"} == ArgParameter("", "", int).schema
     assert {"type": "boolean"} == KeywordParameter("", "", bool).schema
     assert {
         "type": "object"
     } == KeywordParameter("", "", {
         "type": "object"
     }).schema
예제 #2
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()
예제 #3
0
    def test_validate_schema_with_args(self):
        api = API("name", "desc", [
            ArgParameter("a1", "d1", str),
            KeywordParameter("a2", "d2", int),
            ArgParameter("a3", "d3", bool),
        ])

        api.validate_call("a1", False, a2=5)

        with pytest.raises(BadArguments):
            api.validate_call()

        with pytest.raises(BadArguments):
            api.validate_call("a", True, {1: 2}, a4=5)
예제 #4
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())
예제 #5
0
 def test_info(self):
     assert ArgParameter("a", "b", str).info == {
         "name": "a",
         "description": "b",
         "schema": {
             "type": "string"
         }
     }
예제 #6
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()
예제 #7
0
    def test_api_reconstruct(self):
        api = API("name", "desc", [
            KeywordParameter("a2", "d2", int),
            ArgParameter("a1", "d1", str),
            KeywordParameter("a3", "d3", bool),
        ])

        assert API.from_info(api.info).info == api.info
예제 #8
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)
예제 #9
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"]
예제 #10
0
    def test_info(self):
        api = API("name", "desc", [
            KeywordParameter("a2", "d2", int),
            ArgParameter("a1", "d1", str),
            KeywordParameter("a3", "d3", bool),
        ])

        assert api.info == {
            "name": "name",
            "description": "desc",
            "args": [x.info for x in api.args],
            "kwargs": {p.name: p.info
                       for p in api.kwargs},
            "request_schema": {
                "additionalProperties": False,
                "properties": {
                    "args": {
                        "items": [{
                            "type": "string"
                        }],
                        "maxItems": 1,
                        "minItems": 1,
                        "type": "array"
                    },
                    "command": {
                        "enum": ["name"]
                    },
                    "id": {
                        "type": "string"
                    },
                    "kwargs": {
                        "properties": {
                            "a2": {
                                "type": "number"
                            },
                            "a3": {
                                "type": "boolean"
                            }
                        },
                        "required": ["a2", "a3"],
                        "type": "object"
                    }
                },
                "required": ["command", "id", "args", "kwargs"],
                "type": "object"
            },
            "response_schema": {}
        }
예제 #11
0
    def test_validate_call(self):
        api = API("name", "desc", [
            KeywordParameter("a2", "d2", int),
            ArgParameter("a1", "d1", str),
            KeywordParameter("a3", "d3", bool),
        ])

        obj = api.validate_call("str", a2=5, a3=False)
        obj.pop("id")
        assert obj == {
            "command": "name",
            "args": ["str"],
            "kwargs": {
                "a2": 5,
                "a3": False
            }
        }
예제 #12
0
    def test_validate_schema_with_callable(self):
        count = 0

        def schema():
            return [
                Exactly({"h": [1, 2, "hi", {
                    "a": "b"
                }]}),
                OneOf({"a": "b"}, {"c": "d"}, [1, 2]),
                JsonSchema({"type": "string"}),
                ListOf(Exactly(1)),
                Type(int),
            ][count]

        api = API("name", "desc", [ArgParameter("a1", "d1", schema)])

        with pytest.raises(BadArguments):
            api.validate_call({"h": [1, 2, "hi", {"a": "c"}]})
        api.validate_call({"h": [1, 2, "hi", {"a": "b"}]})

        count = 1
        with pytest.raises(BadArguments):
            api.validate_call({"h": [1, 2, "hi", {"a": "b"}]})
        api.validate_call({"a": "b"})
        api.validate_call({"c": "d"})
        api.validate_call([1, 2])

        count = 2
        with pytest.raises(BadArguments):
            api.validate_call([1, 2])
        api.validate_call("test")

        count = 3
        with pytest.raises(BadArguments):
            api.validate_call("test")
        api.validate_call([1])
        api.validate_call([1] * 10)

        count = 4
        with pytest.raises(BadArguments):
            api.validate_call([1])
        api.validate_call(5)
        api.validate_call(6)
예제 #13
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()
예제 #14
0
    def test_arg_from_bad_info(self):
        with pytest.raises(BadArguments):
            KeywordParameter.from_info({})

        with pytest.raises(BadArguments):
            ArgParameter.from_info({})
예제 #15
0
 def test_arg_parameter_from_info(self):
     obj = {"name": "a", "description": "b", "schema": {"type": "object"}}
     assert ArgParameter.from_info(obj).info == obj
예제 #16
0
 def test_bad_type(self):
     with pytest.raises(ValueError):
         ArgParameter("", "", dict)
예제 #17
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 = {}