Skip to content

Commit

Permalink
[dns] try empty search scope
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
mikz committed Feb 24, 2017
1 parent 01e773d commit 1e47e2d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use stale DNS cache when there is a query in progress for that record [PR #260](https://github.com/3scale/apicast/pull/260)
- Bump s2i-openresty to 1.11.2.2-2 [PR #260](https://github.com/3scale/apicast/pull/260)
- Echo API on port 8081 listens accepts any Host [PR #268](https://github.com/3scale/apicast/pull/268)
- Always use DNS search scopes [PR #271](https://github.com/3scale/apicast/pull/271)

### Added

Expand Down
32 changes: 9 additions & 23 deletions apicast/src/resty/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local default_resolver_port = 53
local _M = {
_VERSION = '0.1',
_nameservers = {},
search = {}
search = { '' }
}

local mt = { __index = _M }
Expand Down Expand Up @@ -67,7 +67,7 @@ function _M.parse_nameservers(path)

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

local search = {}
local search = { '' }
local nameservers = { search = search }
local resolver = getenv('RESOLVER')
local domains = match(resolv_conf, 'search%s+([^\n]+)')
Expand Down Expand Up @@ -185,14 +185,6 @@ local function is_ip(address)
end
end

local function has_tld(qname)
return match(qname, '%.')
end

local function have_addresses(answers)
return answers and next(answers.addresses or {}) and #answers > 0 and not answers.errcode
end

local function convert_answers(answers, port)
local servers = {}

Expand All @@ -216,18 +208,13 @@ local function lookup(dns, qname, search, options)
ngx.log(ngx.DEBUG, 'host is ip address: ', qname)
answers = { new_answer(qname) }
else
answers, err = dns:query(qname, options)
for i=1, #search do
local query = qname .. '.' .. search[i]
ngx.log(ngx.DEBUG, 'resolver query: ', qname, ' search: ', search[i], ' query: ', query)
answers, err = dns:query(query, options)

if not has_tld(qname) and not have_addresses(answers) then
for i=1, #search do

local query = qname .. '.' .. search[i]
ngx.log(ngx.DEBUG, 'resolver query: ', qname, ' search: ', search[i], ' query: ', query)
answers, err = dns:query(query, options)

if answers and not answers.errcode and #answers > 0 then
break
end
if answers and not answers.errcode and #answers > 0 then
break
end
end
end
Expand All @@ -249,8 +236,7 @@ function _M.get_servers(self, qname, opts)
end

local cache = self.cache
local search = self.search or {}

local search = self.search or _M.search

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

Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ services:
environment:
TEST_NGINX_BINARY: openresty
TEST_NGINX_REDIS_HOST: redis
dns_search:
- example.com
test:
image: ${IMAGE_NAME}
depends_on:
- gateway
entrypoint: ""
dns: 127.0.0.1
dns_search:
- example.com
prove:
image: ${IMAGE_NAME}
user: root
Expand All @@ -35,6 +39,8 @@ services:
TEST_NGINX_APICAST_PATH: /opt/app
TEST_NGINX_REDIS_HOST: redis
command: prove
dns_search:
- example.com
depends_on:
- redis
volumes:
Expand Down
12 changes: 6 additions & 6 deletions spec/resty/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('resty.resolver', function()

assert.falsy(err)
assert.equal(1, #servers)
assert.spy(dns.query).was.called_with(dns, '3scale.net', { qtype = 'A' })
assert.spy(dns.query).was.called_with(dns, '3scale.net.', { qtype = 'A' })
end)

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

assert.falsy(err)
assert.equal(1, #servers)
assert.spy(dns.query).was.called_with(dns, 'www.3scale.net', {})
assert.spy(dns.query).was.called_with(dns, 'www.3scale.net.', {})
end)

it('searches domains', function()
Expand All @@ -77,13 +77,13 @@ describe('resty.resolver', function()
end
end)
resolver.options = { qtype = 'A' }
resolver.search = { 'example.com', 'net' }
resolver.search = { '', 'example.com', 'net' }

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

assert.falsy(err)
assert.equal(1, #servers)
assert.spy(dns.query).was.called_with(dns, '3scale', resolver.options)
assert.spy(dns.query).was.called_with(dns, '3scale.', resolver.options)
assert.spy(dns.query).was.called_with(dns, '3scale.example.com', resolver.options)
assert.spy(dns.query).was.called_with(dns, '3scale.net', resolver.options)
end)
Expand Down Expand Up @@ -139,8 +139,8 @@ describe('resty.resolver', function()
it('returns search domains', function()
local search = resty_resolver.parse_nameservers(tmpname).search

assert.equal(2, #search)
assert.same({ 'localdomain.example.com', 'local' }, search)
assert.equal(3, #search)
assert.same({ '', 'localdomain.example.com', 'local' }, search)
end)

end)
Expand Down

0 comments on commit 1e47e2d

Please sign in to comment.