Feature: Enhance Performance, Logging & Stability: Precompile Regex, Command Matching, Golang 1.24, History Cleanup & memLimitMiB Flag. (#182)

* Feat: Add support for logging which "command" was matched for SSH and HTTP strategies.

* Feat: Convert to precompiling regexp at config load time. This allows for errors to be presented to the user during startup, and provides better performance for complex regexp.

* Feat:Bump Golang version to latest stable 1.24

* Feat: Add a cleanup routine for HistoryStore, default TTL for events is 1 hour since last interaction.

* Feat: Add new command line flag "memLimitMiB" with a default value of 100.

---------

Signed-off-by: Bryan Nolen <bryan@arc.net.au>
Signed-off-by: Mario Candela <mario.candela.personal@gmail.com>
Co-authored-by: Mario Candela <mario.candela.personal@gmail.com>
This commit is contained in:
Bryan Nolen
2025-03-24 05:16:34 +11:00
committed by GitHub
parent 16b012784c
commit d677cd20b9
14 changed files with 249 additions and 89 deletions

View File

@ -3,6 +3,7 @@ package parser
import (
"errors"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
@ -56,6 +57,11 @@ commands:
handler: "login"
headers:
- "Content-Type: text/html"
- name: "wp-admin"
regex: "wp-admin"
handler: "login"
headers:
- "Content-Type: text/html"
fallbackCommand:
handler: "404 Not Found!"
statusCode: 404
@ -131,12 +137,14 @@ func TestReadConfigurationsServicesValid(t *testing.T) {
assert.Equal(t, firstBeelzebubServiceConfiguration.Protocol, "http")
assert.Equal(t, firstBeelzebubServiceConfiguration.ApiVersion, "v1")
assert.Equal(t, firstBeelzebubServiceConfiguration.Address, ":8080")
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Regex, "wp-admin")
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 2)
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 2)
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].RegexStr, "wp-admin")
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Regex.String(), "wp-admin")
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Handler, "login")
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands[0].Headers), 1)
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Headers[0], "Content-Type: text/html")
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[1].Name, "wp-admin")
assert.Equal(t, firstBeelzebubServiceConfiguration.FallbackCommand.Handler, "404 Not Found!")
assert.Equal(t, firstBeelzebubServiceConfiguration.FallbackCommand.StatusCode, 404)
assert.Equal(t, firstBeelzebubServiceConfiguration.Plugin.OpenAISecretKey, "qwerty")
@ -199,3 +207,79 @@ func TestReadFileBytesByFilePath(t *testing.T) {
assert.Equal(t, "", string(bytes))
}
func TestCompileCommandRegex(t *testing.T) {
tests := []struct {
name string
config BeelzebubServiceConfiguration
expectedError bool
}{
{
name: "Valid Regex",
config: BeelzebubServiceConfiguration{
Commands: []Command{
{RegexStr: "^/api/v1/.*$"},
{RegexStr: "wp-admin"},
},
},
expectedError: false,
},
{
name: "Empty Regex",
config: BeelzebubServiceConfiguration{
Commands: []Command{
{RegexStr: ""},
{RegexStr: ""},
},
},
expectedError: false,
},
{
name: "Invalid Regex",
config: BeelzebubServiceConfiguration{
Commands: []Command{
{RegexStr: "["},
},
},
expectedError: true,
},
{
name: "Mixed valid and Invalid Regex",
config: BeelzebubServiceConfiguration{
Commands: []Command{
{RegexStr: "^/api/v1/.*$"},
{RegexStr: "["},
{RegexStr: "test"},
},
},
expectedError: true,
},
{
name: "No commands",
config: BeelzebubServiceConfiguration{},
expectedError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.CompileCommandRegex()
if tt.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
for _, command := range tt.config.Commands {
if command.RegexStr != "" {
assert.NotNil(t, command.Regex)
_, err := regexp.Compile(command.RegexStr)
assert.NoError(t, err)
} else {
assert.Nil(t, command.Regex)
}
}
}
})
}
}