From 9f3046f9a03f1a710bdb7fcddd37ff50e71e31c3 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 22 Dec 2016 13:26:30 -0800 Subject: [PATCH] Move imageID validation to stringid pkg Signed-off-by: Tonis Tiigi --- image/v1/imagev1.go | 10 ++-------- pkg/stringid/stringid.go | 14 +++++++++++++- reference/reference.go | 6 +++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/image/v1/imagev1.go b/image/v1/imagev1.go index 9c167a2ce9..b52b447e82 100644 --- a/image/v1/imagev1.go +++ b/image/v1/imagev1.go @@ -2,9 +2,7 @@ package v1 import ( "encoding/json" - "fmt" "reflect" - "regexp" "strings" "github.com/Sirupsen/logrus" @@ -12,10 +10,9 @@ import ( "github.com/docker/docker/api/types/versions" "github.com/docker/docker/image" "github.com/docker/docker/layer" + "github.com/docker/docker/pkg/stringid" ) -var validHex = regexp.MustCompile(`^([a-f0-9]{64})$`) - // noFallbackMinVersion is the minimum version for which v1compatibility // information will not be marshaled through the Image struct to remove // blank fields. @@ -149,8 +146,5 @@ func rawJSON(value interface{}) *json.RawMessage { // 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 + return stringid.ValidateID(id) } diff --git a/pkg/stringid/stringid.go b/pkg/stringid/stringid.go index fa35d8bad5..82f85596c7 100644 --- a/pkg/stringid/stringid.go +++ b/pkg/stringid/stringid.go @@ -4,6 +4,7 @@ package stringid import ( "crypto/rand" "encoding/hex" + "fmt" "io" "regexp" "strconv" @@ -14,7 +15,10 @@ import ( const shortLen = 12 -var validShortID = regexp.MustCompile("^[a-z0-9]{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 { @@ -67,3 +71,11 @@ func GenerateRandomID() string { func GenerateNonCryptoID() string { return generateID(false) } + +// 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 +} diff --git a/reference/reference.go b/reference/reference.go index 996fc50704..15854e5a5b 100644 --- a/reference/reference.go +++ b/reference/reference.go @@ -7,7 +7,7 @@ import ( "github.com/docker/distribution/digest" distreference "github.com/docker/distribution/reference" - "github.com/docker/docker/image/v1" + "github.com/docker/docker/pkg/stringid" ) const ( @@ -163,7 +163,7 @@ func IsNameOnly(ref Named) bool { // ParseIDOrReference parses string for an image ID or a reference. ID can be // without a default prefix. func ParseIDOrReference(idOrRef string) (digest.Digest, Named, error) { - if err := v1.ValidateID(idOrRef); err == nil { + if err := stringid.ValidateID(idOrRef); err == nil { idOrRef = "sha256:" + idOrRef } if dgst, err := digest.ParseDigest(idOrRef); err == nil { @@ -209,7 +209,7 @@ func normalize(name string) (string, error) { } func validateName(name string) error { - if err := v1.ValidateID(name); err == nil { + if err := stringid.ValidateID(name); err == nil { return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name) } return nil