Skip to content

Commit

Permalink
hotfix(config) allow specifying 'headers' via config file
Browse files Browse the repository at this point in the history
This fixes a bug introduced in 976dd87 (#3300) where the `headers`
config option were not properly written to `.kong_env`. This would be an
issue when the value was specified from the configuration file (vs. via
environment variables).

From #3419

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
  • Loading branch information
hbagdi authored and thibaultcha committed May 3, 2018
1 parent 60c335e commit 75e071b
Show file tree
Hide file tree
Showing 5 changed files with 65 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 @@ -607,31 +607,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 @@ -713,20 +713,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
20 changes: 20 additions & 0 deletions spec/02-integration/05-proxy/13-server_tokens_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local helpers = require "spec.helpers"
local constants = require "kong.constants"
local cjson = require "cjson"


local default_server_header = _KONG._NAME .. "/" .. _KONG._VERSION
Expand Down Expand Up @@ -492,6 +493,25 @@ describe("headers [#" .. strategy .. "]", function()
assert.is_nil(res.headers[constants.HEADERS.PROXY_LATENCY])
end)

it("can be specified via configuration file", function()
-- A regression test added with https://github.com/Kong/kong/pull/3419
-- to ensure that the `headers` configuration value can be specified
-- via the configuration file (vs. environment variables as the rest
-- of this test suite uses).
-- This regression occured because of the dumping of config values into
-- .kong_env (and the lack of serialization for the `headers` table).
assert(helpers.kong_exec("restart -c spec/fixtures/headers.conf"))

local admin_client = helpers.admin_client()
local res = assert(admin_client:send {
method = "GET",
path = "/",
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal("server_tokens", json.configuration.headers[1])
assert.equal("X-Kong-Proxy-Latency", json.configuration.headers[2])
end)
end)

describe("(with headers='server_tokens, off, X-Kong-Proxy-Latency')", function()
Expand Down
28 changes: 28 additions & 0 deletions spec/fixtures/headers.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 1st digit is 9 for our test instances
admin_listen = 127.0.0.1:9001
proxy_listen = 0.0.0.0:9000, 0.0.0.0:9443 ssl

ssl_cert = spec/fixtures/kong_spec.crt
ssl_cert_key = spec/fixtures/kong_spec.key

admin_ssl_cert = spec/fixtures/kong_spec.crt
admin_ssl_cert_key = spec/fixtures/kong_spec.key

dns_resolver = 8.8.8.8
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_database = kong_tests
cassandra_keyspace = kong_tests
cassandra_timeout = 10000
anonymous_reports = off

dns_hostsfile = spec/fixtures/hosts

nginx_worker_processes = 1
nginx_optimizations = off

prefix = servroot
log_level = debug

headers = server_tokens, X-Kong-Proxy-Latency

0 comments on commit 75e071b

Please sign in to comment.