1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-22 08:08:07 +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/json"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -53,7 +52,7 @@ func TestYamlFiles(t *testing.T) {
} }
if !info.IsDir() { if !info.IsDir() {
t.Logf("reading file: %s", path) t.Logf("reading file: %s", path)
in, err := ioutil.ReadFile(path) in, err := os.ReadFile(path)
if err != nil { if err != nil {
t.Fatalf("error opening file %s: %v", path, err) t.Fatalf("error opening file %s: %v", path, err)
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -16,7 +16,6 @@ package cmd
import ( import (
"errors" "errors"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -399,7 +398,7 @@ func TestGetServiceFiles(t *testing.T) {
func TestGetDatadirFiles(t *testing.T) { func TestGetDatadirFiles(t *testing.T) {
var err error 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 { if err != nil {
t.Fatalf("Failed to create temp directory") t.Fatalf("Failed to create temp directory")
} }
@ -474,7 +473,7 @@ func TestMakeSubsitutions(t *testing.T) {
func TestGetConfigFilePath(t *testing.T) { func TestGetConfigFilePath(t *testing.T) {
var err error var err error
cfgDir, err = ioutil.TempDir("", "kube-bench-test") cfgDir, err = os.MkdirTemp("", "kube-bench-test")
if err != nil { if err != nil {
t.Fatalf("Failed to create temp directory") t.Fatalf("Failed to create temp directory")
} }
@ -484,7 +483,7 @@ func TestGetConfigFilePath(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create temp dir") 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 { if err != nil {
t.Logf("Failed to create temp file") t.Logf("Failed to create temp file")
} }
@ -545,7 +544,7 @@ func TestDecrementVersion(t *testing.T) {
} }
func TestGetYamlFilesFromDir(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 { if err != nil {
t.Fatalf("Failed to create temp directory") t.Fatalf("Failed to create temp directory")
} }
@ -557,11 +556,11 @@ func TestGetYamlFilesFromDir(t *testing.T) {
t.Fatalf("Failed to create temp dir") 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 { if err != nil {
t.Fatalf("error writing file %v", err) 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 { if err != nil {
t.Fatalf("error writing file %v", err) t.Fatalf("error writing file %v", err)
} }