Skip to content

Commit 24d864b

Browse files
Avoid parsing URL in verification logic
Signed-off-by: Ilya Dmitrichenko <[email protected]>
1 parent 9ba369d commit 24d864b

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

internal/controller/ocirepository_controller.go

+13-17
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (r *OCIRepositoryReconciler) digestFromRevision(revision string) string {
611611
// verifySignature verifies the authenticity of the given image reference URL.
612612
// First, it tries to use a key if a Secret with a valid public key is provided.
613613
// If not, it falls back to a keyless approach for verification.
614-
func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv1.OCIRepository, url string, opt ...remote.Option) error {
614+
func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv1.OCIRepository, ref name.Reference, opt ...remote.Option) error {
615615
ctxTimeout, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
616616
defer cancel()
617617

@@ -622,15 +622,6 @@ func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv
622622
soci.WithRemoteOptions(opt...),
623623
}
624624

625-
var nameOpts []name.Option
626-
if obj.Spec.Insecure {
627-
nameOpts = append(nameOpts, name.Insecure)
628-
}
629-
ref, err := name.ParseReference(url, nameOpts...)
630-
if err != nil {
631-
return err
632-
}
633-
634625
// get the public keys from the given secret
635626
if secretRef := obj.Spec.Verify.SecretRef; secretRef != nil {
636627
certSecretName := types.NamespacedName{
@@ -665,7 +656,7 @@ func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv
665656
}
666657

667658
if !signatureVerified {
668-
return fmt.Errorf("no matching signatures were found for '%s'", url)
659+
return fmt.Errorf("no matching signatures were found for '%s'", ref)
669660
}
670661

671662
return nil
@@ -687,20 +678,25 @@ func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv
687678
return nil
688679
}
689680

690-
return fmt.Errorf("no matching signatures were found for '%s'", url)
681+
return fmt.Errorf("no matching signatures were found for '%s'", ref)
691682
}
692683

693684
return nil
694685
}
695686

696687
// parseRepository validates and extracts the repository URL.
697-
func (r *OCIRepositoryReconciler) parseRepository(url string) (name.Repository, error) {
698-
if !strings.HasPrefix(url, ociv1.OCIRepositoryPrefix) {
688+
func (r *OCIRepositoryReconciler) parseRepository(obj *ociv1.OCIRepository) (name.Repository, error) {
689+
if !strings.HasPrefix(obj.Spec.URL, ociv1.OCIRepositoryPrefix) {
699690
return name.Repository{}, fmt.Errorf("URL must be in format 'oci://<domain>/<org>/<repo>'")
700691
}
701692

702-
url = strings.TrimPrefix(url, ociv1.OCIRepositoryPrefix)
703-
repo, err := name.NewRepository(url)
693+
url := strings.TrimPrefix(obj.Spec.URL, ociv1.OCIRepositoryPrefix)
694+
695+
options := []name.Option{}
696+
if obj.Spec.Insecure {
697+
options = append(options, name.Insecure)
698+
}
699+
repo, err := name.NewRepository(url, options...)
704700
if err != nil {
705701
return name.Repository{}, err
706702
}
@@ -715,7 +711,7 @@ func (r *OCIRepositoryReconciler) parseRepository(url string) (name.Repository,
715711

716712
// getArtifactRef determines which tag or revision should be used and returns the OCI artifact FQN.
717713
func (r *OCIRepositoryReconciler) getArtifactRef(obj *ociv1.OCIRepository, options []remote.Option) (name.Reference, error) {
718-
repo, err := r.parseRepository(obj.Spec.URL)
714+
repo, err := r.parseRepository(obj)
719715
if err != nil {
720716
return nil, invalidOCIURLError{err}
721717
}

0 commit comments

Comments
 (0)