Skip to content

Commit

Permalink
fix(config) parse headers config option from .kong_env
Browse files Browse the repository at this point in the history
This fixes a bug introduced in 976dd87 (#3300)
where headers config option was being overwritten
as a table. The table is not persisted in the intermediate
config file and hence the config option does not take effect.

This commit also exposes `get_running_conf` helper function to
read conf from prefix directory in use during the current test.
  • Loading branch information
hbagdi committed Apr 27, 2018
1 parent da4e36b commit 3eb3d0b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
22 changes: 12 additions & 10 deletions kong/conf_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -606,31 +606,33 @@ local function load(path, custom_conf)

-- load headers configuration
do
local headers_enabled = {}
local enabled_headers = {}

for _, v in pairs(header_key_to_name) do
headers_enabled[v] = false
enabled_headers[v] = false
end

if #conf.headers > 0 and conf.headers[1] ~= "off" then
for _, token in ipairs(conf.headers) do
if token ~= "off" then
headers_enabled[header_key_to_name[string.lower(token)]] = true
enabled_headers[header_key_to_name[string.lower(token)]] = true
end
end
end

if headers_enabled.server_tokens then
headers_enabled[headers.VIA] = true
headers_enabled[headers.SERVER] = true
if enabled_headers.server_tokens then
enabled_headers[headers.VIA] = true
enabled_headers[headers.SERVER] = true
end

if headers_enabled.latency_tokens then
headers_enabled[headers.PROXY_LATENCY] = true
headers_enabled[headers.UPSTREAM_LATENCY] = true
if enabled_headers.latency_tokens then
enabled_headers[headers.PROXY_LATENCY] = true
enabled_headers[headers.UPSTREAM_LATENCY] = true
end

conf.headers = headers_enabled
conf.enabled_headers = setmetatable(enabled_headers, {
__tostring = function() return "" end,
})
end

-- load absolute paths
Expand Down
2 changes: 1 addition & 1 deletion kong/error_handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ return function(ngx)
local status = ngx.status
message = BODIES["s" .. status] and BODIES["s" .. status] or format(BODIES.default, status)

if singletons.configuration.headers[constants.HEADERS.SERVER] then
if singletons.configuration.enabled_headers[constants.HEADERS.SERVER] then
ngx.header[constants.HEADERS.SERVER] = SERVER_HEADER
end

Expand Down
8 changes: 4 additions & 4 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -698,20 +698,20 @@ return {
local header = ngx.header

if ctx.KONG_PROXIED then
if singletons.configuration.headers[constants.HEADERS.UPSTREAM_LATENCY] then
if singletons.configuration.enabled_headers[constants.HEADERS.UPSTREAM_LATENCY] then
header[constants.HEADERS.UPSTREAM_LATENCY] = ctx.KONG_WAITING_TIME
end

if singletons.configuration.headers[constants.HEADERS.PROXY_LATENCY] then
if singletons.configuration.enabled_headers[constants.HEADERS.PROXY_LATENCY] then
header[constants.HEADERS.PROXY_LATENCY] = ctx.KONG_PROXY_LATENCY
end

if singletons.configuration.headers[constants.HEADERS.VIA] then
if singletons.configuration.enabled_headers[constants.HEADERS.VIA] then
header[constants.HEADERS.VIA] = server_header
end

else
if singletons.configuration.headers[constants.HEADERS.SERVER] then
if singletons.configuration.enabled_headers[constants.HEADERS.SERVER] then
header[constants.HEADERS.SERVER] = server_header

else
Expand Down
19 changes: 19 additions & 0 deletions spec/02-integration/05-proxy/13-server_tokens_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ describe("headers [#" .. strategy .. "]", function()

teardown(helpers.stop_kong)

it("intermediate kong conf in prefix should contain 'headers=server_tokens,latency_tokens'", function()
local conf = helpers.get_running_conf()
assert.equal("server_tokens", conf.headers[1])
assert.equal("latency_tokens", conf.headers[2])
assert.equal(2, #conf.headers)
end)

it("should return Kong 'Via' header but not change the 'Server' header when request was proxied", function()
local res = assert(proxy_client:send {
method = "GET",
Expand Down Expand Up @@ -122,6 +129,12 @@ describe("headers [#" .. strategy .. "]", function()

teardown(helpers.stop_kong)

it("intermediate kong conf in prefix should contain 'headers=Server'", function()
local conf = helpers.get_running_conf()
assert.equal("Server", conf.headers[1])
assert.equal(1, #conf.headers)
end)

it("should not return Kong 'Via' header but not change the 'Server' header when request was proxied", function()
local res = assert(proxy_client:send {
method = "GET",
Expand Down Expand Up @@ -160,6 +173,12 @@ describe("headers [#" .. strategy .. "]", function()

teardown(helpers.stop_kong)

it("intermediate kong conf in prefix should contain 'headers=server_tokens'", function()
local conf = helpers.get_running_conf()
assert.equal("server_tokens", conf.headers[1])
assert.equal(1, #conf.headers)
end)

it("should return Kong 'Via' header but not change the 'Server' header when request was proxied", function()
local res = assert(proxy_client:send {
method = "GET",
Expand Down
1 change: 1 addition & 0 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ return {
clean_prefix = clean_prefix,
wait_for_invalidation = wait_for_invalidation,
each_strategy = each_strategy,
get_running_conf = get_running_conf,

-- miscellaneous
intercept = intercept,
Expand Down

0 comments on commit 3eb3d0b

Please sign in to comment.