Skip to content

Commit

Permalink
auditbeat: replace deprecated io/ioutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
mmat11 committed Nov 3, 2023
1 parent 48e0660 commit 8c369de
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 72 deletions.
9 changes: 4 additions & 5 deletions auditbeat/helper/hasher/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package hasher

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -28,14 +27,14 @@ import (
)

func TestHasher(t *testing.T) {
dir, err := ioutil.TempDir("", "auditbeat-hasher-test")
dir, err := os.MkdirTemp("", "auditbeat-hasher-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

file := filepath.Join(dir, "exe")
if err = ioutil.WriteFile(file, []byte("test exe\n"), 0o600); err != nil {
if err = os.WriteFile(file, []byte("test exe\n"), 0o600); err != nil {
t.Fatal(err)
}

Expand All @@ -62,14 +61,14 @@ func TestHasher(t *testing.T) {
}

func TestHasherLimits(t *testing.T) {
dir, err := ioutil.TempDir("", "auditbeat-hasher-test")
dir, err := os.MkdirTemp("", "auditbeat-hasher-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

file := filepath.Join(dir, "exe")
if err = ioutil.WriteFile(file, []byte("test exe\n"), 0o600); err != nil {
if err = os.WriteFile(file, []byte("test exe\n"), 0o600); err != nil {
t.Fatal(err)
}

Expand Down
3 changes: 1 addition & 2 deletions auditbeat/module/auditd/audit_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
Expand Down Expand Up @@ -366,7 +365,7 @@ func buildSampleEvent(t testing.TB, lines []string, filename string) {
t.Fatal(err)
}

if err := ioutil.WriteFile(filename, output, 0o644); err != nil {
if err := os.WriteFile(filename, output, 0o644); err != nil {
t.Fatal(err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions auditbeat/module/auditd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package auditd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -166,7 +166,7 @@ audit_rules: |
makeRuleFlags(0, 2),
}, "\n")

dir1, err := ioutil.TempDir("", "rules1")
dir1, err := os.MkdirTemp("", "rules1")
if err != nil {
t.Fatal(err)
}
Expand All @@ -189,12 +189,12 @@ audit_rules: |
makeRuleFlags(1+file.order, 2),
makeRuleFlags(1+file.order, 3),
}, "\n"))
if err = ioutil.WriteFile(path, content, fileMode); err != nil {
if err = os.WriteFile(path, content, fileMode); err != nil {
t.Fatal(err)
}
}

dir2, err := ioutil.TempDir("", "rules0")
dir2, err := os.MkdirTemp("", "rules0")
if err != nil {
t.Fatal(err)
}
Expand All @@ -215,7 +215,7 @@ audit_rules: |
makeRuleFlags(10+file.order, 2),
makeRuleFlags(10+file.order, 3),
}, "\n"))
if err = ioutil.WriteFile(path, content, fileMode); err != nil {
if err = os.WriteFile(path, content, fileMode); err != nil {
t.Fatal(err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions auditbeat/module/auditd/golden_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"context"
"encoding/json"
"flag"
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -87,7 +86,7 @@ func readLines(path string) (lines []string, err error) {
}

func readGoldenFile(t testing.TB, path string) (events []mapstr.M) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("can't read golden file '%s': %v", path, err)
}
Expand Down Expand Up @@ -216,7 +215,7 @@ func TestGoldenFiles(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err = ioutil.WriteFile(goldenPath, data, 0o644); err != nil {
if err = os.WriteFile(goldenPath, data, 0o644); err != nil {
t.Fatalf("failed writing golden file '%s': %v", goldenPath, err)
}
}
Expand Down
7 changes: 3 additions & 4 deletions auditbeat/module/file_integrity/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"math"
"os"
"os/user"
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestDiffEvents(t *testing.T) {
}

func TestHashFile(t *testing.T) {
f, err := ioutil.TempFile("", "input.txt")
f, err := os.CreateTemp("", "input.txt")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -288,7 +287,7 @@ func TestHashFile(t *testing.T) {
}

func TestNewEventFromFileInfoHash(t *testing.T) {
f, err := ioutil.TempFile("", "input.txt")
f, err := os.CreateTemp("", "input.txt")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -361,7 +360,7 @@ func TestNewEventFromFileInfoHash(t *testing.T) {
}

func BenchmarkHashFile(b *testing.B) {
f, err := ioutil.TempFile("", "hash")
f, err := os.CreateTemp("", "hash")
if err != nil {
b.Fatal(err)
}
Expand Down
15 changes: 7 additions & 8 deletions auditbeat/module/file_integrity/eventreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package file_integrity

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -43,7 +42,7 @@ const ErrorSharingViolation syscall.Errno = 32
func TestEventReader(t *testing.T) {
t.Skip("Flaky test: about 1/10 of builds fails https://github.com/elastic/beats/issues/21302")
// Make dir to monitor.
dir, err := ioutil.TempDir("", "audit")
dir, err := os.MkdirTemp("", "audit")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestEventReader(t *testing.T) {
txt1 := filepath.Join(dir, "test1.txt")
var fileMode os.FileMode = 0o640
mustRun(t, "created", func(t *testing.T) {
if err = ioutil.WriteFile(txt1, []byte("hello"), fileMode); err != nil {
if err = os.WriteFile(txt1, []byte("hello"), fileMode); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -194,7 +193,7 @@ func TestEventReader(t *testing.T) {
var moveInOrig string
moveIn := filepath.Join(dir, "test3.txt")
mustRun(t, "move in", func(t *testing.T) {
f, err := ioutil.TempFile("", "test3.txt")
f, err := os.CreateTemp("", "test3.txt")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -232,7 +231,7 @@ func TestEventReader(t *testing.T) {
// Test that it does not monitor recursively.
subFile := filepath.Join(subDir, "foo.txt")
mustRun(t, "non-recursive", func(t *testing.T) {
if err = ioutil.WriteFile(subFile, []byte("foo"), fileMode); err != nil {
if err = os.WriteFile(subFile, []byte("foo"), fileMode); err != nil {
t.Fatal(err)
}

Expand All @@ -252,7 +251,7 @@ func TestRaces(t *testing.T) {

dirs := make([]string, N)
for i := range dirs {
dir, err := ioutil.TempDir("", "audit")
dir, err := os.MkdirTemp("", "audit")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -286,7 +285,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)
os.WriteFile(fname, []byte("hello"), fileMode)

Check failure on line 288 in auditbeat/module/file_integrity/eventreader_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `os.WriteFile` is not checked (errcheck)

Check failure on line 288 in auditbeat/module/file_integrity/eventreader_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value of `os.WriteFile` is not checked (errcheck)
}
}
}()
Expand All @@ -298,7 +297,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)
os.WriteFile(fname, []byte("hello"), fileMode)

Check failure on line 300 in auditbeat/module/file_integrity/eventreader_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `os.WriteFile` is not checked (errcheck)

Check failure on line 300 in auditbeat/module/file_integrity/eventreader_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value of `os.WriteFile` is not checked (errcheck)
}

got := 0
Expand Down
5 changes: 2 additions & 3 deletions auditbeat/module/file_integrity/fileinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package file_integrity

import (
"fmt"
"io/ioutil"
"os"
"os/user"
"runtime"
Expand All @@ -30,7 +29,7 @@ import (
)

func TestNewMetadata(t *testing.T) {
f, err := ioutil.TempFile("", "metadata")
f, err := os.CreateTemp("", "metadata")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestNewMetadata(t *testing.T) {
}

func TestSetUIDSetGIDBits(t *testing.T) {
f, err := ioutil.TempFile("", "setuid")
f, err := os.CreateTemp("", "setuid")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions auditbeat/module/file_integrity/fileinfo_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package file_integrity

import (
"io/ioutil"
"os"
"testing"

Expand All @@ -32,7 +31,7 @@ import (
// a method that doesn't need to open the file for reading.
// (GetNamedSecurityInfo vs CreateFile+GetSecurityInfo)
func TestFileInfoPermissions(t *testing.T) {
f, err := ioutil.TempFile("", "metadata")
f, err := os.CreateTemp("", "metadata")
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 13 additions & 14 deletions auditbeat/module/file_integrity/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package file_integrity

import (
"crypto/sha1"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -40,7 +39,7 @@ import (
func TestData(t *testing.T) {
defer abtest.SetupDataDir(t)()

dir, err := ioutil.TempDir("", "audit-file")
dir, err := os.MkdirTemp("", "audit-file")
if err != nil {
t.Fatal(err)
}
Expand All @@ -49,7 +48,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)
os.WriteFile(file, []byte("hello world"), 0o600)

Check failure on line 51 in auditbeat/module/file_integrity/metricset_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `os.WriteFile` is not checked (errcheck)
}()

ms := mbtest.NewPushMetricSetV2(t, getConfig(dir))
Expand All @@ -76,7 +75,7 @@ func TestActions(t *testing.T) {
defer bucket.Close()

// First directory
dir, err := ioutil.TempDir("", "audit-file")
dir, err := os.MkdirTemp("", "audit-file")
if err != nil {
t.Fatal(err)
}
Expand All @@ -88,7 +87,7 @@ func TestActions(t *testing.T) {
}

// Second directory (to be reported with "initial_scan")
newDir, err := ioutil.TempDir("", "audit-file-new")
newDir, err := os.MkdirTemp("", "audit-file-new")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -136,8 +135,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)
os.WriteFile(createdFilepath, []byte("hello world"), 0o600)
os.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 @@ -185,7 +184,7 @@ func TestExcludedFiles(t *testing.T) {
}
defer bucket.Close()

dir, err := ioutil.TempDir("", "audit-file")
dir, err := os.MkdirTemp("", "audit-file")
if err != nil {
t.Fatal(err)
}
Expand All @@ -201,7 +200,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)
os.WriteFile(file, []byte("hello world"), 0o600)
}
}()

Expand Down Expand Up @@ -241,7 +240,7 @@ func TestIncludedExcludedFiles(t *testing.T) {
}
defer bucket.Close()

dir, err := ioutil.TempDir("", "audit-file")
dir, err := os.MkdirTemp("", "audit-file")
if err != nil {
t.Fatal(err)
}
Expand All @@ -264,7 +263,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)
err := os.WriteFile(file, []byte("hello world"), 0o600)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -311,7 +310,7 @@ func TestErrorReporting(t *testing.T) {
}
defer abtest.SetupDataDir(t)()

dir, err := ioutil.TempDir("", "audit-file")
dir, err := os.MkdirTemp("", "audit-file")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -487,7 +486,7 @@ func (e expectedEvent) validate(t *testing.T, ms *MetricSet) {
type expectedEvents []expectedEvent

func (e expectedEvents) validate(t *testing.T) {
store, err := ioutil.TempFile("", "bucket")
store, err := os.CreateTemp("", "bucket")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -759,7 +758,7 @@ func TestEventFailedHash(t *testing.T) {
}

func TestEventDelete(t *testing.T) {
store, err := ioutil.TempFile("", "bucket")
store, err := os.CreateTemp("", "bucket")
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 8c369de

Please sign in to comment.