Skip to content

Commit 906bf84

Browse files
committed
[dns] try empty search scope
because in some configurations it is possible to have "naked" domain like in docker when linking containers so it is necessary to query for "redis." not for "redis"
1 parent 720c50b commit 906bf84

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

apicast/src/resty/resolver.lua

+9-23
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ local default_resolver_port = 53
3131
local _M = {
3232
_VERSION = '0.1',
3333
_nameservers = {},
34-
search = {}
34+
search = { '' }
3535
}
3636

3737
local mt = { __index = _M }
@@ -67,7 +67,7 @@ function _M.parse_nameservers(path)
6767

6868
ngx.log(ngx.DEBUG, '/etc/resolv.conf:\n', resolv_conf)
6969

70-
local search = {}
70+
local search = { '' }
7171
local nameservers = { search = search }
7272
local resolver = getenv('RESOLVER')
7373
local domains = match(resolv_conf, 'search%s+([^\n]+)')
@@ -185,14 +185,6 @@ local function is_ip(address)
185185
end
186186
end
187187

188-
local function has_tld(qname)
189-
return match(qname, '%.')
190-
end
191-
192-
local function have_addresses(answers)
193-
return answers and next(answers.addresses or {}) and #answers > 0 and not answers.errcode
194-
end
195-
196188
local function convert_answers(answers, port)
197189
local servers = {}
198190

@@ -216,18 +208,13 @@ local function lookup(dns, qname, search, options)
216208
ngx.log(ngx.DEBUG, 'host is ip address: ', qname)
217209
answers = { new_answer(qname) }
218210
else
219-
answers, err = dns:query(qname, options)
211+
for i=1, #search do
212+
local query = qname .. '.' .. search[i]
213+
ngx.log(ngx.DEBUG, 'resolver query: ', qname, ' search: ', search[i], ' query: ', query)
214+
answers, err = dns:query(query, options)
220215

221-
if not has_tld(qname) and not have_addresses(answers) then
222-
for i=1, #search do
223-
224-
local query = qname .. '.' .. search[i]
225-
ngx.log(ngx.DEBUG, 'resolver query: ', qname, ' search: ', search[i], ' query: ', query)
226-
answers, err = dns:query(query, options)
227-
228-
if answers and not answers.errcode and #answers > 0 then
229-
break
230-
end
216+
if answers and not answers.errcode and #answers > 0 then
217+
break
231218
end
232219
end
233220
end
@@ -249,8 +236,7 @@ function _M.get_servers(self, qname, opts)
249236
end
250237

251238
local cache = self.cache
252-
local search = self.search or {}
253-
239+
local search = self.search or _M.search
254240

255241
-- TODO: pass proper options to dns resolver (like SRV query type)
256242

docker-compose.yml

+6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ services:
2121
environment:
2222
TEST_NGINX_BINARY: openresty
2323
TEST_NGINX_REDIS_HOST: redis
24+
dns_search:
25+
- example.com
2426
test:
2527
image: ${IMAGE_NAME}
2628
depends_on:
2729
- gateway
2830
entrypoint: ""
2931
dns: 127.0.0.1
32+
dns_search:
33+
- example.com
3034
prove:
3135
image: ${IMAGE_NAME}
3236
user: root
@@ -35,6 +39,8 @@ services:
3539
TEST_NGINX_APICAST_PATH: /opt/app
3640
TEST_NGINX_REDIS_HOST: redis
3741
command: prove
42+
dns_search:
43+
- example.com
3844
depends_on:
3945
- redis
4046
volumes:

spec/resty/resolver_spec.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('resty.resolver', function()
4848

4949
assert.falsy(err)
5050
assert.equal(1, #servers)
51-
assert.spy(dns.query).was.called_with(dns, '3scale.net', { qtype = 'A' })
51+
assert.spy(dns.query).was.called_with(dns, '3scale.net.', { qtype = 'A' })
5252
end)
5353

5454
it('skips answers with no address', function()
@@ -63,7 +63,7 @@ describe('resty.resolver', function()
6363

6464
assert.falsy(err)
6565
assert.equal(1, #servers)
66-
assert.spy(dns.query).was.called_with(dns, 'www.3scale.net', {})
66+
assert.spy(dns.query).was.called_with(dns, 'www.3scale.net.', {})
6767
end)
6868

6969
it('searches domains', function()
@@ -77,13 +77,13 @@ describe('resty.resolver', function()
7777
end
7878
end)
7979
resolver.options = { qtype = 'A' }
80-
resolver.search = { 'example.com', 'net' }
80+
resolver.search = { '', 'example.com', 'net' }
8181

8282
local servers, err = resolver:get_servers('3scale')
8383

8484
assert.falsy(err)
8585
assert.equal(1, #servers)
86-
assert.spy(dns.query).was.called_with(dns, '3scale', resolver.options)
86+
assert.spy(dns.query).was.called_with(dns, '3scale.', resolver.options)
8787
assert.spy(dns.query).was.called_with(dns, '3scale.example.com', resolver.options)
8888
assert.spy(dns.query).was.called_with(dns, '3scale.net', resolver.options)
8989
end)
@@ -139,8 +139,8 @@ describe('resty.resolver', function()
139139
it('returns search domains', function()
140140
local search = resty_resolver.parse_nameservers(tmpname).search
141141

142-
assert.equal(2, #search)
143-
assert.same({ 'localdomain.example.com', 'local' }, search)
142+
assert.equal(3, #search)
143+
assert.same({ '', 'localdomain.example.com', 'local' }, search)
144144
end)
145145

146146
end)

0 commit comments

Comments
 (0)