Skip to content

Commit

Permalink
chore: add test cases (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvd11 authored Aug 9, 2021
1 parent eb88e52 commit 558cad7
Show file tree
Hide file tree
Showing 7 changed files with 442 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

quote_type = double

[*.{yml,yaml}]
indent_size = 2
Expand Down
13 changes: 8 additions & 5 deletions kong/plugins/advanced-router/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ end

function generate_boolean_function(proposition)
if proposition['condition'] == 'default' then
return assert(loadstring("return " .. "\"" .. proposition["value"] .. "\""))
return assert(loadstring("return " .. "\"" .. proposition["upstream_url"] .. "\""))
end
return assert(loadstring("if " .. proposition["condition"] .. "then return " .. "\"" .. proposition["value"] .. "\"" .. " end"))
return assert(loadstring("if " .. proposition["condition"] .. "then return " .. "\"" .. proposition["upstream_url"] .. "\"" .. " end"))
end

function get_upstream_url(conf)
Expand Down Expand Up @@ -60,10 +60,13 @@ end

function set_upstream(upstream_url)
local parsed_url = url.parse(upstream_url)
local scheme = parsed_url['port'] or 'http'
local host = parsed_url['host']
local path = parsed_url['path']
local port = tonumber(parsed_url['port']) or 80
kong.log.debug("Upstream set by advanced router::" .. inspect(upstream_url))
kong.service.request.set_scheme(scheme)
kong.log.debug("Upstream URL::" .. inspect(upstream_url))
kong.log.debug("Parsed Upstream URL::" .. inspect(parsed_url))
kong.service.set_target(host, port)
if path then
kong.service.request.set_path(path)
Expand All @@ -75,15 +78,15 @@ function AdvancedRouterHandler:access(conf)
local io_data, err = get_io_data(conf)
if err then
kong.log.err("Error in getting io data" .. inspect(err))
return kong.response.exit(500, "Error in getting io data")
return kong.response.exit(500, { error = "Error in getting io data" .. inspect(err) })
end
kong.ctx.plugin.io_data = io_data

local upstream_url = get_upstream_url(conf)
if not upstream_url then
return kong.response.exit(500, "Not able to resolve upstream in advanced router")
end
upstream_url = replaceStringEnvVariables(upstream_url)
upstream_url = replaceStringEnvVariables(upstream_url, io_data)
set_upstream(upstream_url)
end

Expand Down
20 changes: 12 additions & 8 deletions kong/plugins/advanced-router/io.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ local function get_http_client(conf)
end

function extract_from_request(object, key)
local value
if object == 'headers' then
local r = extract(key, kong.request.get_headers())
return r
value = extract(string.lower(key), kong.request.get_headers())
return value
else
local r = extract(key, kong.request.get_query())
return r
value = extract(key, kong.request.get_query())
return value
end
return value
end

function extract_io_data_from_request(conf)
Expand All @@ -37,7 +39,6 @@ function extract_io_data_from_request(conf)
body = {}
}
local req_parts = { "headers", "query", "body" }

for _, part in ipairs(req_parts) do
if io_request_template[part] then
for key, value in pairs(io_request_template[part]) do
Expand All @@ -55,7 +56,6 @@ function extract_io_data_from_request(conf)
end
end
end

return io_req
end

Expand All @@ -78,15 +78,18 @@ function get_io_data_from_remote(request_data, conf)
query = request_data.query
}
)
kong.log.debug("IO call error::" .. inspect(err1))
if not res or err1 then
return nil, err1
end

if not err1 and res and res.status ~= 200 then
return nil, "IO call failed - Response status:" .. res.status
end
local bodyJson, err2 = cjson_safe.decode(res["body"])
if not bodyJson then
return nil, err2
end

kong.log.debug("Data from I/O::" .. inspect(bodyJson.data))
local cacheTTL
if conf.cache_io_response then
cacheTTL = res.headers[conf.cache_ttl_header] or conf["default_edge_ttl_sec"]
Expand Down Expand Up @@ -116,6 +119,7 @@ end

function create_io_request(conf)
local io_request = extract_io_data_from_request(conf)
kong.log.debug("conf" .. inspect(conf))
io_request["io_url"] = replaceStringEnvVariables(conf.io_url)
io_request["io_http_method"] = conf.io_http_method
return io_request
Expand Down
39 changes: 31 additions & 8 deletions kong/plugins/advanced-router/schema.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
local typedefs = require "kong.db.schema.typedefs"
local json_safe = require "cjson.safe"
local url = require "socket.url"

local belongs = require "kong.plugins.advanced-router.utils".belongs

local function json_validator(config_string)
local config_table, err = json_safe.decode(config_string)
local decoded, err = json_safe.decode(config_string)

if config_table == nil then
if decoded == nil then
return nil, "Invalid Json " .. inspect(err)
end

return true, nil, decoded
end

local function validate_propositions_json(config_string)
-- This functions validates the port and protocols of the upstream urls in the propositions_json
local result, err, propositions_json = json_validator(config_string)
if not result then
return nil, err
end
local valid_schemes = { 'http', 'https' }
for _, v in ipairs(propositions_json) do
local upstream_url = v['upstream_url']
local parsed_url = url.parse(upstream_url)
local scheme = parsed_url['port'] or 'http'
if not belongs(scheme, valid_schemes) then
return nil, "Invalid protocol: " .. scheme " for url: " .. upstream_url
end

if parsed_url['port'] and not tonumber(parsed_url['port']) then
return nil, "Invalid port: " .. parsed_url['port'] " for url: " .. upstream_url
end
end
return true
end

local function schema_validator(conf)
return json_validator(conf.propositions_json) and json_validator(conf.io_request_template)
return validate_propositions_json(conf.propositions_json) and json_validator(conf.io_request_template)
end

return {
Expand All @@ -27,21 +52,19 @@ return {
{
propositions_json = {
type = "string",
default = "[\n {\n \"condition\":\"extract('a') == 'x' and extract('b') == 'y'\",\n \"value\":\"alpha.com\"\n },\n {\n \"condition\":\"extract('a') == z or extract('b') == 'z'\",\n \"value\":\"beta.com\"\n },\n {\n \"condition\":\"default\",\n \"value\":\"default.com\"\n }\n]"
default = "[\n {\n \"condition\":\"extract_from_io_response('a') == 'x' and extract_from_io_response('b') == 'y'\",\n \"upstream_url\":\"alpha.com\"\n },\n {\n \"condition\":\"extract_from_io_response('a') == z or extract_from_io_response('b') == 'z'\",\n \"upstream_url\":\"beta.com\"\n },\n {\n \"condition\":\"default\",\n \"upstream_url\":\"default.com\"\n }\n]"
}
},
{
io_url = {
type = "string",
default = "http://192.168.1.40/round"

default = "http://io-call%placeholder%/round"
}
},
{
io_request_template = {
type = "string",
default = "{\n \"header\": {\n \"a\":extract_from_request(\"header.x\")\n },\n \"query\": {\n \"b\": extract_from_request(\"query.y\")\n },\n \"body\": {\n \"c\": extract_from_request(\"body.z\"),\n \"d\": \"hardcoded\"\n }\n}"

default = "{\n \"headers\": {\n \"a\":\"headers.x\"\n },\n \"query\": {\n \"b\": \"query.y\"\n },\n \"body\": {\n \"c\": \"query.z\",\n \"d\": \"hardcoded\"\n }\n}"
}
},
{
Expand Down
6 changes: 5 additions & 1 deletion kong/plugins/advanced-router/utils.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
local pl_utils = require "pl.utils"
local sha256 = require "resty.sha256"
local encode_base64 = ngx.encode_base64
local inspect = require "inspect"

local _M = {}

function _M.replaceStringEnvVariables(s, data)
return
string.gsub(
s,
"%%[A-Za-z_]+%%",
"%%[A-Za-z_%.]+%%",
function(str)

local variable = string.sub(str, 2, string.len(str) - 1)
kong.log.debug("string=" .. variable)
if data then
local value_from_data = _M.extract(variable, data)
if value_from_data then
return value_from_data
end
end
kong.log.debug("from env=" .. inspect(os.getenv(variable)))
return os.getenv(variable)
end
)
Expand Down
Loading

0 comments on commit 558cad7

Please sign in to comment.