@@ -503,84 +503,84 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, obj *sour
503
503
// verifyOCISourceSignature verifies the authenticity of the given image reference url. First, it tries to keyful approach
504
504
// by looking at whether the given secret exists. Then, if it does not exist, it pushes a keyless approach for verification.
505
505
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 ,
517
529
}
518
530
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 {
521
533
return err
522
534
}
523
535
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
+ }
529
546
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
+ }
534
551
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
555
555
}
556
556
}
557
+ }
557
558
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
+ }
562
562
563
- return nil
563
+ return nil
564
564
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
+ }
571
571
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
+ }
576
576
577
- if len (signatures ) > 0 {
578
- return nil
579
- }
577
+ if len (signatures ) > 0 {
578
+ return nil
580
579
}
581
- return nil
582
580
}
581
+ return nil
583
582
}
583
+
584
584
return nil
585
585
}
586
586
0 commit comments