Skip to content

Commit

Permalink
[resty.resolver] properly parse ipv6 RESOLVER env
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed Nov 29, 2017
1 parent dc31685 commit 4344391
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Fixed

- Loading installed luarocks from outside rover [PR #503](https://github.com/3scale/apicast/pull/503)
- Support IPv6 addresses in `/etc/resolv.conf` [PR #511](https://github.com/3scale/apicast/pull/511)

## [3.2.0-alpha1]

Expand Down
18 changes: 14 additions & 4 deletions gateway/src/resty/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ local insert = table.insert
local getenv = os.getenv
local concat = table.concat
local io_type = io.type

require('resty.core.regex') -- to allow use of ngx.re.match in the init phase

local re_match = ngx.re.match
local resolver_cache = require 'resty.resolver.cache'
local dns_client = require 'resty.resolver.dns_client'
Expand Down Expand Up @@ -98,10 +101,17 @@ function _M.parse_nameservers(path)
end

if resolver then
local m = re.split(resolver, ':', 'oj')
insert(nameservers, nameserver.new(m[1], m[2]))
-- we are going to use all resolvers, because we can't trust dnsmasq
-- see https://github.com/3scale/apicast/issues/321 for more details
local m

m, err = re_match(resolver, '^(.+?)(?<!:)(?:\\:(\\d+))?$', 'oj')

if m then
-- we are going to use all resolvers, because we can't trust dnsmasq
-- see https://github.com/3scale/apicast/issues/321 for more details
insert(nameservers, nameserver.new(m[1], m[2]))
else
ngx.log(ngx.ERR, 'invalid resolver ', resolver, ' error: ', err)
end
end

for server in gmatch(resolv_conf, 'nameserver%s+([^%s]+)') do
Expand Down
25 changes: 25 additions & 0 deletions t/resolver.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ servers: 2
2.3.4.5:6789
--- no_error_log
[error]
=== TEST 3: can have ipv6 RESOLVER
RESOLVER env variable can be IPv6 address
--- ONLY
--- main_config
env RESOLVER=[dead::beef]:5353;
--- 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, ' ', tostring(nameservers[1]))
}
}
--- request
GET /t
--- response_body
nameservers: 1 [dead::beef]:5353
--- user_files
>>> resolv.conf

0 comments on commit 4344391

Please sign in to comment.