1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/pkg/imageverifiers/cosign/verifier.go
Vishal Choudhary 221c559247
feat: cosign verifier for new image verifier crd (#12196)
* feat: cosign verifier for new image verifier crd

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* chore(deps): bump github.com/sigstore/sigstore/pkg/signature/kms/gcp (#12170)

Bumps [github.com/sigstore/sigstore/pkg/signature/kms/gcp](https://github.com/sigstore/sigstore) from 1.8.12 to 1.8.14.
- [Release notes](https://github.com/sigstore/sigstore/releases)
- [Commits](https://github.com/sigstore/sigstore/compare/v1.8.12...v1.8.14)

---
updated-dependencies:
- dependency-name: github.com/sigstore/sigstore/pkg/signature/kms/gcp
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: shuting <shuting@nirmata.com>

* feat: add MutatingPolicies CRD (#12150)

Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>

* README: fix markdown syntax (#12176)

Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>

* chore(deps): bump sigs.k8s.io/controller-runtime from 0.20.1 to 0.20.2 (#12180)

Bumps [sigs.k8s.io/controller-runtime](https://github.com/kubernetes-sigs/controller-runtime) from 0.20.1 to 0.20.2.
- [Release notes](https://github.com/kubernetes-sigs/controller-runtime/releases)
- [Changelog](https://github.com/kubernetes-sigs/controller-runtime/blob/main/RELEASE.md)
- [Commits](https://github.com/kubernetes-sigs/controller-runtime/compare/v0.20.1...v0.20.2)

---
updated-dependencies:
- dependency-name: sigs.k8s.io/controller-runtime
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore: cel policies nits (#12184)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* use serviceAccountName instead of deprecated serviceAccount (#12158)

Signed-off-by: Francesco Ilario <filario@redhat.com>
Co-authored-by: shuting <shuting@nirmata.com>

* chore(deps): bump github.com/sigstore/sigstore/pkg/signature/kms/azure (#12179)

Bumps [github.com/sigstore/sigstore/pkg/signature/kms/azure](https://github.com/sigstore/sigstore) from 1.8.12 to 1.8.14.
- [Release notes](https://github.com/sigstore/sigstore/releases)
- [Commits](https://github.com/sigstore/sigstore/compare/v1.8.12...v1.8.14)

---
updated-dependencies:
- dependency-name: github.com/sigstore/sigstore/pkg/signature/kms/azure
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* chore(deps): bump github.com/awslabs/amazon-ecr-credential-helper/ecr-login (#12178)

Bumps [github.com/awslabs/amazon-ecr-credential-helper/ecr-login](https://github.com/awslabs/amazon-ecr-credential-helper) from 0.0.0-20241227172826-c97b94eac159 to 0.9.1.
- [Release notes](https://github.com/awslabs/amazon-ecr-credential-helper/releases)
- [Changelog](https://github.com/awslabs/amazon-ecr-credential-helper/blob/main/CHANGELOG.md)
- [Commits](https://github.com/awslabs/amazon-ecr-credential-helper/commits/v0.9.1)

---
updated-dependencies:
- dependency-name: github.com/awslabs/amazon-ecr-credential-helper/ecr-login
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* feat: add notary verifier with tsa support (#12160)

* feat: add notary repository

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add notary verifier

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: more tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: more tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: ci

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: update types

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>

* fix: codegen (#12195)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* feat(gctx): add jmespath caching through projections (#11833)

feat(gctx): move ready check to runtime

Signed-off-by: Khaled Emara <khaled.emara@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>

* fix: publish codecov reports (#12197)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* chore: format conformance.yaml workflow file (#12194)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix: add result count for VPs in the CLI (#12193)

Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* feat: implement functions

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: Francesco Ilario <filario@redhat.com>
Signed-off-by: Khaled Emara <khaled.emara@nirmata.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com>
Co-authored-by: Koichi Shiraishi <zchee.io@gmail.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Francesco Ilario <filario@redhat.com>
Co-authored-by: Khaled Emara <khaled.emara@nirmata.com>
2025-02-21 09:03:53 +08:00

128 lines
4.1 KiB
Go

package cosign
import (
"context"
"fmt"
"github.com/go-logr/logr"
policiesv1alpha1 "github.com/kyverno/kyverno/api/policies.kyverno.io/v1alpha1"
"github.com/kyverno/kyverno/pkg/imagedataloader"
"github.com/kyverno/kyverno/pkg/logging"
"github.com/pkg/errors"
"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/cosign/v2/pkg/policy"
k8scorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type cosignVerifier struct {
secretInterface k8scorev1.SecretInterface
log logr.Logger
}
func NewVerifier(secretInterface k8scorev1.SecretInterface) *cosignVerifier {
return &cosignVerifier{
log: logging.WithName("Notary"),
secretInterface: secretInterface,
}
}
func (v *cosignVerifier) VerifyImageSignature(ctx context.Context, image *imagedataloader.ImageData, attestor *policiesv1alpha1.Attestor) error {
if attestor.Cosign == nil {
return fmt.Errorf("cosign verifier only supports cosign attestor")
}
logger := v.log.WithValues("image", image.Image, "digest", image.Digest, "attestor", attestor.Name)
logger.V(2).Info("verifying cosign image signature", "image", image.Image)
cOpts, err := checkOptions(ctx, attestor.Cosign, image.RemoteOpts, image.NameOpts, v.secretInterface)
if err != nil {
err := errors.Wrapf(err, "failed to build cosign verification opts")
logger.Error(err, "image verification failed")
return err
}
cOpts.ClaimVerifier = cosign.SimpleClaimVerifier
sigs, verified, err := cosign.VerifyImageSignatures(ctx, image.NameRef, cOpts)
if err != nil {
err := errors.Wrapf(err, "failed to verify cosign signatures")
logger.Error(err, "image verification failed")
return err
} else if !verified {
err := fmt.Errorf("cosign bundle verification failed")
logger.Error(err, "image verification failed")
return err
}
// TODO: Check keyless data?
if len(attestor.Cosign.Annotations) != 0 {
for _, sig := range sigs {
if err := checkSignatureAnnotations(sig, attestor.Cosign.Annotations); err != nil {
logger.Error(err, "image verification failed")
return err
}
}
}
return nil
}
func (v *cosignVerifier) VerifyAttestationSignature(ctx context.Context, image *imagedataloader.ImageData, attestation *policiesv1alpha1.Attestation, attestor *policiesv1alpha1.Attestor) error {
if attestation.InToto == nil {
return fmt.Errorf("cosgin verifier only supports intoto referrers as attestations")
}
if attestor.Cosign == nil {
return fmt.Errorf("cosign verifier only supports cosign attestor")
}
logger := v.log.WithValues("image", image.Image, "digest", image.Digest, "attestation", attestation.Name, "attestor", attestor.Name)
logger.V(2).Info("verifying cosign attestation signature", "image", image.Image)
cOpts, err := checkOptions(ctx, attestor.Cosign, image.RemoteOpts, image.NameOpts, v.secretInterface)
if err != nil {
err := errors.Wrapf(err, "failed to build cosign verification opts")
logger.Error(err, "image verification failed")
return err
}
cOpts.ClaimVerifier = cosign.IntotoSubjectClaimVerifier
sigs, verified, err := cosign.VerifyImageAttestations(ctx, image.NameRef, cOpts)
if err != nil {
err := errors.Wrapf(err, "failed to verify cosign signatures")
logger.Error(err, "image verification failed")
return err
} else if !verified {
err := fmt.Errorf("cosign bundle verification failed")
logger.Error(err, "image verification failed")
return err
}
checkedTypes := []string{}
found := false
for _, s := range sigs {
payload, gotType, err := policy.AttestationToPayloadJSON(ctx, attestation.InToto.Type, s)
if err != nil {
return fmt.Errorf("converting to consumable policy validation: %w", err)
}
checkedTypes = append(checkedTypes, gotType)
if len(payload) == 0 {
// This is not the predicate type we're looking for.
continue
}
if err := checkSignatureAnnotations(s, attestor.Cosign.Annotations); err != nil {
logger.Error(err, "image verification failed")
return err
}
found = true
image.AddVerifiedIntotoPayloads(gotType, payload)
}
if !found {
err := fmt.Errorf("required predicate type %s not found, found %v", attestation.InToto.Type, checkedTypes)
logger.Error(err, "image verification failed")
return err
}
return nil
}