Move imageID validation to stringid pkg

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2016-12-22 13:26:30 -08:00
parent feaf5902f6
commit 9f3046f9a0
3 changed files with 18 additions and 12 deletions

View file

@ -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)
}

View file

@ -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
}

View file

@ -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