diff --git a/apisix/debug.lua b/apisix/debug.lua index 363aee172e4f..72c101635881 100644 --- a/apisix/debug.lua +++ b/apisix/debug.lua @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/t/debug/dynamic-hook.t b/t/debug/dynamic-hook.t index 95ac63bcc83e..692942d1f9e4 100644 --- a/t/debug/dynamic-hook.t +++ b/t/debug/dynamic-hook.t @@ -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 @@ -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 @@ -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 diff --git a/t/debug/hook.t b/t/debug/hook.t index 5afac49ce589..1a9ebc140437 100644 --- a/t/debug/hook.t +++ b/t/debug/hook.t @@ -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 @@ -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