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

feat(conf_loader): added a validation functionality for kong.conf #13864

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions kong/conf_loader/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local conf_constants = require "kong.conf_loader.constants"
local tools_system = require "kong.tools.system" -- for unit-testing
local tools_ip = require "kong.tools.ip"
local tools_string = require "kong.tools.string"

local validators = require "kong.conf_loader.validators"

local normalize_ip = tools_ip.normalize_ip
local is_valid_ip_or_cidr = tools_ip.is_valid_ip_or_cidr
Expand Down Expand Up @@ -221,18 +221,18 @@ local function check_and_parse(conf, opts)

for k, value in pairs(conf) do
local v_schema = conf_constants.CONF_PARSERS[k] or {}

value = parse_value(value, v_schema.typ)

local typ = v_schema.typ or "string"
if value and not conf_constants.TYP_CHECKS[typ](value) then
errors[#errors + 1] = fmt("%s is not a %s: '%s'", k, typ,
tostring(value))

elseif v_schema.enum and not tablex.find(v_schema.enum, value) then
errors[#errors + 1] = fmt("%s has an invalid value: '%s' (%s)", k,
tostring(value), concat(v_schema.enum, ", "))
if value then
local ok, err = validators:check({
schema = v_schema,
value = value,
name = k,
})

if not ok then
errors[#errors + 1] = err
end
end

conf[k] = value
Expand Down
79 changes: 79 additions & 0 deletions kong/conf_loader/validators.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
local concat = table.concat

local Validators = {}

Validators.validation_errors = {
-- types
ARRAY = "%s is not a array: '%s'",
STRING = "%s is not a string: '%s'",
NUMBER = "%s is not a number: '%s'",
BOOLEAN = "%s is not a boolean: '%s'",
NGX_BOOLEAN = "%s is not a ngx_boolean: '%s'",
-- validations
BETWEEN = "%s value should be between %d and %d, it's not '%s'",
ENUM = "%s has an invalid value: '%s', expected one of: %s",
VALIDATION = "%s failed validating: %s",
}

Validators.validators = {
between = function(name, value, limits)
if value < limits[1] or value > limits[2] then
return nil, Validators.validation_errors.BETWEEN:format(name, limits[1], limits[2], value)
end
return true
end,

enum = function(name, value, options)
for i = 1, #options do
if value == options[i] then
return true
end
end
return nil, Validators.validation_errors.ENUM:format(name, value, concat(options, ", "))
end
}

Validators.validators_order = {
"between",
"enum",
}

Validators.TYP_CHECKS = {
array = function(v) return type(v) == "table" end,
string = function(v) return type(v) == "string" end,
number = function(v) return type(v) == "number" end,
boolean = function(v) return type(v) == "boolean" end,
ngx_boolean = function(v) return v == "on" or v == "off" end,
}

function Validators:check(config)
local validators = self.validators_order

local schema = config.schema
local name = config.name
local value = config.value
local typ = schema.typ or "string"

if not self.TYP_CHECKS[typ](value) then
return false, self.validation_errors[typ:upper()]:format(name, value)
end

for i = 1, #validators do
local k = validators[i]
if schema[k] ~= nil then

local ok, err = self.validators[k](name, value, schema[k])
if not ok then
if not err then
err = (self.validation_errors[k:upper()]
or self.validation_errors.VALIDATION):format(name, value)
end
return false, err
end
end
end

return true
end

return Validators
Loading