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
|
|
|
)
|
|
|
|
|
2022-11-09 14:20:23 +00:00
|
|
|
const (
|
2016-06-15 22:40:15 +00:00
|
|
|
legacyDefaultDomain = "index.docker.io"
|
|
|
|
defaultDomain = "docker.io"
|
2022-11-09 14:20:23 +00:00
|
|
|
officialRepoPrefix = "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
|
|
|
}
|
|
|
|
|
2022-11-11 14:02:10 +00:00
|
|
|
// namedTaggedDigested is a reference that has both a tag and a digest.
|
|
|
|
type namedTaggedDigested interface {
|
|
|
|
NamedTagged
|
|
|
|
Digested
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseDockerRef normalizes the image reference following the docker convention,
|
|
|
|
// which allows for references to contain both a tag and a digest. It returns a
|
|
|
|
// reference that is either tagged or digested. For references containing both
|
|
|
|
// a tag and a digest, it returns a digested reference. For example, the following
|
|
|
|
// reference:
|
|
|
|
//
|
|
|
|
// docker.io/library/busybox:latest@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa
|
|
|
|
//
|
|
|
|
// Is returned as a digested reference (with the ":latest" tag removed):
|
|
|
|
//
|
|
|
|
// docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa
|
|
|
|
//
|
|
|
|
// References that are already "tagged" or "digested" are returned unmodified:
|
|
|
|
//
|
|
|
|
// // Already a digested reference
|
|
|
|
// docker.io/library/busybox@sha256:7cc4b5aefd1d0cadf8d97d4350462ba51c694ebca145b08d7d41b41acc8db5aa
|
|
|
|
//
|
|
|
|
// // Already a named reference
|
|
|
|
// docker.io/library/busybox:latest
|
2018-12-10 22:51:58 +00:00
|
|
|
func ParseDockerRef(ref string) (Named, error) {
|
|
|
|
named, err := ParseNormalizedNamed(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-11 14:02:10 +00:00
|
|
|
if canonical, ok := named.(namedTaggedDigested); ok {
|
|
|
|
// The reference is both tagged and digested; only return digested.
|
|
|
|
newNamed, err := WithName(canonical.Name())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-10 22:51:58 +00:00
|
|
|
}
|
2022-11-11 14:02:10 +00:00
|
|
|
return WithDigest(newNamed, canonical.Digest())
|
2018-12-10 22:51:58 +00:00
|
|
|
}
|
|
|
|
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, '/') {
|
2022-11-09 14:20:23 +00:00
|
|
|
remainder = officialRepoPrefix + 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>"
|
2022-11-09 14:20:23 +00:00
|
|
|
if strings.HasPrefix(repo.path, officialRepoPrefix) {
|
|
|
|
// TODO(thaJeztah): this check may be too strict, as it assumes the
|
|
|
|
// "library/" namespace does not have nested namespaces. While this
|
|
|
|
// is true (currently), technically it would be possible for Docker
|
|
|
|
// Hub to use those (e.g. "library/distros/ubuntu:latest").
|
|
|
|
// See https://github.com/distribution/distribution/pull/3769#issuecomment-1302031785.
|
|
|
|
if remainder := strings.TrimPrefix(repo.path, officialRepoPrefix); !strings.ContainsRune(remainder, '/') {
|
|
|
|
repo.path = remainder
|
|
|
|
}
|
2017-01-11 18:46:48 +00:00
|
|
|
}
|
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
|
|
|
}
|