Skip to content

Commit

Permalink
Merge pull request #530 from mq-cloudpak/pranav-2680-remove-ioutil-cd
Browse files Browse the repository at this point in the history
2680 - Remove ioutil dependency from mq-container CD branch
  • Loading branch information
Pranav Goyal authored and GitHub Enterprise committed Oct 12, 2023
2 parents 4fd3a48 + 502f3a3 commit a26f76b
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 60 deletions.
3 changes: 1 addition & 2 deletions cmd/runmqdevserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/runmqserver/license.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,7 +17,6 @@ package main

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cmd/runmqserver/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/runmqserver/main_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,7 +17,6 @@ package main

import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/runmqserver/mirror_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,7 +18,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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())
Expand Down
13 changes: 6 additions & 7 deletions cmd/runmqserver/qmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/runmqserver/qmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
package main

import (
"io/ioutil"
"os"
"testing"
)

Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions internal/containerruntime/amicontained.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/containerruntime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package containerruntime

import (
"fmt"
"io/ioutil"
"os"
"strings"
)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions internal/htpasswd/htpasswd.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,7 +20,7 @@ package htpasswd

import (
"fmt"
"io/ioutil"
"os"
"strings"

"golang.org/x/crypto/bcrypt"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions internal/ready/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ready

import (
"context"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit a26f76b

Please sign in to comment.