diff mbox series

suricatta/wfx: Fix proxy handling

Message ID 41AE80A0-68B2-4A6B-A280-269A0E49DD7B@siemens.com
State Accepted
Headers show
Series suricatta/wfx: Fix proxy handling | expand

Commit Message

Storm, Christian Nov. 30, 2023, 12:36 p.m. UTC
wfx's proxy handling works with an explicitly given proxy.
It didn't for fallback to proxy environment variables.
Hence, introduce USE_PROXY_ENV as already used in hawkBit.
While at it, sanity-check whether the environment variables
to fall back to are actually existing.

Reported-by: Enes Colpan <enes.colpan@siemens.com>
Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 suricatta/server_lua.c   | 23 ++++++++++++++++++++---
 suricatta/server_wfx.lua | 18 +++++++++++++++++-
 suricatta/suricatta.lua  |  6 ++++++
 3 files changed, 43 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/suricatta/server_lua.c b/suricatta/server_lua.c
index 62bc35be..efeb092b 100644
--- a/suricatta/server_lua.c
+++ b/suricatta/server_lua.c
@@ -526,7 +526,11 @@  static void channel_push_options(lua_State *L, channel_data_t *channel_data)
 	push_to_table(L, "sslkey",             channel_data->sslkey);
 	push_to_table(L, "sslcert",            channel_data->sslcert);
 	push_to_table(L, "ciphers",            channel_data->ciphers);
-	push_to_table(L, "proxy",              channel_data->proxy);
+	if (channel_data->proxy && channel_data->proxy == USE_PROXY_ENV) {
+		push_to_table(L, "proxy",      "");
+	} else {
+		push_to_table(L, "proxy",      channel_data->proxy);
+	}
 	push_to_table(L, "info",               channel_data->info);
 	push_to_table(L, "auth_token",         channel_data->auth_token);
 	push_to_table(L, "content_type",       channel_data->content_type);
@@ -568,7 +572,6 @@  static void channel_set_options(lua_State *L, channel_data_t *channel_data)
 	get_from_table(L, "sslkey",             channel_data->sslkey, COPY_DEST);
 	get_from_table(L, "sslcert",            channel_data->sslcert, COPY_DEST);
 	get_from_table(L, "ciphers",            channel_data->ciphers, COPY_DEST);
-	get_from_table(L, "proxy",              channel_data->proxy, COPY_DEST);
 	get_from_table(L, "info",               channel_data->info, COPY_DEST);
 	get_from_table(L, "auth_token",         channel_data->auth_token, COPY_DEST);
 	get_from_table(L, "content_type",       channel_data->content_type, COPY_DEST);
@@ -589,6 +592,14 @@  static void channel_set_options(lua_State *L, channel_data_t *channel_data)
 		channel_data->max_download_speed = (unsigned int)ustrtoull(max_download_speed, NULL, 10);
 		free(max_download_speed);
 	}
+	lua_getfield(L, -1, "proxy");
+	if (lua_isstring(L, -1)) {
+		channel_data->proxy = strnlen(lua_tostring(L, -1), 1) > 0
+			? strdup(lua_tostring(L, -1)) : USE_PROXY_ENV;
+	} else {
+		channel_data->proxy = NULL;
+	}
+	lua_pop(L, 1);
 }
 
 
@@ -613,7 +624,9 @@  static void channel_free_options(channel_data_t *channel_data)
 	free(channel_data->sslkey);
 	free(channel_data->sslcert);
 	free(channel_data->ciphers);
-	free(channel_data->proxy);
+	if (channel_data->proxy && channel_data->proxy != USE_PROXY_ENV) {
+		free(channel_data->proxy);
+	}
 	free(channel_data->info);
 }
 
@@ -1620,6 +1633,10 @@  static int suricatta_lua_module(lua_State *L)
 	channel_push_options(L, &channel_data_defaults);
 	lua_settable(L, -3);
 
+	lua_pushstring(L, "USE_PROXY_ENV");
+	lua_pushstring(L, "");
+	lua_settable(L, -3);
+
 	lua_pushstring(L, "content");
 	lua_newtable(L);
 	push_to_table(L, "RAW",  CHANNEL_PARSE_RAW);
diff --git a/suricatta/server_wfx.lua b/suricatta/server_wfx.lua
index 3fd61b57..2bd7478d 100644
--- a/suricatta/server_wfx.lua
+++ b/suricatta/server_wfx.lua
@@ -2020,7 +2020,23 @@  function M.suricatta_funcs.server_start(defaults, argv, fconfig)
         elseif opt == "i" then
             configuration.id = tostring(arg)
         elseif opt == "y" then
-            configuration.proxy = tostring(arg)
+            if not arg then
+                io.stderr:write("ERROR: proxy parameter is not a valid string.\n")
+                return suricatta.status.EINIT
+            end
+            configuration.proxy = tostring(arg):gsub('["\']', '')
+            if #configuration.proxy == 0 then
+                configuration.proxy = suricatta.channel.USE_PROXY_ENV
+                if
+                    not os.getenv("http_proxy")
+                    and not os.getenv("https_proxy")
+                    and not os.getenv("HTTPS_PROXY")
+                    and not os.getenv("ALL_PROXY")
+                then
+                    io.stderr:write("ERROR: Should use proxy but no proxy environment variables nor proxy URL set.\n")
+                    return suricatta.status.EINIT
+                end
+            end
         elseif opt == "p" then
             configuration.polldelay = tonumber(arg) or configuration.polldelay
         elseif opt == "r" then
diff --git a/suricatta/suricatta.lua b/suricatta/suricatta.lua
index 23a964c0..6a4fa131 100644
--- a/suricatta/suricatta.lua
+++ b/suricatta/suricatta.lua
@@ -196,6 +196,12 @@  suricatta.server.register = function(function_p, purpose) end
 
 --- @class suricatta.channel
 suricatta.channel = {
+
+    -- Lua-alike of proxy environment variable usage marker as in `include/channel_curl.h`.
+    -- An empty `proxy` string means to use proxy environment variables.
+    -- @type string
+    USE_PROXY_ENV = "",
+
     --- @enum suricatta.channel.content
     --- Content type passed over the channel as in `include/channel_curl.h`.
     content = {