2015-07-10 18:36:04 +00:00
|
|
|
// Package reference provides a general type to represent any way of referencing images within the registry.
|
|
|
|
// Its main purpose is to abstract tags and digests (content-addressable hash).
|
|
|
|
//
|
|
|
|
// Grammar
|
|
|
|
//
|
|
|
|
// reference := repository [ ":" tag ] [ "@" digest ]
|
|
|
|
//
|
|
|
|
// // repository.go
|
|
|
|
// repository := hostname ['/' component]+
|
2015-09-08 23:00:48 +00:00
|
|
|
// hostname := hostcomponent [':' port-number]
|
2015-10-09 23:01:01 +00:00
|
|
|
// component := subcomponent [separator subcomponent]*
|
|
|
|
// subcomponent := alpha-numeric ['-'* alpha-numeric]*
|
2015-09-08 23:00:48 +00:00
|
|
|
// hostcomponent := [hostpart '.']* hostpart
|
2015-10-09 23:01:01 +00:00
|
|
|
// alpha-numeric := /[a-z0-9]+/
|
|
|
|
// separator := /([_.]|__)/
|
2015-07-10 18:36:04 +00:00
|
|
|
// port-number := /[0-9]+/
|
2015-10-09 23:01:01 +00:00
|
|
|
// hostpart := /([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])/
|
2015-07-10 18:36:04 +00:00
|
|
|
//
|
|
|
|
// // tag.go
|
|
|
|
// tag := /[\w][\w.-]{0,127}/
|
|
|
|
//
|
|
|
|
// // from the digest package
|
|
|
|
// digest := digest-algorithm ":" digest-hex
|
|
|
|
// digest-algorithm := digest-algorithm-component [ digest-algorithm-separator digest-algorithm-component ]
|
|
|
|
// digest-algorithm-separator := /[+.-_]/
|
2015-09-08 23:00:48 +00:00
|
|
|
// digest-algorithm-component := /[A-Za-z][A-Za-z0-9]*/
|
|
|
|
// digest-hex := /[0-9a-fA-F]{32,}/ ; Atleast 128 bit digest value
|
2015-07-10 18:36:04 +00:00
|
|
|
package reference
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-09-08 23:00:48 +00:00
|
|
|
"fmt"
|
2015-07-10 18:36:04 +00:00
|
|
|
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
)
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
const (
|
|
|
|
// NameTotalLengthMax is the maximum total number of characters in a repository name.
|
|
|
|
NameTotalLengthMax = 255
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrReferenceInvalidFormat represents an error while trying to parse a string as a reference.
|
|
|
|
ErrReferenceInvalidFormat = errors.New("invalid reference format")
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// ErrNameEmpty is returned for empty, invalid repository names.
|
|
|
|
ErrNameEmpty = errors.New("repository name must have at least one component")
|
|
|
|
|
|
|
|
// ErrNameTooLong is returned when a repository name is longer than
|
|
|
|
// RepositoryNameTotalLengthMax
|
|
|
|
ErrNameTooLong = fmt.Errorf("repository name must not be more than %v characters", NameTotalLengthMax)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reference is an opaque object reference identifier that may include
|
|
|
|
// modifiers such as a hostname, name, tag, and digest.
|
2015-07-10 18:36:04 +00:00
|
|
|
type Reference interface {
|
2015-09-08 23:00:48 +00:00
|
|
|
// String returns the full reference
|
2015-07-10 18:36:04 +00:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:36:19 +00:00
|
|
|
// Field provides a wrapper type for resolving correct reference types when
|
|
|
|
// working with encoding.
|
|
|
|
type Field struct {
|
|
|
|
reference Reference
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsField wraps a reference in a Field for encoding.
|
|
|
|
func AsField(reference Reference) Field {
|
|
|
|
return Field{reference}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reference unwraps the reference type from the field to
|
|
|
|
// return the Reference object. This object should be
|
|
|
|
// of the appropriate type to further check for different
|
|
|
|
// reference types.
|
|
|
|
func (f Field) Reference() Reference {
|
|
|
|
return f.reference
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText serializes the field to byte text which
|
|
|
|
// is the string of the reference.
|
|
|
|
func (f Field) MarshalText() (p []byte, err error) {
|
|
|
|
return []byte(f.reference.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText parses text bytes by invoking the
|
|
|
|
// reference parser to ensure the appropriately
|
|
|
|
// typed reference object is wrapped by field.
|
|
|
|
func (f *Field) UnmarshalText(p []byte) error {
|
|
|
|
r, err := Parse(string(p))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.reference = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// Named is an object with a full name
|
|
|
|
type Named interface {
|
|
|
|
Name() string
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// Tagged is an object which has a tag
|
|
|
|
type Tagged interface {
|
|
|
|
Tag() string
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// Digested is an object which has a digest
|
|
|
|
// in which it can be referenced by
|
|
|
|
type Digested interface {
|
|
|
|
Digest() digest.Digest
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// Canonical reference is an object with a fully unique
|
|
|
|
// name including a name with hostname and digest
|
|
|
|
type Canonical interface {
|
|
|
|
Reference
|
|
|
|
Named
|
|
|
|
Digested
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitHostname splits a named reference into a
|
|
|
|
// hostname and name string. If no valid hostname is
|
|
|
|
// found, the hostname is empty and the full value
|
|
|
|
// is returned as name
|
|
|
|
func SplitHostname(named Named) (string, string) {
|
|
|
|
name := named.Name()
|
|
|
|
match := anchoredNameRegexp.FindStringSubmatch(name)
|
|
|
|
if match == nil || len(match) != 3 {
|
|
|
|
return "", name
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
2015-09-08 23:00:48 +00:00
|
|
|
return match[1], match[2]
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses s and returns a syntactically valid Reference.
|
|
|
|
// If an error was encountered it is returned, along with a nil Reference.
|
2015-09-08 23:00:48 +00:00
|
|
|
// NOTE: Parse will not handle short digests.
|
2015-07-10 18:36:04 +00:00
|
|
|
func Parse(s string) (Reference, error) {
|
2015-09-08 23:00:48 +00:00
|
|
|
matches := ReferenceRegexp.FindStringSubmatch(s)
|
|
|
|
if matches == nil {
|
|
|
|
if s == "" {
|
|
|
|
return nil, ErrNameEmpty
|
|
|
|
}
|
|
|
|
// TODO(dmcgowan): Provide more specific and helpful error
|
|
|
|
return nil, ErrReferenceInvalidFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches[1]) > NameTotalLengthMax {
|
|
|
|
return nil, ErrNameTooLong
|
|
|
|
}
|
|
|
|
|
|
|
|
ref := reference{
|
|
|
|
name: matches[1],
|
|
|
|
tag: matches[2],
|
|
|
|
}
|
|
|
|
if matches[3] != "" {
|
|
|
|
var err error
|
|
|
|
ref.digest, err = digest.ParseDigest(matches[3])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
2015-09-08 23:00:48 +00:00
|
|
|
|
|
|
|
r := getBestReferenceType(ref)
|
|
|
|
if r == nil {
|
|
|
|
return nil, ErrNameEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseNamed parses the input string and returns a named
|
|
|
|
// object representing the given string. If the input is
|
|
|
|
// invalid ErrReferenceInvalidFormat will be returned.
|
|
|
|
func ParseNamed(name string) (Named, error) {
|
|
|
|
if !anchoredNameRegexp.MatchString(name) {
|
2015-07-10 18:36:04 +00:00
|
|
|
return nil, ErrReferenceInvalidFormat
|
|
|
|
}
|
2015-09-08 23:00:48 +00:00
|
|
|
return repository(name), nil
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func getBestReferenceType(ref reference) Reference {
|
|
|
|
if ref.name == "" {
|
|
|
|
// Allow digest only references
|
|
|
|
if ref.digest != "" {
|
|
|
|
return digestReference(ref.digest)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ref.tag == "" {
|
|
|
|
if ref.digest != "" {
|
|
|
|
return canonicalReference{
|
|
|
|
name: ref.name,
|
|
|
|
digest: ref.digest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return repository(ref.name)
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
2015-09-08 23:00:48 +00:00
|
|
|
if ref.digest == "" {
|
|
|
|
return taggedReference{
|
|
|
|
name: ref.name,
|
|
|
|
tag: ref.tag,
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
2015-09-08 23:00:48 +00:00
|
|
|
|
|
|
|
return ref
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
type reference struct {
|
|
|
|
name string
|
|
|
|
tag string
|
|
|
|
digest digest.Digest
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (r reference) String() string {
|
|
|
|
return r.name + ":" + r.tag + "@" + r.digest.String()
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (r reference) Name() string {
|
|
|
|
return r.name
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (r reference) Tag() string {
|
|
|
|
return r.tag
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (r reference) Digest() digest.Digest {
|
|
|
|
return r.digest
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
type repository string
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (r repository) String() string {
|
|
|
|
return string(r)
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (r repository) Name() string {
|
|
|
|
return string(r)
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
type digestReference digest.Digest
|
|
|
|
|
|
|
|
func (d digestReference) String() string {
|
|
|
|
return d.String()
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (d digestReference) Digest() digest.Digest {
|
|
|
|
return digest.Digest(d)
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
type taggedReference struct {
|
|
|
|
name string
|
|
|
|
tag string
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (t taggedReference) String() string {
|
|
|
|
return t.name + ":" + t.tag
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (t taggedReference) Name() string {
|
|
|
|
return t.name
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (t taggedReference) Tag() string {
|
|
|
|
return t.tag
|
|
|
|
}
|
|
|
|
|
|
|
|
type canonicalReference struct {
|
|
|
|
name string
|
|
|
|
digest digest.Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c canonicalReference) String() string {
|
|
|
|
return c.name + "@" + c.digest.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c canonicalReference) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
2015-07-10 18:36:04 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
func (c canonicalReference) Digest() digest.Digest {
|
|
|
|
return c.digest
|
2015-07-10 18:36:04 +00:00
|
|
|
}
|