Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ipv6): allow disabling IPv6 resolve #6023

Merged
merged 7 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ stream {
lua_shared_dict plugin-limit-conn-stream {* stream.lua_shared_dict["plugin-limit-conn-stream"] *};
lua_shared_dict etcd-cluster-health-check-stream {* stream.lua_shared_dict["etcd-cluster-health-check-stream"] *};

resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} {% if dns_resolver_valid then %} valid={*dns_resolver_valid*}{% end %};
resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} {% if dns_resolver_valid then %} valid={*dns_resolver_valid*}{% end %} ipv6={% if enable_ipv6 then %}on{% else %}off{% end %};
resolver_timeout {*resolver_timeout*};

{% if ssl.ssl_trusted_certificate ~= nil then %}
Expand Down Expand Up @@ -248,7 +248,7 @@ http {

lua_socket_log_errors off;

resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} {% if dns_resolver_valid then %} valid={*dns_resolver_valid*}{% end %};
resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} {% if dns_resolver_valid then %} valid={*dns_resolver_valid*}{% end %} ipv6={% if enable_ipv6 then %}on{% else %}off{% end %};
resolver_timeout {*resolver_timeout*};

lua_http10_buffering off;
Expand Down
19 changes: 18 additions & 1 deletion apisix/core/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
-- limitations under the License.
--
local require = require
local config_local = require("apisix.core.config_local")
local log = require("apisix.core.log")
local json = require("apisix.core.json")
local table = require("apisix.core.table")
local insert_tab = table.insert
local math_random = math.random
local package_loaded = package.loaded
local ipairs = ipairs
local table_remove = table.remove
local setmetatable = setmetatable


Expand Down Expand Up @@ -130,7 +132,22 @@ end


function _M.new(opts)
opts.ipv6 = true
local local_conf = config_local.local_conf()

if opts.enable_ipv6 == nil then
opts.enable_ipv6 = local_conf.apisix.enable_ipv6
end

-- ensure the resolver throws an error when ipv6 is disabled
if not opts.enable_ipv6 then
for i, v in ipairs(opts.order) do
if v == "AAAA" then
table_remove(opts.order, i)
break
end
end
end

opts.timeout = 2000 -- 2 sec
opts.retrans = 5 -- 5 retransmissions on receive timeout

Expand Down
22 changes: 19 additions & 3 deletions t/cli/test_dns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ apisix:

make init

if ! grep "resolver 127.0.0.1 \[::1\]:5353 valid=30;" conf/nginx.conf > /dev/null; then
if ! grep "resolver 127.0.0.1 \[::1\]:5353 valid=30 ipv6=on;" conf/nginx.conf > /dev/null; then
echo "failed: dns_resolver_valid doesn't take effect"
exit 1
fi
Expand All @@ -52,7 +52,7 @@ apisix:

make init

count=$(grep -c "resolver 127.0.0.1 \[::1\]:5353 valid=30;" conf/nginx.conf)
count=$(grep -c "resolver 127.0.0.1 \[::1\]:5353 valid=30 ipv6=on;" conf/nginx.conf)
if [ "$count" -ne 2 ]; then
echo "failed: dns_resolver_valid doesn't take effect"
exit 1
Expand All @@ -73,10 +73,26 @@ apisix:

make init

count=$(grep -c "resolver 127.0.0.1 \[::1\] \[::2\];" conf/nginx.conf)
count=$(grep -c "resolver 127.0.0.1 \[::1\] \[::2\] ipv6=on;" conf/nginx.conf)
if [ "$count" -ne 2 ]; then
echo "failed: can't handle IPv6 resolver w/o bracket"
exit 1
fi

echo "pass: handle IPv6 resolver w/o bracket"

# ipv6 config test
echo '
apisix:
enable_ipv6: false
dns_resolver:
- 127.0.0.1
dns_resolver_valid: 30
' > conf/config.yaml

make init

if ! grep "resolver 127.0.0.1 valid=30 ipv6=off;" conf/nginx.conf > /dev/null; then
echo "failed: ipv6 config doesn't take effect"
exit 1
fi
48 changes: 48 additions & 0 deletions t/core/utils.t
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,51 @@ GET /t
--- error_log
error: failed to query the DNS server
--- timeout: 10



=== TEST 10: 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 domain = "ipv6.local"
local ip_info, err = core.utils.dns_parse(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: {"address":"[::1]","class":1,"name":"ipv6.local","ttl":315360000,"type":28}



=== TEST 11: 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 domain = "ipv6.local"
local ip_info, err = core.utils.dns_parse(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
--- error_log
failed to parse domain: ipv6.local
1 change: 1 addition & 0 deletions utils/set-dns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set -ex

# test a domain name is configured as upstream
echo "127.0.0.1 test.com" | sudo tee -a /etc/hosts
echo "::1 ipv6.local" | sudo tee -a /etc/hosts
# test certificate verification
echo "127.0.0.1 admin.apisix.dev" | sudo tee -a /etc/hosts
cat /etc/hosts # check GitHub Action's configuration
Expand Down