Skip to content

Commit

Permalink
auditbeat: fix golangci lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mmat11 committed Nov 7, 2023
1 parent 28152d5 commit 404c45e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion auditbeat/helper/hasher/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (hasher *FileHasher) HashFile(path string) (map[HashType]Digest, error) {
}
}

var hashes []hash.Hash
hashes := make([]hash.Hash, 0)
for _, hashType := range hasher.config.HashTypes {
h, valid := validHashes[hashType]
if !valid {
Expand Down
2 changes: 1 addition & 1 deletion auditbeat/module/auditd/audit_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestBuildMetricbeatEvent(t *testing.T) {
}

func buildSampleEvent(t testing.TB, lines []string, filename string) {
var msgs []*auparse.AuditMessage
msgs := make([]*auparse.AuditMessage, 0)
for _, txt := range lines {
m, err := auparse.ParseLogLine(txt)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions auditbeat/module/auditd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
)

func TestConfig(t *testing.T) {
logp.TestingSetup()
require.NoError(t, logp.TestingSetup())

t.Run("Validate", func(t *testing.T) {
data := `
Expand Down Expand Up @@ -271,7 +272,7 @@ func parseConfig(t testing.TB, yaml string) (Config, error) {
}

func commands(rules []auditRule) []string {
var cmds []string
cmds := make([]string, 0)
for _, r := range rules {
cmds = append(cmds, r.flags)
}
Expand Down
19 changes: 12 additions & 7 deletions auditbeat/module/file_integrity/eventreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package file_integrity

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -30,6 +31,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func init() {
Expand Down Expand Up @@ -145,8 +147,9 @@ func TestEventReader(t *testing.T) {
if err != nil {
t.Fatal(err)
}
f.WriteString(" world!")
f.Sync()
_, err = f.WriteString(" world!")
require.NoError(t, err)
require.NoError(t, f.Sync())
f.Close()

event := readTimeout(t, events)
Expand Down Expand Up @@ -198,8 +201,9 @@ func TestEventReader(t *testing.T) {
if err != nil {
t.Fatal(err)
}
f.WriteString("move-in")
f.Sync()
_, err = f.WriteString("move-in")
require.NoError(t, err)
require.NoError(t, f.Sync())
f.Close()
moveInOrig = f.Name()

Expand Down Expand Up @@ -286,7 +290,7 @@ func TestRaces(t *testing.T) {
for i := 0; i < 10; i++ {
for _, dir := range dirs {
fname := filepath.Join(dir, fmt.Sprintf("%d.dat", i))
ioutil.WriteFile(fname, []byte("hello"), fileMode)
require.NoError(t, ioutil.WriteFile(fname, []byte("hello"), fileMode))
}
}
}()
Expand All @@ -298,7 +302,7 @@ func TestRaces(t *testing.T) {
const marker = "test_file"
for _, dir := range dirs {
fname := filepath.Join(dir, marker)
ioutil.WriteFile(fname, []byte("hello"), fileMode)
require.NoError(t, ioutil.WriteFile(fname, []byte("hello"), fileMode))
}

got := 0
Expand Down Expand Up @@ -431,7 +435,8 @@ func rename(t *testing.T, oldPath, newPath string) {
return
}

if linkErr, ok := err.(*os.LinkError); ok && linkErr.Err == ErrorSharingViolation {
var e *os.LinkError
if errors.As(err, &e) && errors.Is(e.Err, ErrorSharingViolation) {
time.Sleep(time.Millisecond)
continue
}
Expand Down
5 changes: 3 additions & 2 deletions auditbeat/module/file_integrity/fileinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewMetadata(t *testing.T) {
Expand All @@ -40,7 +41,7 @@ func TestNewMetadata(t *testing.T) {
if err != nil {
t.Fatal(err)
}
f.Sync()
require.NoError(t, f.Sync())
f.Close()

info, err := os.Lstat(f.Name())
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestSetUIDSetGIDBits(t *testing.T) {
if err != nil {
t.Fatal(err)
}
f.Sync()
require.NoError(t, f.Sync())
f.Close()

info, err := os.Lstat(f.Name())
Expand Down
2 changes: 1 addition & 1 deletion auditbeat/module/file_integrity/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
namespace = "."
)

var underTest = false
var underTest = false //nolint:unused // Used in Darwin-only builds.

func init() {
mb.Registry.MustAddMetricSet(moduleName, metricsetName, New,
Expand Down
20 changes: 10 additions & 10 deletions auditbeat/module/file_integrity/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/auditbeat/core"
"github.com/elastic/beats/v7/auditbeat/datastore"
Expand All @@ -49,7 +50,7 @@ func TestData(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
file := filepath.Join(dir, "file.data")
ioutil.WriteFile(file, []byte("hello world"), 0o600)
require.NoError(t, ioutil.WriteFile(file, []byte("hello world"), 0o600))
}()

ms := mbtest.NewPushMetricSetV2(t, getConfig(dir))
Expand Down Expand Up @@ -136,8 +137,8 @@ func TestActions(t *testing.T) {
}

// Create some files in first directory
ioutil.WriteFile(createdFilepath, []byte("hello world"), 0o600)
ioutil.WriteFile(updatedFilepath, []byte("hello world"), 0o600)
require.NoError(t, ioutil.WriteFile(createdFilepath, []byte("hello world"), 0o600))
require.NoError(t, ioutil.WriteFile(updatedFilepath, []byte("hello world"), 0o600))

ms := mbtest.NewPushMetricSetV2(t, getConfig(dir, newDir))
events := mbtest.RunPushMetricSetV2(10*time.Second, 5, ms)
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestExcludedFiles(t *testing.T) {
go func() {
for _, f := range []string{"FILE.TXT", "FILE.TXT.SWP", "file.txt.swo", ".git/HEAD", ".gitignore"} {
file := filepath.Join(dir, f)
ioutil.WriteFile(file, []byte("hello world"), 0o600)
_ = ioutil.WriteFile(file, []byte("hello world"), 0o600)
}
}()

Expand Down Expand Up @@ -264,10 +265,7 @@ func TestIncludedExcludedFiles(t *testing.T) {

for _, f := range []string{"FILE.TXT", ".ssh/known_hosts", ".ssh/known_hosts.swp"} {
file := filepath.Join(dir, f)
err := ioutil.WriteFile(file, []byte("hello world"), 0o600)
if err != nil {
t.Fatal(err)
}
require.NoError(t, ioutil.WriteFile(file, []byte("hello world"), 0o600))
}

events := mbtest.RunPushMetricSetV2(10*time.Second, 3, ms)
Expand Down Expand Up @@ -339,8 +337,9 @@ func TestErrorReporting(t *testing.T) {
done := make(chan struct{}, 1)
go func() {
for {
f.WriteString("can't read this\n")
f.Sync()
_, err := f.WriteString("can't read this\n")
require.NoError(t, err)
require.NoError(t, f.Sync())
select {
case <-done:
return
Expand All @@ -366,6 +365,7 @@ func TestErrorReporting(t *testing.T) {

var event *mb.Event
for idx, ev := range events {
ev := ev
t.Log("event[", idx, "] = ", ev)
if match(&ev) {
event = &ev
Expand Down
2 changes: 1 addition & 1 deletion auditbeat/module/file_integrity/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func New(recursive bool, IsExcludedPath func(path string) bool) (Watcher, error)
// Use our simulated recursive watches unless the fsnotify implementation
// supports OS-provided recursive watches
if recursive && watcher.SetRecursive() != nil {
return newRecursiveWatcher(watcher, IsExcludedPath), nil
return newRecursiveWatcher(watcher, IsExcludedPath), nil //nolint:nilerr // Ignore SetRecursive() errors.
}
return (*nonRecursiveWatcher)(watcher), nil
}
7 changes: 3 additions & 4 deletions auditbeat/module/file_integrity/monitor/recursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,19 @@ func (watcher *recursiveWatcher) deliver(ev fsnotify.Event) {
}
}

func (watcher *recursiveWatcher) forwardEvents() error {
func (watcher *recursiveWatcher) forwardEvents() {
defer watcher.close()

for {
select {
case <-watcher.done:
return nil

return
case path := <-watcher.addC:
watcher.addErrC <- watcher.addRecursive(path)

case event, ok := <-watcher.inner.Events:
if !ok {
return nil
return
}
if event.Name == "" {
continue
Expand Down

0 comments on commit 404c45e

Please sign in to comment.