This is an automated email from the ASF dual-hosted git repository.
monkeydluffy 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 67057b8cd fix(proxy-cache): provide error instead of nil panic when
cache_zone is missing (#10138)
67057b8cd is described below
commit 67057b8cd5ded80956199f3aa5f06da45fa7ded3
Author: Ashish Tiwari <[email protected]>
AuthorDate: Wed Sep 13 12:26:08 2023 +0530
fix(proxy-cache): provide error instead of nil panic when cache_zone is
missing (#10138)
---
apisix/plugins/proxy-cache/init.lua | 16 ++++++++++---
apisix/plugins/proxy-cache/memory.lua | 11 +++++++++
t/plugin/proxy-cache/memory.t | 43 +++++++++++++++++++++++++++++++++++
3 files changed, 67 insertions(+), 3 deletions(-)
diff --git a/apisix/plugins/proxy-cache/init.lua
b/apisix/plugins/proxy-cache/init.lua
index 333c20e20..918f75599 100644
--- a/apisix/plugins/proxy-cache/init.lua
+++ b/apisix/plugins/proxy-cache/init.lua
@@ -25,6 +25,7 @@ local plugin_name = "proxy-cache"
local STRATEGY_DISK = "disk"
local STRATEGY_MEMORY = "memory"
+local DEFAULT_CACHE_ZONE = "disk_cache_one"
local schema = {
type = "object",
@@ -33,7 +34,7 @@ local schema = {
type = "string",
minLength = 1,
maxLength = 100,
- default = "disk_cache_one",
+ default = DEFAULT_CACHE_ZONE,
},
cache_strategy = {
type = "string",
@@ -129,14 +130,23 @@ function _M.check_schema(conf)
local found = false
local local_conf = core.config.local_conf()
if local_conf.apisix.proxy_cache then
+ local err = "cache_zone " .. conf.cache_zone .. " not found"
for _, cache in ipairs(local_conf.apisix.proxy_cache.zones) do
+ -- cache_zone passed in plugin config matched one of the
proxy_cache zones
if cache.name == conf.cache_zone then
- found = true
+ -- check for the mismatch between cache_strategy and
corresponding cache zone
+ if (conf.cache_strategy == STRATEGY_MEMORY and
cache.disk_path) or
+ (conf.cache_strategy == STRATEGY_DISK and not cache.disk_path)
then
+ err = "invalid or empty cache_zone for cache_strategy:
"..conf.cache_strategy
+ else
+ found = true
+ end
+ break
end
end
if found == false then
- return false, "cache_zone " .. conf.cache_zone .. " not found"
+ return false, err
end
end
diff --git a/apisix/plugins/proxy-cache/memory.lua
b/apisix/plugins/proxy-cache/memory.lua
index 0112db63b..9d5c665a8 100644
--- a/apisix/plugins/proxy-cache/memory.lua
+++ b/apisix/plugins/proxy-cache/memory.lua
@@ -32,6 +32,10 @@ end
function _M:set(key, obj, ttl)
+ if self.dict == nil then
+ return nil, "invalid cache_zone provided"
+ end
+
local obj_json = core.json.encode(obj)
if not obj_json then
return nil, "could not encode object"
@@ -43,6 +47,10 @@ end
function _M:get(key)
+ if self.dict == nil then
+ return nil, "invalid cache_zone provided"
+ end
+
-- If the key does not exist or has expired, then res_json will be nil.
local res_json, err = self.dict:get(key)
if not res_json then
@@ -63,6 +71,9 @@ end
function _M:purge(key)
+ if self.dict == nil then
+ return nil, "invalid cache_zone provided"
+ end
self.dict:delete(key)
end
diff --git a/t/plugin/proxy-cache/memory.t b/t/plugin/proxy-cache/memory.t
index 5984dbd47..e617f8a1b 100644
--- a/t/plugin/proxy-cache/memory.t
+++ b/t/plugin/proxy-cache/memory.t
@@ -661,3 +661,46 @@ GET /hello
--- more_headers
Cache-Control: only-if-cached
--- error_code: 504
+
+
+
+=== TEST 36: configure plugin without memory_cache zone for cache_strategy =
memory
+--- 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,
+ [[{
+ "plugins": {
+ "proxy-cache": {
+ "cache_strategy": "memory",
+ "cache_key":["$host","$uri"],
+ "cache_bypass": ["$arg_bypass"],
+ "cache_control": true,
+ "cache_method": ["GET"],
+ "cache_ttl": 10,
+ "cache_http_status": [200]
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1986": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body_like
+.*err: invalid or empty cache_zone for cache_strategy: memory.*
+--- error_code: 400