From 1984e451d5ae2081996cbcfcc6d15dc9fde0259b Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Thu, 12 Jul 2018 16:40:45 -0700 Subject: [PATCH] Replace stringid with simple rand reader Signed-off-by: Derek McGowan --- cmd/containerd/containerd.go | 6 ++ cmd/ctr/main.go | 6 ++ pkg/util/id.go | 9 +- .../docker/docker/pkg/stringid/README.md | 1 - .../docker/docker/pkg/stringid/stringid.go | 99 ------------------- 5 files changed, 19 insertions(+), 102 deletions(-) delete mode 100644 vendor/github.com/docker/docker/pkg/stringid/README.md delete mode 100644 vendor/github.com/docker/docker/pkg/stringid/stringid.go diff --git a/cmd/containerd/containerd.go b/cmd/containerd/containerd.go index 563eb5a9b..91e519392 100644 --- a/cmd/containerd/containerd.go +++ b/cmd/containerd/containerd.go @@ -18,7 +18,9 @@ package main import ( "fmt" + "math/rand" "os" + "time" _ "github.com/containerd/containerd/diff/walking/plugin" _ "github.com/containerd/containerd/gc/scheduler" @@ -43,6 +45,10 @@ import ( "github.com/sirupsen/logrus" ) +func init() { + rand.Seed(time.Now().UnixNano()) +} + func main() { app := command.App() logrus.Warn("This customized containerd is only for CI test, DO NOT use it for distribution.") diff --git a/cmd/ctr/main.go b/cmd/ctr/main.go index 604e60e70..81e58efdf 100644 --- a/cmd/ctr/main.go +++ b/cmd/ctr/main.go @@ -18,13 +18,19 @@ package main import ( "fmt" + "math/rand" "os" + "time" ctrapp "github.com/containerd/containerd/cmd/ctr/app" cricli "github.com/containerd/cri/cli" ) +func init() { + rand.Seed(time.Now().UnixNano()) +} + func main() { app := ctrapp.New() app.Commands = append(app.Commands, cricli.Command) diff --git a/pkg/util/id.go b/pkg/util/id.go index ae7fbdcc6..11b0a70a6 100644 --- a/pkg/util/id.go +++ b/pkg/util/id.go @@ -16,9 +16,14 @@ limitations under the License. package util -import "github.com/docker/docker/pkg/stringid" +import ( + "encoding/hex" + "math/rand" +) // GenerateID generates a random unique id. func GenerateID() string { - return stringid.GenerateNonCryptoID() + b := make([]byte, 32) + rand.Read(b) + return hex.EncodeToString(b) } diff --git a/vendor/github.com/docker/docker/pkg/stringid/README.md b/vendor/github.com/docker/docker/pkg/stringid/README.md deleted file mode 100644 index 37a5098fd..000000000 --- a/vendor/github.com/docker/docker/pkg/stringid/README.md +++ /dev/null @@ -1 +0,0 @@ -This package provides helper functions for dealing with string identifiers diff --git a/vendor/github.com/docker/docker/pkg/stringid/stringid.go b/vendor/github.com/docker/docker/pkg/stringid/stringid.go deleted file mode 100644 index a0c7c42a0..000000000 --- a/vendor/github.com/docker/docker/pkg/stringid/stringid.go +++ /dev/null @@ -1,99 +0,0 @@ -// Package stringid provides helper functions for dealing with string identifiers -package stringid - -import ( - cryptorand "crypto/rand" - "encoding/hex" - "fmt" - "io" - "math" - "math/big" - "math/rand" - "regexp" - "strconv" - "strings" - "time" -) - -const shortLen = 12 - -var ( - validShortID = regexp.MustCompile("^[a-f0-9]{12}$") - validHex = regexp.MustCompile(`^[a-f0-9]{64}$`) -) - -// IsShortID determines if an arbitrary string *looks like* a short ID. -func IsShortID(id string) bool { - return validShortID.MatchString(id) -} - -// TruncateID returns a shorthand version of a string identifier for convenience. -// A collision with other shorthands is very unlikely, but possible. -// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller -// will need to use a longer prefix, or the full-length Id. -func TruncateID(id string) string { - if i := strings.IndexRune(id, ':'); i >= 0 { - id = id[i+1:] - } - if len(id) > shortLen { - id = id[:shortLen] - } - return id -} - -func generateID(r io.Reader) string { - b := make([]byte, 32) - for { - if _, err := io.ReadFull(r, b); err != nil { - panic(err) // This shouldn't happen - } - id := hex.EncodeToString(b) - // if we try to parse the truncated for as an int and we don't have - // an error then the value is all numeric and causes issues when - // used as a hostname. ref #3869 - if _, err := strconv.ParseInt(TruncateID(id), 10, 64); err == nil { - continue - } - return id - } -} - -// GenerateRandomID returns a unique id. -func GenerateRandomID() string { - return generateID(cryptorand.Reader) -} - -// GenerateNonCryptoID generates unique id without using cryptographically -// secure sources of random. -// It helps you to save entropy. -func GenerateNonCryptoID() string { - return generateID(readerFunc(rand.Read)) -} - -// ValidateID checks whether an ID string is a valid image ID. -func ValidateID(id string) error { - if ok := validHex.MatchString(id); !ok { - return fmt.Errorf("image ID %q is invalid", id) - } - return nil -} - -func init() { - // safely set the seed globally so we generate random ids. Tries to use a - // crypto seed before falling back to time. - var seed int64 - if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil { - // This should not happen, but worst-case fallback to time-based seed. - seed = time.Now().UnixNano() - } else { - seed = cryptoseed.Int64() - } - - rand.Seed(seed) -} - -type readerFunc func(p []byte) (int, error) - -func (fn readerFunc) Read(p []byte) (int, error) { - return fn(p) -}