Skip to content

Commit

Permalink
Merge pull request #56 from Moesif/1.3.8
Browse files Browse the repository at this point in the history
Add: Support for regex and company sampling
  • Loading branch information
dkm199 authored Nov 4, 2021
2 parents c23b298 + 6063522 commit 4694f05
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 151 deletions.
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ is_original = yes
license = 2bsd
lib_dir = lua/
doc_dir = .
version = 1.3.7
version = 1.3.8
repo_link = https://github.com/Moesif/lua-resty-moesif
main_module = lua/resty/moesif/send_event.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package = "lua-resty-moesif" -- TODO: rename, must match the info in the filename of this rockspec!
-- as a convention; stick to the prefix: `kong-plugin-`
version = "1.3.7-1" -- TODO: renumber, must match the info in the filename of this rockspec!
-- The version '1.3.7' is the source code version, the trailing '1' is the version of this rockspec.
version = "1.3.8-1" -- TODO: renumber, must match the info in the filename of this rockspec!
-- The version '1.3.8' is the source code version, the trailing '1' is the version of this rockspec.
-- whenever the source version changes, the rockspec should be reset to 1. The rockspec version is only
-- updated (incremented) when this file changes, but the source remains the same.

Expand All @@ -12,7 +12,7 @@ local pluginName = package:match("^lua%-resty%-(.+)$") -- "moesif"
supported_platforms = {"linux", "macosx"}
source = {
url = "git://github.com/Moesif/lua-resty-moesif",
tag = "1.3.7"
tag = "1.3.8"
}

description = {
Expand Down
4 changes: 2 additions & 2 deletions lua/resty/moesif/connection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function _M.get_connection(config, api_endpoint, url_path, sock)
local parsed_url = helper.parse_url(api_endpoint..url_path)
local host = parsed_url.host
local port = tonumber(parsed_url.port)
local debug = config:get("debug")
local debug = config.debug

sock:settimeout(config:get("connect_timeout"))
sock:settimeout(config.connect_timeout)
local ok, err = sock:connect(host, port)
if not ok then
if debug then
Expand Down
92 changes: 90 additions & 2 deletions lua/resty/moesif/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ local base64 = require "base64"
-- @param `config` Configuration table
-- @return `response` a string with the api call response details
function _M.read_socket_data(socket, config)
socket:settimeout(config:get("timeout"))
socket:settimeout(config.timeout)
local response, err, partial = socket:receive("*a")
if (not response) and (err ~= 'timeout') then
return nil, err
end
response = response or partial
if not response then return nil, 'timeout' end
return response
return response, nil
end

-- Parse host url
Expand Down Expand Up @@ -71,4 +71,92 @@ function _M.parse_authorization_header(token, field)
return nil
end

-- Function to perform the regex matching with event value and condition value
-- @param `event_value` Value associated with event (request)
-- @param `condition_value` Value associated with the regex config condition
-- @return `regex_matched` Boolean flag to determine if the regex match was successful
local function regex_match (event_value, condition_value)
-- Perform regex match between event value and regex config condition value
return string.match(event_value, condition_value)
end

-- Function to fetch the sample rate and determine if request needs to be block or not
-- @param `gr_regex_configs` Regex configs associated with the governance rule
-- @param `request_config_mapping` Config associated with the request
-- @return `sample_rate, block` Sample rate and boolean flag (block or not)
function _M.fetch_sample_rate_block_request_on_regex_match(gr_regex_configs, request_config_mapping)
-- Iterate through the list of governance rule regex configs
for _, regex_rule in pairs(gr_regex_configs) do
-- Fetch the sample rate
local sample_rate = regex_rule["sample_rate"]
-- Fetch the conditions
local conditions = regex_rule["conditions"]
-- Bool flag to determine if the regex conditions are matched
local regex_matched = nil
-- Create a table to hold the conditions mapping (path and value)
local condition_table = {}

-- Iterate through the regex rule conditions and map the path and value
for _, condition in pairs(conditions) do
-- Add condition path -> value to the condition table
condition_table[condition["path"]] = condition["value"]
end

-- Iterate through conditions table and perform `and` operation between each conditions
for path, values in pairs(condition_table) do
-- Check if the path exists in the request config mapping
if request_config_mapping[path] ~= nil then
-- Fetch the value of the path in request config mapping
local event_data = request_config_mapping[path]
-- Perform regex matching with event value
regex_matched = regex_match(event_data, values)
else
-- Path does not exists in request config mapping, so no need to match regex condition rule
regex_matched = false
end

-- If one of the rule does not match, skip the condition and avoid matching other rules for the same condition
if not regex_matched then
break
end
end

-- If regex conditions matched, return sample rate and block request (true)
if regex_matched then
return sample_rate, true
end
end
-- If regex conditions are not matched, return default sample rate (nil) and do not block request (false)
return nil, false
end

-- Function to prepare config mapping
-- @param `message` Message to be logged
-- @return `regex_conifg` Regex config mapping
function _M.prepare_config_mapping(message)
local regex_config = {}
-- Config mapping for request.verb
if (message["request"]["verb"] ~= nil) then
regex_config["request.verb"] = message["request"]["verb"]
end
-- Config mapping for request.uri
if (message["request"]["uri"] ~= nil) then
local extracted = string.match(message["request"]["uri"], "http[s]*://[^/]+(/[^?]+)")
if extracted == nil then
extracted = '/'
end
regex_config["request.route"] = extracted
end
-- Config mapping for request.ip_address
if (message["request"]["ip_address"] ~= nil) then
regex_config["request.ip_address"] = message["request"]["ip_address"]
end
-- Config mapping for response.status
if (message["response"]["status"] ~= nil) then
regex_config["response.status"] = message["response"]["status"]
end

return regex_config
end

return _M
Loading

0 comments on commit 4694f05

Please sign in to comment.