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

chore(dns) error handling and debug improvements #8902

Merged
merged 1 commit into from
Jun 7, 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
21 changes: 13 additions & 8 deletions kong/resty/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ local log = ngx.log
local ERR = ngx.ERR
local WARN = ngx.WARN
local DEBUG = ngx.DEBUG
--[[
DEBUG = ngx.WARN
--]]
local PREFIX = "[dns-client] "
local timer_at = ngx.timer.at
local get_phase = ngx.get_phase
Expand Down Expand Up @@ -67,6 +70,8 @@ local typeOrder -- array with order of types to try
local clientErrors = { -- client specific errors
[100] = "cache only lookup failed",
[101] = "empty record received",
[102] = "invalid name, bad IPv4",
[103] = "invalid name, bad IPv6",
}

for _,v in ipairs(orderValids) do orderValids[v:upper()] = v end
Expand Down Expand Up @@ -980,8 +985,8 @@ local function check_ipv6(qname, qtype, try_list)
-- return a "server error"
try_status(try_list, "bad IPv6")
record = {
errcode = 3,
errstr = "name error",
errcode = 103,
errstr = clientErrors[103],
}
end
cacheinsert(record, qname, qtype)
Expand Down Expand Up @@ -1018,8 +1023,8 @@ local function check_ipv4(qname, qtype, try_list)
-- return a "server error"
try_status(try_list, "bad IPv4")
record = {
errcode = 3,
errstr = "name error",
errcode = 102,
errstr = clientErrors[102],
}
end
cacheinsert(record, qname, qtype)
Expand Down Expand Up @@ -1162,7 +1167,7 @@ local function resolve(qname, r_opts, dnsCacheOnly, try_list)
records = nil
-- luacheck: pop
err = "recursion detected"
try_status(try_list, "recursion detected")
try_status(try_list, err)
return nil, err, try_list
end
end
Expand All @@ -1181,7 +1186,7 @@ local function resolve(qname, r_opts, dnsCacheOnly, try_list)
-- check for CNAME records, and dereferencing the CNAME
if (records[1] or EMPTY).type == _M.TYPE_CNAME and qtype ~= _M.TYPE_CNAME then
opts.qtype = nil
try_status(try_list, "dereferencing")
try_status(try_list, "dereferencing CNAME")
return resolve(records[1].cname, opts, dnsCacheOnly, try_list)
end

Expand All @@ -1208,7 +1213,7 @@ local function resolve(qname, r_opts, dnsCacheOnly, try_list)
if records.errcode then
-- the query type didn't match the ip address, or a bad ip address
return nil,
("dns server error: %s %s"):format(records.errcode, records.errstr),
("dns client error: %s %s"):format(records.errcode, records.errstr),
try_list
end
-- valid ipv4 or ipv6
Expand Down Expand Up @@ -1289,7 +1294,7 @@ local function resolve(qname, r_opts, dnsCacheOnly, try_list)
if records[1].type == _M.TYPE_CNAME and qtype ~= _M.TYPE_CNAME then
-- dereference CNAME
opts.qtype = nil
try_status(try_list, "dereferencing")
try_status(try_list, "dereferencing CNAME")
return resolve(records[1].cname, opts, dnsCacheOnly, try_list)
end

Expand Down
8 changes: 5 additions & 3 deletions spec/01-unit/21-dns-client/02-client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local pretty = require("pl.pretty").write
-- define a constant for that error message
local NOT_FOUND_ERROR = "dns server error: 3 name error"
local EMPTY_ERROR = "dns client error: 101 empty record received"
local BAD_IPV4_ERROR = "dns client error: 102 invalid name, bad IPv4"
local BAD_IPV6_ERROR = "dns client error: 103 invalid name, bad IPv6"

local gettime, sleep
if ngx then
Expand Down Expand Up @@ -845,7 +847,7 @@ describe("[DNS client]", function()
false
)
assert.equal(0, callcount)
assert.equal(NOT_FOUND_ERROR, err)
assert.equal(BAD_IPV4_ERROR, err)
end)

it("fetching IPv6 address as AAAA type", function()
Expand Down Expand Up @@ -893,7 +895,7 @@ describe("[DNS client]", function()
false
)
assert.equal(0, callcount)
assert.equal(NOT_FOUND_ERROR, err)
assert.equal(BAD_IPV6_ERROR, err)
end)

it("fetching invalid IPv6 address", function()
Expand All @@ -908,7 +910,7 @@ describe("[DNS client]", function()

local answers, err, history = client.resolve(host)
assert.is_nil(answers)
assert.equal(NOT_FOUND_ERROR, err)
assert.equal(BAD_IPV6_ERROR, err)
assert(tostring(history):find("bad IPv6", nil, true))
end)

Expand Down