|
30 | 30 | // Transport is an ImageTransport for OCI directories.
|
31 | 31 | Transport = ociTransport{}
|
32 | 32 |
|
| 33 | + // ErrEmptyIndex is an error returned when the index includes no image. |
| 34 | + ErrEmptyIndex = errors.New("no image in oci") |
| 35 | + |
33 | 36 | // ErrMoreThanOneImage is an error returned when the manifest includes
|
34 | 37 | // more than one image and the user should choose which one to use.
|
35 | 38 | ErrMoreThanOneImage = errors.New("more than one image in oci, choose an image")
|
@@ -250,11 +253,33 @@ func (ref ociReference) getManifestDescriptor() (imgspecv1.Descriptor, int, erro
|
250 | 253 |
|
251 | 254 | default:
|
252 | 255 | // return manifest if only one image is in the oci directory
|
253 |
| - if len(index.Manifests) != 1 { |
254 |
| - // ask user to choose image when more than one image in the oci directory |
| 256 | + if len(index.Manifests) == 0 { |
| 257 | + return imgspecv1.Descriptor{}, -1, ErrEmptyIndex |
| 258 | + } |
| 259 | + // if there's one image return it, even if it is a signature |
| 260 | + if len(index.Manifests) == 1 { |
| 261 | + return index.Manifests[0], 0, nil |
| 262 | + } |
| 263 | + // when there's more than one image, try to get a non-signature image |
| 264 | + var desc imgspecv1.Descriptor |
| 265 | + idx := -1 |
| 266 | + for i, md := range index.Manifests { |
| 267 | + if isSigstoreTag(md.Annotations[imgspecv1.AnnotationRefName]) { |
| 268 | + continue |
| 269 | + } |
| 270 | + // More than one non-signature image was found |
| 271 | + if idx != -1 { |
| 272 | + // ask user to choose image when more than one image in the oci directory |
| 273 | + return imgspecv1.Descriptor{}, -1, ErrMoreThanOneImage |
| 274 | + } |
| 275 | + desc = md |
| 276 | + idx = i |
| 277 | + } |
| 278 | + // there's only multiple signature images |
| 279 | + if idx == -1 { |
255 | 280 | return imgspecv1.Descriptor{}, -1, ErrMoreThanOneImage
|
256 | 281 | }
|
257 |
| - return index.Manifests[0], 0, nil |
| 282 | + return desc, idx, nil |
258 | 283 | }
|
259 | 284 | }
|
260 | 285 |
|
@@ -391,3 +416,13 @@ func (ref ociReference) getOCIDescriptorContents(desc imgspecv1.Descriptor, maxS
|
391 | 416 | }
|
392 | 417 | return payload, nil
|
393 | 418 | }
|
| 419 | + |
| 420 | +// isSigstoreTag returns true if the tag is sigstore signature tag. |
| 421 | +func isSigstoreTag(tag string) bool { |
| 422 | + if !strings.HasSuffix(tag, ".sig") { |
| 423 | + return false |
| 424 | + } |
| 425 | + digestPart := strings.TrimSuffix(tag, ".sig") |
| 426 | + digestPart = strings.Replace(digestPart, "-", ":", 1) |
| 427 | + return digest.Digest(digestPart).Validate() == nil |
| 428 | +} |
0 commit comments