2016-11-11 20:07:27 +00:00
|
|
|
package reference
|
|
|
|
|
2016-06-15 22:40:15 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-01-09 23:44:13 +00:00
|
|
|
"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 {
|
2020-10-05 01:17:37 +00:00
|
|
|
return nil, fmt.Errorf("invalid reference format: repository name (%s) must be lowercase", remoteName)
|
2016-06-15 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-10 22:51:58 +00:00
|
|
|
// ParseDockerRef normalizes the image reference following the docker convention. This is added
|
|
|
|
// mainly for backward compatibility.
|
|
|
|
// The reference returned can only be either tagged or digested. For reference contains both tag
|
|
|
|
// and digest, the function returns digested reference, e.g. docker.io/library/busybox:latest@
|
|
|
|
// sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa will be returned as
|
|
|
|
// docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa.
|
|
|
|
func ParseDockerRef(ref string) (Named, error) {
|
|
|
|
named, err := ParseNormalizedNamed(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, ok := named.(NamedTagged); ok {
|
|
|
|
if canonical, ok := named.(Canonical); ok {
|
|
|
|
// The reference is both tagged and digested, only
|
|
|
|
// return digested.
|
|
|
|
newNamed, err := WithName(canonical.Name())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newCanonical, err := WithDigest(newNamed, canonical.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newCanonical, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TagNameOnly(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, '/')
|
2019-08-14 22:27:59 +00:00
|
|
|
if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost" && strings.ToLower(name[:i]) == name[:i]) {
|
2016-06-15 22:40:15 +00:00
|
|
|
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
|
|
|
}
|