haproxytech / haproxy-lua-http

Simple Lua HTTP helper && client for use with HAProxy.
Apache License 2.0
56 stars 23 forks source link

Require json #17

Open rocklee44 opened 2 years ago

rocklee44 commented 2 years ago

Hi all, My environment : Centos 8 + haproxy-2.2.10 + lua-5.3.5 install from source following instruction : https://kifarunix.com/install-haproxy-on-rocky-linux-8/ I followed instruction at : https://www.authelia.com/docs/deployment/supported-proxies/haproxy.html

global

Path to haproxy-lua-http, below example assumes /usr/local/etc/haproxy/haproxy-lua-http/http.lua

lua-prepend-path /usr/local/etc/haproxy/http.lua
# Path to haproxy-auth-request
lua-load /usr/local/etc/haproxy/auth-request.lua
log stdout format raw local0 debug

When I check haproxy cfg I get errors:

haproxy -c -f haproxy.cfg [NOTICE] (26946) : haproxy version is 2.4.2-553dee3 [NOTICE] (26946) : path to executable is /usr/local/sbin/haproxy [ALERT] (26946) : parsing [haproxy.cfg:5] : Lua runtime error: error loading module 'json' from file '/usr/local/etc/haproxy/http.lua': /usr/local/etc/haproxy/http.lua:673: too many C levels (limit is 200) in function at line 619 near 'v'

[ALERT] (26946) : parsing [haproxy.cfg:51]: 'http-request' expects 'wait-for-handshake', 'use-service', 'send-spoe-group', 'sc-inc-gpc0()', 'sc-inc-gpc1()', 'sc-set-gpt0()', 'do-resolve()', 'cache-use', 'add-acl()', 'add-header', 'allow', 'auth', 'capture', 'del-acl()', 'del-header', 'del-map()', 'deny', 'disable-l7-retry', 'early-hint', 'normalize-uri', 'redirect', 'reject', 'replace-header', 'replace-path', 'replace-pathq', 'replace-uri', 'replace-value', 'return', 'set-header', 'set-log-level', 'set-map()', 'set-method', 'set-mark', 'set-nice', 'set-path', 'set-pathq', 'set-query', 'set-tos', 'set-uri', 'strict-mode', 'tarpit', 'track-sc()', 'set-timeout', 'wait-for-body', 'set-var()', 'unset-var(*)', 'set-priority-class', 'set-priority-offset', 'silent-drop', 'set-src', 'set-src-port', 'set-dst', 'set-dst-port', but got 'lua.auth-request'. [ALERT] (26946) : Error(s) found in configuration file : haproxy.cfg

How can I fix it ? Please give me some advice , thank you very much.

rocklee44 commented 2 years ago

My server Centos 8(minimal) has already lua-5.3.4 installed , and I also installed lua-json

rpm -qa | grep lua
lua-json-1.3.2-9.el8.noarch
lua-libs-5.3.4-12.el8.x86_64
lua-lpeg-1.0.1-6.el8.x86_64
lua-5.3.4-12.el8.x86_64

I fixed it by editing /usr/local/etc/haproxy/http.lua

--local json = require "json"
local json = { _version = "1.3.2" }

It works fine now.

schklom commented 1 year ago

I don't remember where I read this, but I loaded https://github.com/rxi/json.lua with

    lua-load        /var/etc/haproxy/luascript_json.lua
    lua-load        /var/etc/haproxy/luascript_auth-request.lua
    lua-prepend-path /usr/local/share/lua/5.3/haproxy-lua-http.lua

to work for Authelia, and it works.

james-d-elliott commented 5 months ago

Pretty sure this is a dist issue with the various operating systems. Here's my test script I used to help diagnose it:

local json = require "json"

print("hello world")

Running that reveals the if the issue is exists because of the install or if it's something else (if it prints an error it's due to the versions installed of lua and lua-json which are conflicting, if it runs it's something else, if it runs after suggested fixes it shows it should now work), also reveals the search path lua is using (snippet 2). If you then use a tool like mlocate to find the locations json.lua exists you'll note that it may not exist for the version of lua you have installed. In which case you have to install a compatible lua version or use the workaround by @schklom.

Some example outputs for Ubuntu 22.04:

# lua -v
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
# lua example.lua
lua: example.lua:1: module 'json' not found:
    no field package.preload['json']
    no file '/usr/local/share/lua/5.4/json.lua'
    no file '/usr/local/share/lua/5.4/json/init.lua'
    no file '/usr/local/lib/lua/5.4/json.lua'
    no file '/usr/local/lib/lua/5.4/json/init.lua'
    no file '/usr/share/lua/5.4/json.lua'
    no file '/usr/share/lua/5.4/json/init.lua'
    no file './json.lua'
    no file './json/init.lua'
    no file '/usr/local/lib/lua/5.4/json.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.4/json.so'
    no file '/usr/lib/lua/5.4/json.so'
    no file '/usr/local/lib/lua/5.4/loadall.so'
    no file './json.so'
stack traceback:
    [C]: in function 'require'
    example.lua:1: in main chunk
    [C]: in ?
# locate json.lua
/usr/share/lua/5.1/json.lua
/usr/share/lua/5.2/json.lua
/usr/share/lua/5.3/json.lua

Fix for Ubuntu 22.04:

apt remove lua5.4 && apt install lua5.3

Seems in this specific fix it doesn't fix it for haproxy, it still has issues loading the module which is very odd.