diff --git a/apisix/core.lua b/apisix/core.lua index 4b5bc2b99b9d..726b13e78949 100644 --- a/apisix/core.lua +++ b/apisix/core.lua @@ -50,5 +50,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 dfd7044614e2..a6ad1a77c851 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 849b1e96bfc4..c158011d294d 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 b4fae8579a32..9947e53a1ce0 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]