# Custom plugin loads but can't create: lua ipairs error

**URL:** <https://discuss.konghq.com/t/custom-plugin-loads-but-cant-create-lua-ipairs-error/9258>\
**Category:** Questions\
**Created:** [October 25, 2021, 7:37am UTC](https://discuss.konghq.com/t/custom-plugin-loads-but-cant-create-lua-ipairs-error/9258 "2021-10-25T07:37:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwin\_Brand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/erwin_brand/32/3065_2.png) [@Erwin\_Brand](https://discuss.konghq.com/u/Erwin_Brand)\
**Post date:** [October 25, 2021, 7:37am UTC](https://discuss.konghq.com/t/custom-plugin-loads-but-cant-create-lua-ipairs-error/9258/1 "2021-10-25T07:37:41Z")

</div>

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](https://github.com/enioka/kong-plugin-upstream-oauth2), 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/](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

---

<div class="post-metadata">

**Author:** ![Erwin\_Brand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/erwin_brand/32/3065_2.png) [@Erwin\_Brand](https://discuss.konghq.com/u/Erwin_Brand)\
**Post date:** [October 26, 2021, 10:43am UTC](https://discuss.konghq.com/t/custom-plugin-loads-but-cant-create-lua-ipairs-error/9258/2 "2021-10-26T10:43:30Z")

</div>

This error only occurs when Kong config has keyring\_enabled=on.

---

<div class="post-metadata">

**Author:** ![Erwin\_Brand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/erwin_brand/32/3065_2.png) [@Erwin\_Brand](https://discuss.konghq.com/u/Erwin_Brand)\
**Post date:** [October 26, 2021, 12:26pm UTC](https://discuss.konghq.com/t/custom-plugin-loads-but-cant-create-lua-ipairs-error/9258/3 "2021-10-26T12:26:10Z")

</div>

Now gladfully I know the root-cause, which is a clash of my defined parameters with the ones in openid-connect plugin, which is builtin and only available in Kong Enterprise. My error only occurs when I use DB encryption, which is again an enterprise only feature.

The root-cause is that my client\_id and client\_secret parameters are defined as string but openid-connect parameters are defined as array, somehow when encrypt and storing to DB the type of built-in parameters is used hence the error occurs with no more explanation than that something went wrong. This bug still exists in the latest Kong version.

My work-around is to rename my parameters.

Question is answered.
