Skip to content

Commit

Permalink
fix(core): respect custom proxy_access_log (#12073)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
outsinre authored and fffonion committed Nov 29, 2023
1 parent 3d7ccc1 commit 3ccfbde
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/respect-custom-proxy_access_log.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "respect custom `proxy_access_log`"
type: bugfix
scope: Configuration
13 changes: 12 additions & 1 deletion kong/cmd/utils/prefix_handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ local function compile_conf(kong_config, conf_template)
-- 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,
Expand All @@ -248,6 +247,18 @@ local function compile_conf(kong_config, conf_template)
}
}

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

do
local worker_rlimit_nofile_auto
if kong_config.nginx_main_directives then
Expand Down
4 changes: 4 additions & 0 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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
Expand Down
56 changes: 47 additions & 9 deletions spec/01-unit/04-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3ccfbde

Please sign in to comment.