Lua Plugin - io.open

I’ve been trying to find a solution for a day now, but I can’t find one, so maybe someone can help me.

I am trying to develop a lua plugin that checks if some conditions are true based on if content XY is in a file. So far it doesn’t sound that complex, but I fail already at reading the file when I start my Haproxy with the following code:

...
local file = io.open(file_path, "r")
if file == nil then
-- THIS IS ALWAYS THE CASE / ALWAYS TRUE
-- DO STUFF WHEN THE FILE CANNOT BE READ
else
local contents = file:read("*all")
file:close()
-- DO CHECKS ETC.
end
...

Then I always get the error message:

[ALERT] 082/140647 (12357) : Lua sample-fetch 'check_whitelist': runtime error: /etc/haproxy/lua_plugins/ipauac.lua:14: attempt to index a nil value (global 'file') from /etc/haproxy/lua_plugins/ipauac.lua:14 C function line 1.

I have already tested some other things, such as executing these checks based on a string, and this all works, but not with the file.

The Haproxy Config looks like this:

global

lua-load /etc/haproxy/lua_plugins/ipauac.lua

...

frontend my_frontend

...

http-request set-var(txn.user_ip) src

http-response set-header Cache-Control no-store

http-request redirect location google.com code 302 if !{ lua.check_whitelist(txn.user_ip) -m bool }

...

The path to the file etc. is certainly correct because funnily enough the code (open the file etc.) works when I execute it directly with Lua but not with haproxy.

You cannot use blocking LUA functions in haproxy, like everything IO related, also see:

https://www.mail-archive.com/haproxy@formilux.org/msg42795.html

What other possibility do I have to read out the file? I can’t save the content in a string because the content in the file is constantly updated.

You cannot interact with files during haproxy runtime.

You can interact with HTTP services, so I suggest to move the file handling to a external HTTP service and use HTTP to communicate between the two.