Browse code

Move imageID validation to stringid pkg

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Tonis Tiigi authored on 2016/12/23 06:26:30
Showing 3 changed files
... ...
@@ -2,9 +2,7 @@ package v1
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"fmt"
6 5
 	"reflect"
7
-	"regexp"
8 6
 	"strings"
9 7
 
10 8
 	"github.com/Sirupsen/logrus"
... ...
@@ -12,10 +10,9 @@ import (
12 12
 	"github.com/docker/docker/api/types/versions"
13 13
 	"github.com/docker/docker/image"
14 14
 	"github.com/docker/docker/layer"
15
+	"github.com/docker/docker/pkg/stringid"
15 16
 )
16 17
 
17
-var validHex = regexp.MustCompile(`^([a-f0-9]{64})$`)
18
-
19 18
 // noFallbackMinVersion is the minimum version for which v1compatibility
20 19
 // information will not be marshaled through the Image struct to remove
21 20
 // blank fields.
... ...
@@ -149,8 +146,5 @@ func rawJSON(value interface{}) *json.RawMessage {
149 149
 
150 150
 // ValidateID checks whether an ID string is a valid image ID.
151 151
 func ValidateID(id string) error {
152
-	if ok := validHex.MatchString(id); !ok {
153
-		return fmt.Errorf("image ID %q is invalid", id)
154
-	}
155
-	return nil
152
+	return stringid.ValidateID(id)
156 153
 }
... ...
@@ -4,6 +4,7 @@ package stringid
4 4
 import (
5 5
 	"crypto/rand"
6 6
 	"encoding/hex"
7
+	"fmt"
7 8
 	"io"
8 9
 	"regexp"
9 10
 	"strconv"
... ...
@@ -14,7 +15,10 @@ import (
14 14
 
15 15
 const shortLen = 12
16 16
 
17
-var validShortID = regexp.MustCompile("^[a-z0-9]{12}$")
17
+var (
18
+	validShortID = regexp.MustCompile("^[a-f0-9]{12}$")
19
+	validHex     = regexp.MustCompile(`^[a-f0-9]{64}$`)
20
+)
18 21
 
19 22
 // IsShortID determines if an arbitrary string *looks like* a short ID.
20 23
 func IsShortID(id string) bool {
... ...
@@ -67,3 +71,11 @@ func GenerateRandomID() string {
67 67
 func GenerateNonCryptoID() string {
68 68
 	return generateID(false)
69 69
 }
70
+
71
+// ValidateID checks whether an ID string is a valid image ID.
72
+func ValidateID(id string) error {
73
+	if ok := validHex.MatchString(id); !ok {
74
+		return fmt.Errorf("image ID %q is invalid", id)
75
+	}
76
+	return nil
77
+}
... ...
@@ -7,7 +7,7 @@ import (
7 7
 
8 8
 	"github.com/docker/distribution/digest"
9 9
 	distreference "github.com/docker/distribution/reference"
10
-	"github.com/docker/docker/image/v1"
10
+	"github.com/docker/docker/pkg/stringid"
11 11
 )
12 12
 
13 13
 const (
... ...
@@ -163,7 +163,7 @@ func IsNameOnly(ref Named) bool {
163 163
 // ParseIDOrReference parses string for an image ID or a reference. ID can be
164 164
 // without a default prefix.
165 165
 func ParseIDOrReference(idOrRef string) (digest.Digest, Named, error) {
166
-	if err := v1.ValidateID(idOrRef); err == nil {
166
+	if err := stringid.ValidateID(idOrRef); err == nil {
167 167
 		idOrRef = "sha256:" + idOrRef
168 168
 	}
169 169
 	if dgst, err := digest.ParseDigest(idOrRef); err == nil {
... ...
@@ -209,7 +209,7 @@ func normalize(name string) (string, error) {
209 209
 }
210 210
 
211 211
 func validateName(name string) error {
212
-	if err := v1.ValidateID(name); err == nil {
212
+	if err := stringid.ValidateID(name); err == nil {
213 213
 		return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name)
214 214
 	}
215 215
 	return nil