Skip to content

Commit

Permalink
feat: add lua http module (#6)
Browse files Browse the repository at this point in the history
* feat: add lua http module

* feat: pass variables to lua
  • Loading branch information
skynet2 authored Jul 10, 2024
1 parent d794455 commit 857585a
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 113 deletions.
9 changes: 8 additions & 1 deletion cmd/client/apimonkey/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ func (i *Instance) sendAndProcess(ctx context.Context) error {
if strings.TrimSpace(i.cfg.BodyScript) != "" {
zerolog.Ctx(ctx).Trace().Str("script", i.cfg.BodyScript).Msg("executing script")

scriptResult, scriptErr := i.executor.Execute(ctx, i.cfg.BodyScript, value, resp.StatusCode)
scriptResult, scriptErr := i.executor.Execute(
ctx,
i.cfg.BodyScript,
value,
resp.StatusCode,
i.cfg.Headers,
i.cfg.TemplateParameters,
)
if scriptErr != nil {
return errors.Wrap(scriptErr, "error executing script")
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/client/apimonkey/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ package main
import "context"

type ScriptExecutor interface {
Execute(ctx context.Context, script string, rawBody string, statusCode int) (string, error)
Execute(
ctx context.Context,
script string,
rawBody string,
statusCode int,
headers map[string]string,
templateVariables map[string]string,
) (string, error)
}
17 changes: 17 additions & 0 deletions cmd/scripts/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package scripts

import (
"context"
"net/http"

"github.com/cjoudrey/gluahttp"
lua "github.com/yuin/gopher-lua"
luajson "layeh.com/gopher-json"
)
Expand All @@ -19,14 +21,29 @@ func (e *Lua) Execute(
script string,
rawBody string,
statusCode int,
headers map[string]string,
templateVariables map[string]string,
) (string, error) {
l := lua.NewState()
defer l.Close()
luajson.Preload(l)
l.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader)

l.SetGlobal("ResponseBody", lua.LString(rawBody))
l.SetGlobal("ResponseStatusCode", lua.LNumber(statusCode))

luaHeaders := l.NewTable()
for k, v := range headers {
l.SetTable(luaHeaders, lua.LString(k), lua.LString(v))
}
l.SetGlobal("Headers", luaHeaders)

luaTemplateVariables := l.NewTable()
for k, v := range templateVariables {
l.SetTable(luaTemplateVariables, lua.LString(k), lua.LString(v))
}
l.SetGlobal("TemplateVariables", luaTemplateVariables)

if err := l.DoString(script); err != nil {
return "", err
}
Expand Down
17 changes: 16 additions & 1 deletion cmd/scripts/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@ func TestLua(t *testing.T) {

resp, err := executor.Execute(context.TODO(), `
message = "Hello, world!" .. tostring(_G.ResponseStatusCode)
return message .. _G.ResponseBody`, jsonString, 200)
return message .. _G.ResponseBody`, jsonString, 200, nil, nil)
assert.NoError(t, err)

assert.Equal(t, "Hello, world!200{\"alerts\":[{\"labels\":{\"alertname\":\"Watchdog\"}},{\"labels\":{\"alertname\":\"CriticalAlert\"}},{\"labels\":{\"alertname\":\"AnotherAlert\"}}]}", resp)
}

func TestLuaWithHeadersAndTemplate(t *testing.T) {
executor := scripts.NewLua()

resp, err := executor.Execute(context.TODO(), `
message = "Hello, world!" .. tostring(_G.Headers["SomeHeader"]) .. tostring(_G.TemplateVariables["key1"])
return message`,
jsonString,
200,
map[string]string{"Content-Type": "application/json", "SomeHeader": "SomeValue"},
map[string]string{"key1": "value1", "key2": "value2"},
)
assert.NoError(t, err)
assert.Equal(t, "Hello, world!SomeValuevalue1", resp)
}
54 changes: 27 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,65 @@ go 1.22
toolchain go1.22.1

require (
github.com/cockroachdb/errors v1.11.1
github.com/gofiber/fiber/v2 v2.52.2
github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9
github.com/cockroachdb/errors v1.11.3
github.com/gofiber/fiber/v2 v2.52.5
github.com/google/go-github/v38 v38.1.0
github.com/google/uuid v1.6.0
github.com/imroc/req/v3 v3.43.1
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.8.2
github.com/imroc/req/v3 v3.43.7
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.1
github.com/valyala/fastjson v1.6.4
github.com/yuin/gopher-lua v1.1.1
golang.org/x/oauth2 v0.18.0
golang.org/x/oauth2 v0.21.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
meow.tf/streamdeck/sdk v0.0.0-20221205105203-dab3b98dc3bd
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/getsentry/sentry-go v0.28.1 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20240320155624-b11c3daa6f07 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/onsi/ginkgo/v2 v2.17.1 // indirect
github.com/onsi/ginkgo/v2 v2.19.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.42.0 // indirect
github.com/refraction-networking/utls v1.6.3 // indirect
github.com/quic-go/quic-go v0.45.1 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/mock v0.4.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 857585a

Please sign in to comment.