-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
policy/conditional: add first version of Engine
This is just a first very basic version of the conditional engine.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
local _M = {} | ||
|
||
local value_of = { | ||
request_method = function() return ngx.req.get_method() end, | ||
request_host = function() return ngx.var.host end, | ||
request_path = function() return ngx.var.uri end | ||
} | ||
|
||
function _M.evaluate(expression) | ||
local match_attr = ngx.re.match(expression, [[^([\w]+)$]], 'oj') | ||
|
||
if match_attr then | ||
return value_of[match_attr[1]]() | ||
end | ||
|
||
local match_attr_and_value = ngx.re.match(expression, [[^([\w]+) == "([\w/]+)"$]], 'oj') | ||
|
||
if not match_attr_and_value then | ||
return nil, 'Error while parsing the condition' | ||
end | ||
|
||
local entity = match_attr_and_value[1] | ||
local value = match_attr_and_value[2] | ||
|
||
return value_of[entity]() == value | ||
end | ||
|
||
return _M |