From 5482f0905f9a9b19915b0b4f17012359b89b36f2 Mon Sep 17 00:00:00 2001 From: Keyur Date: Tue, 16 Mar 2021 12:24:29 -0700 Subject: [PATCH] Add: Support to skip capturing events for location context Add: Support to skip capturing events for location context Refactor: README.md Bump version to 1.3.5 --- README.md | 3 + dist.ini | 2 +- ...kspec => lua-resty-moesif-1.3.5-1.rockspec | 6 +- lua/resty/moesif/send_event.lua | 11 +- lua/resty/moesif/send_event_3Scale.lua | 179 +++++++++--------- 5 files changed, 105 insertions(+), 96 deletions(-) rename lua-resty-moesif-1.3.4-1.rockspec => lua-resty-moesif-1.3.5-1.rockspec (92%) diff --git a/README.md b/README.md index 29a231d..5e0ea0e 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,9 @@ _If you installed for 3scale, you do not need to set this field as this is handl #### __`moesif_api_version`__ (optional) _boolean_, An optional API Version you want to tag this request with. +#### __`moesif_log_event`__ +(optional) _boolean_, An optional flag if set to `false`, will skip capturing api call for that location context. By default, all the api calls will be captured. For example, when `set $moesif_log_event false;` for a location context, Moesif will not log api calls for that location. + ## Troubleshooting ### Response body not being logged diff --git a/dist.ini b/dist.ini index 4c9d508..5f41e37 100644 --- a/dist.ini +++ b/dist.ini @@ -6,6 +6,6 @@ is_original = yes license = 2bsd lib_dir = lua/ doc_dir = . -version = 1.3.4 +version = 1.3.5 repo_link = https://github.com/Moesif/lua-resty-moesif main_module = lua/resty/moesif/send_event.lua \ No newline at end of file diff --git a/lua-resty-moesif-1.3.4-1.rockspec b/lua-resty-moesif-1.3.5-1.rockspec similarity index 92% rename from lua-resty-moesif-1.3.4-1.rockspec rename to lua-resty-moesif-1.3.5-1.rockspec index 1fb5550..aa27f2a 100644 --- a/lua-resty-moesif-1.3.4-1.rockspec +++ b/lua-resty-moesif-1.3.5-1.rockspec @@ -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.4-1" -- TODO: renumber, must match the info in the filename of this rockspec! --- The version '1.3.4' is the source code version, the trailing '1' is the version of this rockspec. +version = "1.3.5-1" -- TODO: renumber, must match the info in the filename of this rockspec! +-- The version '1.3.5' 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. @@ -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.4" + tag = "1.3.5" } description = { diff --git a/lua/resty/moesif/send_event.lua b/lua/resty/moesif/send_event.lua index dd1a4b3..0d72851 100644 --- a/lua/resty/moesif/send_event.lua +++ b/lua/resty/moesif/send_event.lua @@ -105,14 +105,17 @@ if isempty(config:get("authorization_user_id_field")) then end -- User Agent String -local user_agent_string = "lua-resty-moesif/1.3.4" +local user_agent_string = "lua-resty-moesif/1.3.5" -- Log Event if isempty(config:get("application_id")) then ngx.log(ngx.ERR, "[moesif] Please provide the Moesif Application Id"); else - local message = moesif_ser.prepare_message(config) + local logEvent = ngx.var.moesif_log_event + if (logEvent == nil or logEvent == '') or (string.lower(logEvent) == "true") then + local message = moesif_ser.prepare_message(config) - -- Execute/Log message - log.execute(config, message, user_agent_string, config:get("debug")) + -- Execute/Log message + log.execute(config, message, user_agent_string, config:get("debug")) + end end diff --git a/lua/resty/moesif/send_event_3Scale.lua b/lua/resty/moesif/send_event_3Scale.lua index a86756f..e2b7d37 100644 --- a/lua/resty/moesif/send_event_3Scale.lua +++ b/lua/resty/moesif/send_event_3Scale.lua @@ -148,7 +148,7 @@ if isempty(config:get("authorization_header_name")) then end -- User Agent String -local user_agent_string = "lua-resty-moesif-3scale/1.3.4" +local user_agent_string = "lua-resty-moesif-3scale/1.3.5" function dump(o) if type(o) == 'table' then @@ -374,117 +374,120 @@ end if isempty(config:get("application_id")) then ngx_log(ngx_log_ERR, "[moesif] Please provide the Moesif Application Id"); else - -- Prepare the Message - local message = moesif_ser.prepare_message(config) - - if nonEmpty(config:get("3scale_domain")) and nonEmpty(config:get("3scale_access_token")) then - - local debug = config:get("debug") - if debug then - ngx_log(ngx.DEBUG, "[moesif] 3Scale accessToken and domainName are provided. Will fetch the application configuration. - ") - end - - local auth_api_key = nil - local auth_app_id = nil - local auth_app_key_pair = nil - local user_id_name = string.lower(config:get("3scale_user_id_name")) - local company_id_name = string.lower(config:get("3scale_company_id_name")) - local auth_api_key_name = string.lower(config:get("3scale_auth_api_key")) - local auth_app_id_name = string.lower(config:get("3scale_auth_app_id")) - local auth_app_key_pair_name = string.lower(config:get("3scale_auth_app_key_pair")) - - if debug then - ngx_log(ngx.DEBUG, "[moesif] 3Scale User Id Name - ", user_id_name) - ngx_log(ngx.DEBUG, "[moesif] 3Scale Company Id Name - ", company_id_name) - ngx_log(ngx.DEBUG, "[moesif] 3Scale Auth API Key Name - ", auth_api_key_name) - ngx_log(ngx.DEBUG, "[moesif] 3Scale Auth App Id Name - ", auth_app_id_name) - ngx_log(ngx.DEBUG, "[moesif] 3Scale Auth App Key Pari Name - ", auth_app_key_pair_name) - end - - -- Read Request headers / query params - local req_headers = ngx.req.get_headers() - if debug then - ngx_log(ngx.DEBUG, "[moesif] Reading the request headers to fetch the credentials like app_id, app_key or user_key - ", dump(req_headers)) - end - local req_query_params = ngx.req.get_uri_args() - if debug then - ngx_log(ngx.DEBUG, "[moesif] Reading the request query params to fetch the credentials like app_id, app_key or user_key - ", dump(req_query_params)) - end - - -- Fetch credential from request header or query parameter location - auth_api_key = fetch_credentials(auth_api_key_name, req_headers, req_query_params) - auth_app_id = fetch_credentials(auth_app_id_name, req_headers, req_query_params) - auth_app_key_pair = fetch_credentials(auth_app_key_pair_name, req_headers, req_query_params) - - if debug then - ngx_log(ngx.DEBUG, "[moesif] Auth Api key after reading the request headers and query params - ", auth_api_key) - ngx_log(ngx.DEBUG, "[moesif] Auth App Id after reading the request headers and query params - ", auth_app_id) - ngx_log(ngx.DEBUG, "[moesif] Auth App key Pair after reading the request headers and query params - ", auth_app_key_pair) - end - - -- Authentication Mode - if nonEmpty(auth_app_id) and nonEmpty(auth_app_key_pair) then - message["session_token"] = auth_app_key_pair - if nonEmpty(ngx.shared.user_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair)) or nonEmpty(ngx.shared.company_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair)) then + local logEvent = ngx.var.moesif_log_event + if (logEvent == nil or logEvent == '') or (string.lower(logEvent) == "true") then + -- Prepare the Message + local message = moesif_ser.prepare_message(config) + + if nonEmpty(config:get("3scale_domain")) and nonEmpty(config:get("3scale_access_token")) then + + local debug = config:get("debug") if debug then - ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set User ID with AppId and App_Key Auth method - ", auth_app_id .. "-" .. auth_app_key_pair) + ngx_log(ngx.DEBUG, "[moesif] 3Scale accessToken and domainName are provided. Will fetch the application configuration. - ") end - message = set_entity_id(ngx.shared.user_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair), message, debug, "user_id") + + local auth_api_key = nil + local auth_app_id = nil + local auth_app_key_pair = nil + local user_id_name = string.lower(config:get("3scale_user_id_name")) + local company_id_name = string.lower(config:get("3scale_company_id_name")) + local auth_api_key_name = string.lower(config:get("3scale_auth_api_key")) + local auth_app_id_name = string.lower(config:get("3scale_auth_app_id")) + local auth_app_key_pair_name = string.lower(config:get("3scale_auth_app_key_pair")) + if debug then - ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set Company ID with AppId and App_Key Auth method - ", auth_app_id .. "-" .. auth_app_key_pair) + ngx_log(ngx.DEBUG, "[moesif] 3Scale User Id Name - ", user_id_name) + ngx_log(ngx.DEBUG, "[moesif] 3Scale Company Id Name - ", company_id_name) + ngx_log(ngx.DEBUG, "[moesif] 3Scale Auth API Key Name - ", auth_api_key_name) + ngx_log(ngx.DEBUG, "[moesif] 3Scale Auth App Id Name - ", auth_app_id_name) + ngx_log(ngx.DEBUG, "[moesif] 3Scale Auth App Key Pari Name - ", auth_app_key_pair_name) end - message = set_entity_id(ngx.shared.company_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair), message, debug, "company_id") + + -- Read Request headers / query params + local req_headers = ngx.req.get_headers() if debug then - ngx_log(ngx.DEBUG, "[moesif] Log the Event for AppId and App_Key Auth method - ", auth_app_id .. "-" .. auth_app_key_pair) + ngx_log(ngx.DEBUG, "[moesif] Reading the request headers to fetch the credentials like app_id, app_key or user_key - ", dump(req_headers)) end - logEvent(config, message) - else + local req_query_params = ngx.req.get_uri_args() if debug then - ngx_log(ngx.DEBUG, "[moesif] Calling the function to fetch the 3Scale config with AppId and App_Key Auth method and log the Event - ", auth_app_id .. "-" .. auth_app_key_pair) + ngx_log(ngx.DEBUG, "[moesif] Reading the request query params to fetch the credentials like app_id, app_key or user_key - ", dump(req_query_params)) end - authPairConfig, authPairConfigErr = ngx.timer.at(0, get_3Scale_config, config, auth_api_key, auth_app_id, auth_app_key_pair, true, user_id_name, company_id_name, logEvent, message, debug) - if not authPairConfig then - if debug then - ngx_log(ngx_log_ERR, "[moesif] Error while getting the 3Scale Application config for AppId and App_Key Auth method ", authPairConfigErr) - end + + -- Fetch credential from request header or query parameter location + auth_api_key = fetch_credentials(auth_api_key_name, req_headers, req_query_params) + auth_app_id = fetch_credentials(auth_app_id_name, req_headers, req_query_params) + auth_app_key_pair = fetch_credentials(auth_app_key_pair_name, req_headers, req_query_params) + + if debug then + ngx_log(ngx.DEBUG, "[moesif] Auth Api key after reading the request headers and query params - ", auth_api_key) + ngx_log(ngx.DEBUG, "[moesif] Auth App Id after reading the request headers and query params - ", auth_app_id) + ngx_log(ngx.DEBUG, "[moesif] Auth App key Pair after reading the request headers and query params - ", auth_app_key_pair) end - end - elseif nonEmpty(auth_api_key) then - message["session_token"] = auth_api_key - if nonEmpty(ngx.shared.user_id_cache:get(auth_api_key)) or nonEmpty(ngx.shared.company_id_cache:get(auth_api_key)) then + + -- Authentication Mode + if nonEmpty(auth_app_id) and nonEmpty(auth_app_key_pair) then + message["session_token"] = auth_app_key_pair + if nonEmpty(ngx.shared.user_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair)) or nonEmpty(ngx.shared.company_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair)) then if debug then - ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set User ID with API Key (user_key) Auth method - ", auth_api_key) + ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set User ID with AppId and App_Key Auth method - ", auth_app_id .. "-" .. auth_app_key_pair) end - message = set_entity_id(ngx.shared.user_id_cache:get(auth_api_key), message, debug, "user_id") + message = set_entity_id(ngx.shared.user_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair), message, debug, "user_id") if debug then - ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set Company ID with API Key (user_key) Auth method - ", auth_api_key) + ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set Company ID with AppId and App_Key Auth method - ", auth_app_id .. "-" .. auth_app_key_pair) end - message = set_entity_id(ngx.shared.company_id_cache:get(auth_api_key), message, debug, "company_id") + message = set_entity_id(ngx.shared.company_id_cache:get(auth_app_id .. "-" .. auth_app_key_pair), message, debug, "company_id") if debug then - ngx_log(ngx.DEBUG, "[moesif] Log the Event for API Key (user_key) Auth method - ", auth_api_key) + ngx_log(ngx.DEBUG, "[moesif] Log the Event for AppId and App_Key Auth method - ", auth_app_id .. "-" .. auth_app_key_pair) end logEvent(config, message) else if debug then - ngx_log(ngx.DEBUG, "[moesif] Calling the function to fetch the 3Scale config with API Key (user_key) Auth method and log the Event - ", auth_api_key) + ngx_log(ngx.DEBUG, "[moesif] Calling the function to fetch the 3Scale config with AppId and App_Key Auth method and log the Event - ", auth_app_id .. "-" .. auth_app_key_pair) end - authKeyConfig, authKeyConfigErr = ngx.timer.at(0, get_3Scale_config, config, auth_api_key, auth_app_id, auth_app_key_pair, false, user_id_name, company_id_name, logEvent, message, debug) - if not authKeyConfig then + authPairConfig, authPairConfigErr = ngx.timer.at(0, get_3Scale_config, config, auth_api_key, auth_app_id, auth_app_key_pair, true, user_id_name, company_id_name, logEvent, message, debug) + if not authPairConfig then if debug then - ngx_log(ngx.DEBUG, "[moesif] Error while getting the 3Scale Application config for API Key (user_key) Auth method ", authKeyConfigErr) + ngx_log(ngx_log_ERR, "[moesif] Error while getting the 3Scale Application config for AppId and App_Key Auth method ", authPairConfigErr) end end end + elseif nonEmpty(auth_api_key) then + message["session_token"] = auth_api_key + if nonEmpty(ngx.shared.user_id_cache:get(auth_api_key)) or nonEmpty(ngx.shared.company_id_cache:get(auth_api_key)) then + if debug then + ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set User ID with API Key (user_key) Auth method - ", auth_api_key) + end + message = set_entity_id(ngx.shared.user_id_cache:get(auth_api_key), message, debug, "user_id") + if debug then + ngx_log(ngx.DEBUG, "[moesif] Calling the helper function to Set Company ID with API Key (user_key) Auth method - ", auth_api_key) + end + message = set_entity_id(ngx.shared.company_id_cache:get(auth_api_key), message, debug, "company_id") + if debug then + ngx_log(ngx.DEBUG, "[moesif] Log the Event for API Key (user_key) Auth method - ", auth_api_key) + end + logEvent(config, message) + else + if debug then + ngx_log(ngx.DEBUG, "[moesif] Calling the function to fetch the 3Scale config with API Key (user_key) Auth method and log the Event - ", auth_api_key) + end + authKeyConfig, authKeyConfigErr = ngx.timer.at(0, get_3Scale_config, config, auth_api_key, auth_app_id, auth_app_key_pair, false, user_id_name, company_id_name, logEvent, message, debug) + if not authKeyConfig then + if debug then + ngx_log(ngx.DEBUG, "[moesif] Error while getting the 3Scale Application config for API Key (user_key) Auth method ", authKeyConfigErr) + end + end + end + else + if debug then + ngx_log(ngx.DEBUG, "No 3Scale userId found as authentication key - user_key or app_id/app_key is not provided in headers or query params.") + end + logEvent(config, message) + end else - if debug then - ngx_log(ngx.DEBUG, "No 3Scale userId found as authentication key - user_key or app_id/app_key is not provided in headers or query params.") + if config:get("debug") then + ngx_log(ngx.DEBUG, "3Scale accessToken or userKey or domainName is not provided") end logEvent(config, message) - end - else - if config:get("debug") then - ngx_log(ngx.DEBUG, "3Scale accessToken or userKey or domainName is not provided") - end - logEvent(config, message) - end + end + end end