You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/07/31 02:34:04 UTC

[GitHub] [incubator-apisix] membphis commented on a change in pull request #1947: add signature plugin

membphis commented on a change in pull request #1947:
URL: https://github.com/apache/incubator-apisix/pull/1947#discussion_r463373607



##########
File path: apisix/plugins/signature.lua
##########
@@ -0,0 +1,214 @@
+--
+-- 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.
+--
+local core     = require("apisix.core")
+local redis_new = require("resty.redis").new
+local ngx      = ngx
+local md5      = ngx.md5
+local encode_args = ngx.encode_args
+local tonumber = tonumber
+local plugin_name = "signature"
+
+local schema = {
+    type = "object",
+    properties = {
+        appkey = {type = "string",minLength = 5,maxLength = 32,pattern = [[^[a-zA-Z0-9_-]{5,32}$]]},
+        secret = {type = "string",minLength = 1},
+        algorithm = {
+            type = "string",
+            enum = {"md5"},
+            default = "md5"
+        },
+        timeout = {type = "integer", minimum = 10, default = 10},
+        anti_reply = {
+            type = "boolean",
+            default = true
+        },
+        policy = {
+            type = "string",
+            enum = {"redis"},
+            default = "redis"
+        },
+        redis_host = {
+            type = "string", minLength = 2, default = "127.0.0.1"
+        },
+        redis_port = {
+            type = "integer", minimum = 1, default = 6379
+        },
+        redis_password = {
+            type = "string", minLength = 0, default=""
+        },
+        redis_timeout = {
+            type = "integer", minimum = 1
+        },
+        redis_keepalive = {
+            type = "integer", minimum = 10
+        },
+        redis_poolsize = {
+            type = "integer", minimum = 100
+        },
+    },
+    required = {"appkey", "secret", "timeout", "algorithm"}
+}
+
+local _M = {
+    version = 0.1,
+    priority = 2513,
+    type = 'auth',
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return false, err
+    end
+
+    if not conf.algorithm then
+        conf.algorithm = "md5"
+    end
+
+    if not conf.timeout then
+        conf.timeout = 10
+    end
+
+    if conf.policy == "redis" then
+        if not conf.redis_host then
+            return false, "missing valid redis option host"
+        end
+
+        conf.redis_port = conf.redis_port or 6379

Review comment:
       ditto

##########
File path: apisix/plugins/signature.lua
##########
@@ -0,0 +1,214 @@
+--
+-- 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.
+--
+local core     = require("apisix.core")
+local redis_new = require("resty.redis").new
+local ngx      = ngx
+local md5      = ngx.md5
+local encode_args = ngx.encode_args
+local tonumber = tonumber
+local plugin_name = "signature"
+
+local schema = {
+    type = "object",
+    properties = {
+        appkey = {type = "string",minLength = 5,maxLength = 32,pattern = [[^[a-zA-Z0-9_-]{5,32}$]]},
+        secret = {type = "string",minLength = 1},
+        algorithm = {
+            type = "string",
+            enum = {"md5"},
+            default = "md5"
+        },
+        timeout = {type = "integer", minimum = 10, default = 10},
+        anti_reply = {
+            type = "boolean",
+            default = true
+        },
+        policy = {
+            type = "string",
+            enum = {"redis"},
+            default = "redis"
+        },
+        redis_host = {
+            type = "string", minLength = 2, default = "127.0.0.1"
+        },
+        redis_port = {
+            type = "integer", minimum = 1, default = 6379
+        },
+        redis_password = {
+            type = "string", minLength = 0, default=""
+        },
+        redis_timeout = {
+            type = "integer", minimum = 1
+        },
+        redis_keepalive = {
+            type = "integer", minimum = 10
+        },
+        redis_poolsize = {
+            type = "integer", minimum = 100
+        },
+    },
+    required = {"appkey", "secret", "timeout", "algorithm"}
+}
+
+local _M = {
+    version = 0.1,
+    priority = 2513,
+    type = 'auth',
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return false, err
+    end
+
+    if not conf.algorithm then
+        conf.algorithm = "md5"

Review comment:
       we can use `default` in schema

##########
File path: apisix/plugins/signature.lua
##########
@@ -0,0 +1,214 @@
+--
+-- 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.
+--
+local core     = require("apisix.core")
+local redis_new = require("resty.redis").new
+local ngx      = ngx
+local md5      = ngx.md5
+local encode_args = ngx.encode_args
+local tonumber = tonumber
+local plugin_name = "signature"
+
+local schema = {
+    type = "object",
+    properties = {
+        appkey = {type = "string",minLength = 5,maxLength = 32,pattern = [[^[a-zA-Z0-9_-]{5,32}$]]},
+        secret = {type = "string",minLength = 1},
+        algorithm = {
+            type = "string",
+            enum = {"md5"},
+            default = "md5"
+        },
+        timeout = {type = "integer", minimum = 10, default = 10},
+        anti_reply = {
+            type = "boolean",
+            default = true
+        },
+        policy = {
+            type = "string",
+            enum = {"redis"},
+            default = "redis"
+        },
+        redis_host = {
+            type = "string", minLength = 2, default = "127.0.0.1"
+        },
+        redis_port = {
+            type = "integer", minimum = 1, default = 6379
+        },
+        redis_password = {
+            type = "string", minLength = 0, default=""
+        },
+        redis_timeout = {
+            type = "integer", minimum = 1
+        },
+        redis_keepalive = {
+            type = "integer", minimum = 10
+        },
+        redis_poolsize = {
+            type = "integer", minimum = 100
+        },
+    },
+    required = {"appkey", "secret", "timeout", "algorithm"}
+}
+
+local _M = {
+    version = 0.1,
+    priority = 2513,
+    type = 'auth',
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return false, err
+    end
+
+    if not conf.algorithm then
+        conf.algorithm = "md5"
+    end
+
+    if not conf.timeout then
+        conf.timeout = 10

Review comment:
       ditto

##########
File path: doc/plugins/signature.md
##########
@@ -0,0 +1,144 @@
+<!--
+#
+# 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.
+#
+-->
+
+[Chinese](../zh-cn/plugins/signature.md)
+
+# signature
+
+Plugin Signature definition is similar [Wechat Pay Signature Specification](https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3),it's better to verify request is valid or not 
+in p2p communication scenario.
+
+## Attributes
+
+|Name            |Requirement |Description|
+|----------------|------------|-----------|
+|appkey          |required    |application key|
+|secret          |required    |application secret|
+|algorithm       |required    |signature algorithm, only support md5 now|
+|timeout         |required    |request duration timeout,default 10s|
+|anti_reply      |optional    |anti reply attack, default false|
+|policy          |optional    |anti_reply policies to use for retrieving request is a reply attack, only support `redis` now|
+|redis_host      |optional    |when using the `redis` policy, this property specifies the address of the Redis server|
+|redis_port      |optional    |when using the `redis` policy, this property specifies the port of the Redis server. The default port is 6379|
+|redis_password  |optional    |when using the `redis` policy, this property specifies the password of the Redis server|
+|redis_timeout   |optional    |when using the `redis` policy, this property specifies the timeout in milliseconds of any command submitted to the Redis server. The default timeout is 1000 ms(1 second).|
+|redis_keepalive |optional    |when using the `redis` policy, this property specifies redis connection cached time (keepalive time). default 10s|
+|redis_poolsize  |optional    |when using the `redis` policy, this property specifies the pool size of redis connections. default size 100|
+
+## signature specification
+Required Keys in Request Header:
+
+|Name        |Requirement  |Description|
+|------------|-------------|-----------|
+|Appkey      |required     |application Key|
+|Timestamp   |required     |request timestamp|
+|Sign        |required     |sign|
+|Nonce       | required    |request nonce|
+
+Signature formula:
+
+```
+sign = md5(encode_args(ngx.req.get_uri_args()) .. (ngx.req.get_body_data() or "") .. secret .. timestamp .. nonce)
+```
+
+#### Enable Plugin
+
+Here's an example, enable the `signature` plugin on the specified route:
+
+```shell
+curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "uri": "/api/verify",
+    "plugins": {
+        "signature": {
+        	"appkey":"your_appkey",

Review comment:
       bad indentation




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org