2016-11-11 20:07:27 +00:00
|
|
|
package reference
|
|
|
|
|
2016-06-15 22:40:15 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-01-09 23:44:13 +00:00
|
|
|
"github.com/docker/distribution/digestset"
|
|
|
|
"github.com/opencontainers/go-digest"
|
2016-06-15 22:40:15 +00:00
|
|
|
)
|
|
|
|
|
2016-11-11 20:07:27 +00:00
|
|
|
var (
|
2016-06-15 22:40:15 +00:00
|
|
|
legacyDefaultDomain = "index.docker.io"
|
|
|
|
defaultDomain = "docker.io"
|
2017-01-11 18:46:48 +00:00
|
|
|
officialRepoName = "library"
|
2016-06-15 22:40:15 +00:00
|
|
|
defaultTag = "latest"
|
2016-11-11 20:07:27 +00:00
|
|
|
)
|
|
|
|
|
2017-01-11 20:58:00 +00:00
|
|
|
// normalizedNamed represents a name which has been
|
2016-06-27 23:41:28 +00:00
|
|
|
// normalized and has a familiar form. A familiar name
|
|
|
|
// is what is used in Docker UI. An example normalized
|
|
|
|
// name is "docker.io/library/ubuntu" and corresponding
|
|
|
|
// familiar name of "ubuntu".
|
2017-01-11 20:58:00 +00:00
|
|
|
type normalizedNamed interface {
|
2016-06-27 23:41:28 +00:00
|
|
|
Named
|
|
|
|
Familiar() Named
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseNormalizedNamed parses a string into a named reference
|
2016-06-15 22:40:15 +00:00
|
|
|
// transforming a familiar name from Docker UI to a fully
|
|
|
|
// qualified reference. If the value may be an identifier
|
|
|
|
// use ParseAnyReference.
|
2017-01-11 20:58:00 +00:00
|
|
|
func ParseNormalizedNamed(s string) (Named, error) {
|
2016-06-15 22:40:15 +00:00
|
|
|
if ok := anchoredIdentifierRegexp.MatchString(s); ok {
|
|
|
|
return nil, fmt.Errorf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings", s)
|
|
|
|
}
|
|
|
|
domain, remainder := splitDockerDomain(s)
|
|
|
|
var remoteName string
|
|
|
|
if tagSep := strings.IndexRune(remainder, ':'); tagSep > -1 {
|
|
|
|
remoteName = remainder[:tagSep]
|
|
|
|
} else {
|
|
|
|
remoteName = remainder
|
|
|
|
}
|
|
|
|
if strings.ToLower(remoteName) != remoteName {
|
|
|
|
return nil, errors.New("invalid reference format: repository name must be lowercase")
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:41:28 +00:00
|
|
|
ref, err := Parse(domain + "/" + remainder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-11 20:58:00 +00:00
|
|
|
named, isNamed := ref.(Named)
|
2016-06-27 23:41:28 +00:00
|
|
|
if !isNamed {
|
|
|
|
return nil, fmt.Errorf("reference %s has no name", ref.String())
|
|
|
|
}
|
|
|
|
return named, nil
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// splitDockerDomain splits a repository name to domain and remotename string.
|
|
|
|
// If no valid domain is found, the default domain is used. Repository name
|
|
|
|
// needs to be already validated before.
|
|
|
|
func splitDockerDomain(name string) (domain, remainder string) {
|
|
|
|
i := strings.IndexRune(name, '/')
|
|
|
|
if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
|
|
|
|
domain, remainder = defaultDomain, name
|
|
|
|
} else {
|
|
|
|
domain, remainder = name[:i], name[i+1:]
|
|
|
|
}
|
|
|
|
if domain == legacyDefaultDomain {
|
|
|
|
domain = defaultDomain
|
|
|
|
}
|
|
|
|
if domain == defaultDomain && !strings.ContainsRune(remainder, '/') {
|
2017-01-11 18:46:48 +00:00
|
|
|
remainder = officialRepoName + "/" + remainder
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:41:28 +00:00
|
|
|
// familiarizeName returns a shortened version of the name familiar
|
2016-06-15 22:40:15 +00:00
|
|
|
// to to the Docker UI. Familiar names have the default domain
|
|
|
|
// "docker.io" and "library/" repository prefix removed.
|
|
|
|
// For example, "docker.io/library/redis" will have the familiar
|
|
|
|
// name "redis" and "docker.io/dmcgowan/myapp" will be "dmcgowan/myapp".
|
2016-06-27 23:41:28 +00:00
|
|
|
// Returns a familiarized named only reference.
|
2017-01-09 23:44:59 +00:00
|
|
|
func familiarizeName(named namedRepository) repository {
|
2016-06-27 23:41:28 +00:00
|
|
|
repo := repository{
|
|
|
|
domain: named.Domain(),
|
|
|
|
path: named.Path(),
|
|
|
|
}
|
2016-06-15 22:40:15 +00:00
|
|
|
|
|
|
|
if repo.domain == defaultDomain {
|
|
|
|
repo.domain = ""
|
2017-01-11 18:46:48 +00:00
|
|
|
// Handle official repositories which have the pattern "library/<official repo name>"
|
|
|
|
if split := strings.Split(repo.path, "/"); len(split) == 2 && split[0] == officialRepoName {
|
|
|
|
repo.path = split[1]
|
|
|
|
}
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
2016-06-27 23:41:28 +00:00
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r reference) Familiar() Named {
|
|
|
|
return reference{
|
2017-01-09 23:44:59 +00:00
|
|
|
namedRepository: familiarizeName(r.namedRepository),
|
2016-06-27 23:41:28 +00:00
|
|
|
tag: r.tag,
|
|
|
|
digest: r.digest,
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
2016-06-27 23:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r repository) Familiar() Named {
|
|
|
|
return familiarizeName(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t taggedReference) Familiar() Named {
|
|
|
|
return taggedReference{
|
2017-01-09 23:44:59 +00:00
|
|
|
namedRepository: familiarizeName(t.namedRepository),
|
2016-06-27 23:41:28 +00:00
|
|
|
tag: t.tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c canonicalReference) Familiar() Named {
|
|
|
|
return canonicalReference{
|
2017-01-09 23:44:59 +00:00
|
|
|
namedRepository: familiarizeName(c.namedRepository),
|
2016-06-27 23:41:28 +00:00
|
|
|
digest: c.digest,
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:00 +00:00
|
|
|
// TagNameOnly adds the default tag "latest" to a reference if it only has
|
2016-11-11 20:07:27 +00:00
|
|
|
// a repo name.
|
2017-01-26 19:46:00 +00:00
|
|
|
func TagNameOnly(ref Named) Named {
|
|
|
|
if IsNameOnly(ref) {
|
2016-11-11 20:07:27 +00:00
|
|
|
namedTagged, err := WithTag(ref, defaultTag)
|
|
|
|
if err != nil {
|
|
|
|
// Default tag must be valid, to create a NamedTagged
|
|
|
|
// type with non-validated input the WithTag function
|
|
|
|
// should be used instead
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return namedTagged
|
|
|
|
}
|
2017-01-26 19:46:00 +00:00
|
|
|
return ref
|
2016-11-11 20:07:27 +00:00
|
|
|
}
|
2016-06-15 22:40:15 +00:00
|
|
|
|
|
|
|
// ParseAnyReference parses a reference string as a possible identifier,
|
|
|
|
// full digest, or familiar name.
|
|
|
|
func ParseAnyReference(ref string) (Reference, error) {
|
|
|
|
if ok := anchoredIdentifierRegexp.MatchString(ref); ok {
|
|
|
|
return digestReference("sha256:" + ref), nil
|
|
|
|
}
|
2017-01-09 23:44:13 +00:00
|
|
|
if dgst, err := digest.Parse(ref); err == nil {
|
2016-06-15 22:40:15 +00:00
|
|
|
return digestReference(dgst), nil
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:41:28 +00:00
|
|
|
return ParseNormalizedNamed(ref)
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAnyReferenceWithSet parses a reference string as a possible short
|
|
|
|
// identifier to be matched in a digest set, a full digest, or familiar name.
|
2017-01-09 23:44:13 +00:00
|
|
|
func ParseAnyReferenceWithSet(ref string, ds *digestset.Set) (Reference, error) {
|
2016-06-15 22:40:15 +00:00
|
|
|
if ok := anchoredShortIdentifierRegexp.MatchString(ref); ok {
|
|
|
|
dgst, err := ds.Lookup(ref)
|
|
|
|
if err == nil {
|
|
|
|
return digestReference(dgst), nil
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-09 23:44:13 +00:00
|
|
|
if dgst, err := digest.Parse(ref); err == nil {
|
2016-06-15 22:40:15 +00:00
|
|
|
return digestReference(dgst), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:41:28 +00:00
|
|
|
return ParseNormalizedNamed(ref)
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|