From f40a3bc8f5101669cd3b9ffd27edc6d0b0a1fdc9 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 30 Sep 2022 15:25:19 +0800 Subject: [PATCH] refactor: move from io/ioutil to io and os packages (#4752) The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun --- cmd/cli/kubectl-kyverno/jp/jp_command.go | 8 +++---- cmd/cli/kubectl-kyverno/test/test_command.go | 8 +++---- .../kubectl-kyverno/utils/common/common.go | 23 ++++++++++--------- cmd/cli/kubectl-kyverno/utils/common/fetch.go | 9 ++++---- pkg/autogen/autogen_test.go | 19 ++++++++------- pkg/config/config_test.go | 3 +-- pkg/registryclient/client.go | 4 ++-- pkg/testrunner/scenario.go | 3 +-- pkg/testrunner/scenario_test.go | 4 ++-- pkg/testrunner/utils.go | 3 +-- pkg/webhooks/handlers/admission.go | 4 ++-- 11 files changed, 43 insertions(+), 45 deletions(-) diff --git a/cmd/cli/kubectl-kyverno/jp/jp_command.go b/cmd/cli/kubectl-kyverno/jp/jp_command.go index 55a1aa7955..216ef39683 100644 --- a/cmd/cli/kubectl-kyverno/jp/jp_command.go +++ b/cmd/cli/kubectl-kyverno/jp/jp_command.go @@ -3,7 +3,7 @@ package jp import ( "encoding/json" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "sort" @@ -37,7 +37,7 @@ func Command() *cobra.Command { // https://github.com/jmespath/jp/blob/54882e03bd277fc4475a677fab1d35eaa478b839/jp.go var expression string if exprFile != "" { - byteExpr, err := ioutil.ReadFile(filepath.Clean(exprFile)) + byteExpr, err := os.ReadFile(filepath.Clean(exprFile)) if err != nil { return fmt.Errorf("error opening expression file: %w", err) } @@ -64,7 +64,7 @@ func Command() *cobra.Command { } var input interface{} if filename != "" { - f, err := ioutil.ReadFile(filepath.Clean(filename)) + f, err := os.ReadFile(filepath.Clean(filename)) if err != nil { return fmt.Errorf("error opening input file: %w", err) } @@ -72,7 +72,7 @@ func Command() *cobra.Command { return fmt.Errorf("error parsing input json: %w", err) } } else { - f, err := ioutil.ReadAll(os.Stdin) + f, err := io.ReadAll(os.Stdin) if err != nil { return fmt.Errorf("error opening input file: %w", err) } diff --git a/cmd/cli/kubectl-kyverno/test/test_command.go b/cmd/cli/kubectl-kyverno/test/test_command.go index 26f1321846..819ef90473 100644 --- a/cmd/cli/kubectl-kyverno/test/test_command.go +++ b/cmd/cli/kubectl-kyverno/test/test_command.go @@ -3,7 +3,7 @@ package test import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/url" "os" "path" @@ -428,7 +428,7 @@ func testCommandExecute(dirPath []string, fileName string, gitBranch string, tes if path.Base(file.Name()) == fileName { testYamlCount++ policyresoucePath := strings.Trim(yamlFilePath, fileName) - bytes, err := ioutil.ReadAll(file) + bytes, err := io.ReadAll(file) if err != nil { errors = append(errors, sanitizederror.NewWithError("Error: failed to read file", err)) continue @@ -483,7 +483,7 @@ func testCommandExecute(dirPath []string, fileName string, gitBranch string, tes func getLocalDirTestFiles(fs billy.Filesystem, path, fileName string, rc *resultCounts, testFiles *int, openAPIController *openapi.Controller, tf *testFilter, failOnly, removeColor bool) []error { var errors []error - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { return []error{fmt.Errorf("failed to read %v: %v", path, err.Error())} } @@ -495,7 +495,7 @@ func getLocalDirTestFiles(fs billy.Filesystem, path, fileName string, rc *result if file.Name() == fileName { *testFiles++ // We accept the risk of including files here as we read the test dir only. - yamlFile, err := ioutil.ReadFile(filepath.Join(path, file.Name())) // #nosec G304 + yamlFile, err := os.ReadFile(filepath.Join(path, file.Name())) // #nosec G304 if err != nil { errors = append(errors, sanitizederror.NewWithError("unable to read yaml", err)) continue diff --git a/cmd/cli/kubectl-kyverno/utils/common/common.go b/cmd/cli/kubectl-kyverno/utils/common/common.go index 6f2ab6a11d..c70531c50b 100644 --- a/cmd/cli/kubectl-kyverno/utils/common/common.go +++ b/cmd/cli/kubectl-kyverno/utils/common/common.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "path/filepath" @@ -105,7 +105,7 @@ func GetPolicies(paths []string) (policies []kyvernov1.PolicyInterface, errors [ // apply file from a directory is possible only if the path is not HTTP URL if !isHTTPPath && fileDesc.IsDir() { - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { err := fmt.Errorf("failed to process %v: %v", path, err.Error()) errors = append(errors, err) @@ -147,7 +147,7 @@ func GetPolicies(paths []string) (policies []kyvernov1.PolicyInterface, errors [ continue } - fileBytes, err = ioutil.ReadAll(resp.Body) + fileBytes, err = io.ReadAll(resp.Body) if err != nil { err := fmt.Errorf("failed to process %v: %v", path, err.Error()) errors = append(errors, err) @@ -156,7 +156,7 @@ func GetPolicies(paths []string) (policies []kyvernov1.PolicyInterface, errors [ } else { path = filepath.Clean(path) // We accept the risk of including a user provided file here. - fileBytes, err = ioutil.ReadFile(path) // #nosec G304 + fileBytes, err = os.ReadFile(path) // #nosec G304 if err != nil { err := fmt.Errorf("failed to process %v: %v", path, err.Error()) errors = append(errors, err) @@ -267,13 +267,13 @@ func GetVariable(variablesString, valuesFile string, fs billy.Filesystem, isGit if err != nil { fmt.Printf("Unable to open variable file: %s. error: %s", valuesFile, err) } - yamlFile, err = ioutil.ReadAll(filep) + yamlFile, err = io.ReadAll(filep) if err != nil { fmt.Printf("Unable to read variable files: %s. error: %s \n", filep, err) } } else { // We accept the risk of including a user provided file here. - yamlFile, err = ioutil.ReadFile(filepath.Join(policyResourcePath, valuesFile)) // #nosec G304 + yamlFile, err = os.ReadFile(filepath.Join(policyResourcePath, valuesFile)) // #nosec G304 if err != nil { fmt.Printf("\n Unable to open variable file: %s. error: %s \n", valuesFile, err) } @@ -621,7 +621,7 @@ func GetPoliciesFromPaths(fs billy.Filesystem, dirPath []string, isGit bool, pol fmt.Printf("Error: file not available with path %s: %v", filep.Name(), err.Error()) continue } - bytes, err := ioutil.ReadAll(filep) + bytes, err := io.ReadAll(filep) if err != nil { fmt.Printf("Error: failed to read file %s: %v", filep.Name(), err.Error()) continue @@ -703,7 +703,7 @@ func GetResourceAccordingToResourcePath(fs billy.Filesystem, resourcePaths []str return nil, err } if fileDesc.IsDir() { - files, err := ioutil.ReadDir(resourcePaths[0]) + files, err := os.ReadDir(resourcePaths[0]) if err != nil { return nil, sanitizederror.NewWithError(fmt.Sprintf("failed to parse %v", resourcePaths[0]), err) } @@ -1013,7 +1013,7 @@ func GetResourceFromPath(fs billy.Filesystem, path string, isGit bool, policyRes if fileErr != nil { fmt.Printf("Unable to open %s file: %s. \nerror: %s", resourceType, path, err) } - resourceBytes, err = ioutil.ReadAll(filep) + resourceBytes, err = io.ReadAll(filep) } } else { resourceBytes, err = getFileBytes(path) @@ -1117,7 +1117,7 @@ func GetUserInfoFromPath(fs billy.Filesystem, path string, isGit bool, policyRes if err != nil { fmt.Printf("Unable to open userInfo file: %s. \nerror: %s", path, err) } - bytes, err := ioutil.ReadAll(filep) + bytes, err := io.ReadAll(filep) if err != nil { fmt.Printf("Error: failed to read file %s: %v", filep.Name(), err.Error()) } @@ -1139,7 +1139,8 @@ func GetUserInfoFromPath(fs billy.Filesystem, path string, isGit bool, policyRes } } else { var errors []error - bytes, err := ioutil.ReadFile(filepath.Join(policyResourcePath, path)) + pathname := filepath.Clean(filepath.Join(policyResourcePath, path)) + bytes, err := os.ReadFile(pathname) if err != nil { errors = append(errors, sanitizederror.NewWithError("unable to read yaml", err)) } diff --git a/cmd/cli/kubectl-kyverno/utils/common/fetch.go b/cmd/cli/kubectl-kyverno/utils/common/fetch.go index 5b709b7765..8dee3f8ec5 100644 --- a/cmd/cli/kubectl-kyverno/utils/common/fetch.go +++ b/cmd/cli/kubectl-kyverno/utils/common/fetch.go @@ -4,8 +4,9 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net/http" + "os" "path/filepath" "strings" @@ -139,7 +140,7 @@ func GetResourcesWithTest(fs billy.Filesystem, policies []kyvernov1.PolicyInterf fmt.Printf("Unable to open resource file: %s. error: %s", resourcePath, err) continue } - resourceBytes, _ = ioutil.ReadAll(filep) + resourceBytes, _ = io.ReadAll(filep) } else { resourceBytes, err = getFileBytes(resourcePath) } @@ -233,14 +234,14 @@ func getFileBytes(path string) ([]byte, error) { return nil, err } - file, err = ioutil.ReadAll(resp.Body) + file, err = io.ReadAll(resp.Body) if err != nil { return nil, err } } else { path = filepath.Clean(path) // We accept the risk of including a user provided file here. - file, err = ioutil.ReadFile(path) // #nosec G304 + file, err = os.ReadFile(path) // #nosec G304 if err != nil { return nil, err } diff --git a/pkg/autogen/autogen_test.go b/pkg/autogen/autogen_test.go index 2e8684a65b..d933091900 100644 --- a/pkg/autogen/autogen_test.go +++ b/pkg/autogen/autogen_test.go @@ -3,7 +3,6 @@ package autogen import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -299,7 +298,7 @@ func Test_Any(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") if err != nil { t.Log(err) } @@ -337,7 +336,7 @@ func Test_All(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") if err != nil { t.Log(err) } @@ -376,7 +375,7 @@ func Test_Exclude(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") if err != nil { t.Log(err) } @@ -410,7 +409,7 @@ func Test_CronJobOnly(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") if err != nil { t.Log(err) } @@ -440,7 +439,7 @@ func Test_ForEachPod(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/policy/mutate/policy_mutate_pod_foreach_with_context.yaml") + file, err := os.ReadFile(baseDir + "/test/policy/mutate/policy_mutate_pod_foreach_with_context.yaml") if err != nil { t.Log(err) } @@ -475,7 +474,7 @@ func Test_CronJob_hasExclude(t *testing.T) { baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") if err != nil { t.Log(err) } @@ -512,7 +511,7 @@ func Test_CronJobAndDeployment(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/disallow_bind_mounts.yaml") if err != nil { t.Log(err) } @@ -543,7 +542,7 @@ func Test_UpdateVariablePath(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/best_practices/select-secrets.yaml") + file, err := os.ReadFile(baseDir + "/test/best_practices/select-secrets.yaml") if err != nil { t.Log(err) } @@ -573,7 +572,7 @@ func Test_Deny(t *testing.T) { dir, err := os.Getwd() baseDir := filepath.Dir(filepath.Dir(dir)) assert.NilError(t, err) - file, err := ioutil.ReadFile(baseDir + "/test/policy/deny/policy.yaml") + file, err := os.ReadFile(baseDir + "/test/policy/deny/policy.yaml") if err != nil { t.Log(err) } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 0af0e64fdb..559f553647 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -2,7 +2,6 @@ package config_test import ( "fmt" - "io/ioutil" "math" "os" "testing" @@ -70,7 +69,7 @@ func createMinimalKubeconfig(t *testing.T) string { func createCustomKubeConfig(t *testing.T, fileName string, hosts map[string]string, currentContext string) { t.Helper() - err := ioutil.WriteFile(fileName, []byte(fmt.Sprintf(` + err := os.WriteFile(fileName, []byte(fmt.Sprintf(` apiVersion: v1 clusters: - cluster: diff --git a/pkg/registryclient/client.go b/pkg/registryclient/client.go index 40f535f988..2b6976eb41 100644 --- a/pkg/registryclient/client.go +++ b/pkg/registryclient/client.go @@ -4,7 +4,7 @@ import ( "context" "crypto/tls" "fmt" - "io/ioutil" + "io" "net/http" ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login" @@ -48,7 +48,7 @@ func InitClient(options ...Option) (Client, error) { baseKeychain := authn.NewMultiKeychain( authn.DefaultKeychain, google.Keychain, - authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(ioutil.Discard))), + authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(io.Discard))), authn.NewKeychainFromHelper(credhelper.NewACRCredentialsHelper()), github.Keychain, ) diff --git a/pkg/testrunner/scenario.go b/pkg/testrunner/scenario.go index 2f7b0d95db..b903135361 100644 --- a/pkg/testrunner/scenario.go +++ b/pkg/testrunner/scenario.go @@ -3,7 +3,6 @@ package testrunner import ( "bytes" "encoding/json" - "io/ioutil" "os" ospath "path" "path/filepath" @@ -119,7 +118,7 @@ func loadFile(t *testing.T, path string) ([]byte, error) { } path = filepath.Clean(path) // We accept the risk of including a user provided file here. - return ioutil.ReadFile(path) // #nosec G304 + return os.ReadFile(path) // #nosec G304 } func runScenario(t *testing.T, s *Scenario) bool { diff --git a/pkg/testrunner/scenario_test.go b/pkg/testrunner/scenario_test.go index 55d67d14bb..455ab0bcb0 100644 --- a/pkg/testrunner/scenario_test.go +++ b/pkg/testrunner/scenario_test.go @@ -1,7 +1,7 @@ package testrunner import ( - "io/ioutil" + "os" "testing" "github.com/kyverno/kyverno/pkg/engine/response" @@ -54,7 +54,7 @@ func Test_parse_file(t *testing.T) { func Test_parse_file2(t *testing.T) { path := getRelativePath("test/scenarios/samples/best_practices/disallow_bind_mounts_fail.yaml") - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) assert.NilError(t, err) strData := string(data) diff --git a/pkg/testrunner/utils.go b/pkg/testrunner/utils.go index e60c4c3013..13b32452cc 100644 --- a/pkg/testrunner/utils.go +++ b/pkg/testrunner/utils.go @@ -1,7 +1,6 @@ package testrunner import ( - "io/ioutil" "os" "path/filepath" @@ -16,7 +15,7 @@ func LoadFile(path string) ([]byte, error) { } path = filepath.Clean(path) // We accept the risk of including a user provided file here. - return ioutil.ReadFile(path) // #nosec G304 + return os.ReadFile(path) // #nosec G304 } var kindToResource = map[string]string{ diff --git a/pkg/webhooks/handlers/admission.go b/pkg/webhooks/handlers/admission.go index 08f9ad2a4e..a1fb220cd1 100644 --- a/pkg/webhooks/handlers/admission.go +++ b/pkg/webhooks/handlers/admission.go @@ -3,7 +3,7 @@ package handlers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -28,7 +28,7 @@ func Admission(logger logr.Logger, inner AdmissionHandler) http.HandlerFunc { return } defer request.Body.Close() - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) if err != nil { logger.Info("failed to read HTTP body", "req", request.URL.String()) http.Error(writer, "failed to read HTTP body", http.StatusBadRequest)