diff --git a/cli/command/container/formatter_stats_test.go b/cli/command/container/formatter_stats_test.go index 8569ef4de4..91c69a5d7c 100644 --- a/cli/command/container/formatter_stats_test.go +++ b/cli/command/container/formatter_stats_test.go @@ -5,13 +5,13 @@ import ( "testing" "github.com/docker/cli/cli/command/formatter" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/cli/internal/test" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestContainerStatsContext(t *testing.T) { - containerID := stringid.GenerateRandomID() + containerID := test.RandomID() var ctx statsContext tt := []struct { diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 3380a2f7e5..6974584003 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -13,7 +13,6 @@ import ( "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types/container" - "github.com/docker/docker/pkg/stringid" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -21,7 +20,7 @@ import ( ) func TestContainerPsContext(t *testing.T) { - containerID := stringid.GenerateRandomID() + containerID := test.RandomID() unix := time.Now().Add(-65 * time.Second).Unix() var ctx ContainerContext diff --git a/cli/command/formatter/image_test.go b/cli/command/formatter/image_test.go index 6f1e9ce757..bb792f0439 100644 --- a/cli/command/formatter/image_test.go +++ b/cli/command/formatter/image_test.go @@ -9,13 +9,12 @@ import ( "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types/image" - "github.com/docker/docker/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestImageContext(t *testing.T) { - imageID := stringid.GenerateRandomID() + imageID := test.RandomID() unix := time.Now().Unix() zeroTime := int64(-62135596800) diff --git a/cli/command/formatter/volume_test.go b/cli/command/formatter/volume_test.go index 212c47d499..cae16c7cd1 100644 --- a/cli/command/formatter/volume_test.go +++ b/cli/command/formatter/volume_test.go @@ -12,13 +12,12 @@ import ( "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types/volume" - "github.com/docker/docker/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestVolumeContext(t *testing.T) { - volumeName := stringid.GenerateRandomID() + volumeName := test.RandomID() var ctx volumeContext cases := []struct { diff --git a/cli/command/image/build/context.go b/cli/command/image/build/context.go index 4a6abf1515..362d68a582 100644 --- a/cli/command/image/build/context.go +++ b/cli/command/image/build/context.go @@ -4,6 +4,8 @@ import ( "archive/tar" "bufio" "bytes" + "crypto/rand" + "encoding/hex" "fmt" "io" "net/http" @@ -18,7 +20,6 @@ import ( "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" - "github.com/docker/docker/pkg/stringid" "github.com/moby/go-archive" "github.com/moby/go-archive/compression" "github.com/moby/patternmatcher" @@ -379,7 +380,7 @@ func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCl return nil, "", err } now := time.Now() - randomName := ".dockerfile." + stringid.GenerateRandomID()[:20] + randomName := ".dockerfile." + randomSuffix() buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{ // Add the dockerfile with a random filename @@ -422,6 +423,15 @@ func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCl return buildCtx, randomName, nil } +// randomSuffix returns a unique, 20-character ID consisting of a-z, 0-9. +func randomSuffix() string { + b := make([]byte, 32) + if _, err := rand.Read(b); err != nil { + panic(err) // This shouldn't happen + } + return hex.EncodeToString(b)[:20] +} + // Compress the build context for sending to the API func Compress(buildCtx io.ReadCloser) (io.ReadCloser, error) { pipeReader, pipeWriter := io.Pipe() diff --git a/cli/command/image/formatter_history_test.go b/cli/command/image/formatter_history_test.go index 3a6eeb976d..611f7d5067 100644 --- a/cli/command/image/formatter_history_test.go +++ b/cli/command/image/formatter_history_test.go @@ -10,7 +10,6 @@ import ( "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types/image" - "github.com/docker/docker/pkg/stringid" "gotest.tools/v3/assert" ) @@ -21,7 +20,7 @@ type historyCase struct { } func TestHistoryContext_ID(t *testing.T) { - id := stringid.GenerateRandomID() + id := test.RandomID() var ctx historyContext cases := []historyCase{ diff --git a/cli/command/network/formatter_test.go b/cli/command/network/formatter_test.go index 2e02a4739e..ddeeb895fd 100644 --- a/cli/command/network/formatter_test.go +++ b/cli/command/network/formatter_test.go @@ -14,13 +14,12 @@ import ( "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types/network" - "github.com/docker/docker/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestNetworkContext(t *testing.T) { - networkID := stringid.GenerateRandomID() + networkID := test.RandomID() var ctx networkContext cases := []struct { diff --git a/cli/command/node/formatter_test.go b/cli/command/node/formatter_test.go index c8e4658e77..8983f337a6 100644 --- a/cli/command/node/formatter_test.go +++ b/cli/command/node/formatter_test.go @@ -14,13 +14,12 @@ import ( "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/system" - "github.com/docker/docker/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestNodeContext(t *testing.T) { - nodeID := stringid.GenerateRandomID() + nodeID := test.RandomID() var ctx nodeContext cases := []struct { diff --git a/cli/command/plugin/formatter_test.go b/cli/command/plugin/formatter_test.go index 7442c093de..63f1ce29e9 100644 --- a/cli/command/plugin/formatter_test.go +++ b/cli/command/plugin/formatter_test.go @@ -12,13 +12,12 @@ import ( "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/internal/test" "github.com/docker/docker/api/types" - "github.com/docker/docker/pkg/stringid" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestPluginContext(t *testing.T) { - pluginID := stringid.GenerateRandomID() + pluginID := test.RandomID() var ctx pluginContext cases := []struct { diff --git a/cli/command/trust/formatter_test.go b/cli/command/trust/formatter_test.go index db1534526f..4c0c194f4c 100644 --- a/cli/command/trust/formatter_test.go +++ b/cli/command/trust/formatter_test.go @@ -5,13 +5,13 @@ import ( "testing" "github.com/docker/cli/cli/command/formatter" - "github.com/docker/docker/pkg/stringid" + "github.com/docker/cli/internal/test" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestTrustTag(t *testing.T) { - digest := stringid.GenerateRandomID() + digest := test.RandomID() trustedTag := "tag" var ctx trustTagContext diff --git a/internal/test/randomid.go b/internal/test/randomid.go new file mode 100644 index 0000000000..fd5ed2ca73 --- /dev/null +++ b/internal/test/randomid.go @@ -0,0 +1,15 @@ +package test + +import ( + "crypto/rand" + "encoding/hex" +) + +// RandomID returns a unique, 64-character ID consisting of a-z, 0-9. +func RandomID() string { + b := make([]byte, 32) + if _, err := rand.Read(b); err != nil { + panic(err) // This shouldn't happen + } + return hex.EncodeToString(b) +} 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 919cd5a500..0000000000 --- a/vendor/github.com/docker/docker/pkg/stringid/stringid.go +++ /dev/null @@ -1,63 +0,0 @@ -// Package stringid provides helper functions for dealing with string identifiers -package stringid - -import ( - "crypto/rand" - "encoding/hex" - "strings" -) - -const ( - shortLen = 12 - fullLen = 64 -) - -// 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 -} - -// GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9. -// It guarantees that the ID, when truncated ([TruncateID]) does not consist -// of numbers only, so that the truncated ID can be used as hostname for -// containers. -func GenerateRandomID() string { - b := make([]byte, 32) - for { - if _, err := rand.Read(b); err != nil { - panic(err) // This shouldn't happen - } - id := hex.EncodeToString(b) - - // make sure that the truncated ID does not consist of only numeric - // characters, as it's used as default hostname for containers. - // - // See: - // - https://github.com/moby/moby/issues/3869 - // - https://bugzilla.redhat.com/show_bug.cgi?id=1059122 - if allNum(id[:shortLen]) { - // all numbers; try again - continue - } - return id - } -} - -// allNum checks whether id consists of only numbers (0-9). -func allNum(id string) bool { - for _, c := range []byte(id) { - if c > '9' || c < '0' { - return false - } - } - return true -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 1abd3baaaa..c8d13ac032 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -102,7 +102,6 @@ github.com/docker/docker/pkg/process github.com/docker/docker/pkg/progress github.com/docker/docker/pkg/stdcopy github.com/docker/docker/pkg/streamformatter -github.com/docker/docker/pkg/stringid github.com/docker/docker/registry # github.com/docker/docker-credential-helpers v0.9.3 ## explicit; go 1.21