Skip to content

[backport -> release/3.6.x] fix(plugins): consistent error responses upon Admin API auth failures #12456

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

Merged
merged 1 commit into from
Jan 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "Enhance error responses for authentication failures in the Admin API"
type: bugfix
scope: Plugin
4 changes: 2 additions & 2 deletions kong/plugins/basic-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ local function do_authentication(conf)
if given_username and given_password then
credential = load_credential_from_db(given_username)
else
return false, unauthorized("Invalid authentication credentials", www_authenticate)
return false, unauthorized("Unauthorized", www_authenticate)
end
end

if not credential or not validate_credentials(credential, given_password) then
return false, unauthorized("Invalid authentication credentials", www_authenticate)
return false, unauthorized("Unauthorized", www_authenticate)
end

-- Retrieve consumer
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/key-auth/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local _realm = 'Key realm="' .. _KONG._NAME .. '"'

local ERR_DUPLICATE_API_KEY = { status = 401, message = "Duplicate API key found" }
local ERR_NO_API_KEY = { status = 401, message = "No API key found in request" }
local ERR_INVALID_AUTH_CRED = { status = 401, message = "Invalid authentication credentials" }
local ERR_INVALID_AUTH_CRED = { status = 401, message = "Unauthorized" }
local ERR_INVALID_PLUGIN_CONF = { status = 500, message = "Invalid plugin configuration" }
local ERR_UNEXPECTED = { status = 500, message = "An unexpected error occurred" }

Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/ldap-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ local function do_authentication(conf)
end

if not is_authorized then
return false, {status = 401, message = "Invalid authentication credentials" }
return false, {status = 401, message = "Unauthorized" }
end

if conf.hide_credentials then
Expand Down
2 changes: 1 addition & 1 deletion spec/02-integration/02-cmd/03-reload_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ describe("key-auth plugin invalidation on dbless reload #off", function()
})
local body = res:read_body()
proxy_client:close()
return body ~= [[{"message":"Invalid authentication credentials"}]]
return body ~= [[{"message":"Unauthorized"}]]
end, 5)

admin_client = assert(helpers.admin_client())
Expand Down
10 changes: 5 additions & 5 deletions spec/03-plugins/09-key-auth/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
end)
it("handles duplicated key in querystring", function()
local res = assert(proxy_client:send {
Expand Down Expand Up @@ -365,7 +365,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
end)

-- lua-multipart doesn't currently handle duplicates at all.
Expand Down Expand Up @@ -461,7 +461,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
end)
end)

Expand Down Expand Up @@ -521,7 +521,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)

res = assert(proxy_client:send {
method = "GET",
Expand All @@ -534,7 +534,7 @@ for _, strategy in helpers.each_strategy() do
body = assert.res_status(401, res)
json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
end)
end)

Expand Down
10 changes: 5 additions & 5 deletions spec/03-plugins/10-basic-auth/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
assert.equal('Basic realm="test-realm"', res.headers["WWW-Authenticate"])
end)

Expand All @@ -200,7 +200,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
assert.equal('Basic realm="test-realm"', res.headers["WWW-Authenticate"])
end)

Expand All @@ -216,7 +216,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
assert.equal('Basic realm="test-realm"', res.headers["WWW-Authenticate"])
end)

Expand All @@ -232,7 +232,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
assert.equal('Basic realm="test-realm"', res.headers["WWW-Authenticate"])
end)

Expand Down Expand Up @@ -308,7 +308,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
assert.equal('Basic realm="test-realm"', res.headers["WWW-Authenticate"])
end)

Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/10-basic-auth/05-declarative_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ for _, strategy in helpers.each_strategy() do
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.matches("Unauthorized", json.message)
end)
end)

Expand Down
6 changes: 3 additions & 3 deletions spec/03-plugins/20-ldap-auth/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ for _, ldap_strategy in pairs(ldap_strategies) do
})
assert.response(res).has.status(401)
local json = assert.response(res).has.jsonbody()
assert.equal("Invalid authentication credentials", json.message)
assert.equal("Unauthorized", json.message)
end)
it("returns 'invalid credentials' when credential value is in wrong format in proxy-authorization header", function()
local res = assert(proxy_client:send {
Expand All @@ -250,7 +250,7 @@ for _, ldap_strategy in pairs(ldap_strategies) do
})
assert.response(res).has.status(401)
local json = assert.response(res).has.jsonbody()
assert.equal("Invalid authentication credentials", json.message)
assert.equal("Unauthorized", json.message)
end)
it("returns 'invalid credentials' when credential value is missing in authorization header", function()
local res = assert(proxy_client:send {
Expand All @@ -263,7 +263,7 @@ for _, ldap_strategy in pairs(ldap_strategies) do
})
assert.response(res).has.status(401)
local json = assert.response(res).has.jsonbody()
assert.equal("Invalid authentication credentials", json.message)
assert.equal("Unauthorized", json.message)
end)
it("passes if credential is valid in post request", function()
local res = assert(proxy_client:send {
Expand Down