Skip to content

Commit

Permalink
feat(dbless): improve validation errors from /config
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Jan 30, 2023
1 parent 486c07a commit 01a3045
Show file tree
Hide file tree
Showing 5 changed files with 1,753 additions and 13 deletions.
37 changes: 34 additions & 3 deletions kong/api/routes/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ local function reports_timer(premature)
end


local function truthy(val)
return val == true
or val == 1
or val == "true"
or val == "1"
or val == "on"
or val == "yes"
end


return {
["/config"] = {
GET = function(self, db)
Expand Down Expand Up @@ -57,6 +67,19 @@ return {
})
end

local opts
local flatten_errors = truthy(self.params.flatten_errors)
self.params.flatten_errors = nil
local processed_input
if flatten_errors then
opts = {
handle_error = function(input, err_t)
processed_input = input
return nil, err_t
end,
}
end

local check_hash, old_hash
if tostring(self.params.check_hash) == "1" then
check_hash = true
Expand All @@ -68,7 +91,7 @@ return {

local entities, _, err_t, meta, new_hash
if self.params._format_version then
entities, _, err_t, meta, new_hash = dc:parse_table(self.params)
entities, _, err_t, meta, new_hash = dc:parse_table(self.params, nil, opts)
else
local config = self.params.config
if not config then
Expand All @@ -82,14 +105,22 @@ return {
end
end
entities, _, err_t, meta, new_hash =
dc:parse_string(config, nil, old_hash)
dc:parse_string(config, nil, old_hash, opts)
end

if not entities then
if check_hash and err_t and err_t.error == "configuration is identical" then
return kong.response.exit(304)
end
return kong.response.exit(400, errors:declarative_config(err_t))

local res
if flatten_errors then
res = errors:declarative_config_flattened(err_t, processed_input)
else
res = errors:declarative_config(err_t)
end

return kong.response.exit(400, res)
end

local ok, err, ttl = declarative.load_into_cache_with_events(entities, meta, new_hash)
Expand Down
16 changes: 10 additions & 6 deletions kong/db/declarative/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ local function pretty_print_error(err_t, item, indent)
end



-- @tparam table|nil opts options to pass to the parser
-- @treturn table|nil a table with the following format:
-- {
-- services: {
Expand All @@ -89,7 +91,7 @@ end
-- _format_version: "2.1",
-- _transform: true,
-- }
function _M:parse_file(filename, old_hash)
function _M:parse_file(filename, old_hash, opts)
if type(filename) ~= "string" then
error("filename must be a string", 2)
end
Expand All @@ -99,7 +101,7 @@ function _M:parse_file(filename, old_hash)
return nil, err
end

return self:parse_string(contents, filename, old_hash)
return self:parse_string(contents, filename, old_hash, opts)
end


Expand All @@ -114,14 +116,15 @@ end
-- @tparam string contents the json/yml/lua being parsed
-- @tparam string|nil filename. If nil, json will be tried first, then yaml
-- @tparam string|nil old_hash used to avoid loading the same content more than once, if present
-- @tparam table|nil opts options to pass to the parser
-- @treturn nil|string error message, only if error happened
-- @treturn nil|table err_t, only if error happened
-- @treturn table|nil a table with the following format:
-- {
-- _format_version: "2.1",
-- _transform: true,
-- }
function _M:parse_string(contents, filename, old_hash)
function _M:parse_string(contents, filename, old_hash, opts)
-- we don't care about the strength of the hash
-- because declarative config is only loaded by Kong administrators,
-- not outside actors that could exploit it for collisions
Expand Down Expand Up @@ -171,7 +174,7 @@ function _M:parse_string(contents, filename, old_hash)
return nil, err, { error = err }
end

return self:parse_table(dc_table, new_hash)
return self:parse_table(dc_table, new_hash, opts)
end


Expand All @@ -185,6 +188,7 @@ end
-- },
-- }
-- This table is not flattened: entities can exist inside other entities
-- @tparam table|nil opts options to pass to the parser
-- @treturn table|nil A table with the following format:
-- {
-- services: {
Expand All @@ -202,12 +206,12 @@ end
-- }
-- @treturn string|nil given hash if everything went well,
-- new hash if everything went well and no given hash,
function _M:parse_table(dc_table, hash)
function _M:parse_table(dc_table, hash, opts)
if type(dc_table) ~= "table" then
error("expected a table as input", 2)
end

local entities, err_t, meta = self.schema:flatten(dc_table)
local entities, err_t, meta = self.schema:flatten(dc_table, opts)
if err_t then
return nil, pretty_print_error(err_t), err_t
end
Expand Down
Loading

0 comments on commit 01a3045

Please sign in to comment.