You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mh...@apache.org on 2020/04/01 14:43:21 UTC
[openwhisk-apigateway] branch master updated: Fix getUriPath logic
to ignore API tenant base path (#363)
This is an automated email from the ASF dual-hosted git repository.
mhamann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-apigateway.git
The following commit(s) were added to refs/heads/master by this push:
new ca0fae0 Fix getUriPath logic to ignore API tenant base path (#363)
ca0fae0 is described below
commit ca0fae060c658a0edc861e90f8661310fa9de125
Author: Hidematsu Sueki <hs...@gmail.com>
AuthorDate: Wed Apr 1 10:43:12 2020 -0400
Fix getUriPath logic to ignore API tenant base path (#363)
---
README.md | 8 ++------
scripts/lua/policies/backendRouting.lua | 8 +++++---
tests/scripts/lua/policies/backendRouting.lua | 28 +++++++++++++++++++--------
3 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/README.md b/README.md
index 3b6ff6c..73b2b40 100644
--- a/README.md
+++ b/README.md
@@ -104,11 +104,7 @@ Then make changes to any configuration file ( i.e. `api-gateway.conf` ), save it
### Testing
- First install the necessary dependencies:
+ To build the test image and run unit tests
```
- make test-build
- ```
- Then, run the unit tests:
- ```
- make test-run
+ make test
```
diff --git a/scripts/lua/policies/backendRouting.lua b/scripts/lua/policies/backendRouting.lua
index c1efbad..646de94 100644
--- a/scripts/lua/policies/backendRouting.lua
+++ b/scripts/lua/policies/backendRouting.lua
@@ -41,7 +41,7 @@ function _M.setRouteWithOverride(backendUrl, gatewayPath, override)
u.path = utils.concatStrings({u.path:sub(1, -16), u.path:sub(-16, -16) == '/' and '' or '/', gatewayPath})
ngx.req.set_uri(u.path)
else
- ngx.req.set_uri(getUriPath(u.path))
+ ngx.req.set_uri(_M.getUriPath(u.path))
end
ngx.var.backendUrl = backendUrl
@@ -74,7 +74,7 @@ function _M.setDynamicRoute(obj)
u = url.parse(utils.concatStrings({'http://', dynamicBackend}))
end
if utils.tableContains(whitelist, u.host) then
- ngx.req.set_uri(getUriPath(u.path))
+ ngx.req.set_uri(_M.getUriPath(u.path))
-- Split the dynamicBackend url to get the query parameters in the exact order that it was passed in.
-- Don't use u.query here because it returns the parameters in an unordered lua table.
local split = {string.match(dynamicBackend, '([^?]*)?(.*)')}
@@ -91,10 +91,12 @@ function _M.setDynamicRoute(obj)
end
end
-function getUriPath(backendPath)
+function _M.getUriPath(backendPath)
local gatewayPath = ngx.unescape_uri(ngx.var.gatewayPath)
gatewayPath = gatewayPath:gsub('-', '%%-')
+ local tenant = ngx.var.tenant:gsub('-', '%%-')
local uri = string.gsub(ngx.var.request_uri, '?.*', '')
+ uri = uri:gsub('/api/' .. tenant, '')
local _, j = uri:find(gatewayPath)
local incomingPath = ((j and uri:sub(j + 1)) or nil)
-- Check for backendUrl path
diff --git a/tests/scripts/lua/policies/backendRouting.lua b/tests/scripts/lua/policies/backendRouting.lua
index 06b812f..2f3e10b 100644
--- a/tests/scripts/lua/policies/backendRouting.lua
+++ b/tests/scripts/lua/policies/backendRouting.lua
@@ -28,17 +28,29 @@ describe('Testing backend routing module', function()
end)
it('should work without override', function()
- ngx.var.request_uri = "/api/23bc46b1-71f6-4ed5-8c54-816aa4f8c502/hello/world"
- backendRouting.setRoute("https://localhost:3233/api/v1/web/guest/default/hello2.json", "hello/world")
+ ngx.var.gatewayPath = 'hello/world'
+ ngx.var.tenant = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502'
+ ngx.var.request_uri = '/api/' .. ngx.var.tenant .. '/' .. ngx.var.gatewayPath
+ backendRouting.setRoute("https://localhost:3233/api/v1/web/guest/default/hello2.json", ngx.var.gatewayPath)
assert.are.same(ngx.var.upstream, 'https://localhost:3233')
assert.are.same(ngx.var.backendUrl, 'https://localhost:3233/api/v1/web/guest/default/hello2.json')
end)
it('should work with override', function()
- ngx.var.request_uri = "/api/23bc46b1-71f6-4ed5-8c54-816aa4f8c502/hello/world"
- backendRouting.setRouteWithOverride("https://localhost:3233/api/v1/web/guest/default/hello2.json", "hello/world",
- "http://172.0.0.1:3456")
- assert.are.same(ngx.var.upstream, 'http://172.0.0.1:3456')
- assert.are.same(ngx.var.backendUrl, 'http://172.0.0.1:3456/api/v1/web/guest/default/hello2.json')
- end)
+ ngx.var.gatewayPath = 'hello/world'
+ ngx.var.tenant = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502'
+ ngx.var.request_uri = '/api/' .. ngx.var.tenant .. '/' .. ngx.var.gatewayPath
+ backendRouting.setRouteWithOverride("https://localhost:3233/api/v1/web/guest/default/hello2.json", ngx.var.gatewayPath,
+ "http://172.0.0.1:3456")
+ assert.are.same(ngx.var.upstream, 'http://172.0.0.1:3456')
+ assert.are.same(ngx.var.backendUrl, 'http://172.0.0.1:3456/api/v1/web/guest/default/hello2.json')
+ end)
+
+ it('should match URI properly, ignoring API tenant base path', function()
+ ngx.var.gatewayPath = 'api'
+ ngx.var.tenant = '23bc46b1-71f6-4ed5-8c54-816aa4f8c502'
+ ngx.var.request_uri = '/api/' .. ngx.var.tenant .. '/' .. ngx.var.gatewayPath
+ actual = backendRouting.getUriPath('/api')
+ assert.are.same(actual, '/api')
+ end)
end)