2015-01-06 01:44:03 +01:00
|
|
|
package v2
|
2014-11-13 01:39:35 +01:00
|
|
|
|
|
|
|
import (
|
2014-11-17 20:28:32 +01:00
|
|
|
"fmt"
|
2014-11-13 01:39:35 +01:00
|
|
|
"regexp"
|
2014-11-17 20:28:32 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-02-13 22:59:50 +01:00
|
|
|
// TODO(stevvooe): Move these definitions back to an exported package. While
|
|
|
|
// they are used with v2 definitions, their relevance expands beyond.
|
|
|
|
// "distribution/names" is a candidate package.
|
|
|
|
|
2014-11-17 20:28:32 +01:00
|
|
|
const (
|
2014-11-18 00:44:07 +01:00
|
|
|
// RepositoryNameComponentMinLength is the minimum number of characters in a
|
|
|
|
// single repository name slash-delimited component
|
2014-11-17 20:28:32 +01:00
|
|
|
RepositoryNameComponentMinLength = 2
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// RepositoryNameComponentMaxLength is the maximum number of characters in a
|
|
|
|
// single repository name slash-delimited component
|
2014-11-17 20:28:32 +01:00
|
|
|
RepositoryNameComponentMaxLength = 30
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// RepositoryNameMinComponents is the minimum number of slash-delimited
|
|
|
|
// components that a repository name must have
|
2015-01-28 23:51:02 +01:00
|
|
|
RepositoryNameMinComponents = 1
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// RepositoryNameMaxComponents is the maximum number of slash-delimited
|
|
|
|
// components that a repository name must have
|
|
|
|
RepositoryNameMaxComponents = 5
|
|
|
|
|
|
|
|
// RepositoryNameTotalLengthMax is the maximum total number of characters in
|
|
|
|
// a repository name
|
2014-11-17 20:28:32 +01:00
|
|
|
RepositoryNameTotalLengthMax = 255
|
2014-11-13 01:39:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RepositoryNameComponentRegexp restricts registtry path components names to
|
|
|
|
// start with at least two letters or numbers, with following parts able to
|
|
|
|
// separated by one period, dash or underscore.
|
2014-11-17 20:28:32 +01:00
|
|
|
var RepositoryNameComponentRegexp = regexp.MustCompile(`[a-z0-9]+(?:[._-][a-z0-9]+)*`)
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// RepositoryNameComponentAnchoredRegexp is the version of
|
|
|
|
// RepositoryNameComponentRegexp which must completely match the content
|
2014-11-17 20:28:32 +01:00
|
|
|
var RepositoryNameComponentAnchoredRegexp = regexp.MustCompile(`^` + RepositoryNameComponentRegexp.String() + `$`)
|
2014-11-13 01:39:35 +01:00
|
|
|
|
2015-01-28 23:51:02 +01:00
|
|
|
// RepositoryNameRegexp builds on RepositoryNameComponentRegexp to allow 1 to
|
2014-11-13 01:39:35 +01:00
|
|
|
// 5 path components, separated by a forward slash.
|
2015-01-28 23:51:02 +01:00
|
|
|
var RepositoryNameRegexp = regexp.MustCompile(`(?:` + RepositoryNameComponentRegexp.String() + `/){0,4}` + RepositoryNameComponentRegexp.String())
|
2014-11-13 01:39:35 +01:00
|
|
|
|
|
|
|
// TagNameRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
|
|
|
var TagNameRegexp = regexp.MustCompile(`[\w][\w.-]{0,127}`)
|
|
|
|
|
|
|
|
// TODO(stevvooe): Contribute these exports back to core, so they are shared.
|
2014-11-17 20:28:32 +01:00
|
|
|
|
|
|
|
var (
|
2014-11-18 00:44:07 +01:00
|
|
|
// ErrRepositoryNameComponentShort is returned when a repository name
|
|
|
|
// contains a component which is shorter than
|
|
|
|
// RepositoryNameComponentMinLength
|
2014-11-17 20:28:32 +01:00
|
|
|
ErrRepositoryNameComponentShort = fmt.Errorf("respository name component must be %v or more characters", RepositoryNameComponentMinLength)
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// ErrRepositoryNameComponentLong is returned when a repository name
|
|
|
|
// contains a component which is longer than
|
|
|
|
// RepositoryNameComponentMaxLength
|
|
|
|
ErrRepositoryNameComponentLong = fmt.Errorf("respository name component must be %v characters or less", RepositoryNameComponentMaxLength)
|
|
|
|
|
|
|
|
// ErrRepositoryNameMissingComponents is returned when a repository name
|
|
|
|
// contains fewer than RepositoryNameMinComponents components
|
2014-11-17 20:28:32 +01:00
|
|
|
ErrRepositoryNameMissingComponents = fmt.Errorf("repository name must have at least %v components", RepositoryNameMinComponents)
|
2014-11-18 00:44:07 +01:00
|
|
|
|
|
|
|
// ErrRepositoryNameTooManyComponents is returned when a repository name
|
|
|
|
// contains more than RepositoryNameMaxComponents components
|
2014-11-17 20:28:32 +01:00
|
|
|
ErrRepositoryNameTooManyComponents = fmt.Errorf("repository name %v or less components", RepositoryNameMaxComponents)
|
|
|
|
|
2014-11-18 00:44:07 +01:00
|
|
|
// ErrRepositoryNameLong is returned when a repository name is longer than
|
|
|
|
// RepositoryNameTotalLengthMax
|
|
|
|
ErrRepositoryNameLong = fmt.Errorf("repository name must not be more than %v characters", RepositoryNameTotalLengthMax)
|
|
|
|
|
|
|
|
// ErrRepositoryNameComponentInvalid is returned when a repository name does
|
|
|
|
// not match RepositoryNameComponentRegexp
|
2014-11-17 20:28:32 +01:00
|
|
|
ErrRepositoryNameComponentInvalid = fmt.Errorf("repository name component must match %q", RepositoryNameComponentRegexp.String())
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateRespositoryName ensures the repository name is valid for use in the
|
|
|
|
// registry. This function accepts a superset of what might be accepted by
|
|
|
|
// docker core or docker hub. If the name does not pass validation, an error,
|
|
|
|
// describing the conditions, is returned.
|
2015-02-27 01:06:36 +01:00
|
|
|
//
|
|
|
|
// Effectively, the name should comply with the following grammar:
|
|
|
|
//
|
|
|
|
// alpha-numeric := /[a-z0-9]+/
|
|
|
|
// separator := /[._-]/
|
|
|
|
// component := alpha-numeric [separator alpha-numeric]*
|
|
|
|
// namespace := component ['/' component]*
|
|
|
|
//
|
|
|
|
// The result of the production, known as the "namespace", should be limited
|
|
|
|
// to 255 characters.
|
2014-11-17 20:28:32 +01:00
|
|
|
func ValidateRespositoryName(name string) error {
|
|
|
|
if len(name) > RepositoryNameTotalLengthMax {
|
|
|
|
return ErrRepositoryNameLong
|
|
|
|
}
|
|
|
|
|
|
|
|
components := strings.Split(name, "/")
|
|
|
|
|
|
|
|
if len(components) < RepositoryNameMinComponents {
|
|
|
|
return ErrRepositoryNameMissingComponents
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(components) > RepositoryNameMaxComponents {
|
|
|
|
return ErrRepositoryNameTooManyComponents
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, component := range components {
|
|
|
|
if len(component) < RepositoryNameComponentMinLength {
|
|
|
|
return ErrRepositoryNameComponentShort
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(component) > RepositoryNameComponentMaxLength {
|
|
|
|
return ErrRepositoryNameComponentLong
|
|
|
|
}
|
|
|
|
|
|
|
|
if !RepositoryNameComponentAnchoredRegexp.MatchString(component) {
|
|
|
|
return ErrRepositoryNameComponentInvalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|