Skip to content

Commit

Permalink
Move the proxy DNS resolver code into gateway/src/resty/resolver/http…
Browse files Browse the repository at this point in the history
….lua

To avoid having to call a resolver when used with proxies, all DNS resolution
should happen inside the connect() method
  • Loading branch information
tkan145 committed Feb 9, 2024
1 parent 32781dd commit 9436b44
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Docker compose up instead of docker compose run [PR #1442](https://github.com/3scale/APIcast/pull/1442)

- Fix integration of upstream connection policy with camel policy [PR #1443](https://github.com/3scale/APIcast/pull/1443) [THREESCALE-10582](https://issues.redhat.com/browse/THREESCALE-10582)

- Upgrade lua-resty-http to 0.17.1 to fix 100 response header are not handled when using `HTTPS_PROXY` [PR #1434](https://github.com/3scale/APIcast/pull/1434) [THREESCALE-10278](https://issues.redhat.com/browse/THREESCALE-10278)

### Added
Expand Down
7 changes: 4 additions & 3 deletions gateway/src/resty/http/proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ local function connect(request)
proxy_uri.port = default_port(proxy_uri)
end

-- Resolve the proxy IP/Port
local proxy_host, proxy_port = httpc:resolve(proxy_uri.host, proxy_uri.port)
local proxy_url = format("%s://%s:%s", proxy_uri.scheme, proxy_host, proxy_port)
local proxy_url = format("%s://%s:%s", proxy_uri.scheme, proxy_uri.host, proxy_uri.port)
local proxy_auth = request.proxy_auth

if scheme == 'http' then
-- Used by http_ng module to send request to 3scale backend through proxy.

-- http proxy needs absolute URL as the request path, lua-resty-http 1.17.1 will
-- construct a path_prefix based on host and port so we only set request path here
--
Expand Down Expand Up @@ -128,6 +128,7 @@ local function connect(request)
ngx.log(ngx.DEBUG, 'targeting server ', host, ':', port)
else
-- Connect direct
-- Mostly used by http_ng module to connect 3scale backend module.
local ok, err = httpc:connect(options)
if not ok then return nil, err end

Expand Down
17 changes: 17 additions & 0 deletions gateway/src/resty/resolver/http.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local resty_http = require 'resty.http'
local resty_resolver = require 'resty.resolver'
local round_robin = require 'resty.balancer.round_robin'
local url_helper = require('resty.url_helper')
local format = string.format

local setmetatable = setmetatable

Expand Down Expand Up @@ -53,6 +55,21 @@ function _M.connect(self, options, ...)
ip, real_port = self:resolve(options.host, options.port)
options.host = ip
options.port = real_port
else
local proxy_uri, err = url_helper.parse_url(proxy)
if not proxy_uri then
return nil, 'invalid proxy: ' .. err

Check warning on line 61 in gateway/src/resty/resolver/http.lua

View check run for this annotation

Codecov / codecov/patch

gateway/src/resty/resolver/http.lua#L61

Added line #L61 was not covered by tests
end

-- Resolve the proxy IP/Port
local proxy_host, proxy_port = self:resolve(proxy_uri.host, proxy_uri.port)
local proxy_url = format("%s://%s:%s", proxy_uri.scheme, proxy_host, proxy_port)

if proxy_opts.http_proxy then
options.proxy_opts.http_proxy = proxy_url
elseif proxy_opts.https_proxy then
options.proxy_opts.https_proxy = proxy_url

Check warning on line 71 in gateway/src/resty/resolver/http.lua

View check run for this annotation

Codecov / codecov/patch

gateway/src/resty/resolver/http.lua#L70-L71

Added lines #L70 - L71 were not covered by tests
end
end

local ok, err = resty_http.connect(self, options, ...)
Expand Down

0 comments on commit 9436b44

Please sign in to comment.