Skip to content

Commit

Permalink
Fix: Update Redis script management for consistency and reuse
Browse files Browse the repository at this point in the history
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 <c@roessner.co>
  • Loading branch information
Christian Roessner committed Oct 25, 2024
1 parent c8c0911 commit ac0adef
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
15 changes: 1 addition & 14 deletions server/lua-plugins.d/actions/haveibeenpwnd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions server/lua-plugins.d/init/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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" })

Expand Down
10 changes: 5 additions & 5 deletions server/lualib/redislib/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -128,23 +128,23 @@ 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
}

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
}
8 changes: 4 additions & 4 deletions server/lualib/redislib/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}
Expand Down Expand Up @@ -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, "")
}
Expand Down

0 comments on commit ac0adef

Please sign in to comment.