Skip to content

Commit 4a93cdd

Browse files
committed
Ignore signature when getManifestDescriptor
1 parent 00dedd3 commit 4a93cdd

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

image/oci/layout/oci_delete.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io/fs"
99
"os"
1010
"slices"
11-
"strings"
1211

1312
digest "github.com/opencontainers/go-digest"
1413
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
@@ -216,13 +215,3 @@ func (ref ociReference) deleteSignatures(ctx context.Context, sys *types.SystemC
216215
}
217216
return err
218217
}
219-
220-
// isSigstoreTag returns true if the tag is sigstore signature tag.
221-
func isSigstoreTag(tag string) bool {
222-
if !strings.HasSuffix(tag, ".sig") {
223-
return false
224-
}
225-
digestPart := strings.TrimSuffix(tag, ".sig")
226-
digestPart = strings.Replace(digestPart, "-", ":", 1)
227-
return digest.Digest(digestPart).Validate() == nil
228-
}

image/oci/layout/oci_transport.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var (
3030
// Transport is an ImageTransport for OCI directories.
3131
Transport = ociTransport{}
3232

33+
// ErrEmptyIndex is an error returned when the index includes no image.
34+
ErrEmptyIndex = errors.New("no image in oci")
35+
3336
// ErrMoreThanOneImage is an error returned when the manifest includes
3437
// more than one image and the user should choose which one to use.
3538
ErrMoreThanOneImage = errors.New("more than one image in oci, choose an image")
@@ -250,11 +253,33 @@ func (ref ociReference) getManifestDescriptor() (imgspecv1.Descriptor, int, erro
250253

251254
default:
252255
// 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 {
255280
return imgspecv1.Descriptor{}, -1, ErrMoreThanOneImage
256281
}
257-
return index.Manifests[0], 0, nil
282+
return desc, idx, nil
258283
}
259284
}
260285

@@ -391,3 +416,13 @@ func (ref ociReference) getOCIDescriptorContents(desc imgspecv1.Descriptor, maxS
391416
}
392417
return payload, nil
393418
}
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

Comments
 (0)