Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(router/atc): unify expression validation function in schema #12878

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions kong/db/schema/entities/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ if kong_router_flavor == "traditional_compatible" or kong_router_flavor == "expr
local router = require("resty.router.router")
local transform = require("kong.router.transform")
local get_schema = require("kong.router.atc").schema
local get_expression = kong_router_flavor == "traditional_compatible" and
require("kong.router.compat").get_expression or
require("kong.router.expressions").transform_expression

local is_null = transform.is_null
local is_empty_field = transform.is_empty_field
local amending_expression = transform.amending_expression

local HTTP_PATH_SEGMENTS_PREFIX = "http.path.segments."
local HTTP_PATH_SEGMENTS_SUFFIX_REG = [[^(0|[1-9]\d*)(_([1-9]\d*))?$]]
Expand Down Expand Up @@ -102,7 +100,7 @@ if kong_router_flavor == "traditional_compatible" or kong_router_flavor == "expr
end

local schema = get_schema(entity.protocols)
local exp = get_expression(entity)
local exp = amending_expression(entity)

local fields, err = router.validate(schema, exp)
if not fields then
Expand Down
5 changes: 0 additions & 5 deletions kong/router/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@ function _M.new(routes_and_services, cache, cache_neg, old_router)
end


-- for schema validation and unit-testing
_M.get_expression = get_expression


-- for unit-testing purposes only
_M._set_ngx = atc._set_ngx
_M._get_priority = get_priority


return _M
98 changes: 39 additions & 59 deletions kong/router/expressions.lua
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
local _M = {}


local re_gsub = ngx.re.gsub


local atc = require("kong.router.atc")
local transform = require("kong.router.transform")


local get_expression = transform.get_expression
local get_priority = transform.get_priority


local gen_for_field = transform.gen_for_field
local OP_EQUAL = transform.OP_EQUAL
local LOGICAL_AND = transform.LOGICAL_AND


local NET_PORT_REG = [[(net\.port)(\s*)([=><!])]]
local NET_PORT_REPLACE = [[net.dst.port$2$3]]

local get_expression
do
local gen_for_field = transform.gen_for_field
local OP_EQUAL = transform.OP_EQUAL
local LOGICAL_AND = transform.LOGICAL_AND

-- map to normal protocol
local PROTOCOLS_OVERRIDE = {
tls_passthrough = "tcp",
grpc = "http",
grpcs = "https",
}
local amending_expression = transform.amending_expression

-- map to normal protocol
local PROTOCOLS_OVERRIDE = {
tls_passthrough = "tcp",
grpc = "http",
grpcs = "https",
}

-- net.port => net.dst.port
local function transform_expression(route)
local exp = get_expression(route)

if not exp then
return nil
end

if not exp:find("net.port", 1, true) then
return exp
local function protocol_val_transform(_, p)
return PROTOCOLS_OVERRIDE[p] or p
end

-- there is "net.port" in expression

local new_exp = re_gsub(exp, NET_PORT_REG, NET_PORT_REPLACE, "jo")
get_expression = function(route)
local exp = amending_expression(route)
if not exp then
return nil
end

local protocols = route.protocols

-- give the chance for http redirection (301/302/307/308/426)
-- and allow tcp works with tls
if protocols and #protocols == 1 and
(protocols[1] == "https" or
protocols[1] == "tls" or
protocols[1] == "tls_passthrough")
then
return exp
end

local gen = gen_for_field("net.protocol", OP_EQUAL, protocols,
protocol_val_transform)
if gen then
exp = exp .. LOGICAL_AND .. gen
end

if exp ~= new_exp then
ngx.log(ngx.WARN, "The field 'net.port' of expression is deprecated " ..
"and will be removed in the upcoming major release, " ..
"please use 'net.dst.port' instead.")
return exp
end

return new_exp
end
_M.transform_expression = transform_expression


local function get_exp_and_priority(route)
local exp = transform_expression(route)
local exp = get_expression(route)
if not exp then
ngx.log(ngx.ERR, "expecting an expression route while it's not (probably a traditional route). ",
"Likely it's a misconfiguration. Please check the 'router_flavor' config in kong.conf")
Expand All @@ -66,26 +66,6 @@ local function get_exp_and_priority(route)

local priority = get_priority(route)

local protocols = route.protocols

-- give the chance for http redirection (301/302/307/308/426)
-- and allow tcp works with tls
if protocols and #protocols == 1 and
(protocols[1] == "https" or
protocols[1] == "tls" or
protocols[1] == "tls_passthrough")
then
return exp, priority
end

local gen = gen_for_field("net.protocol", OP_EQUAL, protocols,
function(_, p)
return PROTOCOLS_OVERRIDE[p] or p
end)
if gen then
exp = exp .. LOGICAL_AND .. gen
end

return exp, priority
end

Expand Down
36 changes: 36 additions & 0 deletions kong/router/transform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,40 @@ local function split_routes_and_services_by_path(routes_and_services)
end


local amending_expression
do
local re_gsub = ngx.re.gsub

local NET_PORT_REG = [[(net\.port)(\s*)([=><!])]]
local NET_PORT_REPLACE = [[net.dst.port$2$3]]

-- net.port => net.dst.port
amending_expression = function(route)
local exp = get_expression(route)

if not exp then
return nil
end

if not exp:find("net.port", 1, true) then
return exp
end

-- there is "net.port" in expression

local new_exp = re_gsub(exp, NET_PORT_REG, NET_PORT_REPLACE, "jo")

if exp ~= new_exp then
ngx.log(ngx.WARN, "The field 'net.port' of expression is deprecated " ..
"and will be removed in the upcoming major release, " ..
"please use 'net.dst.port' instead.")
end

return new_exp
end
end


return {
OP_EQUAL = OP_EQUAL,

Expand All @@ -774,4 +808,6 @@ return {
get_priority = get_priority,

split_routes_and_services_by_path = split_routes_and_services_by_path,

amending_expression = amending_expression,
}