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

fix(schema) more descriptive error logs when type is invalid #6593

Merged
merged 1 commit into from
Mar 18, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion kong/db/schema/entities/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ local utils = require "kong.tools.utils"
local function validate_target(target)
local p = utils.normalize_ip(target)
if not p then
return nil, "Invalid target; not a valid hostname or ip address"
local ok = utils.validate_utf8(target)
if not ok then
return nil, "Invalid target; not a valid hostname or ip address"
end

return nil, "Invalid target ('" .. target .. "'); not a valid hostname or ip address"
end
return true
end
Expand Down
16 changes: 13 additions & 3 deletions kong/db/schema/entities/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ local utils = require "kong.tools.utils"
local null = ngx.null


local function get_name_for_error(name)
local ok = utils.validate_utf8(name)
if not ok then
return "Invalid name"
end

return "Invalid name ('" .. name .. "')"
end


local validate_name = function(name)
local p = utils.normalize_ip(name)
if not p then
return nil, "Invalid name; must be a valid hostname"
return nil, get_name_for_error(name) .. "; must be a valid hostname"
end
if p.type ~= "name" then
return nil, "Invalid name; no ip addresses allowed"
return nil, get_name_for_error(name) .. "; no ip addresses allowed"
end
if p.port then
return nil, "Invalid name; no port allowed"
return nil, get_name_for_error(name) .. "; no port allowed"
end
return true
end
Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/01-db/01-schema/08-targets_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("targets", function()

local ok, err = validate({ target = "\\\\bad\\\\////name////", upstream = upstream })
assert.falsy(ok)
assert.same({ target = "Invalid target; not a valid hostname or ip address"}, err)
assert.same({ target = "Invalid target ('\\\\bad\\\\////name////'); not a valid hostname or ip address"}, err)
end)
end)
end)
6 changes: 3 additions & 3 deletions spec/01-unit/01-db/01-schema/09-upstreams_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ describe("load upstreams", function()

ok, err = Upstreams:validate({ name = "123.123.123.123" })
assert.falsy(ok)
assert.same({ name = "Invalid name; no ip addresses allowed" }, err)
assert.same({ name = "Invalid name ('123.123.123.123'); no ip addresses allowed" }, err)

ok, err = Upstreams:validate({ name = "\\\\bad\\\\////name////" })
assert.falsy(ok)
assert.same({ name = "Invalid name; must be a valid hostname" }, err)
assert.same({ name = "Invalid name ('\\\\bad\\\\////name////'); must be a valid hostname" }, err)

ok, err = Upstreams:validate({ name = "name:80" })
assert.falsy(ok)
assert.same({ name = "Invalid name; no port allowed" }, err)
assert.same({ name = "Invalid name ('name:80'); no port allowed" }, err)
end)

-- acceptance
Expand Down
4 changes: 2 additions & 2 deletions spec/02-integration/04-admin_api/07-upstreams_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe("Admin API: #" .. strategy, function()
body = assert.res_status(400, res)
local json = cjson.decode(body)
assert.equals("schema violation", json.name)
assert.same({ name = "Invalid name; must be a valid hostname" }, json.fields)
assert.same({ name = "Invalid name ('some invalid host name'); must be a valid hostname" }, json.fields)

-- Invalid slots parameter
res = assert(client:send {
Expand Down Expand Up @@ -566,7 +566,7 @@ describe("Admin API: #" .. strategy, function()

body = assert.response(res).has.status(400)
local json = cjson.decode(body)
assert.same("Invalid name; no ip addresses allowed", json.message)
assert.same("Invalid name ('1.2.3.4'); no ip addresses allowed", json.message)
end
end)
end)
Expand Down