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 2020/09/17 14:31:10 UTC

[apisix] branch master updated: bugfix: independently check the "disabled" field to avoid `schema` ve… (#2099)

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/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f90039  bugfix: independently check the "disabled" field to avoid `schema` ve… (#2099)
4f90039 is described below

commit 4f900390f90b3bdf181f5eb298a94cee3b437b0d
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Thu Sep 17 22:31:02 2020 +0800

    bugfix: independently check the "disabled" field to avoid `schema` ve… (#2099)
    
    * bugfix: independently check the "disabled" field to avoid `schema` verification
      with other parameters.
---
 apisix/admin/plugins.lua  | 41 ++++++++++++++++----------------------
 apisix/plugin.lua         |  8 ++++++++
 apisix/schema_def.lua     |  5 +++++
 t/admin/plugins.t         | 14 +++++++++++--
 t/admin/routes.t          | 32 +++++++++++++++++-------------
 t/admin/schema.t          | 20 +++++++++++++++++++
 t/node/merge-route.t      |  4 ++++
 t/plugin/example.t        | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 t/plugin/ip-restriction.t | 35 +++++++++++++++++++++++++++++++++
 9 files changed, 169 insertions(+), 40 deletions(-)

diff --git a/apisix/admin/plugins.lua b/apisix/admin/plugins.lua
index 7b835e1..88aa6f6 100644
--- a/apisix/admin/plugins.lua
+++ b/apisix/admin/plugins.lua
@@ -26,18 +26,7 @@ local table_sort = table.sort
 local table_insert = table.insert
 
 
-local _M = {
-    version = 0.1,
-}
-
-
-local disable_schema = {
-    type = "object",
-    properties = {
-        disable = {type = "boolean", enum={true}}
-    },
-    required = {"disable"}
-}
+local _M = {}
 
 
 function _M.check_schema(plugins_conf)
@@ -50,14 +39,16 @@ function _M.check_schema(plugins_conf)
         end
 
         if plugin_obj.check_schema then
-            local ok = core.schema.check(disable_schema, plugin_conf)
+            local disable = plugin_conf.disable
+            plugin_conf.disable = nil
+
+            local ok, err = plugin_obj.check_schema(plugin_conf)
             if not ok then
-                local ok, err = plugin_obj.check_schema(plugin_conf)
-                if not ok then
-                    return false, "failed to check the configuration of plugin "
-                                  .. name .. " err: " .. err
-                end
+                return false, "failed to check the configuration of plugin "
+                              .. name .. " err: " .. err
             end
+
+            plugin_conf.disable = disable
         end
     end
 
@@ -75,14 +66,16 @@ function _M.stream_check_schema(plugins_conf)
         end
 
         if plugin_obj.check_schema then
-            local ok = core.schema.check(disable_schema, plugin_conf)
+            local disable = plugin_conf.disable
+            plugin_conf.disable = nil
+
+            local ok, err = plugin_obj.check_schema(plugin_conf)
             if not ok then
-                local ok, err = plugin_obj.check_schema(plugin_conf)
-                if not ok then
-                    return false, "failed to check the configuration of "
-                                  .. "stream plugin [" .. name .. "]: " .. err
-                end
+                return false, "failed to check the configuration of "
+                              .. "stream plugin [" .. name .. "]: " .. err
             end
+
+            plugin_conf.disable = disable
         end
     end
 
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index bcd5e35..8c49883 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -78,6 +78,14 @@ local function load_plugin(name, plugins_list, is_stream_plugin)
         return
     end
 
+    if plugin.schema and plugin.schema.type == "object" then
+        if not plugin.schema.properties or
+           core.table.nkeys(plugin.schema.properties) == 0
+        then
+            plugin.schema.properties = core.schema.plugin_disable_schema
+        end
+    end
+
     plugin.name = name
     core.table.insert(plugins_list, plugin)
 
diff --git a/apisix/schema_def.lua b/apisix/schema_def.lua
index 52a7f42..e8d4249 100644
--- a/apisix/schema_def.lua
+++ b/apisix/schema_def.lua
@@ -606,6 +606,11 @@ _M.stream_route = {
 _M.id_schema = id_schema
 
 
+_M.plugin_disable_schema = {
+    disable = {type = "boolean"}
+}
+
+
 setmetatable(_M, {
     __index = schema,
     __newindex = function() error("no modification allowed") end,
diff --git a/t/admin/plugins.t b/t/admin/plugins.t
index fa69ec6..6ca86af 100644
--- a/t/admin/plugins.t
+++ b/t/admin/plugins.t
@@ -61,6 +61,16 @@ GET /apisix/admin/plugins/limit-req
 --- request
 GET /apisix/admin/plugins/node-status
 --- response_body
-{"additionalProperties":false,"type":"object"}
+{"properties":{"disable":{"type":"boolean"}},"additionalProperties":false,"type":"object"}
 --- no_error_log
-[error] 
+[error]
+
+
+
+=== TEST 5: get plugin prometheus schema
+--- request
+GET /apisix/admin/plugins/prometheus
+--- response_body
+{"properties":{"disable":{"type":"boolean"}},"additionalProperties":false,"type":"object"}
+--- no_error_log
+[error]
diff --git a/t/admin/routes.t b/t/admin/routes.t
index d9905c3..ec13481 100644
--- a/t/admin/routes.t
+++ b/t/admin/routes.t
@@ -291,17 +291,17 @@ GET /t
             local core = require("apisix.core")
             local t = require("lib.test_admin").test
             local code, message, res = t('/apisix/admin/routes/1',
-                 ngx.HTTP_PUT,
-                 [[{
-                        "plugins": {
-                            "limit-count": {
-                                "count": 2,
-                                "time_window": 60,
-                                "rejected_code": 503,
-                                "key": "remote_addr"
-                            }
-                        },
-                        "uri": "/index.html"
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr"
+                        }
+                    },
+                    "uri": "/index.html"
                 }]],
                 [[{
                     "node": {
@@ -677,16 +677,20 @@ GET /t
             local core = require("apisix.core")
             local t = require("lib.test_admin").test
             local code, message, res = t('/apisix/admin/routes/1',
-                 ngx.HTTP_PUT,
-                 [[{
+                ngx.HTTP_PUT,
+                [[{
                     "plugins": {
                         "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
                             "disable": true
                         }
                     },
                     "uri": "/index.html"
                 }]]
-                )
+            )
 
             if code >= 300 then
                 ngx.status = code
diff --git a/t/admin/schema.t b/t/admin/schema.t
index cc2e594..1acc3f6 100644
--- a/t/admin/schema.t
+++ b/t/admin/schema.t
@@ -243,3 +243,23 @@ GET /apisix/admin/schema/plugins/grpc-transcode
 qr/"proto_id".*additionalProperties/
 --- no_error_log
 [error]
+
+
+
+=== TEST 13: get plugin prometheus schema
+--- request
+GET /apisix/admin/schema/plugins/prometheus
+--- response_body eval
+qr/"disable":\{"type":"boolean"\}/
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: get plugin node-status schema
+--- request
+GET /apisix/admin/schema/plugins/node-status
+--- response_body eval
+qr/"disable":\{"type":"boolean"\}/
+--- no_error_log
+[error]
diff --git a/t/node/merge-route.t b/t/node/merge-route.t
index 988699a..ec7ab7b 100644
--- a/t/node/merge-route.t
+++ b/t/node/merge-route.t
@@ -176,6 +176,10 @@ qr/1980/
                 [[{
                     "plugins": {
                         "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
                             "disable": true
                         }
                     },
diff --git a/t/plugin/example.t b/t/plugin/example.t
index a1d1560..0fb069b 100644
--- a/t/plugin/example.t
+++ b/t/plugin/example.t
@@ -259,3 +259,53 @@ GET /server_port
 qr/1981/
 --- no_error_log
 [error]
+
+
+
+=== TEST 10: set disable = true
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.example-plugin")
+            local ok, err = plugin.check_schema({
+                i = 1, s = "s", t = {1},
+                disable = true,
+            })
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: set disable = false
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.example-plugin")
+            local ok, err = plugin.check_schema({
+                i = 1, s = "s", t = {1},
+                disable = true,
+            })
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
diff --git a/t/plugin/ip-restriction.t b/t/plugin/ip-restriction.t
index 58b0aee..0ed1c2b 100644
--- a/t/plugin/ip-restriction.t
+++ b/t/plugin/ip-restriction.t
@@ -568,3 +568,38 @@ invalid ip address: ::1/129
 value should match only one schema, but matches none
 --- no_error_log
 [error]
+
+
+
+=== TEST 25: set disable=true
+--- 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",
+                    "plugins": {
+                        "ip-restriction": {
+                            "blacklist": [
+                                "127.0.0.0/24"
+                            ],
+                            "disable": true
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]