Skip to content

Commit b830100

Browse files
author
Shishir Mahajan
committed
Use distribution's ValidateRepositoryName for remote name validation.
Signed-off-by: Shishir Mahajan <shishir.mahajan@redhat.com>
1 parent 85d3b75 commit b830100

File tree

3 files changed

+15
-41
lines changed

3 files changed

+15
-41
lines changed

integration-cli/docker_cli_tag_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,20 @@ func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) {
111111
}
112112
}
113113

114-
func (s *DockerSuite) TestTagWithSuffixHyphen(c *check.C) {
114+
func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
115115
if err := pullImageIfNotExist("busybox:latest"); err != nil {
116116
c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
117117
}
118118
// test repository name begin with '-'
119119
tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "-busybox:test")
120120
out, _, err := runCommandWithOutput(tagCmd)
121-
if err == nil || !strings.Contains(out, "Invalid repository name (-busybox). Cannot begin or end with a hyphen") {
121+
if err == nil || !strings.Contains(out, "repository name component must match") {
122122
c.Fatal("tag a name begin with '-' should failed")
123123
}
124124
// test namespace name begin with '-'
125125
tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-test/busybox:test")
126126
out, _, err = runCommandWithOutput(tagCmd)
127-
if err == nil || !strings.Contains(out, "Invalid namespace name (-test). Cannot begin or end with a hyphen") {
127+
if err == nil || !strings.Contains(out, "repository name component must match") {
128128
c.Fatal("tag a name begin with '-' should failed")
129129
}
130130
// test index name begin wiht '-'

registry/config.go

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"fmt"
77
"net"
88
"net/url"
9-
"regexp"
109
"strings"
1110

11+
"github.com/docker/distribution/registry/api/v2"
1212
"github.com/docker/docker/image"
1313
"github.com/docker/docker/opts"
1414
flag "github.com/docker/docker/pkg/mflag"
@@ -32,8 +32,6 @@ const (
3232
var (
3333
ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
3434
emptyServiceConfig = NewServiceConfig(nil)
35-
validNamespaceChars = regexp.MustCompile(`^([a-z0-9-_]*)$`)
36-
validRepo = regexp.MustCompile(`^([a-z0-9-_.]+)$`)
3735
)
3836

3937
func IndexServerAddress() string {
@@ -206,42 +204,16 @@ func ValidateIndexName(val string) (string, error) {
206204
}
207205

208206
func validateRemoteName(remoteName string) error {
209-
var (
210-
namespace string
211-
name string
212-
)
213-
nameParts := strings.SplitN(remoteName, "/", 2)
214-
if len(nameParts) < 2 {
215-
namespace = "library"
216-
name = nameParts[0]
207+
208+
if !strings.Contains(remoteName, "/") {
217209

218210
// the repository name must not be a valid image ID
219-
if err := image.ValidateID(name); err == nil {
220-
return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name)
211+
if err := image.ValidateID(remoteName); err == nil {
212+
return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", remoteName)
221213
}
222-
} else {
223-
namespace = nameParts[0]
224-
name = nameParts[1]
225-
}
226-
if !validNamespaceChars.MatchString(namespace) {
227-
return fmt.Errorf("Invalid namespace name (%s). Only [a-z0-9-_] are allowed.", namespace)
228-
}
229-
if len(namespace) < 2 || len(namespace) > 255 {
230-
return fmt.Errorf("Invalid namespace name (%s). Cannot be fewer than 2 or more than 255 characters.", namespace)
231-
}
232-
if strings.HasPrefix(namespace, "-") || strings.HasSuffix(namespace, "-") {
233-
return fmt.Errorf("Invalid namespace name (%s). Cannot begin or end with a hyphen.", namespace)
234214
}
235-
if strings.Contains(namespace, "--") {
236-
return fmt.Errorf("Invalid namespace name (%s). Cannot contain consecutive hyphens.", namespace)
237-
}
238-
if !validRepo.MatchString(name) {
239-
return fmt.Errorf("Invalid repository name (%s), only [a-z0-9-_.] are allowed", name)
240-
}
241-
if strings.HasPrefix(name, "-") || strings.HasSuffix(name, "-") {
242-
return fmt.Errorf("Invalid repository name (%s). Cannot begin or end with a hyphen.", name)
243-
}
244-
return nil
215+
216+
return v2.ValidateRepositoryName(remoteName)
245217
}
246218

247219
func validateNoSchema(reposName string) error {

registry/registry_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,6 @@ func TestValidRemoteName(t *testing.T) {
742742
// Allow embedded hyphens.
743743
"docker-rules/docker",
744744

745-
// Allow underscores everywhere (as opposed to hyphens).
746-
"____/____",
747-
748745
//Username doc and image name docker being tested.
749746
"doc/docker",
750747
}
@@ -769,6 +766,11 @@ func TestValidRemoteName(t *testing.T) {
769766
"docker-/docker",
770767
"-docker-/docker",
771768

769+
// Don't allow underscores everywhere (as opposed to hyphens).
770+
"____/____",
771+
772+
"_docker/_docker",
773+
772774
// Disallow consecutive hyphens.
773775
"dock--er/docker",
774776

0 commit comments

Comments
 (0)