From 86f0a6cfcbd477c0a51243c330bdd47a2abff2fc Mon Sep 17 00:00:00 2001 From: Chrono Date: Fri, 29 Dec 2023 14:57:53 +0800 Subject: [PATCH] fix(router): add missing `preserve_host` logic in stream subsystem (#12261) KAG-3032 --- kong/router/atc.lua | 5 +++++ kong/router/fields.lua | 21 ++++++++++++------- .../01-helpers/01-helpers_spec.lua | 1 + .../05-proxy/02-router_spec.lua | 10 +++++---- .../05-proxy/03-upstream_headers_spec.lua | 1 + .../05-proxy/14-server_tokens_spec.lua | 1 + spec/03-plugins/07-loggly/01-log_spec.lua | 1 + .../25-oauth2/04-invalidations_spec.lua | 1 + .../31-proxy-cache/02-access_spec.lua | 1 + 9 files changed, 31 insertions(+), 11 deletions(-) diff --git a/kong/router/atc.lua b/kong/router/atc.lua index f05053f8eb0b..16caac44f559 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -655,6 +655,11 @@ function _M:exec(ctx) else route_match_stat(ctx, "pos") + + -- preserve_host logic, modify cache result + if match_t.route.preserve_host then + match_t.upstream_host = fields.get_value("tls.sni", CACHE_PARAMS) + end end return match_t diff --git a/kong/router/fields.lua b/kong/router/fields.lua index a33b27c8fcd5..59d4cee86ec4 100644 --- a/kong/router/fields.lua +++ b/kong/router/fields.lua @@ -218,15 +218,20 @@ if is_http then end -- is_http -local function fields_visitor(fields, params, ctx, cb) - for _, field in ipairs(fields) do - local func = FIELDS_FUNCS[field] +local function get_value(field, params, ctx) + local func = FIELDS_FUNCS[field] + + if not func then -- unknown field + error("unknown router matching schema field: " .. field) + end -- if func + + return func(params, ctx) +end - if not func then -- unknown field - error("unknown router matching schema field: " .. field) - end -- if func - local value = func(params, ctx) +local function fields_visitor(fields, params, ctx, cb) + for _, field in ipairs(fields) do + local value = get_value(field, params, ctx) local res, err = cb(field, value) if not res then @@ -352,6 +357,8 @@ end return { + get_value = get_value, + get_cache_key = get_cache_key, fill_atc_context = fill_atc_context, diff --git a/spec/02-integration/01-helpers/01-helpers_spec.lua b/spec/02-integration/01-helpers/01-helpers_spec.lua index fa00dbd313aa..c4e383ffd236 100644 --- a/spec/02-integration/01-helpers/01-helpers_spec.lua +++ b/spec/02-integration/01-helpers/01-helpers_spec.lua @@ -26,6 +26,7 @@ for _, strategy in helpers.each_strategy() do bp.routes:insert { hosts = { "mock_upstream" }, protocols = { "http" }, + paths = { "/" }, service = service } diff --git a/spec/02-integration/05-proxy/02-router_spec.lua b/spec/02-integration/05-proxy/02-router_spec.lua index 855e64ebfe9d..26ba41a46176 100644 --- a/spec/02-integration/05-proxy/02-router_spec.lua +++ b/spec/02-integration/05-proxy/02-router_spec.lua @@ -877,15 +877,16 @@ for _, strategy in helpers.each_strategy() do describe("URI arguments (querystring)", function() local routes - before_each(function() + lazy_setup(function() routes = insert_routes(bp, { { hosts = { "mock_upstream" }, + paths = { "/" }, }, }) end) - after_each(function() + lazy_teardown(function() remove_routes(strategy, routes) end) @@ -1301,6 +1302,7 @@ for _, strategy in helpers.each_strategy() do routes = insert_routes(bp, { { protocols = { "https" }, + paths = { "/" }, snis = { "www.example.org" }, service = { name = "service_behind_www.example.org" @@ -1343,7 +1345,7 @@ for _, strategy in helpers.each_strategy() do path = "/status/201", headers = { ["kong-debug"] = 1 }, }) - assert.res_status(flavor == "traditional" and 201 or 200, res) + assert.res_status(201, res) assert.equal("service_behind_www.example.org", res.headers["kong-service-name"]) @@ -1365,7 +1367,7 @@ for _, strategy in helpers.each_strategy() do path = "/status/201", headers = { ["kong-debug"] = 1 }, }) - assert.res_status(flavor == "traditional" and 201 or 200, res) + assert.res_status(201, res) assert.equal("service_behind_example.org", res.headers["kong-service-name"]) end) diff --git a/spec/02-integration/05-proxy/03-upstream_headers_spec.lua b/spec/02-integration/05-proxy/03-upstream_headers_spec.lua index 3132d0a6bfd0..c78203d3b5f5 100644 --- a/spec/02-integration/05-proxy/03-upstream_headers_spec.lua +++ b/spec/02-integration/05-proxy/03-upstream_headers_spec.lua @@ -278,6 +278,7 @@ for _, strategy in helpers.each_strategy() do assert(bp.routes:insert { hosts = { "headers-charset.test" }, + paths = { "/" }, service = service, }) diff --git a/spec/02-integration/05-proxy/14-server_tokens_spec.lua b/spec/02-integration/05-proxy/14-server_tokens_spec.lua index 6cee745a1354..3de5077db9dd 100644 --- a/spec/02-integration/05-proxy/14-server_tokens_spec.lua +++ b/spec/02-integration/05-proxy/14-server_tokens_spec.lua @@ -291,6 +291,7 @@ describe("headers [#" .. strategy .. "]", function() return function() bp.routes:insert { hosts = { "headers-inspect.test" }, + paths = { "/" }, } local service = bp.services:insert({ diff --git a/spec/03-plugins/07-loggly/01-log_spec.lua b/spec/03-plugins/07-loggly/01-log_spec.lua index dd5e35a0199d..4987cbb1d9ab 100644 --- a/spec/03-plugins/07-loggly/01-log_spec.lua +++ b/spec/03-plugins/07-loggly/01-log_spec.lua @@ -19,6 +19,7 @@ for _, strategy in helpers.each_strategy() do local route1 = bp.routes:insert { hosts = { "logging.test" }, + paths = { "/" }, } local route2 = bp.routes:insert { diff --git a/spec/03-plugins/25-oauth2/04-invalidations_spec.lua b/spec/03-plugins/25-oauth2/04-invalidations_spec.lua index 90f7b25bf858..18218b6cfdb6 100644 --- a/spec/03-plugins/25-oauth2/04-invalidations_spec.lua +++ b/spec/03-plugins/25-oauth2/04-invalidations_spec.lua @@ -43,6 +43,7 @@ for _, strategy in helpers.each_strategy() do route = assert(admin_api.routes:insert { hosts = { "oauth2.com" }, protocols = { "http", "https" }, + paths = { "/" }, service = service, }) diff --git a/spec/03-plugins/31-proxy-cache/02-access_spec.lua b/spec/03-plugins/31-proxy-cache/02-access_spec.lua index aa8b350773d7..67e026d9e326 100644 --- a/spec/03-plugins/31-proxy-cache/02-access_spec.lua +++ b/spec/03-plugins/31-proxy-cache/02-access_spec.lua @@ -38,6 +38,7 @@ do local route1 = assert(bp.routes:insert { hosts = { "route-1.test" }, + paths = { "/" }, }) local route2 = assert(bp.routes:insert { hosts = { "route-2.test" },