Skip to content

Commit be14d14

Browse files
kruskallefd6
andauthored
feat: replace multierror with stdlib errors wrapping (#41043)
* feat: replace multierror with stdlib errors wrapping replace multierror library with stdlib error wrapping * lint: fix linting issues * lint: regenerate notice file * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * lint: fix linting issues * Update auditbeat/module/auditd/config.go Co-authored-by: Dan Kortschak <[email protected]> * Update auditbeat/module/file_integrity/config_test.go Co-authored-by: Dan Kortschak <[email protected]> * lint: merge lines * lint: fix linter issues * lint: fix linter issues * lint: fix linter issues * lint: rename errs variable * fix: update wrapping format * lint: go mod tidy * feat: remove new usage * Update wineventlog.go * Update state.go --------- Co-authored-by: Dan Kortschak <[email protected]>
1 parent 3604ffa commit be14d14

File tree

60 files changed

+264
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+264
-307
lines changed

NOTICE.txt

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19766,37 +19766,6 @@ Contents of probable licence file $GOMODCACHE/github.com/jcmturner/gokrb5/v8@v8.
1976619766
limitations under the License.
1976719767

1976819768

19769-
--------------------------------------------------------------------------------
19770-
Dependency : github.com/joeshaw/multierror
19771-
Version: v0.0.0-20140124173710-69b34d4ec901
19772-
Licence type (autodetected): MIT
19773-
--------------------------------------------------------------------------------
19774-
19775-
Contents of probable licence file $GOMODCACHE/github.com/joeshaw/[email protected]/LICENSE:
19776-
19777-
The MIT License (MIT)
19778-
19779-
Copyright (c) 2014 Joe Shaw
19780-
19781-
Permission is hereby granted, free of charge, to any person obtaining a copy
19782-
of this software and associated documentation files (the "Software"), to deal
19783-
in the Software without restriction, including without limitation the rights
19784-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19785-
copies of the Software, and to permit persons to whom the Software is
19786-
furnished to do so, subject to the following conditions:
19787-
19788-
The above copyright notice and this permission notice shall be included in
19789-
all copies or substantial portions of the Software.
19790-
19791-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19792-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19793-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19794-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19795-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19796-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19797-
THE SOFTWARE.
19798-
19799-
1980019769
--------------------------------------------------------------------------------
1980119770
Dependency : github.com/jonboulle/clockwork
1980219771
Version: v0.2.2

auditbeat/helper/hasher/hasher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ package hasher
1919

2020
import (
2121
"encoding/hex"
22+
"errors"
2223
"fmt"
2324
"hash"
2425
"io"
2526
"strings"
2627
"time"
2728

2829
"github.com/dustin/go-humanize"
29-
"github.com/joeshaw/multierror"
3030
"golang.org/x/time/rate"
3131

3232
"github.com/elastic/beats/v7/libbeat/common/file"
@@ -100,7 +100,7 @@ type Config struct {
100100

101101
// Validate validates the config.
102102
func (c *Config) Validate() error {
103-
var errs multierror.Errors
103+
var errs []error
104104

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

125-
return errs.Err()
125+
return errors.Join(errs...)
126126
}
127127

128128
// FileHasher hashes the contents of files.

auditbeat/module/auditd/config.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package auditd
2222
import (
2323
"bufio"
2424
"bytes"
25+
"errors"
2526
"fmt"
2627
"io"
2728
"os"
@@ -30,8 +31,6 @@ import (
3031
"strings"
3132
"time"
3233

33-
"github.com/joeshaw/multierror"
34-
3534
"github.com/elastic/elastic-agent-libs/logp"
3635
"github.com/elastic/go-libaudit/v2/rule"
3736
"github.com/elastic/go-libaudit/v2/rule/flags"
@@ -93,7 +92,7 @@ var defaultConfig = Config{
9392

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

118-
return errs.Err()
117+
return errors.Join(errs...)
119118
}
120119

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

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

230229
if len(errs) != 0 {
231230
if log == nil {
232-
return nil, fmt.Errorf("failed loading rules: %w", errs.Err())
231+
return nil, fmt.Errorf("failed loading rules: %w", errors.Join(errs...))
233232
}
234-
log.Warnf("errors loading rules: %v", errs.Err())
233+
log.Warnf("errors loading rules: %v", errors.Join(errs...))
235234
}
236235
return rules, nil
237236
}

auditbeat/module/file_integrity/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"strings"
2929

3030
"github.com/dustin/go-humanize"
31-
"github.com/joeshaw/multierror"
3231

3332
"github.com/elastic/beats/v7/libbeat/common/match"
3433
)
@@ -122,7 +121,7 @@ func (c *Config) Validate() error {
122121
sort.Strings(c.Paths)
123122
c.Paths = deduplicate(c.Paths)
124123

125-
var errs multierror.Errors
124+
var errs []error
126125
var err error
127126

128127
nextHash:
@@ -178,7 +177,7 @@ nextHash:
178177
errs = append(errs, errors.New("backend can only be specified on linux"))
179178
}
180179

181-
return errs.Err()
180+
return errors.Join(errs...)
182181
}
183182

184183
// deduplicate deduplicates the given sorted string slice. The returned slice

auditbeat/module/file_integrity/config_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"regexp/syntax"
2525
"testing"
2626

27-
"github.com/joeshaw/multierror"
2827
"github.com/stretchr/testify/assert"
2928

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

88-
merr, ok := ucfgErr.Reason().(*multierror.MultiError)
87+
merr, ok := ucfgErr.Reason().(interface {
88+
Unwrap() []error
89+
})
8990
if !ok {
90-
t.Fatal("expected MultiError")
91+
t.Fatal("expected slice error unwrapper")
9192
}
92-
assert.Len(t, merr.Errors, 4)
93+
assert.Len(t, merr.Unwrap(), 4)
9394

9495
config, err = conf.NewConfigFrom(map[string]interface{}{
9596
"paths": []string{"/usr/bin"},

auditbeat/module/file_integrity/fileinfo_posix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ package file_integrity
2121

2222
import (
2323
"bytes"
24+
"errors"
2425
"fmt"
2526
"os"
2627
"os/user"
2728
"strconv"
2829
"syscall"
2930

30-
"github.com/joeshaw/multierror"
3131
"github.com/pkg/xattr"
3232
)
3333

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

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

84-
return fileInfo, errs.Err()
84+
return fileInfo, errors.Join(errs...)
8585
}
8686

8787
func fillExtendedAttributes(md *Metadata, path string) {

auditbeat/module/file_integrity/fileinfo_windows.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
package file_integrity
2121

2222
import (
23+
"errors"
2324
"fmt"
2425
"os"
2526
"syscall"
2627
"time"
2728
"unsafe"
2829

29-
"github.com/joeshaw/multierror"
30-
3130
"github.com/elastic/beats/v7/libbeat/common/file"
3231
)
3332

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

43-
var errs multierror.Errors
42+
var errs []error
4443

4544
state := file.GetOSState(info)
4645

4746
fileInfo := &Metadata{
48-
Inode: uint64(state.IdxHi<<32 + state.IdxLo),
47+
Inode: state.IdxHi<<32 + state.IdxLo,
4948
Mode: info.Mode(),
5049
Size: uint64(info.Size()),
5150
MTime: time.Unix(0, attrs.LastWriteTime.Nanoseconds()).UTC(),
@@ -73,7 +72,7 @@ func NewMetadata(path string, info os.FileInfo) (*Metadata, error) {
7372
if fileInfo.Origin, err = GetFileOrigin(path); err != nil {
7473
errs = append(errs, fmt.Errorf("GetFileOrigin failed: %w", err))
7574
}
76-
return fileInfo, errs.Err()
75+
return fileInfo, errors.Join(errs...)
7776
}
7877

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

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

108-
return sid, owner, errs.Err()
108+
return sid, owner, errors.Join(errs...)
109109
}

auditbeat/module/file_integrity/monitor/recursive.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
package monitor
1919

2020
import (
21+
"errors"
2122
"fmt"
2223
"os"
2324
"path/filepath"
2425

2526
"github.com/fsnotify/fsnotify"
26-
"github.com/joeshaw/multierror"
2727

2828
"github.com/elastic/elastic-agent-libs/logp"
2929
)
@@ -113,7 +113,7 @@ func (watcher *recursiveWatcher) addRecursive(path string) error {
113113
return nil
114114
}
115115

116-
var errs multierror.Errors
116+
var errs []error
117117
if err := watcher.watchFile(path, nil); err != nil {
118118
errs = append(errs, fmt.Errorf("failed adding watcher to '%s': %w", path, err))
119119
}
@@ -147,7 +147,7 @@ func (watcher *recursiveWatcher) addRecursive(path string) error {
147147
if err != nil {
148148
errs = append(errs, fmt.Errorf("failed to walk path '%s': %w", path, err))
149149
}
150-
return errs.Err()
150+
return errors.Join(errs...)
151151
}
152152

153153
func (watcher *recursiveWatcher) close() error {

auditbeat/tracing/perfevent.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"time"
3030
"unsafe"
3131

32-
"github.com/joeshaw/multierror"
3332
"golang.org/x/sys/unix"
3433

3534
"github.com/elastic/go-perf"
@@ -336,7 +335,7 @@ func (c *PerfChannel) Close() error {
336335
defer close(c.errC)
337336
defer close(c.lostC)
338337
}
339-
var errs multierror.Errors
338+
var errs []error
340339
for _, ev := range c.events {
341340
if err := ev.Disable(); err != nil {
342341
errs = append(errs, fmt.Errorf("failed to disable event channel: %w", err))
@@ -345,7 +344,7 @@ func (c *PerfChannel) Close() error {
345344
errs = append(errs, fmt.Errorf("failed to close event channel: %w", err))
346345
}
347346
}
348-
return errs.Err()
347+
return errors.Join(errs...)
349348
}
350349

351350
// doneWrapperContext is a custom context.Context that is tailored to

auditbeat/tracing/tracefs.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ package tracing
2121

2222
import (
2323
"bufio"
24+
"errors"
2425
"fmt"
2526
"os"
2627
"path/filepath"
2728
"regexp"
2829
"strconv"
2930
"strings"
30-
31-
"github.com/joeshaw/multierror"
3231
)
3332

3433
const (
@@ -53,7 +52,7 @@ type TraceFS struct {
5352
// It autodetects a tracefs mounted on /sys/kernel/tracing or via
5453
// debugfs at /sys/kernel/debug/tracing.
5554
func NewTraceFS() (*TraceFS, error) {
56-
var errs multierror.Errors
55+
var errs []error
5756
ptr, err := NewTraceFSWithPath(traceFSPath)
5857
if err != nil {
5958
errs = append(errs, err)
@@ -64,7 +63,7 @@ func NewTraceFS() (*TraceFS, error) {
6463
errs = nil
6564
}
6665
}
67-
return ptr, errs.Err()
66+
return ptr, errors.Join(errs...)
6867
}
6968

7069
// NewTraceFSWithPath creates a new accessor for the event tracing feature

0 commit comments

Comments
 (0)