From da84141d8fd660caa0ca15f6a9291ca45a3eb6a1 Mon Sep 17 00:00:00 2001 From: maneta Date: Mon, 26 Feb 2018 19:43:37 +0100 Subject: [PATCH 1/2] break resolv.conf in lines --- gateway/src/resty/resolver.lua | 24 +++++++++++++++--------- spec/resty/resolver_spec.lua | 8 ++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/gateway/src/resty/resolver.lua b/gateway/src/resty/resolver.lua index 82800acca..43083aca0 100644 --- a/gateway/src/resty/resolver.lua +++ b/gateway/src/resty/resolver.lua @@ -113,13 +113,6 @@ function _M.parse_nameservers(path) local search = { } local nameservers = { search = search } - local domains = match(resolv_conf, 'search%s+([^\n]+)') - - ngx.log(ngx.DEBUG, 'search ', domains) - for domain in gmatch(domains or '', '([^%s]+)') do - ngx.log(ngx.DEBUG, 'search domain: ', domain) - insert(search, domain) - end local resolver resolver, err = _M.parse_resolver(resty_env.value('RESOLVER')) @@ -131,10 +124,23 @@ function _M.parse_nameservers(path) -- see https://github.com/3scale/apicast/issues/321 for more details insert(nameservers, resolver) end + + for _,line in ipairs(re.split(resolv_conf, "\n+")) do + + local domains = match(line, '^search%s+([^\n]+)') + + if domains then + ngx.log(ngx.DEBUG, 'search ', domains) + + for domain in gmatch(domains or '', '([^%s]+)') do + ngx.log(ngx.DEBUG, 'search domain: ', domain) + insert(search, domain) + end + end - for server in gmatch(resolv_conf, 'nameserver%s+([^%s]+)') do + 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 ~= resolver then + if server and server ~= resolver then insert(nameservers, nameserver.new(server)) end end diff --git a/spec/resty/resolver_spec.lua b/spec/resty/resolver_spec.lua index 5e9a6dd3e..3f02781d3 100644 --- a/spec/resty/resolver_spec.lua +++ b/spec/resty/resolver_spec.lua @@ -163,6 +163,14 @@ describe('resty.resolver', function() before_each(function() tmpname = io.tmpfile() + tmpname:write('# nameserver updated in comentary\n') + tmpname:write('#nameserver updated in comentary\n') + tmpname:write('#comentary nameserver 1.2.3.4\n') + tmpname:write('#comentary nameserver\n') + tmpname:write('# search updated.example.com in comentary\n') + tmpname:write('#search updated in comentary\n') + tmpname:write('#search nameserver 1.2.3.4\n') + tmpname:write('#search nameserver\n') tmpname:write('search localdomain.example.com local\n') tmpname:write('nameserver 127.0.0.2\n') tmpname:write('nameserver 127.0.0.1\n') From 4dcf4902cb761485488b60959d7c407e7ddfc3d1 Mon Sep 17 00:00:00 2001 From: maneta Date: Tue, 27 Feb 2018 10:46:04 +0100 Subject: [PATCH 2/2] Test cases prove t/resolver.t Calling Write just once Expanding the t/resolver.t TEST 1 documentation Updating changelog for spliting `resolv.conf` in lines --- CHANGELOG.md | 1 + spec/resty/resolver_spec.lua | 24 +++++++++++++----------- t/resolver.t | 13 ++++++++++++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3d145d3e..9d223e16e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Fixed - 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) ## [3.2.0-beta1] - 2018-02-20 diff --git a/spec/resty/resolver_spec.lua b/spec/resty/resolver_spec.lua index 3f02781d3..75c26da66 100644 --- a/spec/resty/resolver_spec.lua +++ b/spec/resty/resolver_spec.lua @@ -163,17 +163,19 @@ describe('resty.resolver', function() before_each(function() tmpname = io.tmpfile() - tmpname:write('# nameserver updated in comentary\n') - tmpname:write('#nameserver updated in comentary\n') - tmpname:write('#comentary nameserver 1.2.3.4\n') - tmpname:write('#comentary nameserver\n') - tmpname:write('# search updated.example.com in comentary\n') - tmpname:write('#search updated in comentary\n') - tmpname:write('#search nameserver 1.2.3.4\n') - tmpname:write('#search nameserver\n') - tmpname:write('search localdomain.example.com local\n') - tmpname:write('nameserver 127.0.0.2\n') - tmpname:write('nameserver 127.0.0.1\n') + tmpname:write([[ +# nameserver updated in comentary +#nameserver updated in comentary +#comentary nameserver 1.2.3.4 +#comentary nameserver +# search updated.example.com in comentary +#search updated in comentary +#search nameserver 1.2.3.4 +#search nameserver +search localdomain.example.com local +nameserver 127.0.0.2 +nameserver 127.0.0.1 +]]) end) it('returns nameserver touples', function() diff --git a/t/resolver.t b/t/resolver.t index 8e2037f44..0823f08a2 100644 --- a/t/resolver.t +++ b/t/resolver.t @@ -13,7 +13,9 @@ run_tests(); __DATA__ === TEST 1: uses all resolvers -both RESOLVER env variable and resolvers in resolv.conf should be used +both RESOLVER env variable and resolvers in resolv.conf should be used. +checking if commented 'nameserver' and 'search' keywords impact on the +resolv.conf file parsing. --- main_config env RESOLVER=$TEST_NGINX_RESOLVER; --- http_config @@ -34,6 +36,15 @@ GET /t nameservers: 3 127.0.1.15353 1.2.3.453 4.5.6.753 --- user_files >>> resolv.conf +# nameserver updated in comentary +#nameserver updated in comentary +#comentary nameserver 1.2.3.4 +#comentary nameserver +# search updated.example.com in comentary +#search updated in comentary +#search nameserver 1.2.3.4 +#search nameserver +search localdomain.example.com local nameserver 1.2.3.4 nameserver 4.5.6.7