This is an automated email from the ASF dual-hosted git repository.

spacewander 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 1d5ecca  fix: the issue that plugins in global rule may be cached to 
route (#4867)
1d5ecca is described below

commit 1d5ecca14f94145c8f7052836b18a0b7c727a3ab
Author: nic-chen <[email protected]>
AuthorDate: Mon Aug 23 17:32:34 2021 +0800

    fix: the issue that plugins in global rule may be cached to route (#4867)
---
 apisix/core.lua      |   1 -
 apisix/init.lua      |   2 +-
 apisix/plugin.lua    |   2 +-
 t/node/global-rule.t | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 121 insertions(+), 3 deletions(-)

diff --git a/apisix/core.lua b/apisix/core.lua
index b817cdb..3fc61bd 100644
--- a/apisix/core.lua
+++ b/apisix/core.lua
@@ -51,5 +51,4 @@ return {
     tablepool   = require("tablepool"),
     resolver    = require("apisix.core.resolver"),
     os          = require("apisix.core.os"),
-    empty_tab   = {},
 }
diff --git a/apisix/init.lua b/apisix/init.lua
index dfd7044..a6ad1a7 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -694,7 +694,7 @@ function _M.http_log_phase()
     end
 
     core.ctx.release_vars(api_ctx)
-    if api_ctx.plugins and api_ctx.plugins ~= core.empty_tab then
+    if api_ctx.plugins then
         core.tablepool.release("plugins", api_ctx.plugins)
     end
 
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 849b1e9..c158011 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -307,7 +307,7 @@ function _M.filter(conf, plugins, route_conf)
         trace_plugins_info_for_debug(nil)
         -- when 'plugins' is given, always return 'plugins' itself instead
         -- of another one
-        return plugins or core.empty_tab
+        return plugins or core.tablepool.fetch("plugins", 0, 0)
     end
 
     local route_plugin_conf = route_conf and route_conf.value.plugins
diff --git a/t/node/global-rule.t b/t/node/global-rule.t
index b4fae85..9947e53 100644
--- a/t/node/global-rule.t
+++ b/t/node/global-rule.t
@@ -369,3 +369,122 @@ GET /hello
 changed
 --- no_error_log
 [error]
+
+
+
+=== TEST 17: global rule works with the consumer, after deleting the global 
rule, ensure no stale plugins remaining
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "test",
+                    "plugins": {
+                        "basic-auth": {
+                            "username": "test",
+                            "password": "test"
+                        }
+                    },
+                    "desc": "test description"
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+
+            local code, body = t('/apisix/admin/global_rules/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "basic-auth": {}
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+
+            -- sleep for data sync
+            ngx.sleep(0.5)
+
+            -- hit the route without authorization, should be 401
+            local code, body = t('/hello',
+                ngx.HTTP_PUT
+            )
+
+            if code ~= 401 then
+                ngx.status = 400
+                return
+            end
+
+            -- hit the route with authorization
+            local code, body = t('/hello',
+                ngx.HTTP_PUT,
+                nil,
+                nil,
+                {Authorization = "Basic dGVzdDp0ZXN0"}
+            )
+
+            if code ~= 200 then
+                ngx.status = code
+                return
+            end
+
+            local code, body = t('/apisix/admin/global_rules/1',
+                ngx.HTTP_DELETE,
+                [[{
+                    "plugins": {
+                        "basic-auth": {}
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+
+            ngx.sleep(0.5)
+            -- hit the route with authorization, should be 200
+            local code, body = t('/hello',
+                ngx.HTTP_PUT
+            )
+
+            if code ~= 200 then
+                ngx.status = code
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]

Reply via email to