Skip to content

Commit

Permalink
manual replacement of github.com/pkg/error with standard library erro…
Browse files Browse the repository at this point in the history
…r handling

This is the manual part of the change and includes some complete removal of errors
and some silently passed bugs resulting from the use of Wrap.

In one place the exact semantics of errors.Cause is implemented to fit in with a
testing strategy.
  • Loading branch information
efd6 committed Feb 10, 2022
1 parent 3236fdc commit 5a53f65
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 16 deletions.
17 changes: 16 additions & 1 deletion auditbeat/helper/hasher/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,20 @@ func TestHasherLimits(t *testing.T) {
hashes, err := hasher.HashFile(file)
assert.Empty(t, hashes)
assert.Error(t, err)
assert.IsType(t, FileTooLargeError{}, errors.Cause(err))
assert.IsType(t, FileTooLargeError{}, cause(err))
}

func cause(err error) error {
type unwrapper interface {
Unwrap() error
}

for err != nil {
w, ok := err.(unwrapper)
if !ok {
break
}
err = w.Unwrap()
}
return err
}
5 changes: 4 additions & 1 deletion auditbeat/module/auditd/audit_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ func (ms *MetricSet) initClient() error {
// required to ensure that auditing is enabled if the process is only
// given CAP_AUDIT_READ.
err := ms.client.SetEnabled(true, libaudit.NoWait)
return errors.Wrap(err, "failed to enable auditing in the kernel")
if err != nil {
return fmt.Errorf("failed to enable auditing in the kernel: %w", err)
}
return nil
}

// Unicast client initialization (requires CAP_AUDIT_CONTROL and that the
Expand Down
8 changes: 5 additions & 3 deletions auditbeat/module/file_integrity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ nextHash:
}

c.MaxFileSizeBytes, err = humanize.ParseBytes(c.MaxFileSize)
if err != nil || c.MaxFileSizeBytes > MaxValidFileSizeLimit {
errs = append(errs, errors.Wrap(err, "invalid max_file_size value"))
if err != nil {
errs = append(errs, fmt.Errorf("invalid max_file_size value: %w", err))
} else if c.MaxFileSizeBytes > MaxValidFileSizeLimit {
errs = append(errs, fmt.Errorf("invalid max_file_size value: %s is too large (max=%s)", c.MaxFileSize, humanize.Bytes(MaxValidFileSizeLimit)))
} else if c.MaxFileSizeBytes <= 0 {
errs = append(errs, errors.Errorf("max_file_size value (%v) must be positive", c.MaxFileSize))
errs = append(errs, fmt.Errorf("max_file_size value (%v) must be positive", c.MaxFileSize))
}

c.ScanRateBytesPerSec, err = humanize.ParseBytes(c.ScanRatePerSec)
Expand Down
11 changes: 7 additions & 4 deletions auditbeat/module/file_integrity/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ func NewEvent(
hashTypes []HashType,
) Event {
info, err := os.Lstat(path)
if err != nil && os.IsNotExist(err) {
// deleted file is signaled by info == nil
err = nil
if err != nil {
if os.IsNotExist(err) {
// deleted file is signaled by info == nil
err = nil
} else {
err = fmt.Errorf("failed to lstat: %w", err)
}
}
err = errors.Wrap(err, "failed to lstat")
return NewEventFromFileInfo(path, info, err, action, source, maxFileSize, hashTypes)
}

Expand Down
3 changes: 2 additions & 1 deletion auditbeat/module/file_integrity/eventreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -313,7 +314,7 @@ func TestRaces(t *testing.T) {
func readTimeout(t testing.TB, events <-chan Event) Event {
select {
case <-time.After(time.Second):
t.Fatalf("%+v", errors.Errorf("timed-out waiting for event"))
t.Fatalf("timed-out waiting for event:\n%s", debug.Stack())
case e, ok := <-events:
if !ok {
t.Fatal("failed reading from event channel")
Expand Down
16 changes: 12 additions & 4 deletions auditbeat/module/file_integrity/fileorigin_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,27 @@ func GetFileOrigin(path string) ([]string, error) {
defer C.free(unsafe.Pointer(cPath))

// Query length kMDItemWhereFroms extended-attribute
attrSize, err := C.getxattr(cPath, kMDItemWhereFroms, nil, 0, 0, 0)
attrSize, errno := C.getxattr(cPath, kMDItemWhereFroms, nil, 0, 0, 0)
if attrSize == -1 {
return nil, errors.Wrap(filterErrno(err), "getxattr: query attribute length failed")
err := filterErrno(errno)
if err != nil {
return nil, fmt.Errorf("getxattr: query attribute length failed: %w", err)
}
return nil, nil
}
if attrSize == 0 {
return nil, nil
}

// Read the kMDItemWhereFroms attribute
data := make([]byte, attrSize)
newSize, err := C.getxattr(cPath, kMDItemWhereFroms, unsafe.Pointer(&data[0]), C.size_t(attrSize), 0, 0)
newSize, errno := C.getxattr(cPath, kMDItemWhereFroms, unsafe.Pointer(&data[0]), C.size_t(attrSize), 0, 0)
if newSize == -1 {
return nil, errors.Wrap(filterErrno(err), "getxattr failed")
err := filterErrno(errno)
if err != nil {
return nil, fmt.Errorf("getxattr failed: %w", filterErrno(err))
}
return nil, nil
}
if newSize != attrSize {
return nil, errors.New("getxattr: attribute changed while reading")
Expand Down
5 changes: 4 additions & 1 deletion x-pack/auditbeat/module/system/socket/guess/guess.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ func guessOnce(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (r
}

case <-perfchan.LostC():
return nil, errors.Wrap(err, "event loss in perf channel")
if err != nil {
return nil, fmt.Errorf("event loss in perf channel: %w", err)
}
return nil, errors.New("event loss in perf channel")
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion x-pack/auditbeat/module/system/socket/helper/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ func (lo *IPv6Loopback) AddRandomAddress() (addr net.IP, err error) {
}
time.Sleep(time.Millisecond * time.Duration(i))
}
return addr, errors.Wrap(err, "bind failed")
if err != nil {
err = fmt.Errorf("bind failed: %w", err)
}
return addr, err
}

// Cleanup removes the addresses registered to this loopback.
Expand Down

0 comments on commit 5a53f65

Please sign in to comment.