Hi,
When I’m trying to connect to upstream Atlassian JIRA I want to use OAuth2 with rotating refresh token, for this I want to cache the refresh token and I got a very good example of the plugin at GitHub - enioka/kong-plugin-upstream-oauth2: A Kong plugin to negotiate oauth2 authentication with upstream services, a little tuning and in theory it fits my needs.
Just with my tuned code, and also with original plugin, the plugin is loaded but when creating the plugin the following error is returned: “An unexpected error occurred”
The error seems to be in a ipairs function where it expects a table but got a string:
2021/10/25 07:11:18 [error] 26#0: *2062 [lua] api_helpers.lua:449: handle_error(): /usr/local/share/lua/5.1/lapis/application.lua:401: /usr/local/share/lua/5.1/kong/db/schema/entity.lua:85: bad argument #1 to ‘ipairs’ (table expected, got string)
stack traceback:
[C]: in function ‘ipairs’
/usr/local/share/lua/5.1/kong/db/schema/entity.lua:85: in function ‘transform’
/usr/local/share/lua/5.1/kong/db/schema/init.lua:2105: in function ‘transform’
/usr/local/share/lua/5.1/kong/db/dao/init.lua:480: in function ‘check_insert’
/usr/local/share/lua/5.1/kong/db/dao/init.lua:1112: in function ‘insert_entity’
/usr/local/share/lua/5.1/kong/api/endpoints.lua:431: in function ‘fn’
/usr/local/share/lua/5.1/kong/api/api_helpers.lua:270: in function </usr/local/share/lua/5.1/kong/api/api_helpers.lua:253>
stack traceback:
[C]: in function ‘error’
/usr/local/share/lua/5.1/lapis/application.lua:401: in function ‘handler’
/usr/local/share/lua/5.1/lapis/application.lua:130: in function ‘resolve’
/usr/local/share/lua/5.1/lapis/application.lua:167: in function </usr/local/share/lua/5.1/lapis/application.lua:165>
[C]: in function ‘xpcall’
/usr/local/share/lua/5.1/lapis/application.lua:173: in function ‘dispatch’
/usr/local/share/lua/5.1/lapis/nginx.lua:230: in function ‘serve’
/usr/local/share/lua/5.1/kong/init.lua:1479: in function ‘admin_content’
content_by_lua(nginx-kong.conf:300):2: in main chunk, client: 10.160.20.197, server: kong_admin, request: “POST /ASW-GIT-EUR-TSI-Internal/plugins HTTP/1.1”, host: “kongpod:8444”, referrer: https://kongpod:8445/
The root cause of the error I can’t find, when the transformation is saved it will get encrypted
Added ==> below where the error occurs: /usr/local/share/lua/5.1/kong/db/schema/entity.lua:85:
local function add_encryption_transformations(self, name, field)
self.transformations = self.transformations or {}
if field.type == “string” then
…
elseif field.type == “array” then
table.insert(self.transformations, {
input = { name },
on_write = function(value)
local xs = {}
==> for i, x in ipairs(value) do
xs[i] = keyring.encrypt(x)
end
local tbl = {}
set_field(tbl, name, xs)
return tbl
end,
})
end
end
Somehow the transformation field type is array, it tries to do ipairs on value and then got a string instead of a table, resulting in the error.
Any ideas are welcome,
Thanks,
Erwin