From 553384cc843248052d79cfc49fcfdd77575a46c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:52:03 +0800 Subject: [PATCH 01/16] fix: add check plugin's schema in sync data --- apisix/core/config_etcd.lua | 50 +++++++++++++++++++++++++++++++++++++ apisix/core/config_yaml.lua | 33 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua index fbb6df8dbf5d..b24dd976f6e0 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -49,6 +49,16 @@ local mt = { end } +local disable_schema = { + type = "object", + properties = { + disable = {type = "boolean", enum={true}} + }, + required = {"disable"} +} +local local_plugins +local stream_local_plugins + local function readdir(etcd_cli, key) if not etcd_cli then return nil, nil, "not inited" @@ -166,6 +176,39 @@ local function sync_data(self) ", it shoud be a object") end + -- check plugins schema + local plugins_conf = item.plugins + if plugins_conf then + for name, plugin_conf in pairs(plugins_conf) do + local plugin_obj = local_plugins[name] + if plugin_obj then + log.info("check plugin scheme, name: ", name, ", configurations: ", + json.delay_encode(plugin_conf, true)) + else + plugin_obj = stream_local_plugins[name] + if plugin_obj then + log.info("check stream plugin scheme, name: ", name, + ": ", json.delay_encode(plugin_conf, true)) + else + log.error("unknown plugin [" .. name .. "]") + return false + end + end + + if plugin_obj.check_schema then + local ok = check_schema(disable_schema, plugin_conf) + if not ok then + local ok, err = plugin_obj.check_schema(plugin_conf) + if not ok then + log.error("failed to check the configuration of plugin " + .. name .. " err: " .. err) + return false + end + end + end + end + end + if data_valid and self.item_schema then data_valid, err = check_schema(self.item_schema, item.value) if not data_valid then @@ -466,5 +509,12 @@ function _M.server_version(self) return read_etcd_version(self.etcd_cli) end +function _M.init_plugins(plugins, is_stream) + if is_stream then + stream_local_plugins = plugins + else + local_plugins = plugins + end +end return _M diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua index bb1cd250af2a..86f3c60c5d53 100644 --- a/apisix/core/config_yaml.lua +++ b/apisix/core/config_yaml.lua @@ -162,6 +162,39 @@ local function sync_data(self) ", it shoud be a object") end + -- check plugins schema + local plugins_conf = item.plugins + if plugins_conf then + for name, plugin_conf in pairs(plugins_conf) do + local plugin_obj = local_plugins[name] + if plugin_obj then + log.info("check plugin scheme, name: ", name, ", configurations: ", + json.delay_encode(plugin_conf, true)) + else + plugin_obj = stream_local_plugins[name] + if plugin_obj then + log.info("check stream plugin scheme, name: ", name, + ": ", json.delay_encode(plugin_conf, true)) + else + log.error("unknown plugin [" .. name .. "]") + return false + end + end + + if plugin_obj.check_schema then + local ok = check_schema(disable_schema, plugin_conf) + if not ok then + local ok, err = plugin_obj.check_schema(plugin_conf) + if not ok then + log.error("failed to check the configuration of plugin " + .. name .. " err: " .. err) + return false + end + end + end + end + end + local key = item.id or "arr_" .. i local conf_item = {value = item, modifiedIndex = apisix_yaml_ctime, key = "/" .. self.key .. "/" .. key} From 408ecb974289dac2097cd1c0d75e3b26ce9f071d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:56:35 +0800 Subject: [PATCH 02/16] fix: add check plugin's schema in sync data --- apisix/core/config_yaml.lua | 20 ++++++++++++++++++-- apisix/plugin.lua | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua index 86f3c60c5d53..32dd5db1bf59 100644 --- a/apisix/core/config_yaml.lua +++ b/apisix/core/config_yaml.lua @@ -55,9 +55,18 @@ local mt = { end } +local disable_schema = { + type = "object", + properties = { + disable = {type = "boolean", enum={true}} + }, + required = {"disable"} +} +local local_plugins +local stream_local_plugins - local apisix_yaml - local apisix_yaml_ctime +local apisix_yaml +local apisix_yaml_ctime local function read_apisix_yaml(premature, pre_mtime) if premature then return @@ -357,5 +366,12 @@ function _M.init_worker() ngx.timer.every(1, read_apisix_yaml) end +function _M.init_plugins(plugins, is_stream) + if is_stream then + stream_local_plugins = plugins + else + local_plugins = plugins + end +end return _M diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 2cf5296a14ce..3db8a3df7982 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -124,6 +124,7 @@ local function load() _M.load_times = _M.load_times + 1 core.log.info("load plugin times: ", _M.load_times) + core.config.init_plugins(local_plugins_hash, false) return true end @@ -165,6 +166,7 @@ local function load_stream() core.log.info("stream plugins: ", core.json.delay_encode(stream_local_plugins, true)) core.log.info("load stream plugin times: ", _M.stream_load_times) + core.config.init_plugins(stream_local_plugins_hash, true) return true end From 45fd6badf3a3abbc8329505bf4c2d5ad637b5381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:57:22 +0800 Subject: [PATCH 03/16] feat: add check plugin's schema test --- t/config-center/yaml/route-plugin.t | 87 +++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 t/config-center/yaml/route-plugin.t diff --git a/t/config-center/yaml/route-plugin.t b/t/config-center/yaml/route-plugin.t new file mode 100644 index 000000000000..12698605c776 --- /dev/null +++ b/t/config-center/yaml/route-plugin.t @@ -0,0 +1,87 @@ +# +# 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'); +no_root_location(); +no_shuffle(); + +sub read_file($) { + my $infile = shift; + open my $in, $infile + or die "cannot open $infile for reading: $!"; + my $cert = do { local $/; <$in> }; + close $in; + $cert; +} + +our $yaml_config = read_file("conf/config.yaml"); +$yaml_config =~ s/node_listen: 9080/node_listen: 1984/; +$yaml_config =~ s/config_center: etcd/config_center: yaml/; +$yaml_config =~ s/enable_admin: true/enable_admin: false/; + +run_tests(); + +__DATA__ + +=== TEST 1: route with plugin +--- yaml_config eval: $::yaml_config +--- apisix_yaml +routes: + - + uri: /hello + plugins: + proxy-rewrite: + uri: /uri/plugin_proxy_rewrite + headers: + X-Api-Version: v2 + upstream: + nodes: + "127.0.0.1:1980": 1 + type: roundrobin +#END +--- request +GET /hello +--- more_headers +X-Api-Version:v1 +--- response_body +uri: /uri/plugin_proxy_rewrite +host: localhost +x-api-version: v2 +x-real-ip: 127.0.0.1 +--- no_error_log +[error] + + +=== TEST 2: route with invalid plugin +--- yaml_config eval: $::yaml_config +--- apisix_yaml +routes: + - + uri: /hello + plugins: + proxy-rewrite: + headers: + "": "" + upstream: + nodes: + "127.0.0.1:1980": 1 + type: roundrobin +#END +--- error_log +failed to check the configuration of plugin proxy-rewrite err: property "headers" validation failed: expect object to have at least 1 properties \ No newline at end of file From f695a988cd2df3526257f516645e21414a17afbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:58:11 +0800 Subject: [PATCH 04/16] refactor: move config-center-yaml to config-center/yaml --- t/{config-center-yaml => config-center/yaml}/route-service.t | 0 t/{config-center-yaml => config-center/yaml}/route-upstream.t | 0 t/{config-center-yaml => config-center/yaml}/route.t | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename t/{config-center-yaml => config-center/yaml}/route-service.t (100%) rename t/{config-center-yaml => config-center/yaml}/route-upstream.t (100%) rename t/{config-center-yaml => config-center/yaml}/route.t (100%) diff --git a/t/config-center-yaml/route-service.t b/t/config-center/yaml/route-service.t similarity index 100% rename from t/config-center-yaml/route-service.t rename to t/config-center/yaml/route-service.t diff --git a/t/config-center-yaml/route-upstream.t b/t/config-center/yaml/route-upstream.t similarity index 100% rename from t/config-center-yaml/route-upstream.t rename to t/config-center/yaml/route-upstream.t diff --git a/t/config-center-yaml/route.t b/t/config-center/yaml/route.t similarity index 100% rename from t/config-center-yaml/route.t rename to t/config-center/yaml/route.t From 60a7e6c54aa475bc0b80d49eb859220e9dfc9842 Mon Sep 17 00:00:00 2001 From: wrl96 Date: Mon, 20 Jul 2020 07:33:30 +0000 Subject: [PATCH 05/16] fix: fix test case of route-plugin --- t/config-center/yaml/route-plugin.t | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/t/config-center/yaml/route-plugin.t b/t/config-center/yaml/route-plugin.t index 12698605c776..8bb5c0ff7954 100644 --- a/t/config-center/yaml/route-plugin.t +++ b/t/config-center/yaml/route-plugin.t @@ -83,5 +83,8 @@ routes: "127.0.0.1:1980": 1 type: roundrobin #END +--- request +GET /hello +--- error_code: 404 --- error_log -failed to check the configuration of plugin proxy-rewrite err: property "headers" validation failed: expect object to have at least 1 properties \ No newline at end of file +failed to check the configuration of plugin proxy-rewrite err: invalid type as header value, context: ngx.timer From b589c64144f7ebfb47fd619f30e2bd978906be4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:52:03 +0800 Subject: [PATCH 06/16] fix: add check plugin's schema in sync data --- apisix/core/config_etcd.lua | 50 +++++++++++++++++++++++++++++++++++++ apisix/core/config_yaml.lua | 33 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua index fbb6df8dbf5d..b24dd976f6e0 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -49,6 +49,16 @@ local mt = { end } +local disable_schema = { + type = "object", + properties = { + disable = {type = "boolean", enum={true}} + }, + required = {"disable"} +} +local local_plugins +local stream_local_plugins + local function readdir(etcd_cli, key) if not etcd_cli then return nil, nil, "not inited" @@ -166,6 +176,39 @@ local function sync_data(self) ", it shoud be a object") end + -- check plugins schema + local plugins_conf = item.plugins + if plugins_conf then + for name, plugin_conf in pairs(plugins_conf) do + local plugin_obj = local_plugins[name] + if plugin_obj then + log.info("check plugin scheme, name: ", name, ", configurations: ", + json.delay_encode(plugin_conf, true)) + else + plugin_obj = stream_local_plugins[name] + if plugin_obj then + log.info("check stream plugin scheme, name: ", name, + ": ", json.delay_encode(plugin_conf, true)) + else + log.error("unknown plugin [" .. name .. "]") + return false + end + end + + if plugin_obj.check_schema then + local ok = check_schema(disable_schema, plugin_conf) + if not ok then + local ok, err = plugin_obj.check_schema(plugin_conf) + if not ok then + log.error("failed to check the configuration of plugin " + .. name .. " err: " .. err) + return false + end + end + end + end + end + if data_valid and self.item_schema then data_valid, err = check_schema(self.item_schema, item.value) if not data_valid then @@ -466,5 +509,12 @@ function _M.server_version(self) return read_etcd_version(self.etcd_cli) end +function _M.init_plugins(plugins, is_stream) + if is_stream then + stream_local_plugins = plugins + else + local_plugins = plugins + end +end return _M diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua index bb1cd250af2a..86f3c60c5d53 100644 --- a/apisix/core/config_yaml.lua +++ b/apisix/core/config_yaml.lua @@ -162,6 +162,39 @@ local function sync_data(self) ", it shoud be a object") end + -- check plugins schema + local plugins_conf = item.plugins + if plugins_conf then + for name, plugin_conf in pairs(plugins_conf) do + local plugin_obj = local_plugins[name] + if plugin_obj then + log.info("check plugin scheme, name: ", name, ", configurations: ", + json.delay_encode(plugin_conf, true)) + else + plugin_obj = stream_local_plugins[name] + if plugin_obj then + log.info("check stream plugin scheme, name: ", name, + ": ", json.delay_encode(plugin_conf, true)) + else + log.error("unknown plugin [" .. name .. "]") + return false + end + end + + if plugin_obj.check_schema then + local ok = check_schema(disable_schema, plugin_conf) + if not ok then + local ok, err = plugin_obj.check_schema(plugin_conf) + if not ok then + log.error("failed to check the configuration of plugin " + .. name .. " err: " .. err) + return false + end + end + end + end + end + local key = item.id or "arr_" .. i local conf_item = {value = item, modifiedIndex = apisix_yaml_ctime, key = "/" .. self.key .. "/" .. key} From bdec5131473d313741e5fb703d28ff8bbe327d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:56:35 +0800 Subject: [PATCH 07/16] fix: add check plugin's schema in sync data --- apisix/core/config_yaml.lua | 20 ++++++++++++++++++-- apisix/plugin.lua | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua index 86f3c60c5d53..32dd5db1bf59 100644 --- a/apisix/core/config_yaml.lua +++ b/apisix/core/config_yaml.lua @@ -55,9 +55,18 @@ local mt = { end } +local disable_schema = { + type = "object", + properties = { + disable = {type = "boolean", enum={true}} + }, + required = {"disable"} +} +local local_plugins +local stream_local_plugins - local apisix_yaml - local apisix_yaml_ctime +local apisix_yaml +local apisix_yaml_ctime local function read_apisix_yaml(premature, pre_mtime) if premature then return @@ -357,5 +366,12 @@ function _M.init_worker() ngx.timer.every(1, read_apisix_yaml) end +function _M.init_plugins(plugins, is_stream) + if is_stream then + stream_local_plugins = plugins + else + local_plugins = plugins + end +end return _M diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 2cf5296a14ce..3db8a3df7982 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -124,6 +124,7 @@ local function load() _M.load_times = _M.load_times + 1 core.log.info("load plugin times: ", _M.load_times) + core.config.init_plugins(local_plugins_hash, false) return true end @@ -165,6 +166,7 @@ local function load_stream() core.log.info("stream plugins: ", core.json.delay_encode(stream_local_plugins, true)) core.log.info("load stream plugin times: ", _M.stream_load_times) + core.config.init_plugins(stream_local_plugins_hash, true) return true end From 3dfbf27e26d41484411199a23d29b00bf332ed2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:57:22 +0800 Subject: [PATCH 08/16] feat: add check plugin's schema test --- t/config-center/yaml/route-plugin.t | 87 +++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 t/config-center/yaml/route-plugin.t diff --git a/t/config-center/yaml/route-plugin.t b/t/config-center/yaml/route-plugin.t new file mode 100644 index 000000000000..12698605c776 --- /dev/null +++ b/t/config-center/yaml/route-plugin.t @@ -0,0 +1,87 @@ +# +# 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'); +no_root_location(); +no_shuffle(); + +sub read_file($) { + my $infile = shift; + open my $in, $infile + or die "cannot open $infile for reading: $!"; + my $cert = do { local $/; <$in> }; + close $in; + $cert; +} + +our $yaml_config = read_file("conf/config.yaml"); +$yaml_config =~ s/node_listen: 9080/node_listen: 1984/; +$yaml_config =~ s/config_center: etcd/config_center: yaml/; +$yaml_config =~ s/enable_admin: true/enable_admin: false/; + +run_tests(); + +__DATA__ + +=== TEST 1: route with plugin +--- yaml_config eval: $::yaml_config +--- apisix_yaml +routes: + - + uri: /hello + plugins: + proxy-rewrite: + uri: /uri/plugin_proxy_rewrite + headers: + X-Api-Version: v2 + upstream: + nodes: + "127.0.0.1:1980": 1 + type: roundrobin +#END +--- request +GET /hello +--- more_headers +X-Api-Version:v1 +--- response_body +uri: /uri/plugin_proxy_rewrite +host: localhost +x-api-version: v2 +x-real-ip: 127.0.0.1 +--- no_error_log +[error] + + +=== TEST 2: route with invalid plugin +--- yaml_config eval: $::yaml_config +--- apisix_yaml +routes: + - + uri: /hello + plugins: + proxy-rewrite: + headers: + "": "" + upstream: + nodes: + "127.0.0.1:1980": 1 + type: roundrobin +#END +--- error_log +failed to check the configuration of plugin proxy-rewrite err: property "headers" validation failed: expect object to have at least 1 properties \ No newline at end of file From c5d5a75072c668e5b4db6c5e9d816156a909d1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=91=9E=E9=BA=9F?= Date: Mon, 20 Jul 2020 14:58:11 +0800 Subject: [PATCH 09/16] refactor: move config-center-yaml to config-center/yaml --- t/{config-center-yaml => config-center/yaml}/route-service.t | 0 t/{config-center-yaml => config-center/yaml}/route-upstream.t | 0 t/{config-center-yaml => config-center/yaml}/route.t | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename t/{config-center-yaml => config-center/yaml}/route-service.t (100%) rename t/{config-center-yaml => config-center/yaml}/route-upstream.t (100%) rename t/{config-center-yaml => config-center/yaml}/route.t (100%) diff --git a/t/config-center-yaml/route-service.t b/t/config-center/yaml/route-service.t similarity index 100% rename from t/config-center-yaml/route-service.t rename to t/config-center/yaml/route-service.t diff --git a/t/config-center-yaml/route-upstream.t b/t/config-center/yaml/route-upstream.t similarity index 100% rename from t/config-center-yaml/route-upstream.t rename to t/config-center/yaml/route-upstream.t diff --git a/t/config-center-yaml/route.t b/t/config-center/yaml/route.t similarity index 100% rename from t/config-center-yaml/route.t rename to t/config-center/yaml/route.t From 026e2bb934437dc73c200c5265d8b80d055c6458 Mon Sep 17 00:00:00 2001 From: wrl96 Date: Mon, 20 Jul 2020 07:33:30 +0000 Subject: [PATCH 10/16] fix: fix test case of route-plugin --- t/config-center/yaml/route-plugin.t | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/t/config-center/yaml/route-plugin.t b/t/config-center/yaml/route-plugin.t index 12698605c776..8bb5c0ff7954 100644 --- a/t/config-center/yaml/route-plugin.t +++ b/t/config-center/yaml/route-plugin.t @@ -83,5 +83,8 @@ routes: "127.0.0.1:1980": 1 type: roundrobin #END +--- request +GET /hello +--- error_code: 404 --- error_log -failed to check the configuration of plugin proxy-rewrite err: property "headers" validation failed: expect object to have at least 1 properties \ No newline at end of file +failed to check the configuration of plugin proxy-rewrite err: invalid type as header value, context: ngx.timer From 022f8272f3c1ccf2182e510efaef26c5ffeedb05 Mon Sep 17 00:00:00 2001 From: wrl96 Date: Tue, 21 Jul 2020 09:39:49 +0800 Subject: [PATCH 11/16] fix: set pairs in local --- apisix/core/config_etcd.lua | 1 + apisix/core/config_yaml.lua | 1 + 2 files changed, 2 insertions(+) diff --git a/apisix/core/config_etcd.lua b/apisix/core/config_etcd.lua index b24dd976f6e0..ed46e77f8885 100644 --- a/apisix/core/config_etcd.lua +++ b/apisix/core/config_etcd.lua @@ -25,6 +25,7 @@ local exiting = ngx.worker.exiting local insert_tab = table.insert local type = type local ipairs = ipairs +local pairs = pairs local setmetatable = setmetatable local ngx_sleep = ngx.sleep local ngx_timer_at = ngx.timer.at diff --git a/apisix/core/config_yaml.lua b/apisix/core/config_yaml.lua index 32dd5db1bf59..c5d6fbe766e0 100644 --- a/apisix/core/config_yaml.lua +++ b/apisix/core/config_yaml.lua @@ -27,6 +27,7 @@ local exiting = ngx.worker.exiting local insert_tab = table.insert local type = type local ipairs = ipairs +local pairs = pairs local setmetatable = setmetatable local ngx_sleep = ngx.sleep local ngx_timer_at = ngx.timer.at From 48b9c4a680e40c7b9839bcb118a191ac7c8e01e6 Mon Sep 17 00:00:00 2001 From: wrl96 Date: Tue, 21 Jul 2020 16:38:15 +0800 Subject: [PATCH 12/16] feat: add test casesof etcd --- t/config-center/etcd/route-plugin.t | 142 ++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 t/config-center/etcd/route-plugin.t diff --git a/t/config-center/etcd/route-plugin.t b/t/config-center/etcd/route-plugin.t new file mode 100644 index 000000000000..5a49ba6415b7 --- /dev/null +++ b/t/config-center/etcd/route-plugin.t @@ -0,0 +1,142 @@ +# +# 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. +# +BEGIN { + $ENV{"ETCD_ENABLE_AUTH"} = "true" +} + +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +log_level("info"); + +# Authentication is enabled at etcd and credentials are set +system('etcdctl --endpoints="http://127.0.0.1:2379" -u root:5tHkHhYkjr6cQY user add root:5tHkHhYkjr6cQY'); +system('etcdctl --endpoints="http://127.0.0.1:2379" -u root:5tHkHhYkjr6cQY auth enable'); +system('etcdctl --endpoints="http://127.0.0.1:2379" -u root:5tHkHhYkjr6cQY role revoke --path "/*" -rw guest'); + +run_tests; + +# Authentication is disabled at etcd & guest access is granted +system('etcdctl --endpoints="http://127.0.0.1:2379" -u root:5tHkHhYkjr6cQY auth disable'); +system('etcdctl --endpoints="http://127.0.0.1:2379" -u root:5tHkHhYkjr6cQY role grant --path "/*" -rw guest'); + +__DATA__ + +=== TEST 1: set route with plugin +--- config + location /t { + content_by_lua_block { + local conf = { + ["uri"] = "/hello", + ["plugins"] = { + ["proxy-rewrite"] = { + ["uri"] = "/uri/plugin_proxy_rewrite", + ["headers"] = { + "X-Api-Version": "v2" + } + } + }, + ["upstream"] = { + ["nodes"] = { + ["127.0.0.1:1980"]: 1 + }, + ["type"]: "roundrobin" + } + } + local res, err = core.etcd.push("/routes", conf) + if not res then + core.log.error("failed to post route[/routes] to etcd") + ngx.exit(code) + end + ngx.say("done") + } + } +--- request +GET /t +--- response_body +done +--- no_error_log +[error] + + + +=== TEST 2: route with plugin +--- request +GET /hello +--- more_headers +X-Api-Version:v1 +--- response_body +uri: /uri/plugin_proxy_rewrite +host: localhost +x-api-version: v2 +x-real-ip: 127.0.0.1 +--- no_error_log +[error] + + + +=== TEST 3: set route with invalid plugin +--- config + location /t { + content_by_lua_block { + local sub_str = string.sub + local res, err = core.etcd.get("/routes") + if not res then + core.log.error("failed to get route[/routes] from etcd: ", err) + local key = sub_str(res.body.node.nodes[1].key, 8) + local conf = { + ["uri"] = "/hello", + ["plugins"] = { + ["proxy-rewrite"] = { + ["uri"] = "/uri/plugin_proxy_rewrite", + ["headers"] = { + "": "" + } + } + }, + ["upstream"] = { + ["nodes"] = { + ["127.0.0.1:1980"]: 1 + }, + ["type"]: "roundrobin" + } + } + local res, err = core.etcd.set(key, conf) + if not res then + core.log.error("failed to put route[/routes] to etcd") + ngx.exit(code) + end + ngx.say("done") + } + } +--- request +GET /t +--- response_body +done +--- no_error_log +[error] + + + +=== TEST 4: route with invalid plugin +--- request +GET /hello +--- error_code: 404 +--- error_log +failed to check the configuration of plugin proxy-rewrite err: invalid type as header value, context: ngx.timer \ No newline at end of file From 2212d9e53181a77a0f3200a18972dde813e6a6fc Mon Sep 17 00:00:00 2001 From: wrl96 Date: Tue, 21 Jul 2020 16:47:32 +0800 Subject: [PATCH 13/16] feat: add test cases of etcd --- t/config-center/etcd/route-plugin.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/config-center/etcd/route-plugin.t b/t/config-center/etcd/route-plugin.t index 5a49ba6415b7..fd2494ffab3c 100644 --- a/t/config-center/etcd/route-plugin.t +++ b/t/config-center/etcd/route-plugin.t @@ -139,4 +139,4 @@ done GET /hello --- error_code: 404 --- error_log -failed to check the configuration of plugin proxy-rewrite err: invalid type as header value, context: ngx.timer \ No newline at end of file +failed to check the configuration of plugin proxy-rewrite err: invalid type as header value, context: ngx.timer From ee95248c3dd9e2bfa6e2f99dc3187a36eeb5805d Mon Sep 17 00:00:00 2001 From: wrl96 Date: Tue, 21 Jul 2020 17:08:46 +0800 Subject: [PATCH 14/16] feat: add test cases of etcd --- t/config-center/etcd/route-plugin.t | 1 + 1 file changed, 1 insertion(+) diff --git a/t/config-center/etcd/route-plugin.t b/t/config-center/etcd/route-plugin.t index fd2494ffab3c..83eaa1e8fdd9 100644 --- a/t/config-center/etcd/route-plugin.t +++ b/t/config-center/etcd/route-plugin.t @@ -22,6 +22,7 @@ use t::APISIX 'no_plan'; repeat_each(1); no_long_string(); +no_shuffle(); no_root_location(); log_level("info"); From 9778b7d9035227705879fc2c7bff5347108ef90b Mon Sep 17 00:00:00 2001 From: wrl96 Date: Tue, 21 Jul 2020 18:07:48 +0800 Subject: [PATCH 15/16] fix: spell error --- t/config-center/etcd/route-plugin.t | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/t/config-center/etcd/route-plugin.t b/t/config-center/etcd/route-plugin.t index 83eaa1e8fdd9..cd9012f5cd74 100644 --- a/t/config-center/etcd/route-plugin.t +++ b/t/config-center/etcd/route-plugin.t @@ -49,15 +49,15 @@ __DATA__ ["proxy-rewrite"] = { ["uri"] = "/uri/plugin_proxy_rewrite", ["headers"] = { - "X-Api-Version": "v2" + ["X-Api-Version"] = "v2" } } }, ["upstream"] = { ["nodes"] = { - ["127.0.0.1:1980"]: 1 + ["127.0.0.1:1980"] = 1 }, - ["type"]: "roundrobin" + ["type"] = "roundrobin" } } local res, err = core.etcd.push("/routes", conf) @@ -107,15 +107,15 @@ x-real-ip: 127.0.0.1 ["proxy-rewrite"] = { ["uri"] = "/uri/plugin_proxy_rewrite", ["headers"] = { - "": "" + [""] = "" } } }, ["upstream"] = { ["nodes"] = { - ["127.0.0.1:1980"]: 1 + ["127.0.0.1:1980"] = 1 }, - ["type"]: "roundrobin" + ["type"] = "roundrobin" } } local res, err = core.etcd.set(key, conf) From b3c1b3dd442f634be901ce8f19d97fb63671beb6 Mon Sep 17 00:00:00 2001 From: wrl96 Date: Tue, 21 Jul 2020 18:36:17 +0800 Subject: [PATCH 16/16] fix: test case error --- t/config-center/etcd/route-plugin.t | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/config-center/etcd/route-plugin.t b/t/config-center/etcd/route-plugin.t index cd9012f5cd74..c14db6681719 100644 --- a/t/config-center/etcd/route-plugin.t +++ b/t/config-center/etcd/route-plugin.t @@ -43,6 +43,7 @@ __DATA__ --- config location /t { content_by_lua_block { + local core = require("apisix.core") local conf = { ["uri"] = "/hello", ["plugins"] = { @@ -96,10 +97,12 @@ x-real-ip: 127.0.0.1 --- config location /t { content_by_lua_block { + local core = require("apisix.core") local sub_str = string.sub local res, err = core.etcd.get("/routes") if not res then core.log.error("failed to get route[/routes] from etcd: ", err) + end local key = sub_str(res.body.node.nodes[1].key, 8) local conf = { ["uri"] = "/hello",