From 97c392ddf95770ce9c259eda8f65b8ba3061002c Mon Sep 17 00:00:00 2001 From: Girish Ramnani Date: Tue, 16 Jun 2020 14:21:57 +0530 Subject: [PATCH] resolved the gosec issue failing unit tests --- pkg/util/util.go | 10 +++++++--- tests/helper/helper_generic.go | 12 ++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index cf5611cbd5e..a8f13769afe 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -4,10 +4,11 @@ import ( "archive/zip" "bufio" "context" + "crypto/rand" "fmt" "io" "io/ioutil" - "math/rand" + "math/big" "net" "net/http" "net/url" @@ -81,10 +82,13 @@ func ConvertLabelsToSelector(labels map[string]string) string { // GenerateRandomString generates a random string of lower case characters of // the given size func GenerateRandomString(n int) string { - rand.Seed(time.Now().UnixNano()) b := make([]rune, n) + for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] + // this error is ignored because it fails only when the 2nd arg of Int() is less then 0. + // which wont happend + n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes)))) + b[i] = letterRunes[n.Int64()] } return string(b) } diff --git a/tests/helper/helper_generic.go b/tests/helper/helper_generic.go index ee677a3c75a..7fd69a792af 100644 --- a/tests/helper/helper_generic.go +++ b/tests/helper/helper_generic.go @@ -2,9 +2,10 @@ package helper import ( "bytes" + "crypto/rand" "encoding/json" "fmt" - "math/rand" + "math/big" "os" "os/exec" "path/filepath" @@ -16,16 +17,15 @@ import ( "github.com/onsi/gomega/gexec" ) -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -} - // RandString returns a random string of given length func RandString(n int) string { const letterBytes = "abcdefghijklmnopqrstuvwxyz" b := make([]byte, n) for i := range b { - b[i] = letterBytes[rand.Intn(len(letterBytes))] + // this error is ignored because it fails only when the 2nd arg of Int() is less then 0. + // which wont happen + n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterBytes)))) + b[i] = letterBytes[n.Int64()] } return string(b) }