reference.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package reference
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/docker/distribution/digest"
  7. distreference "github.com/docker/distribution/reference"
  8. "github.com/docker/docker/image/v1"
  9. )
  10. const (
  11. // DefaultTag defines the default tag used when performing images related actions and no tag or digest is specified
  12. DefaultTag = "latest"
  13. // DefaultHostname is the default built-in hostname
  14. DefaultHostname = "docker.io"
  15. // LegacyDefaultHostname is automatically converted to DefaultHostname
  16. LegacyDefaultHostname = "index.docker.io"
  17. // DefaultRepoPrefix is the prefix used for default repositories in default host
  18. DefaultRepoPrefix = "library/"
  19. )
  20. // Named is an object with a full name
  21. type Named interface {
  22. // Name returns normalized repository name, like "ubuntu".
  23. Name() string
  24. // String returns full reference, like "ubuntu@sha256:abcdef..."
  25. String() string
  26. // FullName returns full repository name with hostname, like "docker.io/library/ubuntu"
  27. FullName() string
  28. // Hostname returns hostname for the reference, like "docker.io"
  29. Hostname() string
  30. // RemoteName returns the repository component of the full name, like "library/ubuntu"
  31. RemoteName() string
  32. }
  33. // NamedTagged is an object including a name and tag.
  34. type NamedTagged interface {
  35. Named
  36. Tag() string
  37. }
  38. // Canonical reference is an object with a fully unique
  39. // name including a name with hostname and digest
  40. type Canonical interface {
  41. Named
  42. Digest() digest.Digest
  43. }
  44. // ParseNamed parses s and returns a syntactically valid reference implementing
  45. // the Named interface. The reference must have a name, otherwise an error is
  46. // returned.
  47. // If an error was encountered it is returned, along with a nil Reference.
  48. func ParseNamed(s string) (Named, error) {
  49. named, err := distreference.ParseNamed(s)
  50. if err != nil {
  51. return nil, fmt.Errorf("Error parsing reference: %q is not a valid repository/tag", s)
  52. }
  53. r, err := WithName(named.Name())
  54. if err != nil {
  55. return nil, err
  56. }
  57. if canonical, isCanonical := named.(distreference.Canonical); isCanonical {
  58. return WithDigest(r, canonical.Digest())
  59. }
  60. if tagged, isTagged := named.(distreference.NamedTagged); isTagged {
  61. return WithTag(r, tagged.Tag())
  62. }
  63. return r, nil
  64. }
  65. // WithName returns a named object representing the given string. If the input
  66. // is invalid ErrReferenceInvalidFormat will be returned.
  67. func WithName(name string) (Named, error) {
  68. name, err := normalize(name)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if err := validateName(name); err != nil {
  73. return nil, err
  74. }
  75. r, err := distreference.WithName(name)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &namedRef{r}, nil
  80. }
  81. // WithTag combines the name from "name" and the tag from "tag" to form a
  82. // reference incorporating both the name and the tag.
  83. func WithTag(name Named, tag string) (NamedTagged, error) {
  84. r, err := distreference.WithTag(name, tag)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return &taggedRef{namedRef{r}}, nil
  89. }
  90. // WithDigest combines the name from "name" and the digest from "digest" to form
  91. // a reference incorporating both the name and the digest.
  92. func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
  93. r, err := distreference.WithDigest(name, digest)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return &canonicalRef{namedRef{r}}, nil
  98. }
  99. type namedRef struct {
  100. distreference.Named
  101. }
  102. type taggedRef struct {
  103. namedRef
  104. }
  105. type canonicalRef struct {
  106. namedRef
  107. }
  108. func (r *namedRef) FullName() string {
  109. hostname, remoteName := splitHostname(r.Name())
  110. return hostname + "/" + remoteName
  111. }
  112. func (r *namedRef) Hostname() string {
  113. hostname, _ := splitHostname(r.Name())
  114. return hostname
  115. }
  116. func (r *namedRef) RemoteName() string {
  117. _, remoteName := splitHostname(r.Name())
  118. return remoteName
  119. }
  120. func (r *taggedRef) Tag() string {
  121. return r.namedRef.Named.(distreference.NamedTagged).Tag()
  122. }
  123. func (r *canonicalRef) Digest() digest.Digest {
  124. return r.namedRef.Named.(distreference.Canonical).Digest()
  125. }
  126. // WithDefaultTag adds a default tag to a reference if it only has a repo name.
  127. func WithDefaultTag(ref Named) Named {
  128. if IsNameOnly(ref) {
  129. ref, _ = WithTag(ref, DefaultTag)
  130. }
  131. return ref
  132. }
  133. // IsNameOnly returns true if reference only contains a repo name.
  134. func IsNameOnly(ref Named) bool {
  135. if _, ok := ref.(NamedTagged); ok {
  136. return false
  137. }
  138. if _, ok := ref.(Canonical); ok {
  139. return false
  140. }
  141. return true
  142. }
  143. // ParseIDOrReference parses string for an image ID or a reference. ID can be
  144. // without a default prefix.
  145. func ParseIDOrReference(idOrRef string) (digest.Digest, Named, error) {
  146. if err := v1.ValidateID(idOrRef); err == nil {
  147. idOrRef = "sha256:" + idOrRef
  148. }
  149. if dgst, err := digest.ParseDigest(idOrRef); err == nil {
  150. return dgst, nil, nil
  151. }
  152. ref, err := ParseNamed(idOrRef)
  153. return "", ref, err
  154. }
  155. // splitHostname splits a repository name to hostname and remotename string.
  156. // If no valid hostname is found, the default hostname is used. Repository name
  157. // needs to be already validated before.
  158. func splitHostname(name string) (hostname, remoteName string) {
  159. i := strings.IndexRune(name, '/')
  160. if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
  161. hostname, remoteName = DefaultHostname, name
  162. } else {
  163. hostname, remoteName = name[:i], name[i+1:]
  164. }
  165. if hostname == LegacyDefaultHostname {
  166. hostname = DefaultHostname
  167. }
  168. if hostname == DefaultHostname && !strings.ContainsRune(remoteName, '/') {
  169. remoteName = DefaultRepoPrefix + remoteName
  170. }
  171. return
  172. }
  173. // normalize returns a repository name in its normalized form, meaning it
  174. // will not contain default hostname nor library/ prefix for official images.
  175. func normalize(name string) (string, error) {
  176. host, remoteName := splitHostname(name)
  177. if strings.ToLower(remoteName) != remoteName {
  178. return "", errors.New("invalid reference format: repository name must be lowercase")
  179. }
  180. if host == DefaultHostname {
  181. if strings.HasPrefix(remoteName, DefaultRepoPrefix) {
  182. return strings.TrimPrefix(remoteName, DefaultRepoPrefix), nil
  183. }
  184. return remoteName, nil
  185. }
  186. return name, nil
  187. }
  188. func validateName(name string) error {
  189. if err := v1.ValidateID(name); err == nil {
  190. return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name)
  191. }
  192. return nil
  193. }