normalize.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package reference
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/docker/distribution/digestset"
  7. "github.com/opencontainers/go-digest"
  8. )
  9. var (
  10. legacyDefaultDomain = "index.docker.io"
  11. defaultDomain = "docker.io"
  12. officialRepoName = "library"
  13. defaultTag = "latest"
  14. )
  15. // normalizedNamed represents a name which has been
  16. // normalized and has a familiar form. A familiar name
  17. // is what is used in Docker UI. An example normalized
  18. // name is "docker.io/library/ubuntu" and corresponding
  19. // familiar name of "ubuntu".
  20. type normalizedNamed interface {
  21. Named
  22. Familiar() Named
  23. }
  24. // ParseNormalizedNamed parses a string into a named reference
  25. // transforming a familiar name from Docker UI to a fully
  26. // qualified reference. If the value may be an identifier
  27. // use ParseAnyReference.
  28. func ParseNormalizedNamed(s string) (Named, error) {
  29. if ok := anchoredIdentifierRegexp.MatchString(s); ok {
  30. return nil, fmt.Errorf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings", s)
  31. }
  32. domain, remainder := splitDockerDomain(s)
  33. var remoteName string
  34. if tagSep := strings.IndexRune(remainder, ':'); tagSep > -1 {
  35. remoteName = remainder[:tagSep]
  36. } else {
  37. remoteName = remainder
  38. }
  39. if strings.ToLower(remoteName) != remoteName {
  40. return nil, errors.New("invalid reference format: repository name must be lowercase")
  41. }
  42. ref, err := Parse(domain + "/" + remainder)
  43. if err != nil {
  44. return nil, err
  45. }
  46. named, isNamed := ref.(Named)
  47. if !isNamed {
  48. return nil, fmt.Errorf("reference %s has no name", ref.String())
  49. }
  50. return named, nil
  51. }
  52. // splitDockerDomain splits a repository name to domain and remotename string.
  53. // If no valid domain is found, the default domain is used. Repository name
  54. // needs to be already validated before.
  55. func splitDockerDomain(name string) (domain, remainder string) {
  56. i := strings.IndexRune(name, '/')
  57. if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
  58. domain, remainder = defaultDomain, name
  59. } else {
  60. domain, remainder = name[:i], name[i+1:]
  61. }
  62. if domain == legacyDefaultDomain {
  63. domain = defaultDomain
  64. }
  65. if domain == defaultDomain && !strings.ContainsRune(remainder, '/') {
  66. remainder = officialRepoName + "/" + remainder
  67. }
  68. return
  69. }
  70. // familiarizeName returns a shortened version of the name familiar
  71. // to to the Docker UI. Familiar names have the default domain
  72. // "docker.io" and "library/" repository prefix removed.
  73. // For example, "docker.io/library/redis" will have the familiar
  74. // name "redis" and "docker.io/dmcgowan/myapp" will be "dmcgowan/myapp".
  75. // Returns a familiarized named only reference.
  76. func familiarizeName(named namedRepository) repository {
  77. repo := repository{
  78. domain: named.Domain(),
  79. path: named.Path(),
  80. }
  81. if repo.domain == defaultDomain {
  82. repo.domain = ""
  83. // Handle official repositories which have the pattern "library/<official repo name>"
  84. if split := strings.Split(repo.path, "/"); len(split) == 2 && split[0] == officialRepoName {
  85. repo.path = split[1]
  86. }
  87. }
  88. return repo
  89. }
  90. func (r reference) Familiar() Named {
  91. return reference{
  92. namedRepository: familiarizeName(r.namedRepository),
  93. tag: r.tag,
  94. digest: r.digest,
  95. }
  96. }
  97. func (r repository) Familiar() Named {
  98. return familiarizeName(r)
  99. }
  100. func (t taggedReference) Familiar() Named {
  101. return taggedReference{
  102. namedRepository: familiarizeName(t.namedRepository),
  103. tag: t.tag,
  104. }
  105. }
  106. func (c canonicalReference) Familiar() Named {
  107. return canonicalReference{
  108. namedRepository: familiarizeName(c.namedRepository),
  109. digest: c.digest,
  110. }
  111. }
  112. // TagNameOnly adds the default tag "latest" to a reference if it only has
  113. // a repo name.
  114. func TagNameOnly(ref Named) Named {
  115. if IsNameOnly(ref) {
  116. namedTagged, err := WithTag(ref, defaultTag)
  117. if err != nil {
  118. // Default tag must be valid, to create a NamedTagged
  119. // type with non-validated input the WithTag function
  120. // should be used instead
  121. panic(err)
  122. }
  123. return namedTagged
  124. }
  125. return ref
  126. }
  127. // ParseAnyReference parses a reference string as a possible identifier,
  128. // full digest, or familiar name.
  129. func ParseAnyReference(ref string) (Reference, error) {
  130. if ok := anchoredIdentifierRegexp.MatchString(ref); ok {
  131. return digestReference("sha256:" + ref), nil
  132. }
  133. if dgst, err := digest.Parse(ref); err == nil {
  134. return digestReference(dgst), nil
  135. }
  136. return ParseNormalizedNamed(ref)
  137. }
  138. // ParseAnyReferenceWithSet parses a reference string as a possible short
  139. // identifier to be matched in a digest set, a full digest, or familiar name.
  140. func ParseAnyReferenceWithSet(ref string, ds *digestset.Set) (Reference, error) {
  141. if ok := anchoredShortIdentifierRegexp.MatchString(ref); ok {
  142. dgst, err := ds.Lookup(ref)
  143. if err == nil {
  144. return digestReference(dgst), nil
  145. }
  146. } else {
  147. if dgst, err := digest.Parse(ref); err == nil {
  148. return digestReference(dgst), nil
  149. }
  150. }
  151. return ParseNormalizedNamed(ref)
  152. }