Skip to content

Commit daec5ec

Browse files
authored
ErrNoSignaturesFound should be used when there is no signature attached to an image. (#3526)
* ErrNoSignaturesFound should be used when there is no signature attached to an image. Signed-off-by: zhaoyonghe <[email protected]> * Change error message. Signed-off-by: zhaoyonghe <[email protected]> * Add error type tests. Signed-off-by: zhaoyonghe <[email protected]> --------- Signed-off-by: zhaoyonghe <[email protected]>
1 parent 18cdadb commit daec5ec

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

pkg/cosign/verify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ func verifySignatures(ctx context.Context, sigs oci.Signatures, h v1.Hash, co *C
592592
}
593593

594594
if len(sl) == 0 {
595-
return nil, false, &ErrNoMatchingSignatures{
596-
errors.New("no matching signatures"),
595+
return nil, false, &ErrNoSignaturesFound{
596+
errors.New("no signatures found"),
597597
}
598598
}
599599

pkg/cosign/verify_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/sigstore/cosign/v2/internal/pkg/cosign/tsa"
4747
tsaMock "github.com/sigstore/cosign/v2/internal/pkg/cosign/tsa/mock"
4848
"github.com/sigstore/cosign/v2/pkg/cosign/bundle"
49+
"github.com/sigstore/cosign/v2/pkg/oci"
4950
"github.com/sigstore/cosign/v2/pkg/oci/static"
5051
"github.com/sigstore/cosign/v2/pkg/types"
5152
"github.com/sigstore/cosign/v2/test"
@@ -237,6 +238,44 @@ func CreateTestBundle(ctx context.Context, t *testing.T, rekor signature.Signer,
237238
return b
238239
}
239240

241+
func Test_verifySignaturesErrNoSignaturesFound(t *testing.T) {
242+
_, _, err := verifySignatures(context.Background(), &fakeOCISignatures{}, v1.Hash{}, nil)
243+
var e *ErrNoSignaturesFound
244+
if !errors.As(err, &e) {
245+
t.Fatalf("%T{%q} is not a %T", err, err, &ErrNoSignaturesFound{})
246+
}
247+
}
248+
249+
func Test_verifySignaturesErrNoMatchingSignatures(t *testing.T) {
250+
rootCert, rootKey, _ := test.GenerateRootCa()
251+
subCert, subKey, _ := test.GenerateSubordinateCa(rootCert, rootKey)
252+
leafCert, privKey, _ := test.GenerateLeafCert("[email protected]", "oidc-issuer", subCert, subKey)
253+
pemRoot := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rootCert.Raw})
254+
pemSub := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: subCert.Raw})
255+
pemLeaf := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: leafCert.Raw})
256+
257+
rootPool := x509.NewCertPool()
258+
rootPool.AddCert(rootCert)
259+
260+
payload := []byte{1, 2, 3, 4}
261+
h := sha256.Sum256(payload)
262+
signature, _ := privKey.Sign(rand.Reader, h[:], crypto.SHA256)
263+
264+
ociSig, _ := static.NewSignature(payload,
265+
base64.StdEncoding.EncodeToString(signature),
266+
static.WithCertChain(pemLeaf, appendSlices([][]byte{pemSub, pemRoot})))
267+
_, _, err := verifySignatures(context.Background(), &fakeOCISignatures{signatures: []oci.Signature{ociSig}}, v1.Hash{}, &CheckOpts{
268+
RootCerts: rootPool,
269+
IgnoreSCT: true,
270+
IgnoreTlog: true,
271+
Identities: []Identity{{Subject: "[email protected]", Issuer: "oidc-issuer"}}})
272+
273+
var e *ErrNoMatchingSignatures
274+
if !errors.As(err, &e) {
275+
t.Fatalf("%T{%q} is not a %T", err, err, &ErrNoMatchingSignatures{})
276+
}
277+
}
278+
240279
func TestVerifyImageSignatureWithNoChain(t *testing.T) {
241280
ctx := context.Background()
242281
rootCert, rootKey, _ := test.GenerateRootCa()

0 commit comments

Comments
 (0)