From 24589931371e028df1c26c67af5ab6894f98ea86 Mon Sep 17 00:00:00 2001 From: Zachary Hu <6426329+outsinre@users.noreply.github.com> Date: Tue, 28 Nov 2023 17:27:18 +0800 Subject: [PATCH] fix(core): respect custom proxy_access_log (#12073) * fix(core): respect custom proxy_access_log Kong now has a fixed access log format `kong_log_format` that prevents customization and error on `kong start`. Related to #11663. If the `proxy_access_log` is not a valid pathname, then replace `kong_log_format` with the custom value. * fix(config): cover log_format name with hyphen * fix(config): early error when access log format is not defined * fix(config): discard warning or return nil * chore(config): style and comments * chore(*): comments (cherry picked from commit 9ec3494cb558ee03223218c7c74003f8bce3b267) --- .../kong/respect-custom-proxy_access_log.yml | 3 + kong/cmd/utils/prefix_handler.lua | 13 ++++- kong/templates/nginx_kong.lua | 4 ++ spec/01-unit/04-prefix_handler_spec.lua | 56 ++++++++++++++++--- 4 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 changelog/unreleased/kong/respect-custom-proxy_access_log.yml diff --git a/changelog/unreleased/kong/respect-custom-proxy_access_log.yml b/changelog/unreleased/kong/respect-custom-proxy_access_log.yml new file mode 100644 index 000000000000..92b77e6d0680 --- /dev/null +++ b/changelog/unreleased/kong/respect-custom-proxy_access_log.yml @@ -0,0 +1,3 @@ +message: "respect custom `proxy_access_log`" +type: bugfix +scope: Configuration diff --git a/kong/cmd/utils/prefix_handler.lua b/kong/cmd/utils/prefix_handler.lua index ea661fbf4ca0..189c3a03981c 100644 --- a/kong/cmd/utils/prefix_handler.lua +++ b/kong/cmd/utils/prefix_handler.lua @@ -239,7 +239,6 @@ local function compile_conf(kong_config, conf_template, template_env_inject) -- computed config properties for templating local compile_env = { _escape = ">", - proxy_access_log_enabled = kong_config.proxy_access_log ~= "off", pairs = pairs, ipairs = ipairs, tostring = tostring, @@ -248,6 +247,18 @@ local function compile_conf(kong_config, conf_template, template_env_inject) } } + local kong_proxy_access_log = kong_config.proxy_access_log + if kong_proxy_access_log ~= "off" then + compile_env.proxy_access_log_enabled = true + end + if kong_proxy_access_log then + -- example: proxy_access_log = 'logs/some-file.log apigw_json' + local _, custom_format_name = string.match(kong_proxy_access_log, "^(%S+)%s(%S+)") + if custom_format_name then + compile_env.custom_proxy_access_log = true + end + end + compile_env = pl_tablex.merge(compile_env, template_env_inject or {}, true) do diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index 7e9a04bb4f93..260dfde2adfc 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -89,7 +89,11 @@ server { lua_kong_error_log_request_id $kong_request_id; > if proxy_access_log_enabled then +> if custom_proxy_access_log then + access_log ${{PROXY_ACCESS_LOG}}; +> else access_log ${{PROXY_ACCESS_LOG}} kong_log_format; +> end > else access_log off; > end diff --git a/spec/01-unit/04-prefix_handler_spec.lua b/spec/01-unit/04-prefix_handler_spec.lua index 27b109fba1a0..ab73d76ff60e 100644 --- a/spec/01-unit/04-prefix_handler_spec.lua +++ b/spec/01-unit/04-prefix_handler_spec.lua @@ -486,34 +486,72 @@ describe("NGINX conf compiler", function() describe("injected NGINX directives", function() it("injects proxy_access_log directive", function() - local conf = assert(conf_loader(nil, { + local conf, nginx_conf + conf = assert(conf_loader(nil, { proxy_access_log = "/dev/stdout", stream_listen = "0.0.0.0:9100", nginx_stream_tcp_nodelay = "on", })) - local nginx_conf = prefix_handler.compile_kong_conf(conf) + nginx_conf = prefix_handler.compile_kong_conf(conf) assert.matches("access_log%s/dev/stdout%skong_log_format;", nginx_conf) - local nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + nginx_conf = prefix_handler.compile_kong_stream_conf(conf) assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) - local conf = assert(conf_loader(nil, { + conf = assert(conf_loader(nil, { proxy_access_log = "off", stream_listen = "0.0.0.0:9100", nginx_stream_tcp_nodelay = "on", })) - local nginx_conf = prefix_handler.compile_kong_conf(conf) + nginx_conf = prefix_handler.compile_kong_conf(conf) assert.matches("access_log%soff;", nginx_conf) - local nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + nginx_conf = prefix_handler.compile_kong_stream_conf(conf) assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) - local conf = assert(conf_loader(nil, { + conf = assert(conf_loader(nil, { + proxy_access_log = "/dev/stdout apigw-json", + nginx_http_log_format = 'apigw-json "$kong_request_id"', + stream_listen = "0.0.0.0:9100", + nginx_stream_tcp_nodelay = "on", + })) + nginx_conf = prefix_handler.compile_kong_conf(conf) + assert.matches("access_log%s/dev/stdout%sapigw%-json;", nginx_conf) + nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) + + -- configure an undefined log format will error + -- on kong start. This is expected + conf = assert(conf_loader(nil, { + proxy_access_log = "/dev/stdout not-exist", + nginx_http_log_format = 'apigw-json "$kong_request_id"', + stream_listen = "0.0.0.0:9100", + nginx_stream_tcp_nodelay = "on", + })) + nginx_conf = prefix_handler.compile_kong_conf(conf) + assert.matches("access_log%s/dev/stdout%snot%-exist;", nginx_conf) + nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) + + conf = assert(conf_loader(nil, { + proxy_access_log = "/tmp/not-exist.log", + stream_listen = "0.0.0.0:9100", + nginx_stream_tcp_nodelay = "on", + })) + nginx_conf = prefix_handler.compile_kong_conf(conf) + assert.matches("access_log%s/tmp/not%-exist.log%skong_log_format;", nginx_conf) + nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + assert.matches("access_log%slogs/access.log%sbasic;", nginx_conf) + + conf = assert(conf_loader(nil, { + prefix = "servroot_tmp", + nginx_stream_log_format = "custom '$protocol $status'", proxy_stream_access_log = "/dev/stdout custom", stream_listen = "0.0.0.0:9100", nginx_stream_tcp_nodelay = "on", })) - local nginx_conf = prefix_handler.compile_kong_conf(conf) + assert(prefix_handler.prepare_prefix(conf)) + nginx_conf = prefix_handler.compile_kong_conf(conf) assert.matches("access_log%slogs/access.log%skong_log_format;", nginx_conf) - local nginx_conf = prefix_handler.compile_kong_stream_conf(conf) + nginx_conf = prefix_handler.compile_kong_stream_conf(conf) assert.matches("access_log%s/dev/stdout%scustom;", nginx_conf) end)