From ac0adef72976d2021c042a9855780528ebc52499 Mon Sep 17 00:00:00 2001 From: Christian Roessner Date: Fri, 25 Oct 2024 08:02:55 +0200 Subject: [PATCH] Fix: Update Redis script management for consistency and reuse Implemented centralized Redis script loading in `init.lua` and updated usage in `haveibeenpwnd.lua` to reduce code duplication. Adjusted error handling and return order in `scripts.go` and corrected test assertions in `scripts_test.go`. Signed-off-by: Christian Roessner --- .../lua-plugins.d/actions/haveibeenpwnd.lua | 15 +----------- server/lua-plugins.d/init/init.lua | 23 +++++++++++++++++++ server/lualib/redislib/scripts.go | 10 ++++---- server/lualib/redislib/scripts_test.go | 8 +++---- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/server/lua-plugins.d/actions/haveibeenpwnd.lua b/server/lua-plugins.d/actions/haveibeenpwnd.lua index 4f1c8dba..5462d037 100644 --- a/server/lua-plugins.d/actions/haveibeenpwnd.lua +++ b/server/lua-plugins.d/actions/haveibeenpwnd.lua @@ -113,20 +113,7 @@ function nauthilus_call_action(request) nauthilus_context.context_set(N .. "_hash_info", hash:sub(1, 5) .. cmp_hash[2]) nauthilus_builtin.custom_log_add(N .. "_action", "leaked") - local script = [[ - local redis_key = KEYS[1] - local send_mail = redis.call('HGET', redis_key, 'send_mail') - - if send_mail == false then - redis.call('HSET', redis_key, 'send_mail', '1') - - return {'send_email', redis_key} - else - return {'email_already_sent'} - end - ]] - - local script_result, err_run_script = nauthilus_redis.redis_run_script(script, false, { redis_key }, {}) + local script_result, err_run_script = nauthilus_redis.redis_run_script("", "nauthilus_send_mail_hash", { redis_key }, {}) nauthilus_util.if_error_raise(err_run_script) if script_result[1] == "send_mail" then diff --git a/server/lua-plugins.d/init/init.lua b/server/lua-plugins.d/init/init.lua index c7930621..9c947e75 100644 --- a/server/lua-plugins.d/init/init.lua +++ b/server/lua-plugins.d/init/init.lua @@ -21,6 +21,9 @@ local nauthilus_prometheus = require("nauthilus_prometheus") dynamic_loader("nauthilus_psnet") local nauthilus_psnet = require("nauthilus_psnet") +dynamic_loader("nauthilus_redis") +local nauthilus_redis = require("nauthilus_redis") + local N = "init" function nauthilus_run_hook(logging) @@ -29,6 +32,26 @@ function nauthilus_run_hook(logging) result.level = "info" result.caller = N .. ".lua" + local script = [[ + local redis_key = KEYS[1] + local send_mail = redis.call('HGET', redis_key, 'send_mail') + + if send_mail == false then + redis.call('HSET', redis_key, 'send_mail', '1') + + return {'send_email', redis_key} + else + return {'email_already_sent'} + end + ]] + + local upload_script_name = "nauthilus_send_mail_hash" + local sha1, err_upload = nauthilus_redis.redis_upload_script(script, upload_script_name) + + nauthilus_util.if_error_raise(err_upload) + + result[upload_script_name] = sha1 + -- common nauthilus_prometheus.create_gauge_vec("http_client_concurrent_requests_total", "Measure the number of total concurrent HTTP client requests", { "service" }) diff --git a/server/lualib/redislib/scripts.go b/server/lualib/redislib/scripts.go index f251f919..9c357949 100644 --- a/server/lualib/redislib/scripts.go +++ b/server/lualib/redislib/scripts.go @@ -109,14 +109,14 @@ func RedisRunScript(L *lua.LState) int { result, err := evaluateRedisScript(script, uploadScriptName, keyList, argsList...) if err != nil { - L.Push(lua.LString(err.Error())) L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) return 2 } - L.Push(lua.LNil) L.Push(convert.GoToLuaValue(L, result)) + L.Push(lua.LNil) return 2 } @@ -128,8 +128,8 @@ func RedisUploadScript(L *lua.LState) int { sha1, err := uploadRedisScript(script) if err != nil { - L.Push(lua.LString(err.Error())) L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) return 2 } @@ -137,14 +137,14 @@ func RedisUploadScript(L *lua.LState) int { if scriptSha1, okay := sha1.(string); okay { uploads.Set(uploadScriptName, scriptSha1) - L.Push(lua.LNil) L.Push(lua.LString(scriptSha1)) + L.Push(lua.LNil) return 2 } - L.Push(lua.LString(fmt.Sprintf("Could not convert script SHA1 to string: %v", sha1))) L.Push(lua.LNil) + L.Push(lua.LString(fmt.Sprintf("Could not convert script SHA1 to string: %v", sha1))) return 2 } diff --git a/server/lualib/redislib/scripts_test.go b/server/lualib/redislib/scripts_test.go index 8487fa1e..05525690 100644 --- a/server/lualib/redislib/scripts_test.go +++ b/server/lualib/redislib/scripts_test.go @@ -82,14 +82,14 @@ func TestRedisRunScript(t *testing.T) { // Call function and check error numReturned := RedisRunScript(L) - errReturned := L.Get(-2).String() != "nil" + errReturned := L.Get(-1).String() != "nil" assert.Equal(t, tc.expectErr, errReturned, "") assert.Equal(t, 2, numReturned, "") // Check result if no error if !tc.expectErr && numReturned > 0 { - resReturned := L.Get(-1).String() + resReturned := L.Get(-2).String() assert.Equal(t, tc.expectRes, resReturned, "") } @@ -150,14 +150,14 @@ func TestRedisUploadScript(t *testing.T) { L.Push(lua.LString(tc.uploadScriptName)) numReturned := RedisUploadScript(L) - errReturned := L.Get(-2).String() != "nil" + errReturned := L.Get(-1).String() != "nil" assert.Equal(t, tc.expectErr, errReturned, "") assert.Equal(t, 2, numReturned, "") // Check result if no error if !tc.expectErr && numReturned > 0 { - shaReturned := L.Get(-1).String() + shaReturned := L.Get(-2).String() assert.Equal(t, tc.expectedSHA, shaReturned, "") }