I’ve been having this problem in weeks. I made a custom plugin for Kong, written in Lua, that supposed to forward any request that Kong received to a custom auth service. Here is Handler.lua.
local http = require "resty.http"
local utils = require "kong.tools.utils"
local cjson = require("cjson")
local MessageForwarder = {
local function forward_message(conf, request_method, request_path, request_headers, request_body)
local res, err = httpc:request_uri(conf.forward_host .. request_path, {
headers = request_headers,
kong.log.err("Failed to forward request: ", err)
return kong.response.exit(500)
if res.status ~= 200 then
kong.log.err("Responded with status: ",res.status)
return kong.response.exit(500)
return true -- all is well
local function forward_message_new(conf, request_method, request_path, request_headers, request_body)
local server = assert(socket.bind("*", 0))
function MessageForwarder:access(conf)
local request_path = kong.request.get_raw_path()
local request_method = kong.request.get_method()
local request_headers = kong.request.get_headers()
if raw_request_method ~= "GET" then
raw_request_body = kong.request.get_raw_body()
local request_body = cjson.decode(raw_request_body)
local auth_path = conf.auth_token_b2b_endpoint
local bool = forward_message(conf, request_method, request_path, request_headers, request_body)
<code>-- Handler.lua
local http = require "resty.http"
local utils = require "kong.tools.utils"
local cjson = require("cjson")
local MessageForwarder = {
VERSION = "1.0",
PRIORITY = 1000,
}
local function forward_message(conf, request_method, request_path, request_headers, request_body)
-- Make HTTP Request
local httpc = http.new()
local res, err = httpc:request_uri(conf.forward_host .. request_path, {
method = request_method,
headers = request_headers,
body = request_body,
ssl_verify = false
})
-- Checking response
if not res then
kong.log.err("Failed to forward request: ", err)
return kong.response.exit(500)
end
if res.status ~= 200 then
kong.log.err("Responded with status: ",res.status)
return kong.response.exit(500)
end
return true -- all is well
end
local function forward_message_new(conf, request_method, request_path, request_headers, request_body)
local server = assert(socket.bind("*", 0))
end
function MessageForwarder:access(conf)
local request_path = kong.request.get_raw_path()
local request_method = kong.request.get_method()
local request_headers = kong.request.get_headers()
local raw_request_body
if raw_request_method ~= "GET" then
raw_request_body = kong.request.get_raw_body()
end
local request_body = cjson.decode(raw_request_body)
local auth_path = conf.auth_token_b2b_endpoint
local bool = forward_message(conf, request_method, request_path, request_headers, request_body)
return true
end
return MessageForwarder
</code>
-- Handler.lua
local http = require "resty.http"
local utils = require "kong.tools.utils"
local cjson = require("cjson")
local MessageForwarder = {
VERSION = "1.0",
PRIORITY = 1000,
}
local function forward_message(conf, request_method, request_path, request_headers, request_body)
-- Make HTTP Request
local httpc = http.new()
local res, err = httpc:request_uri(conf.forward_host .. request_path, {
method = request_method,
headers = request_headers,
body = request_body,
ssl_verify = false
})
-- Checking response
if not res then
kong.log.err("Failed to forward request: ", err)
return kong.response.exit(500)
end
if res.status ~= 200 then
kong.log.err("Responded with status: ",res.status)
return kong.response.exit(500)
end
return true -- all is well
end
local function forward_message_new(conf, request_method, request_path, request_headers, request_body)
local server = assert(socket.bind("*", 0))
end
function MessageForwarder:access(conf)
local request_path = kong.request.get_raw_path()
local request_method = kong.request.get_method()
local request_headers = kong.request.get_headers()
local raw_request_body
if raw_request_method ~= "GET" then
raw_request_body = kong.request.get_raw_body()
end
local request_body = cjson.decode(raw_request_body)
local auth_path = conf.auth_token_b2b_endpoint
local bool = forward_message(conf, request_method, request_path, request_headers, request_body)
return true
end
return MessageForwarder
The result is always consistent. Consistent errors. The Spring Boot REST API that receives the request forwarded from that plugin returns this error to the Spring Boot log:
<code>WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message]
<code>WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message]
</code>
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message]
I’m not sure if the problem is in my Spring Boot API since I’ve tested it with requests from other sources (Postman, Insomnia, other Spring Boot app, Ajax) and everything works fine, just not from my custom Kong plugin with Lua. I wonder if there are some set of rules in using resty.http that I didn’t follow.
Any suggestion would be highly appreciated.
As an additional information, I adapted this tutorial from Kong blog to fit my requirements: https://konghq.com/blog/engineering/custom-authentication-and-authorization-framework-with-kong