haproxy / haproxy

HAProxy Load Balancer's development branch (mirror of git.haproxy.org)
https://git.haproxy.org/
Other
5.01k stars 799 forks source link

setting global variables in register_init #2527

Open anandcta123 opened 7 months ago

anandcta123 commented 7 months ago

Detailed Description of the Problem

I am planning to read a configuration file. Within an action, trying to open a JSON file gives me error and with register_init function, it works. But using register_init, I am not receiving txn object, so not understanding how we can set variables or read variables in init context.

Expected Behavior

Need to understand, is there any way to manipulate global variables in register_init.

Steps to Reproduce the Behavior

Nothing to reproduce, trying to understand, is there any possibility to set/get variables.

Do you have any idea what may have caused this?

NO

Do you have an idea how to solve the issue?

NO

What is your configuration?

global
        #log /dev/log   local0  info
        lua-prepend-path /etc/haproxy/utils/?.lua
        lua-load /etc/haproxy/rdwr_init.lua
        lua-load-per-thread /etc/haproxy/rdwr_httpclient_connector.lua
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        httpclient.ssl.verify none

        set-var proc.rdwr_app_ep_addr str("789cb174de2d498889cc9d64547b05ab.oop.radwarecloud.net")
        set-var proc.x_rdwr_app_id str("789cb174-de2d-4988-89cc-9d64547b05ab")
        set-var proc.x_rdwr_api_key str("19d5b025-2e2e-4b4a-a81b-205472a6520c")
        set-var proc.rdwr_app_ep_port int(443) #optional
        set-var proc.rdwr_app_ep_timeout int(600) #optional
        set-var proc.rdwr_app_ep_ssl int(1) #optional
        set-var proc.rdwr_partial_body_size int(10420)#optional
        set-var proc.rdwr_true_client_ip_header str("X-Forwarded-For") #optional
        set-var proc.rdwr_bot_manager_enabled str("true")
        set-var proc.rdwr_js_snippet str("")

defaults
        log global
        mode    http
        option  httplog
        option logasap
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        option http-keep-alive

frontend fe_main
    mode http
    bind *:80 v4v6
    bind *:443 ssl crt /etc/ssl/private/mydomain.pem

    http-request wait-for-body time 1s at-least 10420
    http-request lua.rdwrHttpClientConnector

    filter lua.botm-filter

    use_backend be_servers

backend be_servers
    balance roundrobin
    server server1 127.0.0.1:8081 check

~

Output of haproxy -vv

anandam@ubuntu20:/etc/haproxy$ haproxy -vv
HAProxy version 2.8.7-1ppa1~focal 2024/03/02 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2028.
Known bugs: http://www.haproxy.org/bugs/bugs-2.8.7.html
Running on: Linux 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64
Build options :
  TARGET  = linux-glibc
  CPU     = generic
  CC      = cc
  CFLAGS  = -O2 -g -O2 -fdebug-prefix-map=/build/haproxy-A73k7e/haproxy-2.8.7=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -Wall -Wextra -Wundef -Wdeclaration-after-statement -Wfatal-errors -Wtype-limits -Wshift-negative-value -Wshift-overflow=2 -Wduplicated-cond -Wnull-dereference -fwrapv -Wno-address-of-packed-member -Wno-unused-label -Wno-sign-compare -Wno-unused-parameter -Wno-clobbered -Wno-missing-field-initializers -Wno-cast-function-type -Wno-string-plus-int -Wno-atomic-alignment
  OPTIONS = USE_OPENSSL=1 USE_LUA=1 USE_SLZ=1 USE_SYSTEMD=1 USE_QUIC=1 USE_PROMEX=1 USE_PCRE2=1 USE_PCRE2_JIT=1 USE_QUIC_OPENSSL_COMPAT=1
  DEBUG   = -DDEBUG_STRICT -DDEBUG_MEMORY_POOLS

Feature list : -51DEGREES +ACCEPT4 +BACKTRACE -CLOSEFROM +CPU_AFFINITY +CRYPT_H -DEVICEATLAS +DL -ENGINE +EPOLL -EVPORTS +GETADDRINFO -KQUEUE -LIBATOMIC +LIBCRYPT +LINUX_CAP +LINUX_SPLICE +LINUX_TPROXY +LUA +MATH -MEMORY_PROFILING +NETFILTER +NS -OBSOLETE_LINKER +OPENSSL -OPENSSL_WOLFSSL -OT -PCRE +PCRE2 +PCRE2_JIT -PCRE_JIT +POLL +PRCTL -PROCCTL +PROMEX -PTHREAD_EMULATION +QUIC +QUIC_OPENSSL_COMPAT +RT +SHM_OPEN +SLZ +SSL -STATIC_PCRE -STATIC_PCRE2 +SYSTEMD +TFO +THREAD +THREAD_DUMP +TPROXY -WURFL -ZLIB

Default settings :
  bufsize = 16384, maxrewrite = 1024, maxpollevents = 200

Built with multi-threading support (MAX_TGROUPS=16, MAX_THREADS=256, default=16).
Built with OpenSSL version : OpenSSL 1.1.1f  31 Mar 2020
Running on OpenSSL version : OpenSSL 1.1.1f  31 Mar 2020
OpenSSL library supports TLS extensions : yes
OpenSSL library supports SNI : yes
OpenSSL library supports : TLSv1.0 TLSv1.1 TLSv1.2 TLSv1.3
Built with Lua version : Lua 5.3.3
Built with the Prometheus exporter as a service
Built with network namespace support.
Built with libslz for stateless compression.
Compression algorithms supported : identity("identity"), deflate("deflate"), raw-deflate("deflate"), gzip("gzip")
Built with transparent proxy support using: IP_TRANSPARENT IPV6_TRANSPARENT IP_FREEBIND
Built with PCRE2 version : 10.34 2019-11-21
PCRE2 library supports JIT : yes
Encrypted password support via crypt(3): yes
Built with gcc compiler version 9.4.0

Available polling systems :
      epoll : pref=300,  test result OK
       poll : pref=200,  test result OK
     select : pref=150,  test result OK
Total: 3 (3 usable), will use epoll.

Available multiplexer protocols :
(protocols marked as <default> cannot be specified using 'proto' keyword)
       quic : mode=HTTP  side=FE     mux=QUIC  flags=HTX|NO_UPG|FRAMED
         h2 : mode=HTTP  side=FE|BE  mux=H2    flags=HTX|HOL_RISK|NO_UPG
       fcgi : mode=HTTP  side=BE     mux=FCGI  flags=HTX|HOL_RISK|NO_UPG
  <default> : mode=HTTP  side=FE|BE  mux=H1    flags=HTX
         h1 : mode=HTTP  side=FE|BE  mux=H1    flags=HTX|NO_UPG
  <default> : mode=TCP   side=FE|BE  mux=PASS  flags=
       none : mode=TCP   side=FE|BE  mux=PASS  flags=NO_UPG

Available services : prometheus-exporter
Available filters :
        [BWLIM] bwlim-in
        [BWLIM] bwlim-out
        [CACHE] cache
        [COMP] compression
        [FCGI] fcgi-app
        [SPOE] spoe
        [TRACE] trace

Last Outputs and Backtraces

No response

Additional Information

No response

Darlelet commented 7 months ago

Since 2.9 you may use core.get_var() method to fetch variables from proc.* scope: https://www.arpalert.org/src/haproxy-lua-api/3.0/index.html#core.get_var

For now there is no core.set_var() so you cannot set proc.* variables outside of TXN context, see #2212

However you may set global Lua variables within your script and register custom Lua fetches to return desired values and use the fetch results from haproxy configuration. See https://www.arpalert.org/src/haproxy-lua-api/3.0/index.html#core.register_fetches

In the end there doesn't seem to be an actual bug or issue on haproxy side, thus if you need more assistance to solve your use-case feel to ask for help on the community forum (https://discourse.haproxy.org/) or on the mailing list (https://www.haproxy.org/#tact), some users probably faced similar challenges and may be able to help you.

Hope this helps

capflam commented 7 months ago

Can we close the issue or should we change it to a feature request ?