From 0d2e055ec16b99146c31d190f8c1efa6a1c0b8af Mon Sep 17 00:00:00 2001 From: "pranav.goyal" Date: Wed, 11 Oct 2023 13:19:40 +0530 Subject: [PATCH] 2680 - Remove ioutil dependency from mq-container CD branch --- cmd/runmqdevserver/main.go | 3 +-- cmd/runmqserver/license.go | 5 ++--- cmd/runmqserver/logging.go | 3 +-- cmd/runmqserver/main_test.go | 5 ++--- cmd/runmqserver/mirror_test.go | 11 +++++----- cmd/runmqserver/qmgr.go | 13 ++++++------ cmd/runmqserver/qmgr_test.go | 4 ++-- internal/containerruntime/amicontained.go | 9 ++++++-- internal/containerruntime/runtime.go | 6 +++--- internal/htpasswd/htpasswd.go | 8 ++++---- internal/ready/ready.go | 3 +-- internal/tls/tls.go | 25 +++++++++++------------ pkg/mqini/mqini_test.go | 6 +++--- test/container/devconfig_test_util.go | 9 ++++---- test/container/docker_api_test_util.go | 3 +-- test/container/mqmetric_test_util.go | 4 ++-- 16 files changed, 57 insertions(+), 60 deletions(-) diff --git a/cmd/runmqdevserver/main.go b/cmd/runmqdevserver/main.go index 8afca80f..e343d25a 100644 --- a/cmd/runmqdevserver/main.go +++ b/cmd/runmqdevserver/main.go @@ -17,7 +17,6 @@ package main import ( "fmt" - "io/ioutil" "os" "strings" "syscall" @@ -92,7 +91,7 @@ func logTermination(args ...interface{}) { // that Kubernetes will look for termination information. log.Debugf("Writing termination message: %v", msg) // #nosec G306 - its a read by owner/s group, and pose no harm. - err := ioutil.WriteFile("/run/termination-log", []byte(msg), 0660) + err := os.WriteFile("/run/termination-log", []byte(msg), 0660) if err != nil { log.Debug(err) } diff --git a/cmd/runmqserver/license.go b/cmd/runmqserver/license.go index 19f199eb..ff309352 100644 --- a/cmd/runmqserver/license.go +++ b/cmd/runmqserver/license.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2017, 2018 +© Copyright IBM Corporation 2017, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package main import ( "errors" - "io/ioutil" "os" "path/filepath" "strings" @@ -79,7 +78,7 @@ func checkLicense() (bool, error) { case ok && lic == "view": file := filepath.Join("/opt/mqm/licenses", resolveLicenseFile()) // #nosec G304 - buf, err := ioutil.ReadFile(file) + buf, err := os.ReadFile(file) if err != nil { log.Println(err) return false, err diff --git a/cmd/runmqserver/logging.go b/cmd/runmqserver/logging.go index 7b756e98..a86efa4c 100644 --- a/cmd/runmqserver/logging.go +++ b/cmd/runmqserver/logging.go @@ -19,7 +19,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -47,7 +46,7 @@ func logTermination(args ...interface{}) { // that Kubernetes will look for termination information. log.Debugf("Writing termination message: %v", msg) // #nosec G306 - its a read by owner/s group, and pose no harm. - err := ioutil.WriteFile("/run/termination-log", []byte(msg), 0660) + err := os.WriteFile("/run/termination-log", []byte(msg), 0660) if err != nil { log.Debug(err) } diff --git a/cmd/runmqserver/main_test.go b/cmd/runmqserver/main_test.go index 52aba1ac..efbe4710 100644 --- a/cmd/runmqserver/main_test.go +++ b/cmd/runmqserver/main_test.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2017, 2019 +© Copyright IBM Corporation 2017, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package main import ( "flag" - "io/ioutil" "os" "path/filepath" "strconv" @@ -51,7 +50,7 @@ func TestSystem(t *testing.T) { osExit = func(rc int) { // Write the exit code to a file instead log.Printf("Writing exit code %v to file %v", strconv.Itoa(rc), filename) - err := ioutil.WriteFile(filename, []byte(strconv.Itoa(rc)), 0644) + err := os.WriteFile(filename, []byte(strconv.Itoa(rc)), 0644) if err != nil { log.Print(err) } diff --git a/cmd/runmqserver/mirror_test.go b/cmd/runmqserver/mirror_test.go index 3d07402b..befe7958 100644 --- a/cmd/runmqserver/mirror_test.go +++ b/cmd/runmqserver/mirror_test.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2018, 2019 +© Copyright IBM Corporation 2018, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package main import ( "context" "fmt" - "io/ioutil" "os" "strconv" "strings" @@ -32,7 +31,7 @@ func TestMirrorLogWithoutRotation(t *testing.T) { for i := 0; i < 10; i++ { t.Run(t.Name()+strconv.Itoa(i), func(t *testing.T) { // Use just the sub-test name in the file name - tmp, err := ioutil.TempFile("", strings.Split(t.Name(), "/")[1]) + tmp, err := os.CreateTemp("", strings.Split(t.Name(), "/")[1]) if err != nil { t.Fatal(err) } @@ -71,7 +70,7 @@ func TestMirrorLogWithRotation(t *testing.T) { for i := 0; i < 5; i++ { t.Run(t.Name()+strconv.Itoa(i), func(t *testing.T) { // Use just the sub-test name in the file name - tmp, err := ioutil.TempFile("", strings.Split(t.Name(), "/")[1]) + tmp, err := os.CreateTemp("", strings.Split(t.Name(), "/")[1]) if err != nil { t.Fatal(err) } @@ -126,13 +125,13 @@ func TestMirrorLogWithRotation(t *testing.T) { } func testMirrorLogExistingFile(t *testing.T, newQM bool) int { - tmp, err := ioutil.TempFile("", t.Name()) + tmp, err := os.CreateTemp("", t.Name()) if err != nil { t.Fatal(err) } t.Log(tmp.Name()) log.Println("Logging 1 message before we start") - ioutil.WriteFile(tmp.Name(), []byte("{\"message\"=\"A\"}\n"), 0600) + os.WriteFile(tmp.Name(), []byte("{\"message\"=\"A\"}\n"), 0600) defer os.Remove(tmp.Name()) count := 0 ctx, cancel := context.WithCancel(context.Background()) diff --git a/cmd/runmqserver/qmgr.go b/cmd/runmqserver/qmgr.go index 700af92b..3a4a6e01 100644 --- a/cmd/runmqserver/qmgr.go +++ b/cmd/runmqserver/qmgr.go @@ -18,7 +18,6 @@ package main import ( "context" "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -109,19 +108,19 @@ func createQueueManager(name string, devMode bool) (bool, error) { return true, nil } -//readQMIni reads the qm.ini file and returns it as a byte array -//This function is specific to comply with the nosec. +// readQMIni reads the qm.ini file and returns it as a byte array +// This function is specific to comply with the nosec. func readQMIni(dataDir string) ([]byte, error) { qmgrDir := filepath.Join(dataDir, "qm.ini") // #nosec G304 - qmgrDir filepath is derived from dspmqinf - iniFileBytes, err := ioutil.ReadFile(qmgrDir) + iniFileBytes, err := os.ReadFile(qmgrDir) if err != nil { return nil, err } return iniFileBytes, err } -//validateLogFilePageSetting validates if the specified logFilePage number is equal to the existing value in the qm.ini +// validateLogFilePageSetting validates if the specified logFilePage number is equal to the existing value in the qm.ini func validateLogFilePageSetting(iniFileBytes []byte, logFilePages string) bool { lfpString := "LogFilePages=" + logFilePages qminiConfigStr := string(iniFileBytes) @@ -320,7 +319,7 @@ func updateQMini(qmname string) error { qmgrDir := filepath.Join(dataDir, "qm.ini") //read the initial version. // #nosec G304 - qmgrDir filepath is derived from dspmqinf - iniFileBytes, err := ioutil.ReadFile(qmgrDir) + iniFileBytes, err := os.ReadFile(qmgrDir) if err != nil { return err } @@ -330,7 +329,7 @@ func updateQMini(qmname string) error { curFile := re.ReplaceAllString(qminiConfigStr, "") // #nosec G304 G306 - qmgrDir filepath is derived from dspmqinf and // its a read by owner/s group, and pose no harm. - err := ioutil.WriteFile(qmgrDir, []byte(curFile), 0660) + err := os.WriteFile(qmgrDir, []byte(curFile), 0660) if err != nil { return err } diff --git a/cmd/runmqserver/qmgr_test.go b/cmd/runmqserver/qmgr_test.go index 3c16fdcc..97b7827f 100644 --- a/cmd/runmqserver/qmgr_test.go +++ b/cmd/runmqserver/qmgr_test.go @@ -16,7 +16,7 @@ limitations under the License. package main import ( - "io/ioutil" + "os" "testing" ) @@ -73,7 +73,7 @@ func Test_validateLogFilePageSetting(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - iniFileBytes, err := ioutil.ReadFile(tt.args.iniFilePath) + iniFileBytes, err := os.ReadFile(tt.args.iniFilePath) if err != nil { t.Fatal(err) } diff --git a/internal/containerruntime/amicontained.go b/internal/containerruntime/amicontained.go index ad35ef39..85190069 100644 --- a/internal/containerruntime/amicontained.go +++ b/internal/containerruntime/amicontained.go @@ -24,11 +24,15 @@ SOFTWARE. The code was forked when the latest details are as "Latest commit 871fc34 on Sep 18, 2018" */ +// Adding IBM Copyright since the forked code had to be modified to remove deprecated ioutil package +/* +© Copyright IBM Corporation 2023 +*/ + package containerruntime import ( "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -172,7 +176,8 @@ func readFile(file string) []byte { } // filepath.clean was added to resolve the gosec build failure // with error "Potential file inclusion via variable" - b, err := ioutil.ReadFile(filepath.Clean(file)) + // IBM Modified the below line to remove the deprecated ioutil dependency + b, err := os.ReadFile(filepath.Clean(file)) if err != nil { return nil } diff --git a/internal/containerruntime/runtime.go b/internal/containerruntime/runtime.go index 0ca5fb96..3ace75ae 100644 --- a/internal/containerruntime/runtime.go +++ b/internal/containerruntime/runtime.go @@ -17,7 +17,7 @@ package containerruntime import ( "fmt" - "io/ioutil" + "os" "strings" ) @@ -26,7 +26,7 @@ func DetectContainerRuntime() ContainerRuntime { } func GetBaseImage() (string, error) { - buf, err := ioutil.ReadFile("/etc/os-release") + buf, err := os.ReadFile("/etc/os-release") if err != nil { return "", fmt.Errorf("Failed to read /etc/os-release: %v", err) } @@ -70,7 +70,7 @@ func GetSecurityAttributes() string { func readProc(filename string) (value string, err error) { // #nosec G304 - buf, err := ioutil.ReadFile(filename) + buf, err := os.ReadFile(filename) if err != nil { return "", err } diff --git a/internal/htpasswd/htpasswd.go b/internal/htpasswd/htpasswd.go index 2e214c43..535563b6 100644 --- a/internal/htpasswd/htpasswd.go +++ b/internal/htpasswd/htpasswd.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2020, 2021 +© Copyright IBM Corporation 2020, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ package htpasswd import ( "fmt" - "io/ioutil" + "os" "strings" "golang.org/x/crypto/bcrypt" @@ -79,7 +79,7 @@ func (htpfile mapHtPasswd) ReadHtPasswordFile(isTest bool) error { file = "my.htpasswd" } - pwdsBytes, err := ioutil.ReadFile(file) + pwdsBytes, err := os.ReadFile(file) if err != nil { return err } @@ -109,5 +109,5 @@ func (htpfile mapHtPasswd) updateHtPasswordFile(isTest bool) error { file = "my.htpasswd" } // #nosec G306 - its a read by owner/s group, and pose no harm. - return ioutil.WriteFile(file, htpfile.GetBytes(), 0660) + return os.WriteFile(file, htpfile.GetBytes(), 0660) } diff --git a/internal/ready/ready.go b/internal/ready/ready.go index 320d1225..58a2397d 100644 --- a/internal/ready/ready.go +++ b/internal/ready/ready.go @@ -19,7 +19,6 @@ package ready import ( "context" - "io/ioutil" "os" "strings" @@ -55,7 +54,7 @@ func Clear() error { // manager has finished its configuration step func Set() error { // #nosec G306 - this gives permissions to owner/s group only. - return ioutil.WriteFile(fileName, []byte("1"), 0770) + return os.WriteFile(fileName, []byte("1"), 0770) } // Check checks whether or not the queue manager has finished its diff --git a/internal/tls/tls.go b/internal/tls/tls.go index 412cc06b..42fb7cb9 100644 --- a/internal/tls/tls.go +++ b/internal/tls/tls.go @@ -18,7 +18,6 @@ package tls import ( "bufio" "fmt" - "io/ioutil" pwr "math/rand" "os" "path/filepath" @@ -227,11 +226,11 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string, keyLabel := "" // Process all keys - keyList, err := ioutil.ReadDir(keyDir) + keyList, err := os.ReadDir(keyDir) if err == nil && len(keyList) > 0 { // Process each set of keys - each set should contain files: *.key & *.crt for _, keySet := range keyList { - keys, _ := ioutil.ReadDir(filepath.Join(keyDir, keySet.Name())) + keys, _ := os.ReadDir(filepath.Join(keyDir, keySet.Name())) // Ensure the label of the set of keys does not match the name of the PKCS#12 Truststore if keySet.Name() == p12TruststoreName[0:len(p12TruststoreName)-len(filepath.Ext(p12TruststoreName))] { @@ -268,7 +267,7 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string, return "", fmt.Errorf("Failed to encode PKCS#12 Keystore %s: %v", keySet.Name()+".p12", err) } // #nosec G306 - this gives permissions to owner/s group only. - err = ioutil.WriteFile(filepath.Join(keystoreDir, keySet.Name()+".p12"), file, 0644) + err = os.WriteFile(filepath.Join(keystoreDir, keySet.Name()+".p12"), file, 0644) if err != nil { return "", fmt.Errorf("Failed to write PKCS#12 Keystore %s: %v", filepath.Join(keystoreDir, keySet.Name()+".p12"), err) } @@ -299,17 +298,17 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string, func processTrustCertificates(tlsStore *TLSStore, trustDir string) error { // Process all trust certiifcates - trustList, err := ioutil.ReadDir(trustDir) + trustList, err := os.ReadDir(trustDir) if err == nil && len(trustList) > 0 { // Process each set of keys for _, trustSet := range trustList { - keys, _ := ioutil.ReadDir(filepath.Join(trustDir, trustSet.Name())) + keys, _ := os.ReadDir(filepath.Join(trustDir, trustSet.Name())) for _, key := range keys { if strings.HasSuffix(key.Name(), ".crt") { // #nosec G304 - filename variable is derived from contents of 'trustDir' which is a defined constant - file, err := ioutil.ReadFile(filepath.Join(trustDir, trustSet.Name(), key.Name())) + file, err := os.ReadFile(filepath.Join(trustDir, trustSet.Name(), key.Name())) if err != nil { return fmt.Errorf("Failed to read file %s: %v", filepath.Join(trustDir, trustSet.Name(), key.Name()), err) } @@ -360,7 +359,7 @@ func processTrustCertificates(tlsStore *TLSStore, trustDir string) error { } // processPrivateKey processes the private key (*.key) from a set of keys -func processPrivateKey(keyDir string, keySetName string, keys []os.FileInfo) (interface{}, string, error) { +func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (interface{}, string, error) { var privateKey interface{} keyPrefix := "" @@ -369,7 +368,7 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.FileInfo) (in if strings.HasSuffix(key.Name(), ".key") { // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant - file, err := ioutil.ReadFile(filepath.Join(keyDir, keySetName, key.Name())) + file, err := os.ReadFile(filepath.Join(keyDir, keySetName, key.Name())) if err != nil { return nil, "", fmt.Errorf("Failed to read private key %s: %v", filepath.Join(keyDir, keySetName, key.Name()), err) } @@ -395,7 +394,7 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.FileInfo) (in } // processCertificates processes the certificates (*.crt) from a set of keys -func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.FileInfo, cmsKeystore, p12Truststore *KeyStoreData) (*x509.Certificate, []*x509.Certificate, error) { +func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.DirEntry, cmsKeystore, p12Truststore *KeyStoreData) (*x509.Certificate, []*x509.Certificate, error) { var publicCertificate *x509.Certificate var caCertificate []*x509.Certificate @@ -404,7 +403,7 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os. if strings.HasPrefix(key.Name(), keyPrefix) && strings.HasSuffix(key.Name(), ".crt") { // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant - file, err := ioutil.ReadFile(filepath.Join(keyDir, keySetName, key.Name())) + file, err := os.ReadFile(filepath.Join(keyDir, keySetName, key.Name())) if err != nil { return nil, nil, fmt.Errorf("Failed to read public certificate %s: %v", filepath.Join(keyDir, keySetName, key.Name()), err) } @@ -425,7 +424,7 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os. } else if strings.HasSuffix(key.Name(), ".crt") { // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant - file, err := ioutil.ReadFile(filepath.Join(keyDir, keySetName, key.Name())) + file, err := os.ReadFile(filepath.Join(keyDir, keySetName, key.Name())) if err != nil { return nil, nil, fmt.Errorf("Failed to read CA certificate %s: %v", filepath.Join(keyDir, keySetName, key.Name()), err) } @@ -648,7 +647,7 @@ func haveKeysAndCerts(keyDir string) bool { for _, fileInfo := range fileList { // Keys and certs will be supplied in an user defined subdirectory. // Do a listing of the subdirectory and then search for .key and .cert files - keys, _ := ioutil.ReadDir(filepath.Join(keyDir, fileInfo.Name())) + keys, _ := os.ReadDir(filepath.Join(keyDir, fileInfo.Name())) for _, key := range keys { if strings.HasSuffix(key.Name(), ".key") || strings.HasSuffix(key.Name(), ".crt") { // We found at least one key/crt file. diff --git a/pkg/mqini/mqini_test.go b/pkg/mqini/mqini_test.go index 18aeaf7a..e1a9a6af 100644 --- a/pkg/mqini/mqini_test.go +++ b/pkg/mqini/mqini_test.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2018, 2019 +© Copyright IBM Corporation 2018, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ limitations under the License. package mqini import ( - "io/ioutil" + "os" "testing" ) @@ -35,7 +35,7 @@ var getQueueManagerTests = []struct { func TestGetQueueManager(t *testing.T) { for _, table := range getQueueManagerTests { t.Run(table.file, func(t *testing.T) { - b, err := ioutil.ReadFile(table.file) + b, err := os.ReadFile(table.file) if err != nil { t.Fatal(err) } diff --git a/test/container/devconfig_test_util.go b/test/container/devconfig_test_util.go index 4ae732f2..2a0a8542 100644 --- a/test/container/devconfig_test_util.go +++ b/test/container/devconfig_test_util.go @@ -25,9 +25,10 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httputil" + "os" "path/filepath" "strconv" "strings" @@ -198,7 +199,7 @@ func createTLSConfig(t *testing.T, certFile, password string) *tls.Config { t.Fatal(err) } // Read in the cert file - cert, err := ioutil.ReadFile(certFile) + cert, err := os.ReadFile(certFile) if err != nil { t.Fatal(err) } @@ -316,7 +317,7 @@ func testRESTMessaging(t *testing.T, cli ce.ContainerInterface, ID string, tlsCo t.Logf("HTTP response: %+v", resp) t.Fail() } - gotMessage, err := ioutil.ReadAll(resp.Body) + gotMessage, err := io.ReadAll(resp.Body) //gotMessage := string(b) if string(gotMessage) != string(putMessage) { t.Errorf("Expected payload to be \"%s\"; got \"%s\"", putMessage, gotMessage) @@ -331,7 +332,7 @@ func createTLSConfigWithCipher(t *testing.T, certFile, password string, ciphers t.Fatal(err) } // Read in the cert file - cert, err := ioutil.ReadFile(certFile) + cert, err := os.ReadFile(certFile) if err != nil { t.Fatal(err) } diff --git a/test/container/docker_api_test_util.go b/test/container/docker_api_test_util.go index 143e04d7..24f1f8c4 100644 --- a/test/container/docker_api_test_util.go +++ b/test/container/docker_api_test_util.go @@ -23,7 +23,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/rand" "os" "os/exec" @@ -506,7 +505,7 @@ func getCoverageExitCode(t *testing.T, orig int64) int64 { } // Remove the file, ready for the next test defer os.Remove(f) - buf, err := ioutil.ReadFile(f) + buf, err := os.ReadFile(f) if err != nil { t.Log(err) return orig diff --git a/test/container/mqmetric_test_util.go b/test/container/mqmetric_test_util.go index 5eaea0a2..f5062dd5 100644 --- a/test/container/mqmetric_test_util.go +++ b/test/container/mqmetric_test_util.go @@ -18,7 +18,7 @@ package main import ( "bufio" "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -47,7 +47,7 @@ func getMetrics(t *testing.T, port string) []mqmetric { return returned } defer resp.Body.Close() - metricsRaw, err := ioutil.ReadAll(resp.Body) + metricsRaw, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("Error reading metrics data: %v", err) return returned