Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing Policy: Fix upstream selector base path #1122

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Allow to use capture function in liquid templates. [PR #1107](https://github.com/3scale/APIcast/pull/1107), [THREESCALE-1911](https://issues.jboss.org/browse/THREESCALE-1911)
- OAuth 2.0 MTLS policy [PR #1101](https://github.com/3scale/APIcast/pull/1101) [Issue #1003](https://github.com/3scale/APIcast/issues/1003)
- Add an option to enable keepalive_timeout on gateway [THREESCALE-2886](https://issues.jboss.org/browse/THREESCALE-2886) [PR #1106](https://github.com/3scale/APIcast/pull/1106)
- Added a new replace path option in routing policy [THREESCALE-3512](https://issues.jboss.org/browse/THREESCALE-3512) [PR #1119](https://github.com/3scale/APIcast/pull/1119) [PR #1121](https://github.com/3scale/APIcast/pull/1121)
- Added a new replace path option in routing policy [THREESCALE-3512](https://issues.jboss.org/browse/THREESCALE-3512) [PR #1119](https://github.com/3scale/APIcast/pull/1119) [PR #1121](https://github.com/3scale/APIcast/pull/1121) [PR #1122](https://github.com/3scale/APIcast/pull/1122)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion gateway/src/apicast/policy/routing/upstream_selector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function _M.select(_, rules, context)
upstream:use_host_header(rule.host_header)
end
if rule.replace_path then
upstream:set_path(rule.replace_path:render(context))
upstream:append_path(rule.replace_path:render(context))
-- Set uri as nil if not will be appended to the upstream
ngx.req.set_uri("/")
end
Expand Down
14 changes: 14 additions & 0 deletions gateway/src/apicast/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ function _M:set_path(path)
self.uri.path, self.uri.query = url_helper.split_path(path)
end

function _M:append_path(path)
local tmp_path, tmp_query = url_helper.split_path(path)
if not self.uri.path then
self.uri.path = "/"
end
self.uri.path = resty_url.join(self.uri.path, tmp_path)

-- If query is already present, do not need to add more.
if tmp_query and tmp_query ~= "" then
return
end
self.uri.query = tmp_query
end

--- Rewrite request Host header to what is provided in the argument or in the URL.
function _M:rewrite_request()
local uri = self.uri
Expand Down
10 changes: 5 additions & 5 deletions spec/policy/routing/upstream_selector_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('UpstreamSelector', function()
before_each(function()
stub(ngx_variable, 'available_context', function(context) return context end)
stub(ngx.req, 'set_uri', function(_) return true end)
stub(apicast_upstream, 'set_path', function(_) return true end)
stub(apicast_upstream, 'append_path', function(_) return true end)
end)

it('is not set', function()
Expand Down Expand Up @@ -170,8 +170,8 @@ describe('UpstreamSelector', function()
assert.spy(ngx.req.set_uri).was_called()
assert.spy(ngx.req.set_uri).was.called_with("/")

assert.spy(apicast_upstream.set_path).was_called()
assert.spy(apicast_upstream.set_path).was.called_with(upstream, "")
assert.spy(apicast_upstream.append_path).was_called()
assert.spy(apicast_upstream.append_path).was.called_with(upstream, "")
end)

it('is correctly set', function()
Expand All @@ -187,8 +187,8 @@ describe('UpstreamSelector', function()
assert.spy(ngx.req.set_uri).was_called()
assert.spy(ngx.req.set_uri).was.called_with("/")

assert.spy(apicast_upstream.set_path).was_called()
assert.spy(apicast_upstream.set_path).was.called_with(upstream, "bar")
assert.spy(apicast_upstream.append_path).was_called()
assert.spy(apicast_upstream.append_path).was.called_with(upstream, "bar")
end)
end)
end)
Expand Down
16 changes: 16 additions & 0 deletions spec/upstream_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ describe('Upstream', function()
end)
end)


describe(':append_path', function()
it('return valid path when is not set', function()
local up = Upstream.new('http://host:8090')
up:append_path("/test")
assert.same(up.uri.path, "/test")
end)

it('leading slash is removed', function()
local up = Upstream.new('http://host:8090/')
up:append_path("/test/")
assert.same(up.uri.path, "/test/")
end)

end)

local function stub_ngx_request()
ngx.var = { }

Expand Down
53 changes: 53 additions & 0 deletions t/apicast-policy-routing.t
Original file line number Diff line number Diff line change
Expand Up @@ -1741,3 +1741,56 @@ yay, api backend
--- error_code: 200
--- no_error_log
[error]

=== TEST 27: replace path does not overwrite url base path.
--- configuration
{
"services": [
{
"id": 42,
"proxy": {
"policy_chain": [
{
"name": "apicast.policy.routing",
"configuration": {
"rules": [
{
"url": "http://test:$TEST_NGINX_SERVER_PORT/basePath/",
"replace_path": "{{original_request.path }}-test",
"condition": {
"operations": [
{
"match": "liquid",
"liquid_value": "{{ original_request.path }}",
"op": "matches",
"value": "bridge"
}
]
}
}
]
}
},
{
"name": "apicast.policy.echo"
}
]
}
}
]
}
--- upstream
location ~* /basePath/bridge-1-test$ {
content_by_lua_block {
local args = ngx.req.get_uri_args()
require('luassert').equals('foo', args["one"])
ngx.say('yay, api backend');
}
}
--- request
GET /bridge-1?one=foo
--- response_body
yay, api backend
--- error_code: 200
--- no_error_log
[error]