2015-01-06 00:44:03 +00:00
|
|
|
package v2
|
2014-11-13 00:39:35 +00:00
|
|
|
|
|
|
|
import (
|
2014-11-17 19:28:32 +00:00
|
|
|
"fmt"
|
2014-11-13 00:39:35 +00:00
|
|
|
"regexp"
|
2014-11-17 19:28:32 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-07-10 18:00:06 +00:00
|
|
|
// TODO(stevvooe): Move these definitions to the future "reference" package.
|
|
|
|
// While they are used with v2 definitions, their relevance expands beyond.
|
2015-02-13 21:59:50 +00:00
|
|
|
|
2014-11-17 19:28:32 +00:00
|
|
|
const (
|
2014-11-17 23:44:07 +00:00
|
|
|
// RepositoryNameTotalLengthMax is the maximum total number of characters in
|
|
|
|
// a repository name
|
2014-11-17 19:28:32 +00:00
|
|
|
RepositoryNameTotalLengthMax = 255
|
2014-11-13 00:39:35 +00:00
|
|
|
)
|
|
|
|
|
2015-10-07 23:06:53 +00:00
|
|
|
// domainLabelRegexp represents the following RFC-2396 BNF construct:
|
|
|
|
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
|
|
|
|
var domainLabelRegexp = regexp.MustCompile(`[a-z0-9](?:-*[a-z0-9])*`)
|
|
|
|
|
2015-03-05 15:11:37 +00:00
|
|
|
// RepositoryNameComponentRegexp restricts registry path component names to
|
2015-10-07 23:06:53 +00:00
|
|
|
// the allow valid hostnames according to: https://www.ietf.org/rfc/rfc2396.txt
|
|
|
|
// with the following differences:
|
|
|
|
// 1) It DOES NOT allow for fully-qualified domain names, which include a
|
|
|
|
// trailing '.', e.g. "google.com."
|
|
|
|
// 2) It DOES NOT restrict 'top-level' domain labels to start with just alpha
|
|
|
|
// characters.
|
|
|
|
// 3) It DOES allow for underscores to appear in the same situations as dots.
|
|
|
|
//
|
|
|
|
// RFC-2396 uses the BNF construct:
|
|
|
|
// hostname = *( domainlabel "." ) toplabel [ "." ]
|
|
|
|
var RepositoryNameComponentRegexp = regexp.MustCompile(
|
|
|
|
domainLabelRegexp.String() + `(?:[._]` + domainLabelRegexp.String() + `)*`)
|
2014-11-17 23:44:07 +00:00
|
|
|
|
|
|
|
// RepositoryNameComponentAnchoredRegexp is the version of
|
|
|
|
// RepositoryNameComponentRegexp which must completely match the content
|
2014-11-17 19:28:32 +00:00
|
|
|
var RepositoryNameComponentAnchoredRegexp = regexp.MustCompile(`^` + RepositoryNameComponentRegexp.String() + `$`)
|
2014-11-13 00:39:35 +00:00
|
|
|
|
2015-03-05 15:11:37 +00:00
|
|
|
// RepositoryNameRegexp builds on RepositoryNameComponentRegexp to allow
|
|
|
|
// multiple path components, separated by a forward slash.
|
|
|
|
var RepositoryNameRegexp = regexp.MustCompile(`(?:` + RepositoryNameComponentRegexp.String() + `/)*` + RepositoryNameComponentRegexp.String())
|
2014-11-13 00:39:35 +00:00
|
|
|
|
|
|
|
// TagNameRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
|
|
|
var TagNameRegexp = regexp.MustCompile(`[\w][\w.-]{0,127}`)
|
|
|
|
|
2015-07-10 18:00:06 +00:00
|
|
|
// TagNameAnchoredRegexp matches valid tag names, anchored at the start and
|
|
|
|
// end of the matched string.
|
|
|
|
var TagNameAnchoredRegexp = regexp.MustCompile("^" + TagNameRegexp.String() + "$")
|
2014-11-17 19:28:32 +00:00
|
|
|
|
|
|
|
var (
|
2015-07-10 18:00:06 +00:00
|
|
|
// ErrRepositoryNameEmpty is returned for empty, invalid repository names.
|
|
|
|
ErrRepositoryNameEmpty = fmt.Errorf("repository name must have at least one component")
|
2014-11-17 23:44:07 +00: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 19:28:32 +00:00
|
|
|
ErrRepositoryNameComponentInvalid = fmt.Errorf("repository name component must match %q", RepositoryNameComponentRegexp.String())
|
|
|
|
)
|
|
|
|
|
2015-05-19 17:25:08 +00:00
|
|
|
// ValidateRepositoryName ensures the repository name is valid for use in the
|
2014-11-17 19:28:32 +00:00
|
|
|
// 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 00:06:36 +00: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.
|
2015-05-19 17:25:08 +00:00
|
|
|
func ValidateRepositoryName(name string) error {
|
2015-07-10 18:00:06 +00:00
|
|
|
if name == "" {
|
|
|
|
return ErrRepositoryNameEmpty
|
|
|
|
}
|
|
|
|
|
2014-11-17 19:28:32 +00:00
|
|
|
if len(name) > RepositoryNameTotalLengthMax {
|
|
|
|
return ErrRepositoryNameLong
|
|
|
|
}
|
|
|
|
|
|
|
|
components := strings.Split(name, "/")
|
|
|
|
|
|
|
|
for _, component := range components {
|
|
|
|
if !RepositoryNameComponentAnchoredRegexp.MatchString(component) {
|
|
|
|
return ErrRepositoryNameComponentInvalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|