Skip to content

Commit

Permalink
fix: add debug yaml validation
Browse files Browse the repository at this point in the history
  • Loading branch information
qizhendong1 committed Jun 7, 2022
1 parent 22e23ba commit cbefff5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
53 changes: 53 additions & 0 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,6 +39,51 @@ 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
Expand Down Expand Up @@ -93,6 +139,13 @@ local function read_debug_yaml()
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)
end
end


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: false
http_filter:
enable: false # 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: false
http_filter:
enable: false # 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
failed to validate debug configproperty \"hook_conf\" is required

0 comments on commit cbefff5

Please sign in to comment.