Skip to content

Commit

Permalink
[auditbeat] replace deprecated io/ioutil package (#37024)
Browse files Browse the repository at this point in the history
* auditbeat: replace deprecated io/ioutil package

* auditbeat: use t.TempDir instead of os.MkdirTemp
  • Loading branch information
mmat11 authored Nov 8, 2023
1 parent 28152d5 commit df0d35c
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 228 deletions.
17 changes: 4 additions & 13 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,10 @@ import (
)

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

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 +57,10 @@ func TestHasher(t *testing.T) {
}

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

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
18 changes: 6 additions & 12 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,10 +166,7 @@ audit_rules: |
makeRuleFlags(0, 2),
}, "\n")

dir1, err := ioutil.TempDir("", "rules1")
if err != nil {
t.Fatal(err)
}
dir1 := t.TempDir()

for _, file := range []struct {
order int
Expand All @@ -189,15 +186,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")
if err != nil {
t.Fatal(err)
}
dir2 := t.TempDir()

for _, file := range []struct {
order int
Expand All @@ -215,7 +209,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 All @@ -225,7 +219,7 @@ audit_rules: |
fmt.Sprintf("%s/*.conf", dir2),
}

if err = config.Validate(); err != nil {
if err := config.Validate(); 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(t.TempDir(), "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(t.TempDir(), "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(b.TempDir(), "hash")
if err != nil {
b.Fatal(err)
}
Expand Down
39 changes: 7 additions & 32 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,18 +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")
if err != nil {
t.Fatal(err)
}
// under macOS, temp dir has a symlink in the path (/var -> /private/var)
// and the path returned in events has the symlink resolved
if runtime.GOOS == "darwin" {
if dirAlt, err := filepath.EvalSymlinks(dir); err == nil {
dir = dirAlt
}
}
defer os.RemoveAll(dir)
dir := t.TempDir()

// Create a new EventProducer.
config := defaultConfig
Expand All @@ -75,7 +63,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 +182,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 +220,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,22 +240,9 @@ func TestRaces(t *testing.T) {

dirs := make([]string, N)
for i := range dirs {
dir, err := ioutil.TempDir("", "audit")
if err != nil {
t.Fatal(err)
}
if dir, err = filepath.EvalSymlinks(dir); err != nil {
t.Fatal(err)
}
dirs[i] = dir
dirs[i] = t.TempDir()
}

defer func() {
for _, dir := range dirs {
os.RemoveAll(dir)
}
}()

// Create a new EventProducer.
config := defaultConfig
config.Paths = dirs
Expand All @@ -286,7 +261,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)
}
}
}()
Expand All @@ -298,7 +273,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)
}

got := 0
Expand Down
7 changes: 2 additions & 5 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,11 +29,10 @@ import (
)

func TestNewMetadata(t *testing.T) {
f, err := ioutil.TempFile("", "metadata")
f, err := os.CreateTemp(t.TempDir(), "metadata")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())

_, err = f.WriteString("metadata test")
if err != nil {
Expand Down Expand Up @@ -96,11 +94,10 @@ func TestNewMetadata(t *testing.T) {
}

func TestSetUIDSetGIDBits(t *testing.T) {
f, err := ioutil.TempFile("", "setuid")
f, err := os.CreateTemp(t.TempDir(), "setuid")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())

_, err = f.WriteString("metadata test")
if err != nil {
Expand Down
10 changes: 4 additions & 6 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,15 +31,14 @@ 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(t.TempDir(), "metadata")
if err != nil {
t.Fatal(err)
}
defer f.Close()

name := f.Name()
defer func() {
f.Close()
os.Remove(f.Name())
}()

makeFileNonReadable(t, f.Name())
info, err := os.Stat(name)
if err != nil {
Expand Down
Loading

0 comments on commit df0d35c

Please sign in to comment.