1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-21 15:48:06 +00:00

chore: remove refs to deprecated io/ioutil (#1504)

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
Co-authored-by: chenk <hen.keinan@gmail.com>
This commit is contained in:
guangwu 2023-12-05 16:52:24 +08:00 committed by GitHub
parent 292678a907
commit c3e3c4c31c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 35 deletions

View File

@ -19,7 +19,6 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -53,7 +52,7 @@ func TestYamlFiles(t *testing.T) {
}
if !info.IsDir() {
t.Logf("reading file: %s", path)
in, err := ioutil.ReadFile(path)
in, err := os.ReadFile(path)
if err != nil {
t.Fatalf("error opening file %s: %v", path, err)
}

View File

@ -16,7 +16,6 @@ package check
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
@ -29,7 +28,7 @@ var (
func init() {
var err error
in, err = ioutil.ReadFile("data")
in, err = os.ReadFile("data")
if err != nil {
panic("Failed reading test data: " + err.Error())
}

View File

@ -18,7 +18,6 @@ import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -71,7 +70,7 @@ func runChecks(nodetype check.NodeType, testYamlFile, detectedVersion string) {
os.Exit(1)
}
in, err := ioutil.ReadFile(testYamlFile)
in, err := os.ReadFile(testYamlFile)
if err != nil {
exitWithError(fmt.Errorf("error opening %s test file: %v", testYamlFile, err))
}

View File

@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
@ -681,7 +681,7 @@ func TestPrintSummary(t *testing.T) {
os.Stdout = w
printSummary(resultTotals, "totals")
w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
assert.Contains(t, string(out), "49 checks PASS\n12 checks FAIL\n14 checks WARN\n0 checks INFO\n\n")
@ -700,7 +700,7 @@ func TestPrettyPrintNoSummary(t *testing.T) {
noSummary = true
prettyPrint(controlsCollection[0], resultTotals)
w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
assert.NotContains(t, string(out), "49 checks PASS")
@ -719,7 +719,7 @@ func TestPrettyPrintSummary(t *testing.T) {
noSummary = false
prettyPrint(controlsCollection[0], resultTotals)
w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
assert.Contains(t, string(out), "49 checks PASS")
@ -737,7 +737,7 @@ func TestWriteStdoutOutputNoTotal(t *testing.T) {
noTotals = true
writeStdoutOutput(controlsCollection)
w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
assert.NotContains(t, string(out), "49 checks PASS")
@ -757,7 +757,7 @@ func TestWriteStdoutOutputTotal(t *testing.T) {
noTotals = false
writeStdoutOutput(controlsCollection)
w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
@ -767,7 +767,7 @@ func TestWriteStdoutOutputTotal(t *testing.T) {
func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
var result []*check.Controls
d, err := ioutil.ReadFile(filepath)
d, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
@ -782,7 +782,7 @@ func parseControlsJsonFile(filepath string) ([]*check.Controls, error) {
func parseResultJsonFile(filepath string) (JsonOutputFormat, error) {
var result JsonOutputFormat
d, err := ioutil.ReadFile(filepath)
d, err := os.ReadFile(filepath)
if err != nil {
return result, err
}
@ -797,7 +797,7 @@ func parseResultJsonFile(filepath string) (JsonOutputFormat, error) {
func parseResultNoTotalsJsonFile(filepath string) ([]*check.Controls, error) {
var result []*check.Controls
d, err := ioutil.ReadFile(filepath)
d, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
@ -822,7 +822,7 @@ type restoreFn func()
func fakeExecutableInPath(execFile, execCode string) (restoreFn, error) {
pathenv := os.Getenv("PATH")
tmp, err := ioutil.TempDir("", "TestfakeExecutableInPath")
tmp, err := os.MkdirTemp("", "TestfakeExecutableInPath")
if err != nil {
return nil, err
}
@ -833,7 +833,7 @@ func fakeExecutableInPath(execFile, execCode string) (restoreFn, error) {
}
if len(execCode) > 0 {
ioutil.WriteFile(filepath.Join(tmp, execFile), []byte(execCode), 0700)
os.WriteFile(filepath.Join(tmp, execFile), []byte(execCode), 0700)
} else {
f, err := os.OpenFile(execFile, os.O_CREATE|os.O_EXCL, 0700)
if err != nil {

View File

@ -5,7 +5,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
@ -45,7 +45,7 @@ func getKubeVersionFromRESTAPI() (*KubeVersion, error) {
return nil, err
}
tb, err := ioutil.ReadFile(tokenfile)
tb, err := os.ReadFile(tokenfile)
if err != nil {
glog.V(2).Infof("Failed reading token file Error: %s", err)
return nil, err
@ -143,11 +143,11 @@ func getWebData(srvURL, token string, cacert *tls.Certificate) ([]byte, error) {
return nil, err
}
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}
func loadCertificate(certFile string) (*tls.Certificate, error) {
cacert, err := ioutil.ReadFile(certFile)
cacert, err := os.ReadFile(certFile)
if err != nil {
return nil, err
}

View File

@ -3,7 +3,6 @@ package cmd
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@ -12,13 +11,13 @@ import (
)
func TestLoadCertificate(t *testing.T) {
tmp, err := ioutil.TempDir("", "TestFakeLoadCertificate")
tmp, err := os.MkdirTemp("", "TestFakeLoadCertificate")
if err != nil {
t.Fatalf("unable to create temp directory: %v", err)
}
defer os.RemoveAll(tmp)
goodCertFile, _ := ioutil.TempFile(tmp, "good-cert-*")
goodCertFile, _ := os.CreateTemp(tmp, "good-cert-*")
_, _ = goodCertFile.Write([]byte(`-----BEGIN CERTIFICATE-----
MIICyDCCAbCgAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl
cm5ldGVzMB4XDTE5MTEwODAxNDAwMFoXDTI5MTEwNTAxNDAwMFowFTETMBEGA1UE
@ -36,7 +35,7 @@ jLv3UYZRHMpuNS8BJU74BuVzVPHd55RAl+bV8yemdZJ7pPzMvGbZ7zRXWODTDlge
CQb9lY+jYErisH8Sq7uABFPvi7RaTh8SS7V7OxqHZvmttNTdZs4TIkk45JK7Y+Xq
FAjB57z2NcIgJuVpQnGRYtr/JcH2Qdsq8bLtXaojUIWOOqoTDRLYozdMOOQ=
-----END CERTIFICATE-----`))
badCertFile, _ := ioutil.TempFile(tmp, "bad-cert-*")
badCertFile, _ := os.CreateTemp(tmp, "bad-cert-*")
cases := []struct {
file string

View File

@ -1,7 +1,6 @@
package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -45,7 +44,7 @@ func TestGetTestYamlFiles(t *testing.T) {
// Set up temp config directory
var err error
cfgDir, err = ioutil.TempDir("", "kube-bench-test")
cfgDir, err = os.MkdirTemp("", "kube-bench-test")
if err != nil {
t.Fatalf("Failed to create temp directory")
}
@ -59,7 +58,7 @@ func TestGetTestYamlFiles(t *testing.T) {
// We never expect config.yaml to be returned
for _, filename := range []string{"one.yaml", "two.yaml", "three.yaml", "config.yaml"} {
err = ioutil.WriteFile(filepath.Join(d, filename), []byte("hello world"), 0666)
err = os.WriteFile(filepath.Join(d, filename), []byte("hello world"), 0666)
if err != nil {
t.Fatalf("error writing temp file %s: %v", filename, err)
}

View File

@ -16,7 +16,6 @@ package cmd
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@ -399,7 +398,7 @@ func TestGetServiceFiles(t *testing.T) {
func TestGetDatadirFiles(t *testing.T) {
var err error
datadir, err := ioutil.TempDir("", "kube-bench-test-etcd-data-dir")
datadir, err := os.MkdirTemp("", "kube-bench-test-etcd-data-dir")
if err != nil {
t.Fatalf("Failed to create temp directory")
}
@ -474,7 +473,7 @@ func TestMakeSubsitutions(t *testing.T) {
func TestGetConfigFilePath(t *testing.T) {
var err error
cfgDir, err = ioutil.TempDir("", "kube-bench-test")
cfgDir, err = os.MkdirTemp("", "kube-bench-test")
if err != nil {
t.Fatalf("Failed to create temp directory")
}
@ -484,7 +483,7 @@ func TestGetConfigFilePath(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create temp dir")
}
err = ioutil.WriteFile(filepath.Join(d, "master.yaml"), []byte("hello world"), 0666)
err = os.WriteFile(filepath.Join(d, "master.yaml"), []byte("hello world"), 0666)
if err != nil {
t.Logf("Failed to create temp file")
}
@ -545,7 +544,7 @@ func TestDecrementVersion(t *testing.T) {
}
func TestGetYamlFilesFromDir(t *testing.T) {
cfgDir, err := ioutil.TempDir("", "kube-bench-test")
cfgDir, err := os.MkdirTemp("", "kube-bench-test")
if err != nil {
t.Fatalf("Failed to create temp directory")
}
@ -557,11 +556,11 @@ func TestGetYamlFilesFromDir(t *testing.T) {
t.Fatalf("Failed to create temp dir")
}
err = ioutil.WriteFile(filepath.Join(d, "something.yaml"), []byte("hello world"), 0666)
err = os.WriteFile(filepath.Join(d, "something.yaml"), []byte("hello world"), 0666)
if err != nil {
t.Fatalf("error writing file %v", err)
}
err = ioutil.WriteFile(filepath.Join(d, "config.yaml"), []byte("hello world"), 0666)
err = os.WriteFile(filepath.Join(d, "config.yaml"), []byte("hello world"), 0666)
if err != nil {
t.Fatalf("error writing file %v", err)
}