Skip to content

Commit

Permalink
simplify filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
defensivedepth committed May 6, 2024
1 parent 7993c02 commit 66b4006
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 30 deletions.
8 changes: 1 addition & 7 deletions server/modules/elastalert/elastalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"regexp"
Expand Down Expand Up @@ -68,7 +67,6 @@ type IOManager interface {
MakeRequest(*http.Request) (*http.Response, error)
ExecCommand(cmd *exec.Cmd) ([]byte, int, time.Duration, error)
WalkDir(root string, fn fs.WalkDirFunc) error
JoinPath(parts ...string) string
}

type ElastAlertEngine struct {
Expand Down Expand Up @@ -923,7 +921,7 @@ func (e *ElastAlertEngine) loadSigmaPackagesFromDisk() (zipData map[string][]byt
stats := map[string]int{}

for _, pkg := range e.sigmaRulePackages {
filePath := e.JoinPath(e.airgapBasePath, "sigma_"+pkg+".zip")
filePath := filepath.Join(e.airgapBasePath, "sigma_"+pkg+".zip")

data, err := e.ReadFile(filePath)
if err != nil {
Expand Down Expand Up @@ -1342,10 +1340,6 @@ func (_ *ResourceManager) DeleteFile(path string) error {
return os.Remove(path)
}

func (r *ResourceManager) JoinPath(parts ...string) string {
return path.Join(parts...)
}

func (_ *ResourceManager) ReadDir(path string) ([]os.DirEntry, error) {
return os.ReadDir(path)
}
Expand Down
10 changes: 5 additions & 5 deletions server/modules/elastalert/elastalert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestElastAlertModule(t *testing.T) {

err := mod.Init(nil)
assert.NoError(t, err)
assert.False(t, mod.autoUpdateEnabled)
assert.False(t, mod.airgapEnabled)

err = mod.Start()
assert.NoError(t, err)
Expand Down Expand Up @@ -537,14 +537,14 @@ func TestLoadSigmaPackagesFromDisks(t *testing.T) {

// Setting up mocks for each expected file read, except for the "fake" package to simulate an error
for _, pkg := range pkgs[:len(pkgs)-1] {
expectedFilePath := airgapBasePath + "/sigma_" + pkg + ".zip"
mio.EXPECT().JoinPath(airgapBasePath, "sigma_"+pkg+".zip").Return(expectedFilePath)
expectedFilePath := airgapBasePath + "sigma_" + pkg + ".zip"
//mio.EXPECT().Join(airgapBasePath, "sigma_"+pkg+".zip").Return(expectedFilePath)
mio.EXPECT().ReadFile(expectedFilePath).Return([]byte("mocked data for "+pkg), nil)
}

// Simulating an error for the 'fake' package
fakeFilePath := airgapBasePath + "/sigma_fake.zip"
mio.EXPECT().JoinPath(airgapBasePath, "sigma_fake.zip").Return(fakeFilePath)
fakeFilePath := airgapBasePath + "sigma_fake.zip"
//mio.EXPECT().JoinPath(airgapBasePath, "sigma_fake.zip").Return(fakeFilePath)
mio.EXPECT().ReadFile(fakeFilePath).Return(nil, errors.New("file not found"))

engine := ElastAlertEngine{
Expand Down
19 changes: 1 addition & 18 deletions server/modules/elastalert/mock/mock_iomanager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66b4006

Please sign in to comment.