Skip to content

Commit 8dd9e9b

Browse files
authored
Fix replace with compliant image mediatype (#4077)
* Fix replace with non compliant image mediatypes Signed-off-by: Stef Graces <stefgraces@hotmail.com> * Add test for signature mediatype Signed-off-by: Stef Graces <stefgraces@hotmail.com> --------- Signed-off-by: Stef Graces <stefgraces@hotmail.com>
1 parent c5c649d commit 8dd9e9b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pkg/oci/mutate/signatures.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ package mutate
1717

1818
import (
1919
v1 "github.com/google/go-containerregistry/pkg/v1"
20-
"github.com/google/go-containerregistry/pkg/v1/empty"
2120
"github.com/google/go-containerregistry/pkg/v1/mutate"
2221
"github.com/sigstore/cosign/v2/internal/pkg/now"
2322
"github.com/sigstore/cosign/v2/pkg/oci"
23+
"github.com/sigstore/cosign/v2/pkg/oci/empty"
2424
)
2525

2626
const maxLayers = 1000
@@ -83,7 +83,7 @@ func ReplaceSignatures(base oci.Signatures) (oci.Signatures, error) {
8383
Annotations: ann,
8484
})
8585
}
86-
img, err := mutate.Append(empty.Image, adds...)
86+
img, err := mutate.Append(empty.Signatures(), adds...)
8787
if err != nil {
8888
return nil, err
8989
}

pkg/oci/mutate/signatures_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
v1 "github.com/google/go-containerregistry/pkg/v1"
23+
"github.com/google/go-containerregistry/pkg/v1/types"
2324
"github.com/sigstore/cosign/v2/pkg/oci"
2425
"github.com/sigstore/cosign/v2/pkg/oci/empty"
2526
"github.com/sigstore/cosign/v2/pkg/oci/static"
@@ -87,6 +88,34 @@ func TestAppendSignatures(t *testing.T) {
8788
}
8889
}
8990

91+
func TestReplaceSignatures(t *testing.T) {
92+
base := empty.Signatures()
93+
94+
s1, err := static.NewSignature([]byte{}, "s1")
95+
if err != nil {
96+
t.Fatalf("NewSignature() = %v", err)
97+
}
98+
99+
oneSig, err := AppendSignatures(base, false, s1)
100+
if err != nil {
101+
t.Fatalf("AppendSignatures() = %v", err)
102+
}
103+
replaceSig, err := ReplaceSignatures(oneSig)
104+
if err != nil {
105+
t.Fatalf("ReplaceSignatures() = %v", err)
106+
}
107+
if sl, err := replaceSig.Get(); err != nil {
108+
t.Fatalf("Get() = %v", err)
109+
} else if got, want := len(sl), 1; got != want {
110+
t.Errorf("len(Get()) = %d, wanted %d", got, want)
111+
}
112+
if mt, err := replaceSig.MediaType(); err != nil {
113+
t.Fatalf("MediaType() = %v", err)
114+
} else if got, want := mt, types.OCIManifestSchema1; got != want {
115+
t.Errorf("MediaType() = %v, wanted %v", got, want)
116+
}
117+
}
118+
90119
func TestGet(t *testing.T) {
91120
tests := []struct {
92121
name string

0 commit comments

Comments
 (0)