Skip to content

Commit

Permalink
Add ANY method on mapping rules.
Browse files Browse the repository at this point in the history
This commits adds a ANY method to match any method in the mapping_rule
so allow all and only block by the request uri.

Signed-off-by: Eloy Coto <eloy.coto@gmail.com>
  • Loading branch information
eloycoto committed May 17, 2019
1 parent e13b002 commit 068d5e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
6 changes: 4 additions & 2 deletions gateway/src/apicast/mapping_rule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ local re_match = ngx.re.match
local insert = table.insert
local re_gsub = ngx.re.gsub

local _M = {}
local _M = {
any_method = "ANY"
}

local mt = { __index = _M }

Expand Down Expand Up @@ -132,7 +134,7 @@ end
-- @tparam table args Table with the args and values of an HTTP request.
-- @treturn boolean Whether the mapping rule matches the given request.
function _M:matches(method, uri, args)
local match = self.method == method and
local match = (self.method == self.any_method or self.method == method) and
matches_uri(self.regexpified_pattern, uri) and
self.querystring_params(args)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ local default_type = 'plain'

local new = _M.new

local any_method = 'ANY'
local any_method = MappingRule.any_method

local function create_template(value, value_type)
return TemplateString.new(value, value_type or default_type)
Expand Down Expand Up @@ -160,13 +160,6 @@ end

local function validate_scope_access(scope, context, uri, request_method)
for _, method in ipairs(scope.methods) do
-- make a matched method just in case that `ANY` method is defined and
-- the mapping rules does not match, the matched_method need to be
-- cleared in the next interaction to trigger with the correct method.
local matched_method = request_method
if method == any_method then
matched_method = any_method
end

local resource = scope.resource_template_string:render(context)

Expand All @@ -178,7 +171,7 @@ local function validate_scope_access(scope, context, uri, request_method)
metric_system_name = 'hits'
})

if mapping_rule:matches(matched_method, uri) then
if mapping_rule:matches(request_method, uri) then
if match_realm_roles(scope, context) and match_client_roles(scope, context) then
return true
end
Expand Down
20 changes: 20 additions & 0 deletions spec/mapping_rule_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ describe('mapping_rule', function()
assert.is_true(mapping_rule:matches('GET', '/foo/a:b/bar'))
assert.is_true(mapping_rule:matches('GET', "/foo/a%b/bar"))
end)
end)

describe('.any_method', function()

it("Allow connections when any method is defined", function()

local mapping_rule = MappingRule.from_proxy_rule({
http_method = MappingRule.any_method,
pattern = '/foo/',
querystring_parameters = { },
metric_system_name = 'hits',
delta = 1
})

assert.is_true(mapping_rule:matches('GET', '/foo/'))
assert.is_true(mapping_rule:matches('POST', '/foo/'))
assert.is_true(mapping_rule:matches('PUT', "/foo/"))
assert.is_true(mapping_rule:matches('DELETE', "/foo/"))
assert.is_true(mapping_rule:matches('PATCH', "/foo/"))
end)
end)

end)

0 comments on commit 068d5e9

Please sign in to comment.