Browse Source

netclient: math.Rand -> crypto.Rand (#956)

* netclient: math.Rand -> crypto.Rand

Signed-off-by: John Sahhar <[email protected]>

* netclient: math.Rand -> crypto.Rand

* add tests

Signed-off-by: John Sahhar <[email protected]>

* netclient: math.Rand -> crypto.Rand

* add test

Signed-off-by: John Sahhar <[email protected]>
john s 3 years ago
parent
commit
d1fb0b90af

+ 2 - 2
logic/accesskeys.go

@@ -28,7 +28,7 @@ func CreateAccessKey(accesskey models.AccessKey, network models.Network) (models
 	}
 
 	if accesskey.Value == "" {
-		accesskey.Value = genKey()
+		accesskey.Value = GenKey()
 	}
 	if accesskey.Uses == 0 {
 		accesskey.Uses = 1
@@ -238,7 +238,7 @@ func genKeyName() string {
 	return strings.Join([]string{"key", entropy.Text(16)[:16]}, "-")
 }
 
-func genKey() string {
+func GenKey() string {
 	entropy, _ := rand.Int(rand.Reader, maxentropy)
 	return entropy.Text(16)[:16]
 }

+ 1 - 1
logic/accesskeys_test.go

@@ -14,7 +14,7 @@ func Test_genKeyName(t *testing.T) {
 
 func Test_genKey(t *testing.T) {
 	for i := 0; i < 100; i++ {
-		kname := genKey()
+		kname := GenKey()
 		t.Log(kname)
 		if len(kname) != 16 {
 			t.Fatalf("improper length of key name, expected 16 got :%d", len(kname))

+ 2 - 1
netclient/functions/join.go

@@ -11,6 +11,7 @@ import (
 
 	nodepb "github.com/gravitl/netmaker/grpc"
 	"github.com/gravitl/netmaker/logger"
+	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/netclient/auth"
 	"github.com/gravitl/netmaker/netclient/config"
@@ -41,7 +42,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro
 		return err
 	}
 	if cfg.Node.Password == "" {
-		cfg.Node.Password = ncutils.GenPass()
+		cfg.Node.Password = logic.GenKey()
 	}
 	var trafficPubKey, trafficPrivKey, errT = box.GenerateKey(rand.Reader) // generate traffic keys
 	if errT != nil {

+ 4 - 37
netclient/ncutils/netclientutils.go

@@ -2,13 +2,13 @@ package ncutils
 
 import (
 	"bytes"
+	"crypto/rand"
 	"crypto/tls"
 	"encoding/gob"
 	"errors"
 	"fmt"
 	"io"
 	"log"
-	"math/rand"
 	"net"
 	"net/http"
 	"os"
@@ -30,9 +30,6 @@ import (
 // Version - version of the netclient
 var Version = "dev"
 
-// src - for random strings
-var src = rand.NewSource(time.Now().UnixNano())
-
 // MAX_NAME_LENGTH - maximum node name length
 const MAX_NAME_LENGTH = 62
 
@@ -127,23 +124,6 @@ func IsEmptyRecord(err error) bool {
 	return strings.Contains(err.Error(), NO_DB_RECORD) || strings.Contains(err.Error(), NO_DB_RECORDS)
 }
 
-//generate an access key value
-// GenPass - generates a pass
-func GenPass() string {
-
-	var seededRand *rand.Rand = rand.New(
-		rand.NewSource(time.Now().UnixNano()))
-
-	length := 16
-	charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
-
-	b := make([]byte, length)
-	for i := range b {
-		b[i] = charset[seededRand.Intn(len(charset))]
-	}
-	return string(b)
-}
-
 // GetPublicIP - gets public ip
 func GetPublicIP() (string, error) {
 
@@ -592,20 +572,7 @@ func ServerAddrSliceContains(slice []models.ServerAddr, item models.ServerAddr)
 
 // MakeRandomString - generates a random string of len n
 func MakeRandomString(n int) string {
-	sb := strings.Builder{}
-	sb.Grow(n)
-	// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
-	for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
-		if remain == 0 {
-			cache, remain = src.Int63(), letterIdxMax
-		}
-		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
-			sb.WriteByte(letterBytes[idx])
-			i--
-		}
-		cache >>= letterIdxBits
-		remain--
-	}
-
-	return sb.String()
+	result := make([]byte, n)
+	rand.Reader.Read(result)
+	return string(result)
 }

+ 15 - 0
netclient/ncutils/netclientutils_test.go

@@ -0,0 +1,15 @@
+package ncutils
+
+import "testing"
+
+func TestMakeRandomString(t *testing.T) {
+        for testCase := 0; testCase < 100; testCase++ {
+                for size := 2; size < 2058; size++ {
+                        if length := len(MakeRandomString(size)); length != size {
+                                t.Fatalf("expected random string of size %d, got %d instead", size, length)
+                        }
+                }
+        }
+}
+
+