diff --git a/logic/enrollmentkey.go b/logic/enrollmentkey.go index ec1d3c8f..9888f3c3 100644 --- a/logic/enrollmentkey.go +++ b/logic/enrollmentkey.go @@ -9,7 +9,6 @@ import ( "github.com/gravitl/netmaker/database" "github.com/gravitl/netmaker/models" - "github.com/gravitl/netmaker/netclient/ncutils" ) // EnrollmentErrors - struct for holding EnrollmentKey error messages @@ -190,9 +189,9 @@ func getUniqueEnrollmentID() (string, error) { if err != nil { return "", err } - newID := ncutils.MakeRandomString(models.EnrollmentKeyLength) + newID := RandomString(models.EnrollmentKeyLength) for _, ok := currentKeys[newID]; ok; { - newID = ncutils.MakeRandomString(models.EnrollmentKeyLength) + newID = RandomString(models.EnrollmentKeyLength) } return newID, nil } diff --git a/logic/jwts.go b/logic/jwts.go index 4ac722a6..0f355a87 100644 --- a/logic/jwts.go +++ b/logic/jwts.go @@ -19,10 +19,7 @@ var jwtSecretKey []byte func SetJWTSecret() { currentSecret, jwtErr := FetchJWTSecret() if jwtErr != nil { - newValue, err := GenerateCryptoString(64) - if err != nil { - logger.FatalLog("something went wrong when generating JWT signature") - } + newValue := RandomString(64) jwtSecretKey = []byte(newValue) // 512 bit random password if err := StoreJWTSecret(string(jwtSecretKey)); err != nil { logger.FatalLog("something went wrong when configuring JWT authentication") diff --git a/logic/util.go b/logic/util.go index 0724bca8..52ed902a 100644 --- a/logic/util.go +++ b/logic/util.go @@ -2,11 +2,10 @@ package logic import ( - crand "crypto/rand" + "crypto/rand" + "encoding/base32" "encoding/base64" "encoding/json" - "math/big" - "math/rand" "net" "os" "strings" @@ -14,6 +13,7 @@ import ( "github.com/c-robinson/iplib" "github.com/gravitl/netmaker/database" + "github.com/gravitl/netmaker/logger" ) // IsBase64 - checks if a string is in base64 format @@ -68,32 +68,15 @@ func SetNetworkNodesLastModified(networkName string) error { return nil } -// GenerateCryptoString - generates random string of n length -func GenerateCryptoString(n int) (string, error) { - const chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" - ret := make([]byte, n) - for i := range ret { - num, err := crand.Int(crand.Reader, big.NewInt(int64(len(chars)))) - if err != nil { - return "", err - } - ret[i] = chars[num.Int64()] - } - - return string(ret), nil -} - // RandomString - returns a random string in a charset func RandomString(length int) string { - const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - - var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) - - b := make([]byte, length) - for i := range b { - b[i] = charset[seededRand.Intn(len(charset))] + randombytes := make([]byte, length) + _, err := rand.Read(randombytes) + if err != nil { + logger.Log(0, "random string", err.Error()) + return "" } - return string(b) + return base32.StdEncoding.EncodeToString(randombytes)[:length] } // StringSliceContains - sees if a string slice contains a string element diff --git a/netclient/ncutils/netclientutils_test.go b/logic/util_test.go similarity index 79% rename from netclient/ncutils/netclientutils_test.go rename to logic/util_test.go index 526c3924..9dec6b81 100644 --- a/netclient/ncutils/netclientutils_test.go +++ b/logic/util_test.go @@ -1,4 +1,4 @@ -package ncutils +package logic import ( "strings" @@ -7,10 +7,10 @@ import ( "github.com/stretchr/testify/assert" ) -func TestMakeRandomString(t *testing.T) { +func TestRandomString(t *testing.T) { for testCase := 0; testCase < 100; testCase++ { for size := 2; size < 2058; size++ { - if length := len(MakeRandomString(size)); length != size { + if length := len(RandomString(size)); length != size { t.Fatalf("expected random string of size %d, got %d instead", size, length) } } @@ -18,9 +18,9 @@ func TestMakeRandomString(t *testing.T) { } func TestMakeRandomStringValid(t *testing.T) { - lengthStr := MakeRandomString(10) + lengthStr := RandomString(10) assert.Equal(t, len(lengthStr), 10) - validMqID := MakeRandomString(23) + validMqID := RandomString(23) assert.False(t, strings.Contains(validMqID, "#")) assert.False(t, strings.Contains(validMqID, "!")) assert.False(t, strings.Contains(validMqID, "\"")) diff --git a/mq/mq.go b/mq/mq.go index 7db1ffed..5e6fdb72 100644 --- a/mq/mq.go +++ b/mq/mq.go @@ -8,7 +8,7 @@ import ( mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/gravitl/netmaker/logger" - "github.com/gravitl/netmaker/netclient/ncutils" + "github.com/gravitl/netmaker/logic" "github.com/gravitl/netmaker/servercfg" ) @@ -27,7 +27,7 @@ var mqclient mqtt.Client func setMqOptions(user, password string, opts *mqtt.ClientOptions) { broker, _ := servercfg.GetMessageQueueEndpoint() opts.AddBroker(broker) - id := ncutils.MakeRandomString(23) + id := logic.RandomString(23) opts.ClientID = id opts.SetUsername(user) opts.SetPassword(password) diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index 66f27e02..9f79892d 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -2,7 +2,6 @@ package ncutils import ( "bytes" - "crypto/rand" "encoding/gob" ) @@ -32,16 +31,3 @@ func ConvertBytesToKey(data []byte) (*[32]byte, error) { } return result, err } - -// MakeRandomString - generates a random string of len n -func MakeRandomString(n int) string { - const validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - result := make([]byte, n) - if _, err := rand.Reader.Read(result); err != nil { - return "" - } - for i, b := range result { - result[i] = validChars[b%byte(len(validChars))] - } - return string(result) -}