Forráskód Böngészése

Merge pull request #13070 from shishir-a412ed/cleanup_cmd_tag

Use distribution's ValidateRepositoryName for remote name validation.
Sebastiaan van Stijn 10 éve
szülő
commit
206acbcf89

+ 3 - 3
integration-cli/docker_cli_tag_test.go

@@ -111,20 +111,20 @@ func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) {
 	}
 	}
 }
 }
 
 
-func (s *DockerSuite) TestTagWithSuffixHyphen(c *check.C) {
+func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
 	}
 	}
 	// test repository name begin with '-'
 	// test repository name begin with '-'
 	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "-busybox:test")
 	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "-busybox:test")
 	out, _, err := runCommandWithOutput(tagCmd)
 	out, _, err := runCommandWithOutput(tagCmd)
-	if err == nil || !strings.Contains(out, "Invalid repository name (-busybox). Cannot begin or end with a hyphen") {
+	if err == nil || !strings.Contains(out, "repository name component must match") {
 		c.Fatal("tag a name begin with '-' should failed")
 		c.Fatal("tag a name begin with '-' should failed")
 	}
 	}
 	// test namespace name begin with '-'
 	// test namespace name begin with '-'
 	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-test/busybox:test")
 	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-test/busybox:test")
 	out, _, err = runCommandWithOutput(tagCmd)
 	out, _, err = runCommandWithOutput(tagCmd)
-	if err == nil || !strings.Contains(out, "Invalid namespace name (-test). Cannot begin or end with a hyphen") {
+	if err == nil || !strings.Contains(out, "repository name component must match") {
 		c.Fatal("tag a name begin with '-' should failed")
 		c.Fatal("tag a name begin with '-' should failed")
 	}
 	}
 	// test index name begin wiht '-'
 	// test index name begin wiht '-'

+ 7 - 35
registry/config.go

@@ -6,9 +6,9 @@ import (
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 	"net/url"
 	"net/url"
-	"regexp"
 	"strings"
 	"strings"
 
 
+	"github.com/docker/distribution/registry/api/v2"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/image"
 	"github.com/docker/docker/opts"
 	"github.com/docker/docker/opts"
 	flag "github.com/docker/docker/pkg/mflag"
 	flag "github.com/docker/docker/pkg/mflag"
@@ -32,8 +32,6 @@ const (
 var (
 var (
 	ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
 	ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
 	emptyServiceConfig       = NewServiceConfig(nil)
 	emptyServiceConfig       = NewServiceConfig(nil)
-	validNamespaceChars      = regexp.MustCompile(`^([a-z0-9-_]*)$`)
-	validRepo                = regexp.MustCompile(`^([a-z0-9-_.]+)$`)
 )
 )
 
 
 func IndexServerAddress() string {
 func IndexServerAddress() string {
@@ -206,42 +204,16 @@ func ValidateIndexName(val string) (string, error) {
 }
 }
 
 
 func validateRemoteName(remoteName string) error {
 func validateRemoteName(remoteName string) error {
-	var (
-		namespace string
-		name      string
-	)
-	nameParts := strings.SplitN(remoteName, "/", 2)
-	if len(nameParts) < 2 {
-		namespace = "library"
-		name = nameParts[0]
+
+	if !strings.Contains(remoteName, "/") {
 
 
 		// the repository name must not be a valid image ID
 		// the repository name must not be a valid image ID
-		if err := image.ValidateID(name); err == nil {
-			return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name)
+		if err := image.ValidateID(remoteName); err == nil {
+			return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", remoteName)
 		}
 		}
-	} else {
-		namespace = nameParts[0]
-		name = nameParts[1]
-	}
-	if !validNamespaceChars.MatchString(namespace) {
-		return fmt.Errorf("Invalid namespace name (%s). Only [a-z0-9-_] are allowed.", namespace)
-	}
-	if len(namespace) < 2 || len(namespace) > 255 {
-		return fmt.Errorf("Invalid namespace name (%s). Cannot be fewer than 2 or more than 255 characters.", namespace)
-	}
-	if strings.HasPrefix(namespace, "-") || strings.HasSuffix(namespace, "-") {
-		return fmt.Errorf("Invalid namespace name (%s). Cannot begin or end with a hyphen.", namespace)
 	}
 	}
-	if strings.Contains(namespace, "--") {
-		return fmt.Errorf("Invalid namespace name (%s). Cannot contain consecutive hyphens.", namespace)
-	}
-	if !validRepo.MatchString(name) {
-		return fmt.Errorf("Invalid repository name (%s), only [a-z0-9-_.] are allowed", name)
-	}
-	if strings.HasPrefix(name, "-") || strings.HasSuffix(name, "-") {
-		return fmt.Errorf("Invalid repository name (%s). Cannot begin or end with a hyphen.", name)
-	}
-	return nil
+
+	return v2.ValidateRepositoryName(remoteName)
 }
 }
 
 
 func validateNoSchema(reposName string) error {
 func validateNoSchema(reposName string) error {

+ 5 - 3
registry/registry_test.go

@@ -757,9 +757,6 @@ func TestValidRemoteName(t *testing.T) {
 		// Allow embedded hyphens.
 		// Allow embedded hyphens.
 		"docker-rules/docker",
 		"docker-rules/docker",
 
 
-		// Allow underscores everywhere (as opposed to hyphens).
-		"____/____",
-
 		//Username doc and image name docker being tested.
 		//Username doc and image name docker being tested.
 		"doc/docker",
 		"doc/docker",
 	}
 	}
@@ -784,6 +781,11 @@ func TestValidRemoteName(t *testing.T) {
 		"docker-/docker",
 		"docker-/docker",
 		"-docker-/docker",
 		"-docker-/docker",
 
 
+		// Don't allow underscores everywhere (as opposed to hyphens).
+		"____/____",
+
+		"_docker/_docker",
+
 		// Disallow consecutive hyphens.
 		// Disallow consecutive hyphens.
 		"dock--er/docker",
 		"dock--er/docker",