Skip to content

Commit e35dc54

Browse files
refactor(oci): reuse keychain and auth
Signed-off-by: Jason <jagoodse@microsoft.com>
1 parent ef0daf1 commit e35dc54

File tree

1 file changed

+2
-27
lines changed

1 file changed

+2
-27
lines changed

internal/controller/ocirepository_controller.go

+2-27
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
432432
conditions.GetObservedGeneration(obj, sourcev1.SourceVerifiedCondition) != obj.Generation ||
433433
conditions.IsFalse(obj, sourcev1.SourceVerifiedCondition) {
434434

435-
err := r.verifySignature(ctx, obj, ref, opts...)
435+
err := r.verifySignature(ctx, obj, ref, keychain, auth, opts...)
436436
if err != nil {
437437
provider := obj.Spec.Verify.Provider
438438
if obj.Spec.Verify.SecretRef == nil {
@@ -613,7 +613,7 @@ func (r *OCIRepositoryReconciler) digestFromRevision(revision string) string {
613613
// verifySignature verifies the authenticity of the given image reference URL.
614614
// First, it tries to use a key if a Secret with a valid public key is provided.
615615
// If not, it falls back to a keyless approach for verification.
616-
func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv1.OCIRepository, ref name.Reference, opt ...remote.Option) error {
616+
func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv1.OCIRepository, ref name.Reference, keychain authn.Keychain, auth authn.Authenticator, opt ...remote.Option) error {
617617
ctxTimeout, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
618618
defer cancel()
619619

@@ -718,31 +718,6 @@ func (r *OCIRepositoryReconciler) verifySignature(ctx context.Context, obj *ociv
718718
soci.WithNotaryRemoteOptions(opt...),
719719
}
720720

721-
keychain, err := r.keychain(ctx, obj)
722-
if err != nil {
723-
e := serror.NewGeneric(
724-
fmt.Errorf("failed to get credential: %w", err),
725-
sourcev1.AuthenticationFailedReason,
726-
)
727-
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
728-
return e
729-
}
730-
731-
var auth authn.Authenticator
732-
733-
if _, ok := keychain.(soci.Anonymous); obj.Spec.Provider != ociv1.GenericOCIProvider && ok {
734-
var authErr error
735-
auth, authErr = soci.OIDCAuth(ctxTimeout, obj.Spec.URL, obj.Spec.Provider)
736-
if authErr != nil && !errors.Is(authErr, oci.ErrUnconfiguredProvider) {
737-
e := serror.NewGeneric(
738-
fmt.Errorf("failed to get credential from %s: %w", obj.Spec.Provider, authErr),
739-
sourcev1.AuthenticationFailedReason,
740-
)
741-
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
742-
return e
743-
}
744-
}
745-
746721
for k, data := range pubSecret.Data {
747722
if strings.HasSuffix(k, ".crt") || strings.HasSuffix(k, ".pem") {
748723
verifier, err := soci.NewNotaryVerifier(append(defaultNotaryOciOpts, soci.WithNotaryPublicCertificate(data), soci.WithNotaryAuth(auth), soci.WithNotaryKeychain(keychain), soci.WithInsecureRegistry(obj.Spec.Insecure))...)

0 commit comments

Comments
 (0)