Skip to content

Commit 7c72acc

Browse files
committed
Set timeout for cosgin verification
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 697f260 commit 7c72acc

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

controllers/ocirepository_controller.go

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -503,84 +503,84 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, obj *sour
503503
// verifyOCISourceSignature verifies the authenticity of the given image reference url. First, it tries to keyful approach
504504
// by looking at whether the given secret exists. Then, if it does not exist, it pushes a keyless approach for verification.
505505
func (r *OCIRepositoryReconciler) verifyOCISourceSignature(ctx context.Context, obj *sourcev1.OCIRepository, url string, keychain authn.Keychain) error {
506-
// Verify the image
507-
if obj.Spec.Verify != nil {
508-
provider := obj.Spec.Verify.Provider
509-
switch provider {
510-
case "cosign":
511-
// get the public keys from the given secret
512-
secretRef := obj.Spec.Verify.SecretRef
513-
514-
defaultCosignOciOpts := []soci.Options{
515-
soci.WithAuthnKeychain(keychain),
516-
soci.WithContext(ctx),
506+
ctxTimeout, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
507+
defer cancel()
508+
509+
provider := obj.Spec.Verify.Provider
510+
switch provider {
511+
case "cosign":
512+
// get the public keys from the given secret
513+
secretRef := obj.Spec.Verify.SecretRef
514+
515+
defaultCosignOciOpts := []soci.Options{
516+
soci.WithAuthnKeychain(keychain),
517+
soci.WithContext(ctxTimeout),
518+
}
519+
520+
ref, err := name.ParseReference(url)
521+
if err != nil {
522+
return err
523+
}
524+
525+
if secretRef != nil {
526+
certSecretName := types.NamespacedName{
527+
Namespace: obj.Namespace,
528+
Name: secretRef.Name,
517529
}
518530

519-
ref, err := name.ParseReference(url)
520-
if err != nil {
531+
var pubSecret corev1.Secret
532+
if err := r.Get(ctxTimeout, certSecretName, &pubSecret); err != nil {
521533
return err
522534
}
523535

524-
if secretRef != nil {
525-
certSecretName := types.NamespacedName{
526-
Namespace: obj.Namespace,
527-
Name: secretRef.Name,
528-
}
536+
signatureVerified := false
537+
// traverse all public keys and try to verify the signature
538+
// this is brute-force approach, but it is ok for now
539+
for k, data := range pubSecret.Data {
540+
// search for public keys in the secret
541+
if strings.HasSuffix(k, ".pub") {
542+
verifier, err := soci.New(append(defaultCosignOciOpts, soci.WithPublicKey(data))...)
543+
if err != nil {
544+
return err
545+
}
529546

530-
var pubSecret corev1.Secret
531-
if err := r.Get(ctx, certSecretName, &pubSecret); err != nil {
532-
return err
533-
}
547+
signatures, _, err := verifier.VerifyImageSignatures(ctx, ref)
548+
if err != nil {
549+
continue
550+
}
534551

535-
signatureVerified := false
536-
// traverse all public keys and try to verify the signature
537-
// this is brute-force approach, but it is ok for now
538-
for k, data := range pubSecret.Data {
539-
// search for public keys in the secret
540-
if strings.HasSuffix(k, ".pub") {
541-
verifier, err := soci.New(append(defaultCosignOciOpts, soci.WithPublicKey(data))...)
542-
if err != nil {
543-
return err
544-
}
545-
546-
signatures, _, err := verifier.VerifyImageSignatures(ctx, ref)
547-
if err != nil {
548-
continue
549-
}
550-
551-
if signatures != nil {
552-
signatureVerified = true
553-
break
554-
}
552+
if signatures != nil {
553+
signatureVerified = true
554+
break
555555
}
556556
}
557+
}
557558

558-
if !signatureVerified {
559-
ctrl.LoggerFrom(ctx).Error(err, "none of the keys in the secret %s succeeded to verify for the image %s", secretRef.Name)
560-
return fmt.Errorf("no matching signatures were found for the image %s", url)
561-
}
559+
if !signatureVerified {
560+
return fmt.Errorf("no matching signatures were found for '%s'", url)
561+
}
562562

563-
return nil
563+
return nil
564564

565-
} else {
566-
ctrl.LoggerFrom(ctx).Info("no secret reference is provided, trying to verify the image using keyless approach")
567-
verifier, err := soci.New(defaultCosignOciOpts...)
568-
if err != nil {
569-
return err
570-
}
565+
} else {
566+
ctrl.LoggerFrom(ctx).Info("no secret reference is provided, trying to verify the image using keyless approach")
567+
verifier, err := soci.New(defaultCosignOciOpts...)
568+
if err != nil {
569+
return err
570+
}
571571

572-
signatures, _, err := verifier.VerifyImageSignatures(ctx, ref)
573-
if err != nil {
574-
return err
575-
}
572+
signatures, _, err := verifier.VerifyImageSignatures(ctxTimeout, ref)
573+
if err != nil {
574+
return err
575+
}
576576

577-
if len(signatures) > 0 {
578-
return nil
579-
}
577+
if len(signatures) > 0 {
578+
return nil
580579
}
581-
return nil
582580
}
581+
return nil
583582
}
583+
584584
return nil
585585
}
586586

0 commit comments

Comments
 (0)