Skip to content

Commit

Permalink
fix: add debug yaml validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendongcmss committed Jun 8, 2022
1 parent 22e23ba commit dfc7264
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
69 changes: 63 additions & 6 deletions apisix/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local log = require("apisix.core.log")
local profile = require("apisix.core.profile")
local lfs = require("lfs")
local inspect = require("inspect")
local jsonschema = require("jsonschema")
local io = io
local ngx = ngx
local re_find = ngx.re.find
Expand All @@ -38,11 +39,56 @@ local debug_yaml_ctime
local _M = {version = 0.1}


local config_schema = {
type = "object",
properties = {
basic = {
properties = {
enable = {
type = "boolean",
},
}
},
http_filter = {
properties = {
enable = {
type = "boolean",
},
enable_header_name = {
type = "string",
},
}
},
hook_conf = {
properties = {
enable = {
type = "boolean",
},
name = {
type = "string",
},
log_level = {
enum = {"debug", "info", "notice", "warn", "error",
"crit", "alert","emerg"},
},
is_print_input_args = {
type = "boolean",
},
is_print_return_value = {
type = "boolean",
},
}
},
},
required = {"basic", "http_filter", "hook_conf"},
}


local function read_debug_yaml()
local attributes, err = lfs.attributes(debug_yaml_path)
if not attributes then
log.notice("failed to fetch ", debug_yaml_path, " attributes: ", err)
return
return false
end

-- log.info("change: ", json.encode(attributes))
Expand All @@ -54,7 +100,7 @@ local function read_debug_yaml()
local f, err = io.open(debug_yaml_path, "r")
if not f then
log.error("failed to open file ", debug_yaml_path, " : ", err)
return
return false
end

local found_end_flag
Expand All @@ -77,7 +123,7 @@ local function read_debug_yaml()
if size > 8 then
log.warn("missing valid end flag in file ", debug_yaml_path)
end
return
return false
end

f:seek('set')
Expand All @@ -87,12 +133,22 @@ local function read_debug_yaml()
local debug_yaml_new = yaml.parse(yaml_config)
if not debug_yaml_new then
log.error("failed to parse the content of file " .. debug_yaml_path)
return
return false
end

debug_yaml_new.hooks = debug_yaml_new.hooks or {}
debug_yaml = debug_yaml_new
debug_yaml_ctime = last_change_time

-- validate the debug yaml config
local validator = jsonschema.generate_validator(config_schema)
local ok, err = validator(debug_yaml)
if not ok then
log.error("failed to validate debug config " .. err)
return false
end

return true
end


Expand Down Expand Up @@ -204,8 +260,9 @@ local function sync_debug_status(premature)
return
end

read_debug_yaml()
sync_debug_hooks()
if read_debug_yaml() then
sync_debug_hooks()
end
end


Expand Down
2 changes: 2 additions & 0 deletions t/debug/dynamic-hook.t
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ call require("apisix").http_log_phase() return:{}
=== TEST 4: plugin filter log
--- debug_config
basic:
enable: true
http_filter:
enable: true # enable or disable this feature
enable_header_name: X-APISIX-Dynamic-Debug # the header name of dynamic enable
Expand Down
30 changes: 30 additions & 0 deletions t/debug/hook.t
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ call require("apisix").http_log_phase() return:{}
=== TEST 4: plugin filter log
--- debug_config
basic:
enable: true
http_filter:
enable: true # enable or disable this feature
enable_header_name: X-APISIX-Dynamic-Debug # the header name of dynamic enable
hook_conf:
enable: true # enable or disable this feature
name: hook_test # the name of module and function list
Expand All @@ -127,3 +132,28 @@ hello world
--- error_log
filter(): call require("apisix.plugin").filter() args:{
filter(): call require("apisix.plugin").filter() return:{
=== TEST 5: missing hook_conf
--- debug_config
basic:
enable: true
http_filter:
enable: true # enable or disable this feature
enable_header_name: X-APISIX-Dynamic-Debug # the header name of dynamic enable
hook_test: # module and function list, name: hook_test
apisix.plugin: # required module name
- filter # function name
#END
--- request
GET /hello
--- more_headers
Host: foo.com
--- response_body
hello world
--- no_error_log
[error]
--- error_log
read_debug_yaml(): failed to validate debug config property "hook_conf" is required

0 comments on commit dfc7264

Please sign in to comment.