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: add debug yaml validation #7201

Merged
merged 4 commits into from
Jun 10, 2022
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
61 changes: 60 additions & 1 deletion 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,16 @@ 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)
return
end

return true
end


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

read_debug_yaml()
if not read_debug_yaml() then
return
end

sync_debug_hooks()
end

Expand Down
6 changes: 6 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 Expand Up @@ -295,6 +297,8 @@ filter(): call require("apisix.plugin").filter() return:{

=== TEST 5: multiple requests, only output logs of the request with enable_header_name
--- debug_config
basic:
enable: true
http_filter:
enable: true
enable_header_name: X-APISIX-Dynamic-Debug
Expand Down Expand Up @@ -374,6 +378,8 @@ qr/call\srequire\(\"apisix.plugin\"\).filter\(\)\sreturn.*GET\s\/mysleep\?second

=== TEST 6: hook function with ctx as param
--- 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
34 changes: 32 additions & 2 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 @@ -120,10 +125,35 @@ hook_test: # module and function list, name: hook_test
GET /hello
--- more_headers
Host: foo.com
X-APISIX-Dynamic-Debug: true
--- response_body
hello world
--- no_error_log
[error]
--- 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
X-APISIX-Dynamic-Debug: true
--- response_body
hello world
--- error_log
read_debug_yaml(): failed to validate debug config property "hook_conf" is required
--- wait: 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think wait: 2 is enough as we load debug yaml per second:

ngx.timer.every(1, sync_debug_status)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, wait for one more seconds is acceptable