From bed2a9ea068ea6768ae407356e2f8e0fe003ff14 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 5cdce6f51836..d6c0d896d66b 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -659,6 +659,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 3e4144c52460..c4988c1de779 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 de794afe7ebf..7b949a3df1b9 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.com" }, + 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 b75ed2db205e..0b1a7f15fbd4 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.com" }, + 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 ef415c5fb1ef..3f5473fac9fc 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.com" }, + 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 379167a6adec..48120d0d6577 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 5716a6ea458e..b0e326eabb67 100644 --- a/spec/03-plugins/31-proxy-cache/02-access_spec.lua +++ b/spec/03-plugins/31-proxy-cache/02-access_spec.lua @@ -31,6 +31,7 @@ do local route1 = assert(bp.routes:insert { hosts = { "route-1.com" }, + paths = { "/" }, }) local route2 = assert(bp.routes:insert { hosts = { "route-2.com" },