From 0945b404c360de23345579df19dbfe2f812ec5d7 Mon Sep 17 00:00:00 2001 From: MaChao Date: Mon, 14 Nov 2022 10:30:04 +0800 Subject: [PATCH 01/18] feat: add method that parse ip from /etc/hosts in high priority --- apisix/core/resolver.lua | 37 +++++++++++++++-- t/core/resolver.t | 90 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 t/core/resolver.t diff --git a/apisix/core/resolver.lua b/apisix/core/resolver.lua index 056fbf8066d6..7bf70d94412a 100644 --- a/apisix/core/resolver.lua +++ b/apisix/core/resolver.lua @@ -19,15 +19,31 @@ -- -- @module core.resolver -local json = require("apisix.core.json") -local log = require("apisix.core.log") -local utils = require("apisix.core.utils") +local json = require("apisix.core.json") +local log = require("apisix.core.log") +local utils = require("apisix.core.utils") +local dns_utils = require("resty.dns.utils") + + +local HOSTS_IP_MATCH_CACHE = {} local _M = {} +local function init_hosts_ip() + local hosts, err = dns_utils.parseHosts() + if not hosts then + return hosts, err + end + HOSTS_IP_MATCH_CACHE = hosts +end + + function _M.init_resolver(args) + -- initialize /etc/hosts + init_hosts_ip() + local dns_resolver = args and args["dns_resolver"] utils.set_resolver(dns_resolver) log.info("dns resolver ", json.delay_encode(dns_resolver, true)) @@ -42,6 +58,21 @@ end -- @usage -- local ip, err = core.resolver.parse_domain("apache.org") -- "198.18.10.114" function _M.parse_domain(host) + local rev = HOSTS_IP_MATCH_CACHE[host] + if rev then + -- use ipv4 in high priority + local ip = rev["ipv4"] + if not ip then + ip = rev["ipv6"] + end + if ip then + -- meet test case + log.info("dns resolve ", host, ", result: ", json.delay_encode(ip)) + log.info("dns resolver domain: ", host, " to ", ip) + return ip + end + end + local ip_info, err = utils.dns_parse(host) if not ip_info then log.error("failed to parse domain: ", host, ", error: ",err) diff --git a/t/core/resolver.t b/t/core/resolver.t new file mode 100644 index 000000000000..97ee5cd30169 --- /dev/null +++ b/t/core/resolver.t @@ -0,0 +1,90 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +log_level("info"); + +run_tests; + +__DATA__ + +=== TEST 1: resolve host from /etc/hosts +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local resolver = require("apisix.core.resolver") + local domain = "localhost" + local ip_info, err = resolver.parse_domain(domain) + if not ip_info then + core.log.error("failed to parse domain: ", domain, ", error: ",err) + return + end + ngx.say("ip_info: ", require("toolkit.json").encode(ip_info)) + } + } +--- request +GET /t +--- response_body +ip_info: "127.0.0.1" +--- no_error_log +[error] + + + +=== TEST 2: resolve host from dns +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local resolver = require("apisix.core.resolver") + local domain = "apisix.apache.org" + local ip_info, err = resolver.parse_domain(domain) + if not ip_info then + core.log.error("failed to parse domain: ", domain, ", error: ",err) + return + end + ngx.say("ip_info: ", require("toolkit.json").encode(ip_info)) + } + } +--- request +GET /t +--- response_body +ip_info: "151.101.2.132" +--- no_error_log +[error] + + + +=== TEST 3: there is no mapping in /etc/hosts and dns +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local resolver = require("apisix.core.resolver") + local domain = "abc1.test" + resolver.parse_domain(domain) + + } + } +--- request +GET /t +--- error_log +failed to parse domain From 21569ce9c7930a5724b29e89579149d7546a6f0b Mon Sep 17 00:00:00 2001 From: MaChao Date: Thu, 17 Nov 2022 11:12:09 +0800 Subject: [PATCH 02/18] feat: add method that parse ip from /etc/hosts in high priority --- t/node/upstream-node-dns.t | 39 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/t/node/upstream-node-dns.t b/t/node/upstream-node-dns.t index e48f05d7debd..1d63d78d163a 100644 --- a/t/node/upstream-node-dns.t +++ b/t/node/upstream-node-dns.t @@ -67,7 +67,7 @@ passed local utils = require("apisix.core.utils") utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test.com" then + if domain == "test2.com" then return {address = "127.0.0.2"} end @@ -164,8 +164,11 @@ passed local utils = require("apisix.core.utils") utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test.com" or domain == "test2.com" then - return {address = "127.0.0.2"} + if domain == "test.com" then + return {address = "127.0.0.1" } + end + if domain == "test2.com" then + return {address = "127.0.0.2" } end error("unknown domain: " .. domain) @@ -187,12 +190,9 @@ hello world apisix.http_init() local utils = require("apisix.core.utils") - local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser - count = count + 1 - - if domain == "test.com" or domain == "test2.com" then - return {address = "127.0.0." .. count} + if domain == "test2.com" then + return {address = "127.0.0.2" } end error("unknown domain: " .. domain) @@ -214,10 +214,10 @@ GET /t qr/dns resolver domain: \w+.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ --- grep_error_log_out eval qr/call \/hello( +dns resolver domain: test2.com to 127.0.0.2 +dns resolver domain: test.com to 127.0.0.1| dns resolver domain: test.com to 127.0.0.1 -dns resolver domain: test2.com to 127.0.0.2| -dns resolver domain: test2.com to 127.0.0.1 -dns resolver domain: test.com to 127.0.0.2) +dns resolver domain: test2.com to 127.0.0.2) proxy request to 127.0.0.[12]:1980 / @@ -361,12 +361,9 @@ passed apisix.http_init() local utils = require("apisix.core.utils") - local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser - count = count + 1 - - if domain == "test.com" or domain == "test2.com" then - return {address = "127.0.0." .. count} + if domain == "test2.com" then + return {address = "127.0.0.2" } end error("unknown domain: " .. domain) @@ -389,8 +386,8 @@ qr/dns resolver domain: \w+.com to 127.0.0.\d|call \/hello|proxy request to 127. qr/call \/hello( dns resolver domain: test.com to 127.0.0.1 dns resolver domain: test2.com to 127.0.0.2| -dns resolver domain: test2.com to 127.0.0.1 -dns resolver domain: test.com to 127.0.0.2) +dns resolver domain: test2.com to 127.0.0.2 +dns resolver domain: test.com to 127.0.0.1) proxy request to 127.0.0.[12]:1980 / @@ -477,11 +474,9 @@ passed apisix.http_init() local utils = require("apisix.core.utils") - local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser - count = count + 1 - if domain == "test.com" or domain == "test2.com" then - return {address = "127.0.0." .. count} + if domain == "test2.com" then + return {address = "127.0.0.2" } end error("unknown domain: " .. domain) From 97b53aba8792e0317e74f84ce2f8940f4464b4e6 Mon Sep 17 00:00:00 2001 From: MaChao Date: Thu, 17 Nov 2022 23:14:04 +0800 Subject: [PATCH 03/18] feat: add method that parse ip from /etc/hosts in high priority; --- t/core/resolver.t | 27 ++++++++----- t/node/upstream-node-dns.t | 77 ++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/t/core/resolver.t b/t/core/resolver.t index 97ee5cd30169..fddfc1ba2ce9 100644 --- a/t/core/resolver.t +++ b/t/core/resolver.t @@ -21,6 +21,14 @@ no_long_string(); no_root_location(); log_level("info"); +add_block_preprocessor(sub { + my ($block) = @_; + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } +}); + run_tests; __DATA__ @@ -40,8 +48,6 @@ __DATA__ ngx.say("ip_info: ", require("toolkit.json").encode(ip_info)) } } ---- request -GET /t --- response_body ip_info: "127.0.0.1" --- no_error_log @@ -56,6 +62,13 @@ ip_info: "127.0.0.1" local core = require("apisix.core") local resolver = require("apisix.core.resolver") local domain = "apisix.apache.org" + resolver.parse_domain = function(domain) -- mock: resolver parser + + if domain == "apisix.apache.org" then + return {address = "127.0.0.2" } + end + error("unknown domain: " .. domain) + end local ip_info, err = resolver.parse_domain(domain) if not ip_info then core.log.error("failed to parse domain: ", domain, ", error: ",err) @@ -64,12 +77,8 @@ ip_info: "127.0.0.1" ngx.say("ip_info: ", require("toolkit.json").encode(ip_info)) } } ---- request -GET /t --- response_body -ip_info: "151.101.2.132" ---- no_error_log -[error] +ip_info: {"address":"127.0.0.2"} @@ -81,10 +90,8 @@ ip_info: "151.101.2.132" local resolver = require("apisix.core.resolver") local domain = "abc1.test" resolver.parse_domain(domain) - } } ---- request -GET /t + --- error_log failed to parse domain diff --git a/t/node/upstream-node-dns.t b/t/node/upstream-node-dns.t index 1d63d78d163a..e222dc8fa22c 100644 --- a/t/node/upstream-node-dns.t +++ b/t/node/upstream-node-dns.t @@ -35,7 +35,7 @@ __DATA__ [[{ "upstream": { "nodes": { - "test.com:1980": 1 + "test1.com:1980": 1 }, "type": "roundrobin" }, @@ -67,7 +67,7 @@ passed local utils = require("apisix.core.utils") utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test2.com" then + if domain == "test1.com" then return {address = "127.0.0.2"} end @@ -94,7 +94,7 @@ hello world utils.dns_parse = function (domain) -- mock: DNS parser count = count + 1 - if domain == "test.com" then + if domain == "test1.com" then return {address = "127.0.0." .. count} end @@ -113,10 +113,10 @@ location /t { --- request GET /t --- grep_error_log eval -qr/dns resolver domain: test.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ +qr/dns resolver domain: test1.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ --- grep_error_log_out call /hello -dns resolver domain: test.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.1 proxy request to 127.0.0.1:1980 @@ -131,7 +131,7 @@ proxy request to 127.0.0.1:1980 [[{ "upstream": { "nodes": { - "test.com:1980": 1, + "test1.com:1980": 1, "test2.com:1980": 1 }, "type": "roundrobin" @@ -164,11 +164,8 @@ passed local utils = require("apisix.core.utils") utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test.com" then - return {address = "127.0.0.1" } - end - if domain == "test2.com" then - return {address = "127.0.0.2" } + if domain == "test1.com" or domain == "test2.com" then + return {address = "127.0.0.2"} end error("unknown domain: " .. domain) @@ -190,9 +187,12 @@ hello world apisix.http_init() local utils = require("apisix.core.utils") + local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test2.com" then - return {address = "127.0.0.2" } + count = count + 1 + + if domain == "test1.com" or domain == "test2.com" then + return {address = "127.0.0." .. count} end error("unknown domain: " .. domain) @@ -214,10 +214,10 @@ GET /t qr/dns resolver domain: \w+.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ --- grep_error_log_out eval qr/call \/hello( -dns resolver domain: test2.com to 127.0.0.2 -dns resolver domain: test.com to 127.0.0.1| -dns resolver domain: test.com to 127.0.0.1 -dns resolver domain: test2.com to 127.0.0.2) +dns resolver domain: test1.com to 127.0.0.1 +dns resolver domain: test2.com to 127.0.0.2| +dns resolver domain: test2.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.2) proxy request to 127.0.0.[12]:1980 / @@ -232,7 +232,7 @@ proxy request to 127.0.0.[12]:1980 ngx.HTTP_PUT, [[{ "nodes": { - "test.com:1980": 1 + "test1.com:1980": 1 }, "type": "roundrobin", "desc": "new upstream" @@ -294,7 +294,7 @@ passed utils.dns_parse = function (domain) -- mock: DNS parser count = count + 1 - if domain == "test.com" then + if domain == "test1.com" then return {address = "127.0.0." .. count} end @@ -313,10 +313,10 @@ location /t { --- request GET /t --- grep_error_log eval -qr/dns resolver domain: test.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ +qr/dns resolver domain: test1.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ --- grep_error_log_out call /hello -dns resolver domain: test.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.1 proxy request to 127.0.0.1:1980 @@ -330,7 +330,7 @@ proxy request to 127.0.0.1:1980 ngx.HTTP_PUT, [[{ "nodes": { - "test.com:1980": 1, + "test1.com:1980": 1, "test2.com:1980": 1 }, "type": "roundrobin", @@ -361,9 +361,12 @@ passed apisix.http_init() local utils = require("apisix.core.utils") + local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test2.com" then - return {address = "127.0.0.2" } + count = count + 1 + + if domain == "test1.com" or domain == "test2.com" then + return {address = "127.0.0." .. count} end error("unknown domain: " .. domain) @@ -384,10 +387,10 @@ GET /t qr/dns resolver domain: \w+.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ --- grep_error_log_out eval qr/call \/hello( -dns resolver domain: test.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.1 dns resolver domain: test2.com to 127.0.0.2| -dns resolver domain: test2.com to 127.0.0.2 -dns resolver domain: test.com to 127.0.0.1) +dns resolver domain: test2.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.2) proxy request to 127.0.0.[12]:1980 / @@ -403,7 +406,7 @@ proxy request to 127.0.0.[12]:1980 local utils = require("apisix.core.utils") local count = 1 utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test.com" or domain == "test2.com" then + if domain == "test1.com" or domain == "test2.com" then return {address = "127.0.0.1"} end @@ -425,10 +428,10 @@ GET /t qr/dns resolver domain: \w+.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:1980/ --- grep_error_log_out eval qr/call \/hello( -dns resolver domain: test.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.1 dns resolver domain: test2.com to 127.0.0.1| dns resolver domain: test2.com to 127.0.0.1 -dns resolver domain: test.com to 127.0.0.1) +dns resolver domain: test1.com to 127.0.0.1) proxy request to 127.0.0.1:1980 / @@ -443,7 +446,7 @@ proxy request to 127.0.0.1:1980 ngx.HTTP_PUT, [[{ "nodes": { - "test.com:1980": 1, + "test1.com:1980": 1, "127.0.0.5:1981": 1 }, "type": "roundrobin", @@ -474,9 +477,11 @@ passed apisix.http_init() local utils = require("apisix.core.utils") + local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser - if domain == "test2.com" then - return {address = "127.0.0.2" } + count = count + 1 + if domain == "test1.com" or domain == "test2.com" then + return {address = "127.0.0." .. count} end error("unknown domain: " .. domain) @@ -497,7 +502,7 @@ GET /t qr/dns resolver domain: \w+.com to 127.0.0.\d|call \/hello|proxy request to 127.0.0.\d:198\d/ --- grep_error_log_out eval qr/call \/hello -dns resolver domain: test.com to 127.0.0.1 +dns resolver domain: test1.com to 127.0.0.1 proxy request to 127.0.0.(1:1980|5:1981) / @@ -513,7 +518,7 @@ proxy request to 127.0.0.(1:1980|5:1981) [[{ "upstream": { "nodes": { - "test.com:1980": 1 + "test1.com:1980": 1 }, "type": "roundrobin" }, @@ -547,7 +552,7 @@ passed local count = 0 utils.dns_parse = function (domain) -- mock: DNS parser count = count + 1 - if domain == "test.com" then + if domain == "test1.com" then return {address = "127.0.0." .. count} end From a6dbe196ef70ac1c2c82f91d4a5bddd31da5ebd5 Mon Sep 17 00:00:00 2001 From: MaChao Date: Fri, 18 Nov 2022 15:51:04 +0800 Subject: [PATCH 04/18] feat: add method that parse ip from /etc/hosts in high priority; modify test case and use ops enable_ipv6 --- apisix/core/resolver.lua | 4 ++- t/core/resolver.t | 58 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/apisix/core/resolver.lua b/apisix/core/resolver.lua index 7bf70d94412a..3568a9762063 100644 --- a/apisix/core/resolver.lua +++ b/apisix/core/resolver.lua @@ -23,6 +23,7 @@ local json = require("apisix.core.json") local log = require("apisix.core.log") local utils = require("apisix.core.utils") local dns_utils = require("resty.dns.utils") +local config_local = require("apisix.core.config_local") local HOSTS_IP_MATCH_CACHE = {} @@ -59,10 +60,11 @@ end -- local ip, err = core.resolver.parse_domain("apache.org") -- "198.18.10.114" function _M.parse_domain(host) local rev = HOSTS_IP_MATCH_CACHE[host] + local enable_ipv6 = config_local.local_conf().apisix.enable_ipv6 if rev then -- use ipv4 in high priority local ip = rev["ipv4"] - if not ip then + if enable_ipv6 and not ip then ip = rev["ipv6"] end if ip then diff --git a/t/core/resolver.t b/t/core/resolver.t index fddfc1ba2ce9..06d2470b394b 100644 --- a/t/core/resolver.t +++ b/t/core/resolver.t @@ -27,6 +27,10 @@ add_block_preprocessor(sub { if (!$block->request) { $block->set_value("request", "GET /t"); } + + if (!$block->no_error_log && !$block->error_log) { + $block->set_value("no_error_log", "[error]"); + } }); run_tests; @@ -50,8 +54,6 @@ __DATA__ } --- response_body ip_info: "127.0.0.1" ---- no_error_log -[error] @@ -92,6 +94,58 @@ ip_info: {"address":"127.0.0.2"} resolver.parse_domain(domain) } } +--- error_log +failed to parse domain + + + +=== TEST 4: test dns config with ipv6 enable +--- yaml_config +apisix: + enable_ipv6: true +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local resolver = require("apisix.core.resolver") + local domain = "localhost6" + resolver.parse_domain = function(domain) -- mock: resolver parse_domain + if domain == "localhost6" then + return {address = "::1" } + end + error("unknown domain: " .. domain) + + end + local ip_info, err = resolver.parse_domain(domain) + if not ip_info then + core.log.error("failed to parse domain: ", domain, ", error: ",err) + return + end + ngx.say("ip_info: ", require("toolkit.json").encode(ip_info)) + } + } +--- response_body +ip_info: {"address":"::1"} + + +=== TEST 5: test dns config with ipv6 disable +--- yaml_config +apisix: + enable_ipv6: false +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local resolver = require("apisix.core.resolver") + local domain = "localhost6" + local ip_info, err = resolver.parse_domain(domain) + if not ip_info then + core.log.error("failed to parse domain: ", domain, ", error: ",err) + return + end + ngx.say("ip_info: ", require("toolkit.json").encode(ip_info)) + } + } --- error_log failed to parse domain From 38429036baebe096c0955856eed3b81a4dbc29e2 Mon Sep 17 00:00:00 2001 From: MaChao Date: Mon, 20 Mar 2023 18:11:41 +0800 Subject: [PATCH 05/18] feat: traffic-split plugin can't proxy virtual upstream that schema is https(#8996) --- apisix/plugins/traffic-split.lua | 2 ++ apisix/upstream.lua | 1 + 2 files changed, 3 insertions(+) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index 38e272b7be66..647ca6527383 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -173,6 +173,7 @@ local function set_upstream(upstream_info, ctx) key = upstream_info.key, nodes = new_nodes, timeout = upstream_info.timeout, + scheme = upstream_info.scheme } local ok, err = upstream.check_schema(up_conf) @@ -190,6 +191,7 @@ local function set_upstream(upstream_info, ctx) end core.log.info("upstream_key: ", upstream_key) upstream.set(ctx, upstream_key, ctx.conf_version, up_conf) + upstream.set_scheme(ctx, up_conf) return end diff --git a/apisix/upstream.lua b/apisix/upstream.lua index a2a0cd3e899a..6e0bcf34179b 100644 --- a/apisix/upstream.lua +++ b/apisix/upstream.lua @@ -162,6 +162,7 @@ local function set_upstream_scheme(ctx, upstream) ctx.var["upstream_scheme"] = ctx.upstream_scheme end +_M.set_scheme = set_upstream_scheme local scheme_to_port = { From 8f9ceb2d4db29ed02e7db2f5bd7d37bade0a1f18 Mon Sep 17 00:00:00 2001 From: MaChao Date: Sun, 2 Apr 2023 23:58:35 +0800 Subject: [PATCH 06/18] feat: Adjusting unit tests --- t/plugin/traffic-split2.t | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t index 05b8047d3f24..4a0da969fa19 100644 --- a/t/plugin/traffic-split2.t +++ b/t/plugin/traffic-split2.t @@ -58,7 +58,8 @@ __DATA__ upstream = { name = "upstream_A", type = "roundrobin", - nodes = {["127.0.0.1:1981"] = 1} + nodes = {["127.0.0.1:1981"] = 1}, + scheme = "http" }, weight = 1, } @@ -68,7 +69,8 @@ __DATA__ }, upstream = { type = "roundrobin", - nodes = {["127.0.0.1:1980"] = 1} + nodes = {["127.0.0.1:1980"] = 1}, + scheme = "http" } } local code, body = t('/apisix/admin/routes/1', @@ -278,7 +280,7 @@ GET /uri?name=jack host: 127.0.0.1 --- response_body uri: /uri -host: localhost +host: localhost:1981 x-real-ip: 127.0.0.1 From c8426fe759c091a6ce5e0ad051c8bd64616a9c60 Mon Sep 17 00:00:00 2001 From: MaChao Date: Tue, 5 Sep 2023 19:18:37 +0800 Subject: [PATCH 07/18] feat: traffic-split plugin can't proxy virtual upstream that schema is https --- apisix/plugins/traffic-split.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index d4c64dfc44c4..cb4370c0b239 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -56,7 +56,7 @@ local upstreams_schema = { upstream = schema_def.upstream, weight = { description = "used to split traffic between different" .. - "upstreams for plugin configuration", + "upstreams for plugin configuration", type = "integer", default = 1, minimum = 0 @@ -127,7 +127,7 @@ end local function parse_domain_for_node(node) local host = node.domain or node.host if not ipmatcher.parse_ipv4(host) - and not ipmatcher.parse_ipv6(host) + and not ipmatcher.parse_ipv6(host) then node.domain = host @@ -185,7 +185,7 @@ local function set_upstream(upstream_info, ctx) local matched_route = ctx.matched_route up_conf.parent = matched_route local upstream_key = up_conf.type .. "#route_" .. - matched_route.value.id .. "_" .. upstream_info.vid + matched_route.value.id .. "_" .. upstream_info.vid if upstream_info.node_tid then upstream_key = upstream_key .. "_" .. upstream_info.node_tid end From e459d4c0d5ad69d52ecc24b538a5714764cb2e84 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 5 Sep 2023 22:06:43 +0545 Subject: [PATCH 08/18] add tests --- t/plugin/traffic-split2.t | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t index dabfd2deea98..7e4984df40fc 100644 --- a/t/plugin/traffic-split2.t +++ b/t/plugin/traffic-split2.t @@ -801,3 +801,78 @@ GET /t --- error_code: 500 --- error_log failed to find upstream by id: invalid-id + + + +=== TEST 21: use upstream with https scheme +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin") + + local code, body = t.test('/apisix/admin/upstreams/1', + ngx.HTTP_PUT, + [[{ + "nodes": { + "127.0.0.1:1983": 1 + }, + "type": "roundrobin", + "desc": "port 1983 is served under https", + "scheme": "https", + "tls": { + "client_cert_id": 1 + } + }]] + ) + + if code >= 300 then + ngx.status = code + ngx.say(body) + end + + local data = { + uri = "/hello", + plugins = { + ["traffic-split"] = { + rules = { + { + weighted_upstreams = { + { + upstream_id = 1, + weight = 1 + } + } + } + } + } + }, + upstream = { + type = "roundrobin", + nodes = { + ["127.0.0.1:1980"] = 1 + } + } + } + + local code, body = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + json.encode(data) + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 22: hit route +--- request +GET /hello +--- error_code: 200 +--- response_body +hello world From d5153365f666d6e3055d50d361547dffa5cad3f6 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Tue, 5 Sep 2023 22:06:47 +0545 Subject: [PATCH 09/18] revert unnecessary test case changes This reverts commit 8f9ceb2d4db29ed02e7db2f5bd7d37bade0a1f18. --- t/plugin/traffic-split2.t | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t index 7e4984df40fc..2f4856ecba52 100644 --- a/t/plugin/traffic-split2.t +++ b/t/plugin/traffic-split2.t @@ -58,8 +58,7 @@ __DATA__ upstream = { name = "upstream_A", type = "roundrobin", - nodes = {["127.0.0.1:1981"] = 1}, - scheme = "http" + nodes = {["127.0.0.1:1981"] = 1} }, weight = 1, } @@ -69,8 +68,7 @@ __DATA__ }, upstream = { type = "roundrobin", - nodes = {["127.0.0.1:1980"] = 1}, - scheme = "http" + nodes = {["127.0.0.1:1980"] = 1} } } local code, body = t('/apisix/admin/routes/1', @@ -280,7 +278,7 @@ GET /uri?name=jack host: 127.0.0.1 --- response_body uri: /uri -host: localhost:1981 +host: localhost x-real-ip: 127.0.0.1 From d18f0b6f14ec7040a7adabd6fe16d724d0a998a2 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 6 Sep 2023 08:25:46 +0545 Subject: [PATCH 10/18] remove unneccessary changes This reverts commit c8426fe759c091a6ce5e0ad051c8bd64616a9c60. --- apisix/plugins/traffic-split.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index cb4370c0b239..d4c64dfc44c4 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -56,7 +56,7 @@ local upstreams_schema = { upstream = schema_def.upstream, weight = { description = "used to split traffic between different" .. - "upstreams for plugin configuration", + "upstreams for plugin configuration", type = "integer", default = 1, minimum = 0 @@ -127,7 +127,7 @@ end local function parse_domain_for_node(node) local host = node.domain or node.host if not ipmatcher.parse_ipv4(host) - and not ipmatcher.parse_ipv6(host) + and not ipmatcher.parse_ipv6(host) then node.domain = host @@ -185,7 +185,7 @@ local function set_upstream(upstream_info, ctx) local matched_route = ctx.matched_route up_conf.parent = matched_route local upstream_key = up_conf.type .. "#route_" .. - matched_route.value.id .. "_" .. upstream_info.vid + matched_route.value.id .. "_" .. upstream_info.vid if upstream_info.node_tid then upstream_key = upstream_key .. "_" .. upstream_info.node_tid end From b23daef557908200bbda06c255cc3483e9a18e1d Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 6 Sep 2023 10:41:53 +0545 Subject: [PATCH 11/18] unexport method --- apisix/plugins/traffic-split.lua | 7 +++++-- apisix/upstream.lua | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index d4c64dfc44c4..402f0d8daea0 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -148,7 +148,9 @@ local function set_upstream(upstream_info, ctx) local new_nodes = {} if core.table.isarray(nodes) then for _, node in ipairs(nodes) do + core.log.warn("dibag node is: ", core.json.delay_encode(node, true)) parse_domain_for_node(node) + core.log.warn("dibag parsed node is: ", core.json.delay_encode(node, true)) table_insert(new_nodes, node) end else @@ -157,9 +159,11 @@ local function set_upstream(upstream_info, ctx) local port, host host, port = core.utils.parse_addr(addr) node.host = host + core.log.warn("dibag node is: ", core.json.delay_encode(node, true)) parse_domain_for_node(node) node.port = port node.weight = weight + core.log.warn("dibag parsed node is: ", core.json.delay_encode(node, true)) table_insert(new_nodes, node) end end @@ -191,7 +195,6 @@ local function set_upstream(upstream_info, ctx) end core.log.info("upstream_key: ", upstream_key) upstream.set(ctx, upstream_key, ctx.conf_version, up_conf) - upstream.set_scheme(ctx, up_conf) return end @@ -288,7 +291,7 @@ function _M.access(conf, ctx) local upstream = rr_up:find() if upstream and type(upstream) == "table" then - core.log.info("upstream: ", core.json.encode(upstream)) + core.log.warn("upstream: ", core.json.encode(upstream)) return set_upstream(upstream, ctx) elseif upstream and upstream ~= "plugin#upstream#is#empty" then ctx.upstream_id = upstream diff --git a/apisix/upstream.lua b/apisix/upstream.lua index 4e66b701c4f1..57bcb1d5dacc 100644 --- a/apisix/upstream.lua +++ b/apisix/upstream.lua @@ -77,7 +77,6 @@ local function set_directly(ctx, key, ver, conf) ctx.upstream_key = key return end -_M.set = set_directly local function release_checker(healthcheck_parent) @@ -137,6 +136,7 @@ local function create_checker(upstream) local up_hdr = upstream.pass_host == "rewrite" and upstream.upstream_host local use_node_hdr = upstream.pass_host == "node" or nil for _, node in ipairs(upstream.nodes) do + core.log.warn("dibag node is: ", core.json.delay_encode(node, true)) local host_hdr = up_hdr or (use_node_hdr and node.domain) local ok, err = checker:add_target(node.host, port or node.port, host, true, host_hdr) From 7af60ddf633c6653855551f888c73c93da045769 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 6 Sep 2023 10:47:10 +0545 Subject: [PATCH 12/18] remove mistakenly commited lines --- apisix/plugins/traffic-split.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index 402f0d8daea0..a32e5ec16f12 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -148,9 +148,7 @@ local function set_upstream(upstream_info, ctx) local new_nodes = {} if core.table.isarray(nodes) then for _, node in ipairs(nodes) do - core.log.warn("dibag node is: ", core.json.delay_encode(node, true)) parse_domain_for_node(node) - core.log.warn("dibag parsed node is: ", core.json.delay_encode(node, true)) table_insert(new_nodes, node) end else @@ -159,11 +157,9 @@ local function set_upstream(upstream_info, ctx) local port, host host, port = core.utils.parse_addr(addr) node.host = host - core.log.warn("dibag node is: ", core.json.delay_encode(node, true)) parse_domain_for_node(node) node.port = port node.weight = weight - core.log.warn("dibag parsed node is: ", core.json.delay_encode(node, true)) table_insert(new_nodes, node) end end @@ -291,7 +287,7 @@ function _M.access(conf, ctx) local upstream = rr_up:find() if upstream and type(upstream) == "table" then - core.log.warn("upstream: ", core.json.encode(upstream)) + core.log.info("upstream: ", core.json.encode(upstream)) return set_upstream(upstream, ctx) elseif upstream and upstream ~= "plugin#upstream#is#empty" then ctx.upstream_id = upstream From 8a0236d762890b7822404875daceb82f05de6da3 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 6 Sep 2023 10:49:07 +0545 Subject: [PATCH 13/18] remove miscommited line --- apisix/upstream.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/upstream.lua b/apisix/upstream.lua index 57bcb1d5dacc..4e66b701c4f1 100644 --- a/apisix/upstream.lua +++ b/apisix/upstream.lua @@ -77,6 +77,7 @@ local function set_directly(ctx, key, ver, conf) ctx.upstream_key = key return end +_M.set = set_directly local function release_checker(healthcheck_parent) @@ -136,7 +137,6 @@ local function create_checker(upstream) local up_hdr = upstream.pass_host == "rewrite" and upstream.upstream_host local use_node_hdr = upstream.pass_host == "node" or nil for _, node in ipairs(upstream.nodes) do - core.log.warn("dibag node is: ", core.json.delay_encode(node, true)) local host_hdr = up_hdr or (use_node_hdr and node.domain) local ok, err = checker:add_target(node.host, port or node.port, host, true, host_hdr) From 64347b12b06f100a48ec215d4ad6f62a5debe47d Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 6 Sep 2023 10:50:14 +0545 Subject: [PATCH 14/18] unexport method --- apisix/upstream.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/upstream.lua b/apisix/upstream.lua index 4e66b701c4f1..416bbea7cfa4 100644 --- a/apisix/upstream.lua +++ b/apisix/upstream.lua @@ -175,7 +175,6 @@ local function set_upstream_scheme(ctx, upstream) ctx.var["upstream_scheme"] = ctx.upstream_scheme end -_M.set_scheme = set_upstream_scheme local scheme_to_port = { From 4eeced9f42e0a068419cc97ede4d209d7e931abb Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 6 Sep 2023 11:40:00 +0545 Subject: [PATCH 15/18] remove reference to non existing ssl resource --- t/plugin/traffic-split2.t | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t index 2f4856ecba52..47d041e9b45f 100644 --- a/t/plugin/traffic-split2.t +++ b/t/plugin/traffic-split2.t @@ -817,10 +817,7 @@ failed to find upstream by id: invalid-id }, "type": "roundrobin", "desc": "port 1983 is served under https", - "scheme": "https", - "tls": { - "client_cert_id": 1 - } + "scheme": "https" }]] ) From 08f647982991c9ecf2cc5a68a287449c8db6d6bc Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Sun, 17 Sep 2023 14:41:16 +0545 Subject: [PATCH 16/18] set scheme --- apisix/plugins/traffic-split.lua | 2 +- apisix/upstream.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index a32e5ec16f12..4d1a8b1dddf3 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -191,7 +191,7 @@ local function set_upstream(upstream_info, ctx) end core.log.info("upstream_key: ", upstream_key) upstream.set(ctx, upstream_key, ctx.conf_version, up_conf) - + upstream.set_scheme(ctx, up_conf) return end diff --git a/apisix/upstream.lua b/apisix/upstream.lua index 416bbea7cfa4..0ff82fea856a 100644 --- a/apisix/upstream.lua +++ b/apisix/upstream.lua @@ -175,7 +175,7 @@ local function set_upstream_scheme(ctx, upstream) ctx.var["upstream_scheme"] = ctx.upstream_scheme end - +_M.set_scheme = set_upstream_scheme local scheme_to_port = { http = 80, From cb2e3670c7202e1544067a520e6b3388271363a6 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Sun, 17 Sep 2023 15:39:18 +0545 Subject: [PATCH 17/18] add scheme check --- apisix/plugins/traffic-split.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index 4d1a8b1dddf3..f546225c8c95 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -191,7 +191,9 @@ local function set_upstream(upstream_info, ctx) end core.log.info("upstream_key: ", upstream_key) upstream.set(ctx, upstream_key, ctx.conf_version, up_conf) - upstream.set_scheme(ctx, up_conf) + if upstream_info.scheme == "https" then + upstream.set_scheme(ctx, up_conf) + end return end From 1acc511518e54c01c7dfc346568f51e27ac477c0 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Wed, 27 Sep 2023 14:53:11 +0545 Subject: [PATCH 18/18] use real upstream instead of upstream id --- t/plugin/traffic-split2.t | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t index 47d041e9b45f..746d7441bac5 100644 --- a/t/plugin/traffic-split2.t +++ b/t/plugin/traffic-split2.t @@ -809,32 +809,25 @@ failed to find upstream by id: invalid-id local json = require("toolkit.json") local t = require("lib.test_admin") - local code, body = t.test('/apisix/admin/upstreams/1', - ngx.HTTP_PUT, - [[{ - "nodes": { - "127.0.0.1:1983": 1 - }, - "type": "roundrobin", - "desc": "port 1983 is served under https", - "scheme": "https" - }]] - ) - - if code >= 300 then - ngx.status = code - ngx.say(body) - end - local data = { uri = "/hello", plugins = { ["traffic-split"] = { rules = { { + match = { { + vars = { { "arg_scheme", "==", "https" } } + } }, weighted_upstreams = { { - upstream_id = 1, + upstream = { + type = "roundrobin", + pass_host = "node", + nodes = { + ["127.0.0.1:1983"] = 1, + }, + scheme = "https" + }, weight = 1 } } @@ -867,7 +860,5 @@ passed === TEST 22: hit route --- request -GET /hello +GET /hello?scheme=https --- error_code: 200 ---- response_body -hello world