2021-10-05 22:42:42 -07:00
|
|
|
package cosign
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-10-29 11:24:26 +01:00
|
|
|
|
2021-10-05 22:42:42 -07:00
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
"github.com/sigstore/cosign/pkg/cosign"
|
2021-11-03 10:45:35 +03:00
|
|
|
"github.com/sigstore/cosign/pkg/oci"
|
2021-10-05 22:42:42 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetMock(image string, data [][]byte) error {
|
|
|
|
imgRef, err := name.ParseReference(image)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
payloads := make([]cosign.SignedPayload, len(data))
|
|
|
|
for i, p := range data {
|
|
|
|
payloads[i] = cosign.SignedPayload{
|
2021-10-06 11:18:36 -07:00
|
|
|
Payload: p,
|
2021-10-05 22:42:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:18:36 -07:00
|
|
|
client = &mock{data: map[string][]cosign.SignedPayload{
|
2021-10-05 22:42:42 -07:00
|
|
|
imgRef.String(): payloads,
|
|
|
|
}}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:35:12 -07:00
|
|
|
func ClearMock() {
|
|
|
|
client = &driver{}
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:42:42 -07:00
|
|
|
type mock struct {
|
2021-10-06 11:18:36 -07:00
|
|
|
data map[string][]cosign.SignedPayload
|
2021-10-05 22:42:42 -07:00
|
|
|
}
|
|
|
|
|
2022-01-27 21:13:23 -08:00
|
|
|
func (m *mock) VerifyImageSignatures(_ context.Context, signedImgRef name.Reference, _ *cosign.CheckOpts) ([]oci.Signature, bool, error) {
|
|
|
|
return m.getSignatures(signedImgRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mock) VerifyImageAttestations(ctx context.Context, signedImgRef name.Reference, co *cosign.CheckOpts) (checkedAttestations []oci.Signature, bundleVerified bool, err error) {
|
|
|
|
return m.getSignatures(signedImgRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mock) getSignatures(signedImgRef name.Reference) ([]oci.Signature, bool, error) {
|
2021-10-05 22:42:42 -07:00
|
|
|
results, ok := m.data[signedImgRef.String()]
|
|
|
|
if !ok {
|
2021-11-03 10:45:35 +03:00
|
|
|
return nil, false, fmt.Errorf("failed to find mock data for %s", signedImgRef.String())
|
2021-10-05 22:42:42 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 10:45:35 +03:00
|
|
|
sigs := make([]oci.Signature, 0, len(results))
|
|
|
|
for _, sp := range results {
|
|
|
|
sigs = append(sigs, &sig{cosignPayload: sp})
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigs, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sig struct {
|
|
|
|
oci.Signature
|
|
|
|
cosignPayload cosign.SignedPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sig) Payload() ([]byte, error) {
|
|
|
|
return s.cosignPayload.Payload, nil
|
2021-10-05 22:42:42 -07:00
|
|
|
}
|