Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace multierror with stdlib errors wrapping #41043

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
31 changes: 0 additions & 31 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20134,37 +20134,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : github.com/joeshaw/multierror
Version: v0.0.0-20140124173710-69b34d4ec901
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/joeshaw/multierror@v0.0.0-20140124173710-69b34d4ec901/LICENSE:

The MIT License (MIT)

Copyright (c) 2014 Joe Shaw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/jonboulle/clockwork
Version: v0.2.2
Expand Down
6 changes: 3 additions & 3 deletions auditbeat/helper/hasher/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
Expand All @@ -32,7 +33,6 @@ import (

"github.com/cespare/xxhash/v2"
"github.com/dustin/go-humanize"
"github.com/joeshaw/multierror"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/sha3"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -138,7 +138,7 @@ type Config struct {

// Validate validates the config.
func (c *Config) Validate() error {
var errs multierror.Errors
var errs []error

for _, ht := range c.HashTypes {
if !ht.IsValid() {
Expand All @@ -160,7 +160,7 @@ func (c *Config) Validate() error {
errs = append(errs, fmt.Errorf("invalid scan_rate_per_sec value: %w", err))
}

return errs.Err()
return errors.Join(errs...)
}

// FileHasher hashes the contents of files.
Expand Down
13 changes: 6 additions & 7 deletions auditbeat/module/auditd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package auditd
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
Expand All @@ -30,8 +31,6 @@ import (
"strings"
"time"

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/go-libaudit/v2/rule"
"github.com/elastic/go-libaudit/v2/rule/flags"
Expand Down Expand Up @@ -93,7 +92,7 @@ var defaultConfig = Config{

// Validate validates the rules specified in the config.
func (c *Config) Validate() error {
var errs multierror.Errors
var errs []error
err := c.loadRules()
if err != nil {
errs = append(errs, err)
Expand All @@ -115,7 +114,7 @@ func (c *Config) Validate() error {
"'%v' (use unicast, multicast, or don't set a value)", c.SocketType))
}

return errs.Err()
return errors.Join(errs...)
}

// Rules returns a list of rules specified in the config.
Expand Down Expand Up @@ -191,7 +190,7 @@ func (c Config) failureMode() (uint32, error) {
// errors will be logged as warnings and any successfully parsed rules will be
// returned.
func readRules(reader io.Reader, source string, knownRules ruleSet, log *logp.Logger) (rules []auditRule, err error) {
var errs multierror.Errors
var errs []error

s := bufio.NewScanner(reader)
for lineNum := 1; s.Scan(); lineNum++ {
Expand Down Expand Up @@ -229,9 +228,9 @@ func readRules(reader io.Reader, source string, knownRules ruleSet, log *logp.Lo

if len(errs) != 0 {
if log == nil {
return nil, fmt.Errorf("failed loading rules: %w", errs.Err())
return nil, fmt.Errorf("failed loading rules: %w", errors.Join(errs...))
}
log.Warnf("errors loading rules: %v", errs.Err())
log.Warnf("errors loading rules: %v", errors.Join(errs...))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Warnf("errors loading rules: %v", errors.Join(errs...))
log.Warnf("errors loading rules: %w", errors.Join(errs...))

}
return rules, nil
}
5 changes: 2 additions & 3 deletions auditbeat/module/file_integrity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"strings"

"github.com/dustin/go-humanize"
"github.com/joeshaw/multierror"

"github.com/elastic/beats/v7/libbeat/common/match"
)
Expand Down Expand Up @@ -131,7 +130,7 @@ func (c *Config) Validate() error {
sort.Strings(c.Paths)
c.Paths = deduplicate(c.Paths)

var errs multierror.Errors
var errs []error
var err error

nextHash:
Expand Down Expand Up @@ -187,7 +186,7 @@ nextHash:
errs = append(errs, errors.New("backend can only be specified on linux"))
}

return errs.Err()
return errors.Join(errs...)
}

// deduplicate deduplicates the given sorted string slice. The returned slice
Expand Down
7 changes: 4 additions & 3 deletions auditbeat/module/file_integrity/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"regexp/syntax"
"testing"

"github.com/joeshaw/multierror"
"github.com/stretchr/testify/assert"

conf "github.com/elastic/elastic-agent-libs/config"
Expand Down Expand Up @@ -85,11 +84,13 @@ func TestConfigInvalid(t *testing.T) {
t.Fatal("expected ucfg.Error")
}

merr, ok := ucfgErr.Reason().(*multierror.MultiError)
merr, ok := ucfgErr.Reason().(interface {
Unwrap() []error
})
if !ok {
t.Fatal("expected MultiError")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Fatal("expected MultiError")
t.Fatal("expected slice error unwrapper")

}
assert.Len(t, merr.Errors, 4)
assert.Len(t, merr.Unwrap(), 4)

config, err = conf.NewConfigFrom(map[string]interface{}{
"paths": []string{"/usr/bin"},
Expand Down
6 changes: 3 additions & 3 deletions auditbeat/module/file_integrity/fileinfo_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package file_integrity

import (
"bytes"
"errors"
"fmt"
"os"
"os/user"
"strconv"
"syscall"

"github.com/joeshaw/multierror"
"github.com/pkg/xattr"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func NewMetadata(path string, info os.FileInfo) (*Metadata, error) {
}

// Lookup UID and GID
var errs multierror.Errors
var errs []error
owner, err := user.LookupId(strconv.Itoa(int(fileInfo.UID)))
if err != nil {
errs = append(errs, err)
Expand All @@ -81,7 +81,7 @@ func NewMetadata(path string, info os.FileInfo) (*Metadata, error) {
errs = append(errs, err)
}

return fileInfo, errs.Err()
return fileInfo, errors.Join(errs...)
}

func fillExtendedAttributes(md *Metadata, path string) {
Expand Down
14 changes: 7 additions & 7 deletions auditbeat/module/file_integrity/fileinfo_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
package file_integrity

import (
"errors"
"fmt"
"os"
"syscall"
"time"
"unsafe"

"github.com/joeshaw/multierror"

"github.com/elastic/beats/v7/libbeat/common/file"
)

Expand All @@ -40,12 +39,12 @@ func NewMetadata(path string, info os.FileInfo) (*Metadata, error) {
return nil, fmt.Errorf("unexpected fileinfo sys type %T for %v", info.Sys(), path)
}

var errs multierror.Errors
var errs []error

state := file.GetOSState(info)

fileInfo := &Metadata{
Inode: uint64(state.IdxHi<<32 + state.IdxLo),
Inode: state.IdxHi<<32 + state.IdxLo,
Mode: info.Mode(),
Size: uint64(info.Size()),
MTime: time.Unix(0, attrs.LastWriteTime.Nanoseconds()).UTC(),
Expand Down Expand Up @@ -73,7 +72,7 @@ func NewMetadata(path string, info os.FileInfo) (*Metadata, error) {
if fileInfo.Origin, err = GetFileOrigin(path); err != nil {
errs = append(errs, fmt.Errorf("GetFileOrigin failed: %w", err))
}
return fileInfo, errs.Err()
return fileInfo, errors.Join(errs...)
}

// fileOwner returns the SID and name (domain\user) of the file's owner.
Expand All @@ -89,10 +88,11 @@ func fileOwner(path string) (sid, owner string, err error) {
OwnerSecurityInformation, &securityID, nil, nil, nil, &securityDescriptor); err != nil {
return "", "", fmt.Errorf("failed on GetSecurityInfo for %v: %w", path, err)
}
//nolint:errcheck // ignore
defer syscall.LocalFree((syscall.Handle)(unsafe.Pointer(securityDescriptor)))

// Convert SID to a string and lookup the username.
var errs multierror.Errors
var errs []error
sid, err = securityID.String()
if err != nil {
errs = append(errs, err)
Expand All @@ -105,5 +105,5 @@ func fileOwner(path string) (sid, owner string, err error) {
owner = fmt.Sprintf(`%s\%s`, domain, account)
}

return sid, owner, errs.Err()
return sid, owner, errors.Join(errs...)
}
6 changes: 3 additions & 3 deletions auditbeat/module/file_integrity/monitor/recursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package monitor

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/fsnotify/fsnotify"
"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/logp"
)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (watcher *recursiveWatcher) addRecursive(path string) error {
return nil
}

var errs multierror.Errors
var errs []error
if err := watcher.watchFile(path, nil); err != nil {
errs = append(errs, fmt.Errorf("failed adding watcher to '%s': %w", path, err))
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func (watcher *recursiveWatcher) addRecursive(path string) error {
if err != nil {
errs = append(errs, fmt.Errorf("failed to walk path '%s': %w", path, err))
}
return errs.Err()
return errors.Join(errs...)
}

func (watcher *recursiveWatcher) close() error {
Expand Down
5 changes: 2 additions & 3 deletions auditbeat/tracing/perfevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"
"unsafe"

"github.com/joeshaw/multierror"
"golang.org/x/sys/unix"

"github.com/elastic/go-perf"
Expand Down Expand Up @@ -336,7 +335,7 @@ func (c *PerfChannel) Close() error {
defer close(c.errC)
defer close(c.lostC)
}
var errs multierror.Errors
var errs []error
for _, ev := range c.events {
if err := ev.Disable(); err != nil {
errs = append(errs, fmt.Errorf("failed to disable event channel: %w", err))
Expand All @@ -345,7 +344,7 @@ func (c *PerfChannel) Close() error {
errs = append(errs, fmt.Errorf("failed to close event channel: %w", err))
}
}
return errs.Err()
return errors.Join(errs...)
}

// doneWrapperContext is a custom context.Context that is tailored to
Expand Down
7 changes: 3 additions & 4 deletions auditbeat/tracing/tracefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ package tracing

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/joeshaw/multierror"
)

const (
Expand All @@ -53,7 +52,7 @@ type TraceFS struct {
// It autodetects a tracefs mounted on /sys/kernel/tracing or via
// debugfs at /sys/kernel/debug/tracing.
func NewTraceFS() (*TraceFS, error) {
var errs multierror.Errors
var errs []error
ptr, err := NewTraceFSWithPath(traceFSPath)
if err != nil {
errs = append(errs, err)
Expand All @@ -64,7 +63,7 @@ func NewTraceFS() (*TraceFS, error) {
errs = nil
}
}
return ptr, errs.Err()
return ptr, errors.Join(errs...)
}

// NewTraceFSWithPath creates a new accessor for the event tracing feature
Expand Down
Loading
Loading