spacewander commented on a change in pull request #9:
URL: 
https://github.com/apache/apisix-python-plugin-runner/pull/9#discussion_r686417317



##########
File path: .gitignore
##########
@@ -1,3 +1,14 @@
 .idea
 .vscode
 .DS_Store
+.pytest_cache/
+apisix/__pycache__/

Review comment:
       We can simply ignore __pycache__/?

##########
File path: apisix/runner/http/request.py
##########
@@ -0,0 +1,224 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import json
+import apisix.runner.plugin.core as RunnerPlugin
+import apisix.runner.http.method as RunnerMethod
+from apisix.runner.http.protocol import RPC_HTTP_REQ_CALL
+from apisix.runner.http.protocol import RPC_PREPARE_CONF
+from a6pluginproto.HTTPReqCall import Req as A6HTTPReqCallReq
+from a6pluginproto.PrepareConf import Req as A6PrepareConfReq
+
+
+class Request:
+
+    def __init__(self, ty: int = 0, buf: bytes = b''):
+        """
+        Init and parse request
+        :param ty:
+            rpc request protocol type
+        :param buf:
+            rpc request buffer data
+        """
+        self._rpc_type = ty
+        self._rpc_buf = buf
+        self._req_id = 0
+        self._req_conf_token = 0
+        self._req_method = ""
+        self._req_path = ""
+        self._req_headers = {}
+        self._req_configs = {}
+        self._req_args = {}
+        self._req_src_ip = ""
+        self._init()
+
+    @property
+    def rpc_type(self) -> int:
+        return self._rpc_type
+
+    @rpc_type.setter
+    def rpc_type(self, rpc_type: int) -> None:
+        self._rpc_type = rpc_type
+
+    @property
+    def rpc_buf(self) -> bytes:
+        return self._rpc_buf
+
+    @rpc_buf.setter
+    def rpc_buf(self, rpc_buf: bytes) -> None:
+        self._rpc_buf = rpc_buf
+
+    @property
+    def conf_token(self) -> int:
+        return self._req_conf_token
+
+    @conf_token.setter
+    def conf_token(self, req_conf_token: int) -> None:
+        self._req_conf_token = req_conf_token
+
+    @property
+    def id(self) -> int:
+        return self._req_id
+
+    @id.setter
+    def id(self, req_id: int) -> None:
+        self._req_id = req_id
+
+    @property
+    def method(self) -> str:
+        return self._req_method
+
+    @method.setter
+    def method(self, req_method: str) -> None:
+        self._req_method = req_method
+
+    @property
+    def path(self) -> str:
+        return self._req_path
+
+    @path.setter
+    def path(self, req_path: str) -> None:
+        self._req_path = req_path
+
+    @property
+    def headers(self) -> dict:
+        return self._req_headers
+
+    @headers.setter
+    def headers(self, req_headers: dict) -> None:
+        self._req_headers = req_headers
+
+    @property
+    def configs(self) -> dict:
+        return self._req_configs
+
+    @configs.setter
+    def configs(self, req_configs: dict) -> None:
+        self._req_configs = req_configs
+
+    @property
+    def args(self) -> dict:
+        return self._req_args
+
+    @args.setter
+    def args(self, req_args: dict) -> None:
+        self._req_args = req_args
+
+    @property
+    def src_ip(self) -> str:
+        return self._req_src_ip
+
+    @src_ip.setter
+    def src_ip(self, req_src_ip: str) -> None:
+        self._req_src_ip = req_src_ip
+
+    def reset(self) -> None:
+        """
+        reset request class
+        :return:
+        """
+        self._rpc_type = 0
+        self._rpc_buf = b''
+        self._req_id = 0
+        self._req_conf_token = 0
+        self._req_method = ""
+        self._req_path = ""
+        self._req_headers = {}
+        self._req_configs = {}
+        self._req_args = {}
+        self._req_src_ip = ""
+
+    def _parse_src_ip(self, req: A6HTTPReqCallReq) -> None:
+        """
+        parse request source ip address
+        :param req:
+        :return:
+        """
+        if not req.SrcIpIsNone():
+            delimiter = "."
+            if req.SrcIpLength() > 4:
+                delimiter = ":"
+            ipAddress = []
+            for i in range(req.SrcIpLength()):
+                ipAddress.append(str(req.SrcIp(i)))
+            self.src_ip = delimiter.join(ipAddress)

Review comment:
       IPv6 isn't `127:127:127...`. It would be better to call a 
battlefield-test library to do the job for you.

##########
File path: apisix/runner/http/request.py
##########
@@ -0,0 +1,224 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import json
+import apisix.runner.plugin.core as RunnerPlugin
+import apisix.runner.http.method as RunnerMethod
+from apisix.runner.http.protocol import RPC_HTTP_REQ_CALL
+from apisix.runner.http.protocol import RPC_PREPARE_CONF
+from a6pluginproto.HTTPReqCall import Req as A6HTTPReqCallReq
+from a6pluginproto.PrepareConf import Req as A6PrepareConfReq
+
+
+class Request:
+
+    def __init__(self, ty: int = 0, buf: bytes = b''):
+        """
+        Init and parse request
+        :param ty:
+            rpc request protocol type
+        :param buf:
+            rpc request buffer data
+        """
+        self._rpc_type = ty
+        self._rpc_buf = buf
+        self._req_id = 0
+        self._req_conf_token = 0
+        self._req_method = ""
+        self._req_path = ""
+        self._req_headers = {}
+        self._req_configs = {}
+        self._req_args = {}
+        self._req_src_ip = ""
+        self._init()
+
+    @property
+    def rpc_type(self) -> int:
+        return self._rpc_type
+
+    @rpc_type.setter
+    def rpc_type(self, rpc_type: int) -> None:
+        self._rpc_type = rpc_type
+
+    @property
+    def rpc_buf(self) -> bytes:
+        return self._rpc_buf
+
+    @rpc_buf.setter
+    def rpc_buf(self, rpc_buf: bytes) -> None:
+        self._rpc_buf = rpc_buf
+
+    @property
+    def conf_token(self) -> int:
+        return self._req_conf_token
+
+    @conf_token.setter
+    def conf_token(self, req_conf_token: int) -> None:
+        self._req_conf_token = req_conf_token
+
+    @property
+    def id(self) -> int:
+        return self._req_id
+
+    @id.setter
+    def id(self, req_id: int) -> None:

Review comment:
       Do we need to change the id after creating the request?

##########
File path: apisix/runner/http/request.py
##########
@@ -0,0 +1,224 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import json
+import apisix.runner.plugin.core as RunnerPlugin
+import apisix.runner.http.method as RunnerMethod
+from apisix.runner.http.protocol import RPC_HTTP_REQ_CALL
+from apisix.runner.http.protocol import RPC_PREPARE_CONF
+from a6pluginproto.HTTPReqCall import Req as A6HTTPReqCallReq
+from a6pluginproto.PrepareConf import Req as A6PrepareConfReq
+
+
+class Request:
+
+    def __init__(self, ty: int = 0, buf: bytes = b''):
+        """
+        Init and parse request
+        :param ty:
+            rpc request protocol type
+        :param buf:
+            rpc request buffer data
+        """
+        self._rpc_type = ty
+        self._rpc_buf = buf
+        self._req_id = 0
+        self._req_conf_token = 0
+        self._req_method = ""
+        self._req_path = ""
+        self._req_headers = {}
+        self._req_configs = {}
+        self._req_args = {}
+        self._req_src_ip = ""
+        self._init()
+
+    @property
+    def rpc_type(self) -> int:
+        return self._rpc_type
+
+    @rpc_type.setter
+    def rpc_type(self, rpc_type: int) -> None:
+        self._rpc_type = rpc_type
+
+    @property
+    def rpc_buf(self) -> bytes:
+        return self._rpc_buf
+
+    @rpc_buf.setter
+    def rpc_buf(self, rpc_buf: bytes) -> None:
+        self._rpc_buf = rpc_buf
+
+    @property
+    def conf_token(self) -> int:
+        return self._req_conf_token
+
+    @conf_token.setter
+    def conf_token(self, req_conf_token: int) -> None:
+        self._req_conf_token = req_conf_token
+
+    @property
+    def id(self) -> int:
+        return self._req_id
+
+    @id.setter
+    def id(self, req_id: int) -> None:
+        self._req_id = req_id
+
+    @property
+    def method(self) -> str:

Review comment:
       Need to provide docstring to exposed API

##########
File path: apisix/runner/server/handle.py
##########
@@ -0,0 +1,137 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from __future__ import annotations
+from a6pluginproto.Err import Code as A6ErrCode
+import apisix.runner.plugin.core as RunnerPlugin
+import apisix.runner.plugin.cache as RunnerCache
+from apisix.runner.http.response import Response as NewHttpResponse
+from apisix.runner.http.response import RESP_MAX_DATA_SIZE
+from apisix.runner.http.request import Request as NewHttpRequest
+from apisix.runner.server.response import Response as NewServerResponse
+from apisix.runner.server.response import RUNNER_SUCCESS_CODE
+from apisix.runner.server.response import RUNNER_SUCCESS_MESSAGE
+from apisix.runner.http.protocol import RPC_PREPARE_CONF
+from apisix.runner.http.protocol import RPC_HTTP_REQ_CALL
+from apisix.runner.http.protocol import RPC_UNKNOWN
+
+
+class Handle:
+
+    def __init__(self, ty: int = 0, buf: bytes = b'', debug: bool = False):
+        """
+        Init Python runner server
+        :param ty:
+            rpc request protocol type
+        :param buf:
+            rpc request buffer data
+        :param debug:
+            enable debug mode
+        """
+        self.type = ty
+        self.buffer = buf
+        self.debug = debug
+
+    @property
+    def type(self) -> int:
+        return self._type
+
+    @type.setter
+    def type(self, ty: int = 0) -> None:
+        self._type = ty
+
+    @property
+    def buffer(self) -> bytes:
+        return self._buffer
+
+    @buffer.setter
+    def buffer(self, buf: bytes = b'') -> None:
+        self._buffer = buf
+
+    @property
+    def debug(self) -> bool:
+        return self._debug
+
+    @debug.setter
+    def debug(self, debug: bool = False) -> None:
+        self._debug = debug
+
+    def _rpc_config(self) -> NewServerResponse:
+        # init request
+        req = NewHttpRequest(RPC_PREPARE_CONF, self.buffer)
+        # generate token
+        token = RunnerCache.generate_token()
+        # get plugins config
+        configs = req.configs
+        # cache plugins config
+        ok = RunnerCache.set_config_by_token(token, configs)
+        if not ok:
+            return NewServerResponse(code=A6ErrCode.Code.SERVICE_UNAVAILABLE, 
message="cache token failure")
+        # init response
+        resp = NewHttpResponse(RPC_PREPARE_CONF)
+        resp.token = token
+        response = resp.flatbuffers()
+
+        return NewServerResponse(code=RUNNER_SUCCESS_CODE, 
message=RUNNER_SUCCESS_MESSAGE, data=response.Output(),
+                                 ty=self.type)
+
+    def _rpc_call(self) -> NewServerResponse:
+        # init request
+        req = NewHttpRequest(RPC_HTTP_REQ_CALL, self.buffer)
+        # get request token
+        token = req.conf_token
+        # get plugins
+        configs = RunnerCache.get_config_by_token(token)
+        if len(configs) == 0:
+            return NewServerResponse(code=A6ErrCode.Code.CONF_TOKEN_NOT_FOUND, 
message="cache token not found")
+        # init response
+        resp = NewHttpResponse(RPC_HTTP_REQ_CALL)
+        # execute plugins
+        RunnerPlugin.filter(configs, req, resp)
+
+        response = resp.flatbuffers()
+        return NewServerResponse(code=RUNNER_SUCCESS_CODE, 
message=RUNNER_SUCCESS_MESSAGE, data=response.Output(),
+                                 ty=self.type)
+
+    @staticmethod
+    def _rpc_unknown(err_code: int = 0) -> NewServerResponse:
+        resp = NewHttpResponse(RPC_UNKNOWN)
+        resp.error_code = err_code
+        response = resp.flatbuffers()
+        return NewServerResponse(code=RUNNER_SUCCESS_CODE, message="OK", 
data=response.Output(),
+                                 ty=RPC_UNKNOWN)
+
+    def dispatch(self) -> NewServerResponse:
+        err = None

Review comment:
       Should be `resp`?

##########
File path: apisix/runner/server/server.py
##########
@@ -0,0 +1,70 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import os
+import socket
+from threading import Thread as NewThread
+from apisix.runner.server.handle import Handle as NewServerHandle
+from apisix.runner.server.protocol import Protocol as NewServerProtocol
+
+
+def _threaded(conn: socket, debug: bool):
+    while True:
+        buffer = conn.recv(4)
+        protocol = NewServerProtocol(buffer, 0)
+        err = protocol.decode()
+        if err.code != 200:
+            print(err.message)
+            break
+
+        buffer = conn.recv(protocol.length)
+        handler = NewServerHandle(protocol.type, buffer)
+        response = handler.dispatch()
+
+        protocol = NewServerProtocol(response.data, response.type)
+        protocol.encode()
+        response = protocol.buffer
+
+        err = conn.sendall(response)
+        if err:
+            print(err)
+        break
+
+    conn.close()
+
+
+class Server:
+    def __init__(self, fd: str, debug: bool = False):
+        self.fd = fd
+        self.debug = debug
+        if os.path.exists(fd):
+            os.remove(fd)
+        self.socket_address = fd
+        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

Review comment:
       Need to confirm every process can write to this socket, so that the 
nobody workers can talk with the socket created by other users.

##########
File path: apisix/runner/server/handle.py
##########
@@ -0,0 +1,137 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from __future__ import annotations
+from a6pluginproto.Err import Code as A6ErrCode
+import apisix.runner.plugin.core as RunnerPlugin
+import apisix.runner.plugin.cache as RunnerCache
+from apisix.runner.http.response import Response as NewHttpResponse
+from apisix.runner.http.response import RESP_MAX_DATA_SIZE
+from apisix.runner.http.request import Request as NewHttpRequest
+from apisix.runner.server.response import Response as NewServerResponse
+from apisix.runner.server.response import RUNNER_SUCCESS_CODE
+from apisix.runner.server.response import RUNNER_SUCCESS_MESSAGE
+from apisix.runner.http.protocol import RPC_PREPARE_CONF
+from apisix.runner.http.protocol import RPC_HTTP_REQ_CALL
+from apisix.runner.http.protocol import RPC_UNKNOWN
+
+
+class Handle:
+
+    def __init__(self, ty: int = 0, buf: bytes = b'', debug: bool = False):
+        """
+        Init Python runner server
+        :param ty:
+            rpc request protocol type
+        :param buf:
+            rpc request buffer data
+        :param debug:
+            enable debug mode
+        """
+        self.type = ty
+        self.buffer = buf
+        self.debug = debug
+
+    @property
+    def type(self) -> int:
+        return self._type
+
+    @type.setter
+    def type(self, ty: int = 0) -> None:
+        self._type = ty
+
+    @property
+    def buffer(self) -> bytes:
+        return self._buffer
+
+    @buffer.setter
+    def buffer(self, buf: bytes = b'') -> None:
+        self._buffer = buf
+
+    @property
+    def debug(self) -> bool:
+        return self._debug
+
+    @debug.setter
+    def debug(self, debug: bool = False) -> None:
+        self._debug = debug
+
+    def _rpc_config(self) -> NewServerResponse:
+        # init request
+        req = NewHttpRequest(RPC_PREPARE_CONF, self.buffer)
+        # generate token
+        token = RunnerCache.generate_token()
+        # get plugins config
+        configs = req.configs
+        # cache plugins config
+        ok = RunnerCache.set_config_by_token(token, configs)
+        if not ok:
+            return NewServerResponse(code=A6ErrCode.Code.SERVICE_UNAVAILABLE, 
message="cache token failure")
+        # init response
+        resp = NewHttpResponse(RPC_PREPARE_CONF)
+        resp.token = token
+        response = resp.flatbuffers()
+
+        return NewServerResponse(code=RUNNER_SUCCESS_CODE, 
message=RUNNER_SUCCESS_MESSAGE, data=response.Output(),
+                                 ty=self.type)
+
+    def _rpc_call(self) -> NewServerResponse:
+        # init request
+        req = NewHttpRequest(RPC_HTTP_REQ_CALL, self.buffer)
+        # get request token
+        token = req.conf_token
+        # get plugins
+        configs = RunnerCache.get_config_by_token(token)
+        if len(configs) == 0:
+            return NewServerResponse(code=A6ErrCode.Code.CONF_TOKEN_NOT_FOUND, 
message="cache token not found")
+        # init response
+        resp = NewHttpResponse(RPC_HTTP_REQ_CALL)
+        # execute plugins
+        RunnerPlugin.filter(configs, req, resp)
+
+        response = resp.flatbuffers()
+        return NewServerResponse(code=RUNNER_SUCCESS_CODE, 
message=RUNNER_SUCCESS_MESSAGE, data=response.Output(),
+                                 ty=self.type)
+
+    @staticmethod
+    def _rpc_unknown(err_code: int = 0) -> NewServerResponse:
+        resp = NewHttpResponse(RPC_UNKNOWN)
+        resp.error_code = err_code
+        response = resp.flatbuffers()
+        return NewServerResponse(code=RUNNER_SUCCESS_CODE, message="OK", 
data=response.Output(),
+                                 ty=RPC_UNKNOWN)
+
+    def dispatch(self) -> NewServerResponse:
+        err = None
+
+        if self.type == RPC_PREPARE_CONF:
+            err = self._rpc_config()
+
+        if self.type == RPC_HTTP_REQ_CALL:
+            err = self._rpc_call()
+
+        if not err:
+            return self._rpc_unknown()
+
+        size = len(err.data)
+        if (size > RESP_MAX_DATA_SIZE or size <= 0) and err.code == 200:

Review comment:
       Why err.code != 200 can bypass the max size check?

##########
File path: apisix/runner/plugin/core.py
##########
@@ -14,18 +14,32 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+import os
 import importlib
 from pkgutil import iter_modules
 
 
-def instances() -> dict:
-    modules = iter_modules(__import__("plugins").__path__)
+def filter(configs: dict, request, response) -> None:
+    for name in configs:
+        plugin = configs.get(name)
+        if not plugin:
+            print("plugin undefined.")
+            continue
+        try:
+            plugin.filter(request, response)
+        except Exception as e:
+            print(e)

Review comment:
       The error message should be more detailed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to