| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package registry |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"net/url" |
| 21 |
"regexp" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/opencontainers/go-digest" |
| 25 |
"oras.land/oras-go/v2/errdef" |
| 26 |
) |
| 27 |
|
| 28 |
// regular expressions for components. |
| 29 |
var ( |
| 30 |
// repositoryRegexp is adapted from the distribution implementation. The |
| 31 |
// repository name set under OCI distribution spec is a subset of the docker |
| 32 |
// spec. For maximum compatability, the docker spec is verified client-side. |
| 33 |
// Further checks are left to the server-side. |
| 34 |
// |
| 35 |
// References: |
| 36 |
// - https://github.com/distribution/distribution/blob/v2.7.1/reference/regexp.go#L53 |
| 37 |
// - https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#pulling-manifests |
| 38 |
repositoryRegexp = regexp.MustCompile(`^[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)*$`) |
| 39 |
|
| 40 |
// tagRegexp checks the tag name. |
| 41 |
// The docker and OCI spec have the same regular expression. |
| 42 |
// |
| 43 |
// Reference: https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#pulling-manifests |
| 44 |
tagRegexp = regexp.MustCompile(`^[\w][\w.-]{0,127}$`) |
| 45 |
) |
| 46 |
|
| 47 |
// Reference references either a resource descriptor (where Reference.Reference |
| 48 |
// is a tag or a digest), or a resource repository (where Reference.Reference |
| 49 |
// is the empty string). |
| 50 |
type Reference struct { |
| 51 |
// Registry is the name of the registry. It is usually the domain name of |
| 52 |
// the registry optionally with a port. |
| 53 |
Registry string |
| 54 |
|
| 55 |
// Repository is the name of the repository. |
| 56 |
Repository string |
| 57 |
|
| 58 |
// Reference is the reference of the object in the repository. This field |
| 59 |
// can take any one of the four valid forms (see ParseReference). In the |
| 60 |
// case where it's the empty string, it necessarily implies valid form D, |
| 61 |
// and where it is non-empty, then it is either a tag, or a digest |
| 62 |
// (implying one of valid forms A, B, or C). |
| 63 |
Reference string |
| 64 |
} |
| 65 |
|
| 66 |
// ParseReference parses a string (artifact) into an `artifact reference`. |
| 67 |
// Corresponding cryptographic hash implementations are required to be imported |
| 68 |
// as specified by https://pkg.go.dev/github.com/opencontainers/go-digest#readme-usage |
| 69 |
// if the string contains a digest. |
| 70 |
// |
| 71 |
// Note: An "image" is an "artifact", however, an "artifact" is not necessarily |
| 72 |
// an "image". |
| 73 |
// |
| 74 |
// The token `artifact` is composed of other tokens, and those in turn are |
| 75 |
// composed of others. This definition recursivity requires a notation capable |
| 76 |
// of recursion, thus the following two forms have been adopted: |
| 77 |
// |
| 78 |
// 1. Backus–Naur Form (BNF) has been adopted to address the recursive nature |
| 79 |
// of the definition. |
| 80 |
// 2. Token opacity is revealed via its label letter-casing. That is, "opaque" |
| 81 |
// tokens (i.e., tokens that are not final, and must therefore be further |
| 82 |
// broken down into their constituents) are denoted in *lowercase*, while |
| 83 |
// final tokens (i.e., leaf-node tokens that are final) are denoted in |
| 84 |
// *uppercase*. |
| 85 |
// |
| 86 |
// Finally, note that a number of the opaque tokens are polymorphic in nature; |
| 87 |
// that is, they can take on one of numerous forms, not restricted to a single |
| 88 |
// defining form. |
| 89 |
// |
| 90 |
// The top-level token, `artifact`, is composed of two (opaque) tokens; namely |
| 91 |
// `socketaddr` and `path`: |
| 92 |
// |
| 93 |
// <artifact> ::= <socketaddr> "/" <path> |
| 94 |
// |
| 95 |
// The former is described as follows: |
| 96 |
// |
| 97 |
// <socketaddr> ::= <host> | <host> ":" <PORT> |
| 98 |
// <host> ::= <ip> | <FQDN> |
| 99 |
// <ip> ::= <IPV4-ADDR> | <IPV6-ADDR> |
| 100 |
// |
| 101 |
// The latter, which is of greater interest here, is described as follows: |
| 102 |
// |
| 103 |
// <path> ::= <REPOSITORY> | <REPOSITORY> <reference> |
| 104 |
// <reference> ::= "@" <digest> | ":" <TAG> "@" <DIGEST> | ":" <TAG> |
| 105 |
// <digest> ::= <ALGO> ":" <HASH> |
| 106 |
// |
| 107 |
// This second token--`path`--can take on exactly four forms, each of which will |
| 108 |
// now be illustrated: |
| 109 |
// |
| 110 |
// <--- path --------------------------------------------> | - Decode `path` |
| 111 |
// <=== REPOSITORY ===> <--- reference ------------------> | - Decode `reference` |
| 112 |
// <=== REPOSITORY ===> @ <=================== digest ===> | - Valid Form A |
| 113 |
// <=== REPOSITORY ===> : <!!! TAG !!!> @ <=== digest ===> | - Valid Form B (tag is dropped) |
| 114 |
// <=== REPOSITORY ===> : <=== TAG ======================> | - Valid Form C |
| 115 |
// <=== REPOSITORY ======================================> | - Valid Form D |
| 116 |
// |
| 117 |
// Note: In the case of Valid Form B, TAG is dropped without any validation or |
| 118 |
// further consideration. |
| 119 |
func ParseReference(artifact string) (Reference, error) { |
| 120 |
parts := strings.SplitN(artifact, "/", 2) |
| 121 |
if len(parts) == 1 { |
| 122 |
// Invalid Form |
| 123 |
return Reference{}, fmt.Errorf("%w: missing registry or repository", errdef.ErrInvalidReference) |
| 124 |
} |
| 125 |
registry, path := parts[0], parts[1] |
| 126 |
|
| 127 |
var isTag bool |
| 128 |
var repository string |
| 129 |
var reference string |
| 130 |
if index := strings.Index(path, "@"); index != -1 { |
| 131 |
// `digest` found; Valid Form A (if not B) |
| 132 |
isTag = false |
| 133 |
repository = path[:index] |
| 134 |
reference = path[index+1:] |
| 135 |
|
| 136 |
if index = strings.Index(repository, ":"); index != -1 { |
| 137 |
// `tag` found (and now dropped without validation) since `the |
| 138 |
// `digest` already present; Valid Form B |
| 139 |
repository = repository[:index] |
| 140 |
} |
| 141 |
} else if index = strings.Index(path, ":"); index != -1 { |
| 142 |
// `tag` found; Valid Form C |
| 143 |
isTag = true |
| 144 |
repository = path[:index] |
| 145 |
reference = path[index+1:] |
| 146 |
} else { |
| 147 |
// empty `reference`; Valid Form D |
| 148 |
repository = path |
| 149 |
} |
| 150 |
ref := Reference{ |
| 151 |
Registry: registry, |
| 152 |
Repository: repository, |
| 153 |
Reference: reference, |
| 154 |
} |
| 155 |
|
| 156 |
if err := ref.ValidateRegistry(); err != nil { |
| 157 |
return Reference{}, err |
| 158 |
} |
| 159 |
|
| 160 |
if err := ref.ValidateRepository(); err != nil { |
| 161 |
return Reference{}, err |
| 162 |
} |
| 163 |
|
| 164 |
if len(ref.Reference) == 0 { |
| 165 |
return ref, nil |
| 166 |
} |
| 167 |
|
| 168 |
validator := ref.ValidateReferenceAsDigest |
| 169 |
if isTag { |
| 170 |
validator = ref.ValidateReferenceAsTag |
| 171 |
} |
| 172 |
if err := validator(); err != nil { |
| 173 |
return Reference{}, err |
| 174 |
} |
| 175 |
|
| 176 |
return ref, nil |
| 177 |
} |
| 178 |
|
| 179 |
// Validate the entire reference object; the registry, the repository, and the |
| 180 |
// reference. |
| 181 |
func (r Reference) Validate() error { |
| 182 |
if err := r.ValidateRegistry(); err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
|
| 186 |
if err := r.ValidateRepository(); err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
|
| 190 |
return r.ValidateReference() |
| 191 |
} |
| 192 |
|
| 193 |
// ValidateRegistry validates the registry. |
| 194 |
func (r Reference) ValidateRegistry() error { |
| 195 |
if uri, err := url.ParseRequestURI("dummy://" + r.Registry); err != nil || uri.Host == "" || uri.Host != r.Registry { |
| 196 |
return fmt.Errorf("%w: invalid registry %q", errdef.ErrInvalidReference, r.Registry) |
| 197 |
} |
| 198 |
return nil |
| 199 |
} |
| 200 |
|
| 201 |
// ValidateRepository validates the repository. |
| 202 |
func (r Reference) ValidateRepository() error { |
| 203 |
if !repositoryRegexp.MatchString(r.Repository) { |
| 204 |
return fmt.Errorf("%w: invalid repository %q", errdef.ErrInvalidReference, r.Repository) |
| 205 |
} |
| 206 |
return nil |
| 207 |
} |
| 208 |
|
| 209 |
// ValidateReferenceAsTag validates the reference as a tag. |
| 210 |
func (r Reference) ValidateReferenceAsTag() error { |
| 211 |
if !tagRegexp.MatchString(r.Reference) { |
| 212 |
return fmt.Errorf("%w: invalid tag %q", errdef.ErrInvalidReference, r.Reference) |
| 213 |
} |
| 214 |
return nil |
| 215 |
} |
| 216 |
|
| 217 |
// ValidateReferenceAsDigest validates the reference as a digest. |
| 218 |
func (r Reference) ValidateReferenceAsDigest() error { |
| 219 |
if _, err := r.Digest(); err != nil { |
| 220 |
return fmt.Errorf("%w: invalid digest %q: %v", errdef.ErrInvalidReference, r.Reference, err) |
| 221 |
} |
| 222 |
return nil |
| 223 |
} |
| 224 |
|
| 225 |
// ValidateReference where the reference is first tried as an ampty string, then |
| 226 |
// as a digest, and if that fails, as a tag. |
| 227 |
func (r Reference) ValidateReference() error { |
| 228 |
if len(r.Reference) == 0 { |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
if index := strings.IndexByte(r.Reference, ':'); index != -1 { |
| 233 |
return r.ValidateReferenceAsDigest() |
| 234 |
} |
| 235 |
|
| 236 |
return r.ValidateReferenceAsTag() |
| 237 |
} |
| 238 |
|
| 239 |
// Host returns the host name of the registry. |
| 240 |
func (r Reference) Host() string { |
| 241 |
if r.Registry == "docker.io" { |
| 242 |
return "registry-1.docker.io" |
| 243 |
} |
| 244 |
return r.Registry |
| 245 |
} |
| 246 |
|
| 247 |
// ReferenceOrDefault returns the reference or the default reference if empty. |
| 248 |
func (r Reference) ReferenceOrDefault() string { |
| 249 |
if r.Reference == "" { |
| 250 |
return "latest" |
| 251 |
} |
| 252 |
return r.Reference |
| 253 |
} |
| 254 |
|
| 255 |
// Digest returns the reference as a digest. |
| 256 |
// Corresponding cryptographic hash implementations are required to be imported |
| 257 |
// as specified by https://pkg.go.dev/github.com/opencontainers/go-digest#readme-usage |
| 258 |
func (r Reference) Digest() (digest.Digest, error) { |
| 259 |
return digest.Parse(r.Reference) |
| 260 |
} |
| 261 |
|
| 262 |
// String implements `fmt.Stringer` and returns the reference string. |
| 263 |
// The resulted string is meaningful only if the reference is valid. |
| 264 |
func (r Reference) String() string { |
| 265 |
if r.Repository == "" { |
| 266 |
return r.Registry |
| 267 |
} |
| 268 |
ref := r.Registry + "/" + r.Repository |
| 269 |
if r.Reference == "" { |
| 270 |
return ref |
| 271 |
} |
| 272 |
if d, err := r.Digest(); err == nil { |
| 273 |
return ref + "@" + d.String() |
| 274 |
} |
| 275 |
return ref + ":" + r.Reference |
| 276 |
} |