Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide error instead of nil panic when cache_zone is missing #10138

Merged
merged 10 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions apisix/plugins/proxy-cache/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -33,7 +34,7 @@ local schema = {
type = "string",
minLength = 1,
maxLength = 100,
default = "disk_cache_one",
default = DEFAULT_CACHE_ZONE,
},
cache_strategy = {
type = "string",
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions apisix/plugins/proxy-cache/memory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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

Expand Down
43 changes: 43 additions & 0 deletions t/plugin/proxy-cache/memory.t
Original file line number Diff line number Diff line change
Expand Up @@ -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