You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2019/11/01 01:49:15 UTC
[incubator-apisix] branch master updated: change: updated
resty-radixtree to v1.5. (#768)
This is an automated email from the ASF dual-hosted git repository.
membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git
The following commit(s) were added to refs/heads/master by this push:
new ad376f8 change: updated resty-radixtree to v1.5. (#768)
ad376f8 is described below
commit ad376f83f894a2fe8234c38e9cb7b84ad0733f60
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Fri Nov 1 09:49:09 2019 +0800
change: updated resty-radixtree to v1.5. (#768)
* feature: supported userdefine lua function in router `radixtree_uri`.
* feature: supported userdefine lua function in router `radixtree_host_uri`.
---
doc/admin-api-cn.md | 33 ++++++++++-
doc/stream-proxy.md | 2 +-
lua/apisix/admin/routes.lua | 13 +++++
lua/apisix/http/router/radixtree_host_uri.lua | 32 +++++++++++
lua/apisix/http/router/radixtree_uri.lua | 18 ++++++
lua/apisix/schema_def.lua | 19 +++++--
rockspec/apisix-master-0.rockspec | 2 +-
t/admin/routes.t | 72 ++++++++++++++++++++++++
t/node/route-filter-func.t | 80 +++++++++++++++++++++++++++
t/router/radixtree-uri-host.t | 55 ++++++++++++++++++
10 files changed, 318 insertions(+), 8 deletions(-)
diff --git a/doc/admin-api-cn.md b/doc/admin-api-cn.md
index 676eba7..e795cfa 100644
--- a/doc/admin-api-cn.md
+++ b/doc/admin-api-cn.md
@@ -51,7 +51,8 @@
|remote_addr|可选 |匹配规则|客户端请求 IP 地址: `192.168.1.101`、`192.168.1.102` 以及 CIDR 格式的支持 `192.168.1.0/24`。特别的,APISIX 也完整支持 IPv6 地址匹配:`::1`,`fe80::1`, `fe80::1/64` 等。|"192.168.1.0/24"|
|remote_addrs|可选 |匹配规则|列表形态的 `remote_addr`,表示允许有多个不同 IP 地址,符合其中任意一个即可。|{"127.0.0.1", "192.0.0.0/8", "::1"}|
|methods |可选 |匹配规则|如果为空或没有该选项,代表没有任何 `method` 限制,也可以是一个或多个的组合:`GET`, `POST`, `PUT`, `DELETE`, `PATCH`, `HEAD`, `OPTIONS`,`CONNECT`,`TRACE`。|{"GET", "POST"}|
-|vars |可选 |匹配规则(仅支持 `radixtree` 路由)|由一个或多个`{var, operator, val}`元素组成的列表,类似这样:`{{var, operator, val}, {var, operator, val}, ...}`。例如:`{"arg_name", "==", "json"}`,表示当前请求参数 `name` 是 `json`。这里的 `var` 与 NGINX 内部自身变量命名是保持一致,所以也可以使用 `request_uri`、`host` 等;对于 `operator` 部分,目前已支持的运算符有 `==`、`~=`、`>`和`<`,特别的对于后面两个运算符,会把结果先转换成 number 然后再做比较。|{{"arg_name", "==", "json"}, {"arg_age", ">", 18}}|
+|vars |可选 |匹配规则(仅支持 `radixtree` 路由)|由一个或多个`{var, operator, val}`元素组成的列表,类似这样:`{{var, operator, val}, {var, operator, val}, ...}`。例如:`{"arg_name", "==", "json"}`,表示当前请求参数 `name` 是 `json`。这里的 `var` 与 Nginx 内部自身变量命名是保持一致,所以也可以使用 `request_uri`、`host` 等;对于 `operator` 部分,目前已支持的运算符有 `==`、`~=`、`>`、`<` 和 `~~`。对于`>`和`<`两个运算符,会把结果先转换成 number 然后再做比较。查看支持的[运算符列表](#运算符列表)|{{"arg_name", "==", "json"}, {"arg_age", ">", 18}}|
+|filter_func|可选|匹配规则|用户自定义的过滤函数。可以使用它来实现特殊场景的匹配要求实现。该函数默认接受一个名为 vars 的输入参数,可以用它来获取 Nginx 变量。|function(vars) return vars["arg_name"] == "json" end|
|plugins |可选 |Plugin|详见 [Plugin](architecture-design-cn.md#plugin) ||
|upstream |可选 |Upstream|启用的 Upstream 配置,详见 [Upstream](architecture-design-cn.md#upstream)||
|upstream_id|可选 |Upstream|启用的 upstream id,详见 [Upstream](architecture-design-cn.md#upstream)||
@@ -86,5 +87,35 @@ Date: Sat, 31 Aug 2019 01:17:15 GMT
目前是直接返回与 etcd 交互后的结果。
+### 运算符列表
+
+|运算符 |描述 |例子|
+|--------|-----------|-------|
+|== |相等 |{"arg_name", "==", "json"}|
+|~= |不等于 |{"arg_name", "~=", "json"}|
+|> |大于 |{"arg_age", ">", 24}|
+|< |小于 |{"arg_age", "<", 24}|
+|~~ |正则匹配 |{"arg_name", "~~", "[a-z]+"}|
+
+请看下面例子,匹配请求参数 name 等于 json ,age 大于 18 且 address 开头是 China 的请求:
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -X PUT -i -d '
+{
+ "uri": "/index.html",
+ "vars": [
+ ["arg_name", "==", "json"],
+ ["arg_age", ">", "18"],
+ ["arg_address", "~~", "China.*"]
+ ],
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "39.97.63.215:80": 1
+ }
+ }
+}'
+```
+
[Back to TOC](#目录)
diff --git a/doc/stream-proxy.md b/doc/stream-proxy.md
index b18c7e6..980098b 100644
--- a/doc/stream-proxy.md
+++ b/doc/stream-proxy.md
@@ -23,7 +23,7 @@
TCP is the protocol for many popular applications and services, such as LDAP, MySQL, and RTMP. UDP (User Datagram Protocol) is the protocol for many popular non-transactional applications, such as DNS, syslog, and RADIUS.
-APISIX can dynamic load balancing TCP/UDP proxy. In NGINX world, we call TCP/UDP proxy to stream proxy, we followed this statement.
+APISIX can dynamic load balancing TCP/UDP proxy. In Nginx world, we call TCP/UDP proxy to stream proxy, we followed this statement.
## How to enable Stream proxy?
diff --git a/lua/apisix/admin/routes.lua b/lua/apisix/admin/routes.lua
index ff88b10..e27e33a 100644
--- a/lua/apisix/admin/routes.lua
+++ b/lua/apisix/admin/routes.lua
@@ -18,6 +18,7 @@ local core = require("apisix.core")
local schema_plugin = require("apisix.admin.plugins").check_schema
local tostring = tostring
local type = type
+local loadstring = loadstring
local _M = {
@@ -100,6 +101,18 @@ local function check_conf(id, conf, need_id)
end
end
+ if conf.filter_func then
+ local func, err = loadstring("return " .. conf.filter_func)
+ if not func then
+ return nil, {error_msg = "failed to load 'filter_func' string: "
+ .. err}
+ end
+
+ if type(func()) ~= "function" then
+ return nil, {error_msg = "'filter_func' should be a function"}
+ end
+ end
+
return need_id and id or true
end
diff --git a/lua/apisix/http/router/radixtree_host_uri.lua b/lua/apisix/http/router/radixtree_host_uri.lua
index 356533a..a4ebc43 100644
--- a/lua/apisix/http/router/radixtree_host_uri.lua
+++ b/lua/apisix/http/router/radixtree_host_uri.lua
@@ -21,6 +21,7 @@ local plugin = require("apisix.plugin")
local ipairs = ipairs
local type = type
local error = error
+local loadstring = loadstring
local user_routes
local cached_version
local only_uri_routes = {}
@@ -34,11 +35,27 @@ local _M = {version = 0.1}
local function add_host_uri_routes(path, host, route)
core.log.info("add host+uri route, host: ", host, " path: ", path)
+
+ local filter_fun, err
+ if route.value.filter_func then
+ filter_fun, err = loadstring(
+ "return " .. route.value.filter_func,
+ "router#" .. route.value.id)
+ if not filter_fun then
+ core.log.error("failed to load filter function: ", err,
+ " route id: ", route.value.id)
+ return
+ end
+
+ filter_fun = filter_fun()
+ end
+
core.table.insert(host_uri_routes, {
paths = {host .. path},
methods = route.value.methods,
remote_addrs = route.value.remote_addrs or route.value.remote_addr,
vars = route.value.vars,
+ filter_fun = filter_fun,
handler = function (api_ctx)
api_ctx.matched_params = nil
api_ctx.matched_route = route
@@ -81,12 +98,27 @@ local function push_radixtree_host_router(route)
hosts_wildcard = nil
end
+ local filter_fun, err
+ if route.value.filter_func then
+ filter_fun, err = loadstring(
+ "return " .. route.value.filter_func,
+ "router#" .. route.value.id)
+ if not filter_fun then
+ core.log.error("failed to load filter function: ", err,
+ " route id: ", route.value.id)
+ return
+ end
+
+ filter_fun = filter_fun()
+ end
+
core.table.insert(only_uri_routes, {
paths = uris,
method = route.value.methods,
hosts = hosts_wildcard,
remote_addrs = route.value.remote_addrs or route.value.remote_addr,
vars = route.value.vars,
+ filter_fun = filter_fun,
handler = function (api_ctx)
api_ctx.matched_params = nil
api_ctx.matched_route = route
diff --git a/lua/apisix/http/router/radixtree_uri.lua b/lua/apisix/http/router/radixtree_uri.lua
index 2d77d11..6b364b5 100644
--- a/lua/apisix/http/router/radixtree_uri.lua
+++ b/lua/apisix/http/router/radixtree_uri.lua
@@ -21,6 +21,7 @@ local plugin = require("apisix.plugin")
local ipairs = ipairs
local type = type
local error = error
+local loadstring = loadstring
local user_routes
local cached_version
@@ -48,6 +49,20 @@ local function create_radixtree_router(routes)
for _, route in ipairs(routes) do
if type(route) == "table" then
+ local filter_fun, err
+ if route.value.filter_func then
+ filter_fun, err = loadstring(
+ "return " .. route.value.filter_func,
+ "router#" .. route.value.id)
+ if not filter_fun then
+ core.log.error("failed to load filter function: ", err,
+ " route id: ", route.value.id)
+ goto CONTINUE
+ end
+
+ filter_fun = filter_fun()
+ end
+
core.table.insert(uri_routes, {
paths = route.value.uris or route.value.uri,
methods = route.value.methods,
@@ -55,11 +70,14 @@ local function create_radixtree_router(routes)
remote_addrs = route.value.remote_addrs
or route.value.remote_addr,
vars = route.value.vars,
+ filter_fun = filter_fun,
handler = function (api_ctx)
api_ctx.matched_params = nil
api_ctx.matched_route = route
end
})
+
+ ::CONTINUE::
end
end
diff --git a/lua/apisix/schema_def.lua b/lua/apisix/schema_def.lua
index 92bd129..6784ced 100644
--- a/lua/apisix/schema_def.lua
+++ b/lua/apisix/schema_def.lua
@@ -16,9 +16,9 @@
--
local schema = require('apisix.core.schema')
local setmetatable = setmetatable
+local error = error
-local _M = {version = 0.3}
-setmetatable(_M, {__index = schema})
+local _M = {version = 0.4}
local plugins_schema = {
@@ -262,8 +262,7 @@ local upstream_schema = {
}
-
-local route = {
+_M.route = {
type = "object",
properties = {
uri = {type = "string", minLength = 1, maxLength = 4096},
@@ -306,6 +305,11 @@ local route = {
}
}
},
+ filter_func = {
+ type = "string",
+ minLength = 10,
+ pattern = [[^function]],
+ },
plugins = plugins_schema,
upstream = upstream_schema,
@@ -325,7 +329,6 @@ local route = {
},
additionalProperties = false,
}
-_M.route = route
_M.service = {
@@ -425,4 +428,10 @@ _M.stream_route = {
}
+setmetatable(_M, {
+ __index = schema,
+ __newindex = function() error("no modification allowed") end,
+})
+
+
return _M
diff --git a/rockspec/apisix-master-0.rockspec b/rockspec/apisix-master-0.rockspec
index 120051e..198f9e3 100644
--- a/rockspec/apisix-master-0.rockspec
+++ b/rockspec/apisix-master-0.rockspec
@@ -41,7 +41,7 @@ dependencies = {
"lua-resty-cookie = 0.1.0",
"lua-resty-session = 2.24",
"opentracing-openresty = 0.1",
- "lua-resty-radixtree = 1.4",
+ "lua-resty-radixtree = 1.5",
"lua-protobuf = 0.3.1",
"lua-resty-openidc = 1.7.2-1",
"luafilesystem = 1.7.0-2",
diff --git a/t/admin/routes.t b/t/admin/routes.t
index e665a5f..78e26d8 100644
--- a/t/admin/routes.t
+++ b/t/admin/routes.t
@@ -1258,3 +1258,75 @@ GET /t
passed
--- no_error_log
[error]
+
+
+
+=== TEST 35: filter function
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [=[{
+ "uri": "/index.html",
+ "filter_func": "function(vars) return vars.arg_name == 'json' end",
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:8080": 1
+ },
+ "type": "roundrobin"
+ }
+ }]=],
+ [=[{
+ "node": {
+ "value": {
+ "filter_func": "function(vars) return vars.arg_name == 'json' end"
+ }
+ }
+ }]=]
+ )
+
+ ngx.status = code
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 36: filter function (invalid)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [=[{
+ "uri": "/index.html",
+ "filter_func": "function(vars) ",
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:8080": 1
+ },
+ "type": "roundrobin"
+ }
+ }]=]
+ )
+
+ ngx.status = code
+ ngx.print(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 400
+--- response_body
+{"error_msg":"failed to load 'filter_func' string: [string \"return function(vars) \"]:1: 'end' expected near '<eof>'"}
+--- no_error_log
+[error]
diff --git a/t/node/route-filter-func.t b/t/node/route-filter-func.t
new file mode 100644
index 0000000..2bb3907
--- /dev/null
+++ b/t/node/route-filter-func.t
@@ -0,0 +1,80 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+worker_connections(256);
+no_root_location();
+no_shuffle();
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: set route(id: 1)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/hello",
+ "filter_func": "function(vars) return vars.arg_name == 'json' end",
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: not hit: name=unkown
+--- request
+GET /hello?name=unkown
+--- error_code: 404
+--- response_body eval
+qr/404 Not Found/
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: hit routes
+--- request
+GET /hello?name=json
+--- response_body
+hello world
+--- no_error_log
+[error]
diff --git a/t/router/radixtree-uri-host.t b/t/router/radixtree-uri-host.t
index 228824b..f9e1a7b 100644
--- a/t/router/radixtree-uri-host.t
+++ b/t/router/radixtree-uri-host.t
@@ -223,3 +223,58 @@ Host: foo.com
hello world
--- no_error_log
[error]
+
+
+
+=== TEST 12: set route(id: 1)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/hello",
+ "filter_func": "function(vars) return vars.arg_name == 'json' end",
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 13: not hit: name=unkown
+--- request
+GET /hello?name=unkown
+--- error_code: 404
+--- response_body eval
+qr/404 Not Found/
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: hit routes
+--- request
+GET /hello?name=json
+--- response_body
+hello world
+--- no_error_log
+[error]