Skip to content

Commit

Permalink
Merge pull request #636 from 3scale/fix-duplicity-nameserver-resolver
Browse files Browse the repository at this point in the history
Avoid nameserver duplication
  • Loading branch information
H. Daniel Cesario authored Mar 1, 2018
2 parents dbbf2fb + 3afea98 commit 7cdca78
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Error loading policy chain configuration JSON with null value [PR #626](https://github.com/3scale/apicast/pull/626)
- Splitted `resolv.conf` in lines,to avoid commented lines [PR #618](https://github.com/3scale/apicast/pull/618)
- Avoid `nameserver` repetion from `RESOLVER` variable and `resolv.conf` file [PR #636](https://github.com/3scale/apicast/pull/636)

## Added

Expand Down
3 changes: 2 additions & 1 deletion gateway/src/resty/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ function _M.parse_nameservers(path)

local server = match(line, '^nameserver%s+([^%s]+)')
-- TODO: implement port matching based on https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=549190
if server and server ~= resolver then
-- meanwhile assuming default port 53.
if server and format("%s:%s", server, default_resolver_port) ~= tostring(resolver) then
insert(nameservers, nameserver.new(server))
end
end
Expand Down
11 changes: 10 additions & 1 deletion spec/resty/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,19 @@ nameserver 127.0.0.1
local nameservers = resty_resolver.parse_nameservers(tmpname)

assert.equal(2, #nameservers)
assert.same({ '127.0.0.2', 53 }, nameservers[1])
assert.same({ '127.0.0.2', 53 }, nameservers[1])
assert.same({ '127.0.0.1', 53 }, nameservers[2])
end)

it('do not replicates nameservers from resolver env var', function()
resty_env.set('RESOLVER', '127.0.0.1')
local nameservers = resty_resolver.parse_nameservers(tmpname)

assert.equal(2, #nameservers)
assert.same({ '127.0.0.1', 53 }, nameservers[1])
assert.same({ '127.0.0.2', 53 }, nameservers[2])
end)

it('returns search domains', function()
local search = resty_resolver.parse_nameservers(tmpname).search

Expand Down
26 changes: 26 additions & 0 deletions t/resolver.t
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,29 @@ GET /t
nameservers: 1 [dead::beef]:5353
--- user_files
>>> resolv.conf
=== TEST 4: do not duplicate nameserver from RESOLVER
nameservers should not repeat if already configured
--- main_config
env RESOLVER='127.0.1.1:53';
--- http_config
lua_package_path "$TEST_NGINX_LUA_PATH";
init_worker_by_lua_block {
require('resty.resolver').init('$TEST_NGINX_RESOLV_CONF')
}
--- config
location = /t {
content_by_lua_block {
local nameservers = require('resty.resolver').nameservers()
ngx.say('nameservers: ', #nameservers, ' ', nameservers[1], ' ', nameservers[2])
}
}
--- request
GET /t
--- response_body
nameservers: 2 127.0.1.153 1.2.3.453
--- user_files
>>> resolv.conf
nameserver 127.0.1.1
nameserver 1.2.3.4

0 comments on commit 7cdca78

Please sign in to comment.